Skip to content

Commit

Permalink
Merge pull request #183 from GeoNet/health-check
Browse files Browse the repository at this point in the history
add functions CheckQueueExists CheckBucketExists
  • Loading branch information
sue-h-gns authored Nov 29, 2024
2 parents c2acb57 + fcb3308 commit 745247c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
9 changes: 9 additions & 0 deletions aws/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ func (s *S3) PutWithMetadata(bucket, key string, object []byte, metadata Meta) e
return err
}

// CheckBucket checks if the given S3 bucket exists and is accessible.
func (s *S3) CheckBucket(bucket string) error {
_, err := s.client.HeadBucket(context.TODO(), &s3.HeadBucketInput{
Bucket: aws.String(bucket),
})

return err
}

// Exists checks if an object for key already exists in the bucket.
func (s *S3) Exists(bucket, key string) (bool, error) {

Expand Down
18 changes: 18 additions & 0 deletions aws/s3/s3_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@ func awsCmdGetTestObject() string {

// THE TESTS

func TestCheckBucket(t *testing.T) {
setup()
defer teardown()

// test
client, err := New()
assert.Nil(t, err)
//test existing bucket
err = client.CheckBucket(testBucket)
assert.Nil(t, err)

//test none existing bucket
testBucket1 := "test1"
err = client.CheckBucket(testBucket1)
assert.NotNil(t, err)

}

func TestCreateS3ClientAndReady(t *testing.T) {
// ARRANGE
setup()
Expand Down
8 changes: 8 additions & 0 deletions aws/sns/sns.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ func (s *SNS) DeleteTopic(topicArn string) error {
return err
}

// CheckTopic checks if an SNS topic exists and is accessible given its ARN.
func (s *SNS) CheckTopic(topicArn string) error {
_, err := s.client.GetTopicAttributes(context.TODO(), &sns.GetTopicAttributesInput{
TopicArn: aws.String(topicArn),
})
return err
}

// SubscribeQueue subscribes an SQS queue to an SNS topic.
func (s *SNS) SubscribeQueue(topicArn string, queueArn string) (string, error) {
output, err := s.client.Subscribe(context.TODO(), &sns.SubscribeInput{
Expand Down
20 changes: 20 additions & 0 deletions aws/sns/sns_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ func teardown(queueArn, topicArn string) {
os.Unsetenv("AWS_ENDPOINT_URL")
}

func TestCheckTopic(t *testing.T) {
_, topicArn := setup(false, true)
defer teardown("", topicArn)

client, err := New()
ready := client.Ready()
// ASSERT
require.Nil(t, err)
assert.True(t, ready)

//check existing topic
err = client.CheckTopic(topicArn)
assert.Nil(t, err)

//check none existing topic
err = client.CheckTopic(topicArn + "_1")
assert.NotNil(t, err)

}

func TestSNSNewAndReady(t *testing.T) {
// ARRANGE
setup(false, false)
Expand Down
12 changes: 12 additions & 0 deletions aws/sqs/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,18 @@ func (s *SQS) CreateQueue(queueName string, isFifoQueue bool) (string, error) {
return aws.ToString(queue.QueueUrl), err
}

// CheckQueue checks if the given SQS queue exists and is accessible.
func (s *SQS) CheckQueue(queueUrl string) error {
params := sqs.GetQueueAttributesInput{
QueueUrl: aws.String(queueUrl),
AttributeNames: []types.QueueAttributeName{
types.QueueAttributeNameAll,
},
}
_, err := s.client.GetQueueAttributes(context.TODO(), &params)
return err
}

// DeleteQueue deletes an Amazon SQS queue.
func (s *SQS) DeleteQueue(queueUrl string) error {
_, err := s.client.DeleteQueue(context.TODO(), &sqs.DeleteQueueInput{
Expand Down
24 changes: 24 additions & 0 deletions aws/sqs/sqs_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,30 @@ func awsCmdCheckSQSAttribute(url, attribute, expectedValue string) bool {
return false
}

func TestCheckQueue(t *testing.T) {
// ARRANGE
setup()
defer teardown()

client, err := New()

// ASSERT
require.Nil(t, err)

//test existing queue
queue, err := client.GetQueueUrl(testQueue)

assert.Nil(t, err)

err = client.CheckQueue(queue)
assert.Nil(t, err)

//test none existing queue
err = client.CheckQueue(queue + "_1")
assert.NotNil(t, err)

}

func TestSQSNewAndReady(t *testing.T) {
// ARRANGE
setup()
Expand Down

0 comments on commit 745247c

Please sign in to comment.