github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_sns_topic_subscription_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/sns"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSSNSTopicSubscription_basic(t *testing.T) {
    16  	ri := acctest.RandInt()
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckAWSSNSTopicSubscriptionDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccAWSSNSTopicSubscriptionConfig(ri),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
    27  					testAccCheckAWSSNSTopicSubscriptionExists("aws_sns_topic_subscription.test_subscription"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func testAccCheckAWSSNSTopicSubscriptionDestroy(s *terraform.State) error {
    35  	conn := testAccProvider.Meta().(*AWSClient).snsconn
    36  
    37  	for _, rs := range s.RootModule().Resources {
    38  		if rs.Type != "aws_sns_topic" {
    39  			continue
    40  		}
    41  
    42  		// Try to find key pair
    43  		req := &sns.GetSubscriptionAttributesInput{
    44  			SubscriptionArn: aws.String(rs.Primary.ID),
    45  		}
    46  
    47  		_, err := conn.GetSubscriptionAttributes(req)
    48  
    49  		if err == nil {
    50  			return fmt.Errorf("Subscription still exists, can't continue.")
    51  		}
    52  
    53  		// Verify the error is an API error, not something else
    54  		_, ok := err.(awserr.Error)
    55  		if !ok {
    56  			return err
    57  		}
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  func testAccCheckAWSSNSTopicSubscriptionExists(n string) resource.TestCheckFunc {
    64  	return func(s *terraform.State) error {
    65  		rs, ok := s.RootModule().Resources[n]
    66  		if !ok {
    67  			return fmt.Errorf("Not found: %s", n)
    68  		}
    69  
    70  		if rs.Primary.ID == "" {
    71  			return fmt.Errorf("No SNS subscription with that ARN exists")
    72  		}
    73  
    74  		conn := testAccProvider.Meta().(*AWSClient).snsconn
    75  
    76  		params := &sns.GetSubscriptionAttributesInput{
    77  			SubscriptionArn: aws.String(rs.Primary.ID),
    78  		}
    79  		_, err := conn.GetSubscriptionAttributes(params)
    80  
    81  		if err != nil {
    82  			return err
    83  		}
    84  
    85  		return nil
    86  	}
    87  }
    88  
    89  func testAccAWSSNSTopicSubscriptionConfig(i int) string {
    90  	return fmt.Sprintf(`
    91  resource "aws_sns_topic" "test_topic" {
    92      name = "terraform-test-topic"
    93  }
    94  
    95  resource "aws_sqs_queue" "test_queue" {
    96  	name = "terraform-subscription-test-queue-%d"
    97  }
    98  
    99  resource "aws_sns_topic_subscription" "test_subscription" {
   100      topic_arn = "${aws_sns_topic.test_topic.arn}"
   101      protocol = "sqs"
   102      endpoint = "${aws_sqs_queue.test_queue.arn}"
   103  }
   104  `, i)
   105  }