terraform/builtin/providers/aws/resource_aws_sns_topic_test.go

247 lines
5.8 KiB
Go
Raw Normal View History

2015-05-15 01:17:18 +02:00
package aws
import (
"fmt"
"testing"
"github.com/aws/aws-sdk-go/aws"
2015-06-24 07:31:24 +02:00
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/sns"
provider/aws: Add DiffSupressionFunc to `aws_elasticsearch_domain`, `aws_sqs_queue` and `aws_sns_topic` ``` SQS Queue Tests: %make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSSQSQueue' 2 ↵ ✹ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/05 09:46:04 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSSQSQueue -timeout 120m === RUN TestAccAWSSQSQueue_importBasic --- PASS: TestAccAWSSQSQueue_importBasic (18.99s) === RUN TestAccAWSSQSQueue_basic --- PASS: TestAccAWSSQSQueue_basic (44.31s) === RUN TestAccAWSSQSQueue_policy --- PASS: TestAccAWSSQSQueue_policy (32.76s) === RUN TestAccAWSSQSQueue_redrivePolicy --- PASS: TestAccAWSSQSQueue_redrivePolicy (66.42s) === RUN TestAccAWSSQSQueue_Policybasic --- PASS: TestAccAWSSQSQueue_Policybasic (32.40s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 194.895s ``` SNS Topic Tests: % make testacc TEST=./builtin/providers/aws % TESTARGS='-run=TestAccAWSSNSTopic_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/04 22:56:26 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSSNSTopic_ -timeout 120m === RUN TestAccAWSSNSTopic_importBasic --- PASS: TestAccAWSSNSTopic_importBasic (17.67s) === RUN TestAccAWSSNSTopic_basic --- PASS: TestAccAWSSNSTopic_basic (17.92s) === RUN TestAccAWSSNSTopic_policy --- PASS: TestAccAWSSNSTopic_policy (20.99s) === RUN TestAccAWSSNSTopic_withIAMRole --- PASS: TestAccAWSSNSTopic_withIAMRole (26.51s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 83.112s ```
2016-09-04 11:04:10 +02:00
"github.com/hashicorp/terraform/helper/acctest"
2015-05-15 01:17:18 +02:00
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
provider/aws: Add DiffSupressionFunc to `aws_elasticsearch_domain`, `aws_sqs_queue` and `aws_sns_topic` ``` SQS Queue Tests: %make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSSQSQueue' 2 ↵ ✹ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/05 09:46:04 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSSQSQueue -timeout 120m === RUN TestAccAWSSQSQueue_importBasic --- PASS: TestAccAWSSQSQueue_importBasic (18.99s) === RUN TestAccAWSSQSQueue_basic --- PASS: TestAccAWSSQSQueue_basic (44.31s) === RUN TestAccAWSSQSQueue_policy --- PASS: TestAccAWSSQSQueue_policy (32.76s) === RUN TestAccAWSSQSQueue_redrivePolicy --- PASS: TestAccAWSSQSQueue_redrivePolicy (66.42s) === RUN TestAccAWSSQSQueue_Policybasic --- PASS: TestAccAWSSQSQueue_Policybasic (32.40s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 194.895s ``` SNS Topic Tests: % make testacc TEST=./builtin/providers/aws % TESTARGS='-run=TestAccAWSSNSTopic_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/04 22:56:26 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSSNSTopic_ -timeout 120m === RUN TestAccAWSSNSTopic_importBasic --- PASS: TestAccAWSSNSTopic_importBasic (17.67s) === RUN TestAccAWSSNSTopic_basic --- PASS: TestAccAWSSNSTopic_basic (17.92s) === RUN TestAccAWSSNSTopic_policy --- PASS: TestAccAWSSNSTopic_policy (20.99s) === RUN TestAccAWSSNSTopic_withIAMRole --- PASS: TestAccAWSSNSTopic_withIAMRole (26.51s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 83.112s ```
2016-09-04 11:04:10 +02:00
"github.com/jen20/awspolicyequivalence"
2015-05-15 01:17:18 +02:00
)
func TestAccAWSSNSTopic_basic(t *testing.T) {
2015-05-15 01:17:18 +02:00
resource.Test(t, resource.TestCase{
2016-04-21 17:18:04 +02:00
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_sns_topic.test_topic",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
2015-05-15 01:17:18 +02:00
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSSNSTopicConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
),
},
},
})
}
provider/aws: Add DiffSupressionFunc to `aws_elasticsearch_domain`, `aws_sqs_queue` and `aws_sns_topic` ``` SQS Queue Tests: %make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSSQSQueue' 2 ↵ ✹ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/05 09:46:04 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSSQSQueue -timeout 120m === RUN TestAccAWSSQSQueue_importBasic --- PASS: TestAccAWSSQSQueue_importBasic (18.99s) === RUN TestAccAWSSQSQueue_basic --- PASS: TestAccAWSSQSQueue_basic (44.31s) === RUN TestAccAWSSQSQueue_policy --- PASS: TestAccAWSSQSQueue_policy (32.76s) === RUN TestAccAWSSQSQueue_redrivePolicy --- PASS: TestAccAWSSQSQueue_redrivePolicy (66.42s) === RUN TestAccAWSSQSQueue_Policybasic --- PASS: TestAccAWSSQSQueue_Policybasic (32.40s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 194.895s ``` SNS Topic Tests: % make testacc TEST=./builtin/providers/aws % TESTARGS='-run=TestAccAWSSNSTopic_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/04 22:56:26 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSSNSTopic_ -timeout 120m === RUN TestAccAWSSNSTopic_importBasic --- PASS: TestAccAWSSNSTopic_importBasic (17.67s) === RUN TestAccAWSSNSTopic_basic --- PASS: TestAccAWSSNSTopic_basic (17.92s) === RUN TestAccAWSSNSTopic_policy --- PASS: TestAccAWSSNSTopic_policy (20.99s) === RUN TestAccAWSSNSTopic_withIAMRole --- PASS: TestAccAWSSNSTopic_withIAMRole (26.51s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 83.112s ```
2016-09-04 11:04:10 +02:00
func TestAccAWSSNSTopic_policy(t *testing.T) {
rName := acctest.RandString(10)
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"}`
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_sns_topic.test_topic",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSSNSTopicWithPolicy(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
testAccCheckAWSNSTopicHasPolicy("aws_sns_topic.test_topic", expectedPolicy),
),
},
},
})
}
func TestAccAWSSNSTopic_withIAMRole(t *testing.T) {
resource.Test(t, resource.TestCase{
2016-04-21 17:18:04 +02:00
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_sns_topic.test_topic",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSSNSTopicConfig_withIAMRole,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
),
},
},
})
}
provider/aws: Add DiffSupressionFunc to `aws_elasticsearch_domain`, `aws_sqs_queue` and `aws_sns_topic` ``` SQS Queue Tests: %make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSSQSQueue' 2 ↵ ✹ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/05 09:46:04 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSSQSQueue -timeout 120m === RUN TestAccAWSSQSQueue_importBasic --- PASS: TestAccAWSSQSQueue_importBasic (18.99s) === RUN TestAccAWSSQSQueue_basic --- PASS: TestAccAWSSQSQueue_basic (44.31s) === RUN TestAccAWSSQSQueue_policy --- PASS: TestAccAWSSQSQueue_policy (32.76s) === RUN TestAccAWSSQSQueue_redrivePolicy --- PASS: TestAccAWSSQSQueue_redrivePolicy (66.42s) === RUN TestAccAWSSQSQueue_Policybasic --- PASS: TestAccAWSSQSQueue_Policybasic (32.40s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 194.895s ``` SNS Topic Tests: % make testacc TEST=./builtin/providers/aws % TESTARGS='-run=TestAccAWSSNSTopic_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/04 22:56:26 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSSNSTopic_ -timeout 120m === RUN TestAccAWSSNSTopic_importBasic --- PASS: TestAccAWSSNSTopic_importBasic (17.67s) === RUN TestAccAWSSNSTopic_basic --- PASS: TestAccAWSSNSTopic_basic (17.92s) === RUN TestAccAWSSNSTopic_policy --- PASS: TestAccAWSSNSTopic_policy (20.99s) === RUN TestAccAWSSNSTopic_withIAMRole --- PASS: TestAccAWSSNSTopic_withIAMRole (26.51s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 83.112s ```
2016-09-04 11:04:10 +02:00
func testAccCheckAWSNSTopicHasPolicy(n string, expectedPolicyText string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Queue URL specified!")
}
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No SNS topic with that ARN exists")
}
conn := testAccProvider.Meta().(*AWSClient).snsconn
params := &sns.GetTopicAttributesInput{
TopicArn: aws.String(rs.Primary.ID),
}
resp, err := conn.GetTopicAttributes(params)
if err != nil {
return err
}
var actualPolicyText string
for k, v := range resp.Attributes {
if k == "Policy" {
actualPolicyText = *v
break
}
}
equivalent, err := awspolicy.PoliciesAreEquivalent(actualPolicyText, expectedPolicyText)
if err != nil {
return fmt.Errorf("Error testing policy equivalence: %s", err)
}
if !equivalent {
return fmt.Errorf("Non-equivalent policy error:\n\nexpected: %s\n\n got: %s\n",
expectedPolicyText, actualPolicyText)
}
return nil
}
}
2015-05-15 01:17:18 +02:00
func testAccCheckAWSSNSTopicDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).snsconn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_sns_topic" {
continue
}
// Check if the topic exists by fetching its attributes
params := &sns.GetTopicAttributesInput{
TopicArn: aws.String(rs.Primary.ID),
2015-05-15 01:17:18 +02:00
}
_, err := conn.GetTopicAttributes(params)
if err == nil {
return fmt.Errorf("Topic exists when it should be destroyed!")
}
// Verify the error is an API error, not something else
_, ok := err.(awserr.Error)
2015-05-15 01:17:18 +02:00
if !ok {
return err
}
}
return nil
}
func testAccCheckAWSSNSTopicExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No SNS topic with that ARN exists")
}
conn := testAccProvider.Meta().(*AWSClient).snsconn
params := &sns.GetTopicAttributesInput{
TopicArn: aws.String(rs.Primary.ID),
2015-05-15 01:17:18 +02:00
}
_, err := conn.GetTopicAttributes(params)
if err != nil {
return err
}
return nil
}
}
const testAccAWSSNSTopicConfig = `
resource "aws_sns_topic" "test_topic" {
name = "terraform-test-topic"
}
`
provider/aws: Add DiffSupressionFunc to `aws_elasticsearch_domain`, `aws_sqs_queue` and `aws_sns_topic` ``` SQS Queue Tests: %make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSSQSQueue' 2 ↵ ✹ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/05 09:46:04 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSSQSQueue -timeout 120m === RUN TestAccAWSSQSQueue_importBasic --- PASS: TestAccAWSSQSQueue_importBasic (18.99s) === RUN TestAccAWSSQSQueue_basic --- PASS: TestAccAWSSQSQueue_basic (44.31s) === RUN TestAccAWSSQSQueue_policy --- PASS: TestAccAWSSQSQueue_policy (32.76s) === RUN TestAccAWSSQSQueue_redrivePolicy --- PASS: TestAccAWSSQSQueue_redrivePolicy (66.42s) === RUN TestAccAWSSQSQueue_Policybasic --- PASS: TestAccAWSSQSQueue_Policybasic (32.40s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 194.895s ``` SNS Topic Tests: % make testacc TEST=./builtin/providers/aws % TESTARGS='-run=TestAccAWSSNSTopic_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/04 22:56:26 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSSNSTopic_ -timeout 120m === RUN TestAccAWSSNSTopic_importBasic --- PASS: TestAccAWSSNSTopic_importBasic (17.67s) === RUN TestAccAWSSNSTopic_basic --- PASS: TestAccAWSSNSTopic_basic (17.92s) === RUN TestAccAWSSNSTopic_policy --- PASS: TestAccAWSSNSTopic_policy (20.99s) === RUN TestAccAWSSNSTopic_withIAMRole --- PASS: TestAccAWSSNSTopic_withIAMRole (26.51s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 83.112s ```
2016-09-04 11:04:10 +02:00
func testAccAWSSNSTopicWithPolicy(r string) string {
return fmt.Sprintf(`
resource "aws_sns_topic" "test_topic" {
name = "example-%s"
policy = <<EOF
{
"Statement": [
{
"Sid": "Stmt1445931846145",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "sns:Publish",
"Resource": "arn:aws:sns:us-west-2::example"
}
],
"Version": "2012-10-17",
"Id": "Policy1445931846145"
}
EOF
}
`, r)
}
// Test for https://github.com/hashicorp/terraform/issues/3660
const testAccAWSSNSTopicConfig_withIAMRole = `
resource "aws_iam_role" "example" {
name = "terraform_bug"
path = "/test/"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_sns_topic" "test_topic" {
name = "example"
policy = <<EOF
{
"Statement": [
{
"Sid": "Stmt1445931846145",
"Effect": "Allow",
"Principal": {
"AWS": "${aws_iam_role.example.arn}"
},
"Action": "sns:Publish",
"Resource": "arn:aws:sns:us-west-2::example"
}
],
"Version": "2012-10-17",
"Id": "Policy1445931846145"
}
EOF
}
`