Skip to content

Commit

Permalink
resource/aws_iot_policy: Add an acceptance test for the resource update
Browse files Browse the repository at this point in the history
  • Loading branch information
jeandek committed Jul 30, 2019
1 parent 784897a commit 9852ae0
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions aws/resource_aws_iot_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,59 @@ func TestAccAWSIoTPolicy_invalidJson(t *testing.T) {
})
}

func TestAccAWSIoTPolicy_update(t *testing.T) {
rName := acctest.RandomWithPrefix("PubSubToAnyTopic-")
expectedVersions := []string{"1", "2", "3", "5", "6"}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSIoTPolicyDestroy_basic,
Steps: []resource.TestStep{
{
Config: testAccAWSIoTPolicyConfigInitialState(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("aws_iot_policy.pubsub", "name", rName),
resource.TestCheckResourceAttrSet("aws_iot_policy.pubsub", "arn"),
resource.TestCheckResourceAttr("aws_iot_policy.pubsub", "default_version_id", "1"),
resource.TestCheckResourceAttrSet("aws_iot_policy.pubsub", "policy"),
),
},
{
Config: testAccAWSIoTPolicyConfig_updatePolicy(rName, "topic2"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("aws_iot_policy.pubsub", "default_version_id", "2"),
),
},
{
Config: testAccAWSIoTPolicyConfig_updatePolicy(rName, "topic3"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("aws_iot_policy.pubsub", "default_version_id", "3"),
),
},
{
Config: testAccAWSIoTPolicyConfig_updatePolicy(rName, "topic4"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("aws_iot_policy.pubsub", "default_version_id", "4"),
),
},
{
Config: testAccAWSIoTPolicyConfig_updatePolicy(rName, "topic5"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("aws_iot_policy.pubsub", "default_version_id", "5"),
),
},
{
Config: testAccAWSIoTPolicyConfig_updatePolicy(rName, "topic6"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("aws_iot_policy.pubsub", "default_version_id", "6"),
testAccCheckAWSIoTPolicyVersions("aws_iot_policy.pubsub", expectedVersions),
),
},
},
})
}

func testAccCheckAWSIoTPolicyDestroy_basic(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).iotconn

Expand Down Expand Up @@ -83,6 +136,50 @@ func testAccCheckAWSIoTPolicyDestroy_basic(s *terraform.State) error {
return nil
}

func testAccCheckAWSIoTPolicyVersions(rName string, expVersions []string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[rName]
if !ok {
return fmt.Errorf("Not found: %s", rName)
}

conn := testAccProvider.Meta().(*AWSClient).iotconn
params := &iot.ListPolicyVersionsInput{
PolicyName: aws.String(rs.Primary.Attributes["name"]),
}

resp, err := conn.ListPolicyVersions(params)
if err != nil {
return err
}

if len(expVersions) != len(resp.PolicyVersions) {
return fmt.Errorf("Expected %d versions, got %d", len(expVersions), len(resp.PolicyVersions))
}

var actVersions []string
for _, actVer := range resp.PolicyVersions {
actVersions = append(actVersions, *(actVer.VersionId))
}

matchedValue := false
for _, actVer := range actVersions {
matchedValue = false
for _, expVer := range expVersions {
if actVer == expVer {
matchedValue = true
break
}
}
if !matchedValue {
return fmt.Errorf("Expected: %v / Got: %v", expVersions, actVersions)
}
}

return nil
}
}

func testAccAWSIoTPolicyConfigInitialState(rName string) string {
return fmt.Sprintf(`
resource "aws_iot_policy" "pubsub" {
Expand Down Expand Up @@ -120,3 +217,23 @@ EOF
}
`, rName)
}

func testAccAWSIoTPolicyConfig_updatePolicy(rName string, topicName string) string {
return fmt.Sprintf(`
resource "aws_iot_policy" "pubsub" {
name = "%s"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:*"],
"Resource": ["arn:aws:iot:*:*:topic/%s"]
}]
}
EOF
}
`, rName, topicName)

}

0 comments on commit 9852ae0

Please sign in to comment.