Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AWS healthcheck - SQS #187

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Memcached
* InfluxDB
* Nats
* AWS

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* AWS
* Amazon SQS


## Usage

Expand Down
37 changes: 37 additions & 0 deletions checks/aws/sqs/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package sqs

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
)

// Config is the AWS SQS checker configuration settings.
type Config struct {
// Client is the initialized instance of the AWS SQS client used for making API requests to the AWS SQS service.
Client SqsActions
// QueueUrl is a pointer to the string that contains the URL of the SQS queue to which the client connects.
QueueUrl *string
}

// SqsActions defines the set of operations used from the SQS client.
type SqsActions interface {
GetQueueAttributes(ctx context.Context, params *sqs.GetQueueAttributesInput, optFns ...func(*sqs.Options)) (*sqs.GetQueueAttributesOutput, error)
}

// New creates a new AWS SQS health check that verifies if a connection to the queue exists
func New(config Config) func(ctx context.Context) error {
return func(ctx context.Context) error {
arnAttributeName := types.QueueAttributeNameQueueArn
if _, err := config.Client.GetQueueAttributes(ctx, &sqs.GetQueueAttributesInput{
QueueUrl: config.QueueUrl,
AttributeNames: []types.QueueAttributeName{arnAttributeName},
}); err != nil {
return fmt.Errorf("unable to get queue ARN: %w", err)
}

return nil
}
}
71 changes: 71 additions & 0 deletions checks/aws/sqs/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package sqs

import (
"context"
"errors"
"testing"

"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
)

// SimpleMockSQSClient is a manual mock of the SQSAPI interface.
type SimpleMockSQSClient struct {
// Add fields to store mock outputs and any other state
GetQueueAttributesOutput *sqs.GetQueueAttributesOutput
GetQueueAttributesErr error
}

// GetQueueAttributes is the mock method that mimics the corresponding SQSAPI method.
func (m *SimpleMockSQSClient) GetQueueAttributes(ctx context.Context, params *sqs.GetQueueAttributesInput, optFns ...func(*sqs.Options)) (*sqs.GetQueueAttributesOutput, error) {
// Return the mocked response and error stored in the mock struct
return m.GetQueueAttributesOutput, m.GetQueueAttributesErr
}

func TestNew(t *testing.T) {
queueURL := "http://example.com/queue"
mockSQSClient := &SimpleMockSQSClient{
GetQueueAttributesOutput: &sqs.GetQueueAttributesOutput{
Attributes: map[string]string{
string(types.QueueAttributeNameQueueArn): "arn:aws:sqs:us-east-1:123456789012:queue1",
},
},
GetQueueAttributesErr: nil,
}

config := Config{
Client: mockSQSClient,
QueueUrl: &queueURL,
}

checker := New(config)

err := checker(context.Background())
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
}

func TestNewError(t *testing.T) {
queueURL := "http://example.com/queue"
mockSQSClient := &SimpleMockSQSClient{
GetQueueAttributesOutput: &sqs.GetQueueAttributesOutput{
Attributes: map[string]string{
string(types.QueueAttributeNameQueueArn): "arn:aws:sqs:us-east-1:123456789012:queue1",
},
},
GetQueueAttributesErr: errors.New("failed to get queue attributes"),
}

config := Config{
Client: mockSQSClient,
QueueUrl: &queueURL,
}

checker := New(config)

err := checker(context.Background())
if err == nil {
t.Errorf("Expected error, but got none")
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/hellofresh/health-go/v5
go 1.20

require (
github.com/aws/aws-sdk-go-v2/service/sqs v1.31.4
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874
github.com/go-sql-driver/mysql v1.7.1
github.com/gocql/gocql v1.6.0
Expand All @@ -23,6 +24,10 @@ require (

require (
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.26.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
github.com/aws/aws-sdk-go-v2 v1.26.1 h1:5554eUqIYVWpU0YmeeYZ0wU64H2VLBs8TlhRB2L+EkA=
github.com/aws/aws-sdk-go-v2 v1.26.1/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 h1:aw39xVGeRWlWx9EzGVnhOR4yOjQDHPQ6o6NmBlscyQg=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5/go.mod h1:FSaRudD0dXiMPK2UjknVwwTYyZMRsHv3TtkabsZih5I=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 h1:PG1F3OD1szkuQPzDw3CIQsRIrtTlUC3lP84taWzHlq0=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5/go.mod h1:jU1li6RFryMz+so64PpKtudI+QzbKoIEivqdf6LNpOc=
github.com/aws/aws-sdk-go-v2/service/sqs v1.31.4 h1:mE2ysZMEeQ3ulHWs4mmc4fZEhOfeY1o6QXAfDqjbSgw=
github.com/aws/aws-sdk-go-v2/service/sqs v1.31.4/go.mod h1:lCN2yKnj+Sp9F6UzpoPPTir+tSaC9Jwf6LcmTqnXFZw=
github.com/aws/smithy-go v1.20.2 h1:tbp628ireGtzcHDDmLT/6ADHidqnwgF57XOXZe6tp4Q=
github.com/aws/smithy-go v1.20.2/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 h1:mXoPYz/Ul5HYEDvkta6I8/rnYM5gSdSV2tJ6XbZuEtY=
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k=
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
Expand Down