github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/aws/sns_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/service/sns"
    10  	"github.com/gruntwork-io/terratest/modules/random"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestCreateAndDeleteSnsTopic(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	region := GetRandomStableRegion(t, nil, nil)
    18  	uniqueID := random.UniqueId()
    19  	name := fmt.Sprintf("test-sns-topic-%s", uniqueID)
    20  
    21  	arn := CreateSnsTopic(t, region, name)
    22  	defer deleteTopic(t, region, arn)
    23  
    24  	assert.True(t, snsTopicExists(t, region, arn))
    25  }
    26  
    27  func snsTopicExists(t *testing.T, region string, arn string) bool {
    28  	snsClient := NewSnsClient(t, region)
    29  
    30  	input := sns.GetTopicAttributesInput{TopicArn: aws.String(arn)}
    31  
    32  	if _, err := snsClient.GetTopicAttributes(&input); err != nil {
    33  		if strings.Contains(err.Error(), "NotFound") {
    34  			return false
    35  		}
    36  		t.Fatal(err)
    37  	}
    38  
    39  	return true
    40  }
    41  
    42  func deleteTopic(t *testing.T, region string, arn string) {
    43  	DeleteSNSTopic(t, region, arn)
    44  	assert.False(t, snsTopicExists(t, region, arn))
    45  }