From 9852ae0e6342dbcb2720de02be37faccfe4114be Mon Sep 17 00:00:00 2001 From: Jean de Kernier Date: Tue, 30 Jul 2019 14:32:52 +0200 Subject: [PATCH] resource/aws_iot_policy: Add an acceptance test for the resource update --- aws/resource_aws_iot_policy_test.go | 117 ++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/aws/resource_aws_iot_policy_test.go b/aws/resource_aws_iot_policy_test.go index ce0e6b53d7ae..d03cf08dcf82 100644 --- a/aws/resource_aws_iot_policy_test.go +++ b/aws/resource_aws_iot_policy_test.go @@ -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 @@ -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" { @@ -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 = <