github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/resource_aws_sns_topic_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  	"github.com/jen20/awspolicyequivalence"
    14  )
    15  
    16  func TestAccAWSSNSTopic_basic(t *testing.T) {
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:      func() { testAccPreCheck(t) },
    19  		IDRefreshName: "aws_sns_topic.test_topic",
    20  		Providers:     testAccProviders,
    21  		CheckDestroy:  testAccCheckAWSSNSTopicDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccAWSSNSTopicConfig,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccAWSSNSTopic_policy(t *testing.T) {
    34  	rName := acctest.RandString(10)
    35  	expectedPolicy := `{"Statement":[{"Sid":"Stmt1445931846145","Effect":"Allow","Principal":{"AWS":"*"},"Action":"sns:Publish","Resource":"arn:aws:sns:us-west-2::example"}],"Version":"2012-10-17","Id":"Policy1445931846145"}`
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:      func() { testAccPreCheck(t) },
    38  		IDRefreshName: "aws_sns_topic.test_topic",
    39  		Providers:     testAccProviders,
    40  		CheckDestroy:  testAccCheckAWSSNSTopicDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: testAccAWSSNSTopicWithPolicy(rName),
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
    46  					testAccCheckAWSNSTopicHasPolicy("aws_sns_topic.test_topic", expectedPolicy),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func TestAccAWSSNSTopic_withIAMRole(t *testing.T) {
    54  	resource.Test(t, resource.TestCase{
    55  		PreCheck:      func() { testAccPreCheck(t) },
    56  		IDRefreshName: "aws_sns_topic.test_topic",
    57  		Providers:     testAccProviders,
    58  		CheckDestroy:  testAccCheckAWSSNSTopicDestroy,
    59  		Steps: []resource.TestStep{
    60  			resource.TestStep{
    61  				Config: testAccAWSSNSTopicConfig_withIAMRole,
    62  				Check: resource.ComposeTestCheckFunc(
    63  					testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func TestAccAWSSNSTopic_withDeliveryPolicy(t *testing.T) {
    71  	expectedPolicy := `{"http":{"defaultHealthyRetryPolicy": {"minDelayTarget": 20,"maxDelayTarget": 20,"numMaxDelayRetries": 0,"numRetries": 3,"numNoDelayRetries": 0,"numMinDelayRetries": 0,"backoffFunction": "linear"},"disableSubscriptionOverrides": false}}`
    72  	resource.Test(t, resource.TestCase{
    73  		PreCheck:      func() { testAccPreCheck(t) },
    74  		IDRefreshName: "aws_sns_topic.test_topic",
    75  		Providers:     testAccProviders,
    76  		CheckDestroy:  testAccCheckAWSSNSTopicDestroy,
    77  		Steps: []resource.TestStep{
    78  			resource.TestStep{
    79  				Config: testAccAWSSNSTopicConfig_withDeliveryPolicy,
    80  				Check: resource.ComposeTestCheckFunc(
    81  					testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
    82  					testAccCheckAWSNSTopicHasDeliveryPolicy("aws_sns_topic.test_topic", expectedPolicy),
    83  				),
    84  			},
    85  		},
    86  	})
    87  }
    88  
    89  func testAccCheckAWSNSTopicHasPolicy(n string, expectedPolicyText string) resource.TestCheckFunc {
    90  	return func(s *terraform.State) error {
    91  		rs, ok := s.RootModule().Resources[n]
    92  		if !ok {
    93  			return fmt.Errorf("Not found: %s", n)
    94  		}
    95  
    96  		if rs.Primary.ID == "" {
    97  			return fmt.Errorf("No Queue URL specified!")
    98  		}
    99  
   100  		if !ok {
   101  			return fmt.Errorf("Not found: %s", n)
   102  		}
   103  
   104  		if rs.Primary.ID == "" {
   105  			return fmt.Errorf("No SNS topic with that ARN exists")
   106  		}
   107  
   108  		conn := testAccProvider.Meta().(*AWSClient).snsconn
   109  
   110  		params := &sns.GetTopicAttributesInput{
   111  			TopicArn: aws.String(rs.Primary.ID),
   112  		}
   113  		resp, err := conn.GetTopicAttributes(params)
   114  		if err != nil {
   115  			return err
   116  		}
   117  
   118  		var actualPolicyText string
   119  		for k, v := range resp.Attributes {
   120  			if k == "Policy" {
   121  				actualPolicyText = *v
   122  				break
   123  			}
   124  		}
   125  
   126  		equivalent, err := awspolicy.PoliciesAreEquivalent(actualPolicyText, expectedPolicyText)
   127  		if err != nil {
   128  			return fmt.Errorf("Error testing policy equivalence: %s", err)
   129  		}
   130  		if !equivalent {
   131  			return fmt.Errorf("Non-equivalent policy error:\n\nexpected: %s\n\n     got: %s\n",
   132  				expectedPolicyText, actualPolicyText)
   133  		}
   134  
   135  		return nil
   136  	}
   137  }
   138  
   139  func testAccCheckAWSNSTopicHasDeliveryPolicy(n string, expectedPolicyText string) resource.TestCheckFunc {
   140  	return func(s *terraform.State) error {
   141  		rs, ok := s.RootModule().Resources[n]
   142  		if !ok {
   143  			return fmt.Errorf("Not found: %s", n)
   144  		}
   145  
   146  		if rs.Primary.ID == "" {
   147  			return fmt.Errorf("No Queue URL specified!")
   148  		}
   149  
   150  		conn := testAccProvider.Meta().(*AWSClient).snsconn
   151  
   152  		params := &sns.GetTopicAttributesInput{
   153  			TopicArn: aws.String(rs.Primary.ID),
   154  		}
   155  		resp, err := conn.GetTopicAttributes(params)
   156  		if err != nil {
   157  			return err
   158  		}
   159  
   160  		var actualPolicyText string
   161  		for k, v := range resp.Attributes {
   162  			if k == "DeliveryPolicy" {
   163  				actualPolicyText = *v
   164  				break
   165  			}
   166  		}
   167  
   168  		equivalent := suppressEquivalentJsonDiffs("", actualPolicyText, expectedPolicyText, nil)
   169  
   170  		if !equivalent {
   171  			return fmt.Errorf("Non-equivalent delivery policy error:\n\nexpected: %s\n\n     got: %s\n",
   172  				expectedPolicyText, actualPolicyText)
   173  		}
   174  
   175  		return nil
   176  	}
   177  }
   178  
   179  func testAccCheckAWSSNSTopicDestroy(s *terraform.State) error {
   180  	conn := testAccProvider.Meta().(*AWSClient).snsconn
   181  
   182  	for _, rs := range s.RootModule().Resources {
   183  		if rs.Type != "aws_sns_topic" {
   184  			continue
   185  		}
   186  
   187  		// Check if the topic exists by fetching its attributes
   188  		params := &sns.GetTopicAttributesInput{
   189  			TopicArn: aws.String(rs.Primary.ID),
   190  		}
   191  		_, err := conn.GetTopicAttributes(params)
   192  		if err == nil {
   193  			return fmt.Errorf("Topic exists when it should be destroyed!")
   194  		}
   195  
   196  		// Verify the error is an API error, not something else
   197  		_, ok := err.(awserr.Error)
   198  		if !ok {
   199  			return err
   200  		}
   201  	}
   202  
   203  	return nil
   204  }
   205  
   206  func testAccCheckAWSSNSTopicExists(n string) resource.TestCheckFunc {
   207  	return func(s *terraform.State) error {
   208  		rs, ok := s.RootModule().Resources[n]
   209  		if !ok {
   210  			return fmt.Errorf("Not found: %s", n)
   211  		}
   212  
   213  		if rs.Primary.ID == "" {
   214  			return fmt.Errorf("No SNS topic with that ARN exists")
   215  		}
   216  
   217  		conn := testAccProvider.Meta().(*AWSClient).snsconn
   218  
   219  		params := &sns.GetTopicAttributesInput{
   220  			TopicArn: aws.String(rs.Primary.ID),
   221  		}
   222  		_, err := conn.GetTopicAttributes(params)
   223  
   224  		if err != nil {
   225  			return err
   226  		}
   227  
   228  		return nil
   229  	}
   230  }
   231  
   232  const testAccAWSSNSTopicConfig = `
   233  resource "aws_sns_topic" "test_topic" {
   234      name = "terraform-test-topic"
   235  }
   236  `
   237  
   238  func testAccAWSSNSTopicWithPolicy(r string) string {
   239  	return fmt.Sprintf(`
   240  resource "aws_sns_topic" "test_topic" {
   241    name = "example-%s"
   242    policy = <<EOF
   243  {
   244    "Statement": [
   245      {
   246        "Sid": "Stmt1445931846145",
   247        "Effect": "Allow",
   248        "Principal": {
   249          "AWS": "*"
   250         },
   251        "Action": "sns:Publish",
   252        "Resource": "arn:aws:sns:us-west-2::example"
   253      }
   254    ],
   255    "Version": "2012-10-17",
   256    "Id": "Policy1445931846145"
   257  }
   258  EOF
   259  }
   260  `, r)
   261  }
   262  
   263  // Test for https://github.com/hashicorp/terraform/issues/3660
   264  const testAccAWSSNSTopicConfig_withIAMRole = `
   265  resource "aws_iam_role" "example" {
   266    name = "terraform_bug"
   267    path = "/test/"
   268    assume_role_policy = <<EOF
   269  {
   270    "Version": "2012-10-17",
   271    "Statement": [
   272      {
   273        "Action": "sts:AssumeRole",
   274        "Principal": {
   275          "Service": "ec2.amazonaws.com"
   276        },
   277        "Effect": "Allow",
   278        "Sid": ""
   279      }
   280    ]
   281  }
   282  EOF
   283  }
   284  
   285  resource "aws_sns_topic" "test_topic" {
   286    name = "example"
   287    policy = <<EOF
   288  {
   289    "Statement": [
   290      {
   291        "Sid": "Stmt1445931846145",
   292        "Effect": "Allow",
   293        "Principal": {
   294          "AWS": "${aws_iam_role.example.arn}"
   295  			},
   296        "Action": "sns:Publish",
   297        "Resource": "arn:aws:sns:us-west-2::example"
   298      }
   299    ],
   300    "Version": "2012-10-17",
   301    "Id": "Policy1445931846145"
   302  }
   303  EOF
   304  }
   305  `
   306  
   307  // Test for https://github.com/hashicorp/terraform/issues/14024
   308  const testAccAWSSNSTopicConfig_withDeliveryPolicy = `
   309  resource "aws_sns_topic" "test_topic" {
   310    name = "test_delivery_policy"
   311    delivery_policy = <<EOF
   312  {
   313    "http": {
   314      "defaultHealthyRetryPolicy": {
   315        "minDelayTarget": 20,
   316        "maxDelayTarget": 20,
   317        "numRetries": 3,
   318        "numMaxDelayRetries": 0,
   319        "numNoDelayRetries": 0,
   320        "numMinDelayRetries": 0,
   321        "backoffFunction": "linear"
   322      },
   323      "disableSubscriptionOverrides": false
   324    }
   325  }
   326  EOF
   327  }
   328  `