Skip to content

Commit

Permalink
update per review, add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bpeng committed Nov 28, 2024
1 parent 5d1e098 commit 28165cb
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 11 deletions.
7 changes: 3 additions & 4 deletions aws/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,10 @@ func (s *S3) PutWithMetadata(bucket, key string, object []byte, metadata Meta) e
return err
}

// checks if the given S3 bucket exists.
func (s *S3) CheckBucketExists(bucketName string) error {
// Check if the bucket exists.
// 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(bucketName),
Bucket: aws.String(bucket),
})

return err
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
}

// 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
assert.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
16 changes: 9 additions & 7 deletions aws/sqs/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,15 @@ func (s *SQS) CreateQueue(queueName string, isFifoQueue bool) (string, error) {
return aws.ToString(queue.QueueUrl), err
}

// checks if the given SQS queue exists.
func (s *SQS) CheckQueueExists(queueName string) error {
// Check if the queue exists.
_, err := s.client.GetQueueUrl(context.TODO(), &sqs.GetQueueUrlInput{
QueueName: aws.String(queueName),
})

// 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
}

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
assert.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 28165cb

Please sign in to comment.