Skip to content

Commit 9852ae0

Browse files
committed
resource/aws_iot_policy: Add an acceptance test for the resource update
1 parent 784897a commit 9852ae0

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

aws/resource_aws_iot_policy_test.go

+117
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,59 @@ func TestAccAWSIoTPolicy_invalidJson(t *testing.T) {
5050
})
5151
}
5252

53+
func TestAccAWSIoTPolicy_update(t *testing.T) {
54+
rName := acctest.RandomWithPrefix("PubSubToAnyTopic-")
55+
expectedVersions := []string{"1", "2", "3", "5", "6"}
56+
57+
resource.ParallelTest(t, resource.TestCase{
58+
PreCheck: func() { testAccPreCheck(t) },
59+
Providers: testAccProviders,
60+
CheckDestroy: testAccCheckAWSIoTPolicyDestroy_basic,
61+
Steps: []resource.TestStep{
62+
{
63+
Config: testAccAWSIoTPolicyConfigInitialState(rName),
64+
Check: resource.ComposeTestCheckFunc(
65+
resource.TestCheckResourceAttr("aws_iot_policy.pubsub", "name", rName),
66+
resource.TestCheckResourceAttrSet("aws_iot_policy.pubsub", "arn"),
67+
resource.TestCheckResourceAttr("aws_iot_policy.pubsub", "default_version_id", "1"),
68+
resource.TestCheckResourceAttrSet("aws_iot_policy.pubsub", "policy"),
69+
),
70+
},
71+
{
72+
Config: testAccAWSIoTPolicyConfig_updatePolicy(rName, "topic2"),
73+
Check: resource.ComposeTestCheckFunc(
74+
resource.TestCheckResourceAttr("aws_iot_policy.pubsub", "default_version_id", "2"),
75+
),
76+
},
77+
{
78+
Config: testAccAWSIoTPolicyConfig_updatePolicy(rName, "topic3"),
79+
Check: resource.ComposeTestCheckFunc(
80+
resource.TestCheckResourceAttr("aws_iot_policy.pubsub", "default_version_id", "3"),
81+
),
82+
},
83+
{
84+
Config: testAccAWSIoTPolicyConfig_updatePolicy(rName, "topic4"),
85+
Check: resource.ComposeTestCheckFunc(
86+
resource.TestCheckResourceAttr("aws_iot_policy.pubsub", "default_version_id", "4"),
87+
),
88+
},
89+
{
90+
Config: testAccAWSIoTPolicyConfig_updatePolicy(rName, "topic5"),
91+
Check: resource.ComposeTestCheckFunc(
92+
resource.TestCheckResourceAttr("aws_iot_policy.pubsub", "default_version_id", "5"),
93+
),
94+
},
95+
{
96+
Config: testAccAWSIoTPolicyConfig_updatePolicy(rName, "topic6"),
97+
Check: resource.ComposeTestCheckFunc(
98+
resource.TestCheckResourceAttr("aws_iot_policy.pubsub", "default_version_id", "6"),
99+
testAccCheckAWSIoTPolicyVersions("aws_iot_policy.pubsub", expectedVersions),
100+
),
101+
},
102+
},
103+
})
104+
}
105+
53106
func testAccCheckAWSIoTPolicyDestroy_basic(s *terraform.State) error {
54107
conn := testAccProvider.Meta().(*AWSClient).iotconn
55108

@@ -83,6 +136,50 @@ func testAccCheckAWSIoTPolicyDestroy_basic(s *terraform.State) error {
83136
return nil
84137
}
85138

139+
func testAccCheckAWSIoTPolicyVersions(rName string, expVersions []string) resource.TestCheckFunc {
140+
return func(s *terraform.State) error {
141+
rs, ok := s.RootModule().Resources[rName]
142+
if !ok {
143+
return fmt.Errorf("Not found: %s", rName)
144+
}
145+
146+
conn := testAccProvider.Meta().(*AWSClient).iotconn
147+
params := &iot.ListPolicyVersionsInput{
148+
PolicyName: aws.String(rs.Primary.Attributes["name"]),
149+
}
150+
151+
resp, err := conn.ListPolicyVersions(params)
152+
if err != nil {
153+
return err
154+
}
155+
156+
if len(expVersions) != len(resp.PolicyVersions) {
157+
return fmt.Errorf("Expected %d versions, got %d", len(expVersions), len(resp.PolicyVersions))
158+
}
159+
160+
var actVersions []string
161+
for _, actVer := range resp.PolicyVersions {
162+
actVersions = append(actVersions, *(actVer.VersionId))
163+
}
164+
165+
matchedValue := false
166+
for _, actVer := range actVersions {
167+
matchedValue = false
168+
for _, expVer := range expVersions {
169+
if actVer == expVer {
170+
matchedValue = true
171+
break
172+
}
173+
}
174+
if !matchedValue {
175+
return fmt.Errorf("Expected: %v / Got: %v", expVersions, actVersions)
176+
}
177+
}
178+
179+
return nil
180+
}
181+
}
182+
86183
func testAccAWSIoTPolicyConfigInitialState(rName string) string {
87184
return fmt.Sprintf(`
88185
resource "aws_iot_policy" "pubsub" {
@@ -120,3 +217,23 @@ EOF
120217
}
121218
`, rName)
122219
}
220+
221+
func testAccAWSIoTPolicyConfig_updatePolicy(rName string, topicName string) string {
222+
return fmt.Sprintf(`
223+
resource "aws_iot_policy" "pubsub" {
224+
name = "%s"
225+
226+
policy = <<EOF
227+
{
228+
"Version": "2012-10-17",
229+
"Statement": [{
230+
"Effect": "Allow",
231+
"Action": ["iot:*"],
232+
"Resource": ["arn:aws:iot:*:*:topic/%s"]
233+
}]
234+
}
235+
EOF
236+
}
237+
`, rName, topicName)
238+
239+
}

0 commit comments

Comments
 (0)