Skip to content

Commit 40ed147

Browse files
committed
updated tests to use a func to lookup resources when checking destroy is correct
1 parent 95bb04c commit 40ed147

6 files changed

+76
-55
lines changed

kong/resource_kong_api_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,20 @@ func testAccCheckKongApiDestroy(state *terraform.State) error {
6262

6363
client := testAccProvider.Meta().(*gokong.KongAdminClient)
6464

65-
for _, rs := range state.RootModule().Resources {
66-
if rs.Type != "kong_api" {
67-
continue
68-
}
65+
apis := getResourcesByType("kong_api", state)
6966

70-
response, err := client.Apis().GetById(rs.Primary.ID)
67+
if len(apis) != 1 {
68+
return fmt.Errorf("expecting only 1 api resource found %v", len(apis))
69+
}
7170

72-
if err != nil {
73-
return fmt.Errorf("error calling get api by id: %v", err)
74-
}
71+
response, err := client.Apis().GetById(apis[0].Primary.ID)
7572

76-
if response != nil {
77-
return fmt.Errorf("api %s still exists, %+v", rs.Primary.ID, response)
78-
}
73+
if err != nil {
74+
return fmt.Errorf("error calling get api by id: %v", err)
75+
}
76+
77+
if response != nil {
78+
return fmt.Errorf("api %s still exists, %+v", apis[0].Primary.ID, response)
7979
}
8080

8181
return nil

kong/resource_kong_certificate_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ func testAccCheckKongCertificateDestroy(state *terraform.State) error {
3838

3939
client := testAccProvider.Meta().(*gokong.KongAdminClient)
4040

41-
for _, rs := range state.RootModule().Resources {
42-
if rs.Type != "kong_certificate" {
43-
continue
44-
}
41+
certificates := getResourcesByType("kong_certificate", state)
4542

46-
response, err := client.Certificates().GetById(rs.Primary.ID)
43+
if len(certificates) != 1 {
44+
return fmt.Errorf("expecting only 1 certificate resource found %v", len(certificates))
45+
}
4746

48-
if err != nil {
49-
return fmt.Errorf("error calling get certificate by id: %v", err)
50-
}
47+
response, err := client.Certificates().GetById(certificates[0].Primary.ID)
5148

52-
if response != nil {
53-
return fmt.Errorf("certificate %s still exists, %+v", rs.Primary.ID, response)
54-
}
49+
if err != nil {
50+
return fmt.Errorf("error calling get certificate by id: %v", err)
51+
}
52+
53+
if response != nil {
54+
return fmt.Errorf("certificate %s still exists, %+v", certificates[0].Primary.ID, response)
5555
}
5656

5757
return nil

kong/resource_kong_consumer_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ func testAccCheckKongConsumerDestroy(state *terraform.State) error {
3838

3939
client := testAccProvider.Meta().(*gokong.KongAdminClient)
4040

41-
for _, rs := range state.RootModule().Resources {
42-
if rs.Type != "kong_consumer" {
43-
continue
44-
}
41+
consumers := getResourcesByType("kong_consumer", state)
4542

46-
response, err := client.Consumers().GetById(rs.Primary.ID)
43+
if len(consumers) != 1 {
44+
return fmt.Errorf("expecting only 1 consumer resource found %v", len(consumers))
45+
}
4746

48-
if err != nil {
49-
return fmt.Errorf("error calling get consumer by id: %v", err)
50-
}
47+
response, err := client.Consumers().GetById(consumers[0].Primary.ID)
5148

52-
if response != nil {
53-
return fmt.Errorf("consumer %s still exists, %+v", rs.Primary.ID, response)
54-
}
49+
if err != nil {
50+
return fmt.Errorf("error calling get consumer by id: %v", err)
51+
}
52+
53+
if response != nil {
54+
return fmt.Errorf("consumer %s still exists, %+v", consumers[0].Primary.ID, response)
5555
}
5656

5757
return nil

kong/resource_kong_plugin_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,20 @@ func testAccCheckKongPluginDestroy(state *terraform.State) error {
132132

133133
client := testAccProvider.Meta().(*gokong.KongAdminClient)
134134

135-
for _, rs := range state.RootModule().Resources {
136-
if rs.Type != "kong_plugin" {
137-
continue
138-
}
135+
plugins := getResourcesByType("kong_plugin", state)
139136

140-
response, err := client.Plugins().GetById(rs.Primary.ID)
137+
if len(plugins) != 1 {
138+
return fmt.Errorf("expecting only 1 plugin resource found %v", len(plugins))
139+
}
141140

142-
if err != nil {
143-
return fmt.Errorf("error calling get plugin by id: %v", err)
144-
}
141+
response, err := client.Plugins().GetById(plugins[0].Primary.ID)
145142

146-
if response != nil {
147-
return fmt.Errorf("plugin %s still exists, %+v", rs.Primary.ID, response)
148-
}
143+
if err != nil {
144+
return fmt.Errorf("error calling get plugin by id: %v", err)
145+
}
146+
147+
if response != nil {
148+
return fmt.Errorf("plugin %s still exists, %+v", plugins[0].Primary.ID, response)
149149
}
150150

151151
return nil

kong/resource_kong_sni_test.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ func TestAccKongSni(t *testing.T) {
1818
Config: testCreateSniConfig,
1919
Check: resource.ComposeTestCheckFunc(
2020
testAccCheckKongSniExists("kong_sni.sni"),
21+
//testAccCheckForChildIdCorrect("kong_certificate.certificate1", "kong_sni.sni", "certificate_id"),
2122
resource.TestCheckResourceAttr("kong_sni.sni", "name", "www.example.com"),
2223
),
2324
},
2425
{
2526
Config: testUpdateSniConfig,
2627
Check: resource.ComposeTestCheckFunc(
2728
testAccCheckKongSniExists("kong_sni.sni"),
29+
//testAccCheckForChildIdCorrect("kong_certificate.certificate2", "kong_sni.sni", "certificate_id"),
2830
resource.TestCheckResourceAttr("kong_sni.sni", "name", "www.example.com"),
2931
),
3032
},
@@ -36,20 +38,20 @@ func testAccCheckKongSniDestroy(state *terraform.State) error {
3638

3739
client := testAccProvider.Meta().(*gokong.KongAdminClient)
3840

39-
for _, rs := range state.RootModule().Resources {
40-
if rs.Type != "kong_sni" {
41-
continue
42-
}
41+
snis := getResourcesByType("kong_sni", state)
4342

44-
response, err := client.Snis().GetByName(rs.Primary.ID)
43+
if len(snis) != 1 {
44+
return fmt.Errorf("expecting only 1 sni resource found %v", len(snis))
45+
}
4546

46-
if err != nil {
47-
return fmt.Errorf("error calling get sni by id: %v", err)
48-
}
47+
response, err := client.Snis().GetByName(snis[0].Primary.ID)
4948

50-
if response != nil {
51-
return fmt.Errorf("sni %s still exists, %+v", rs.Primary.ID, response)
52-
}
49+
if err != nil {
50+
return fmt.Errorf("error calling get sni by id: %v", err)
51+
}
52+
53+
if response != nil {
54+
return fmt.Errorf("sni %s still exists, %+v", snis[0].Primary.ID, response)
5355
}
5456

5557
return nil

kong/testing.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package kong
2+
3+
import (
4+
"github.com/hashicorp/terraform/terraform"
5+
)
6+
7+
func getResourcesByType(resourceType string, state *terraform.State) []*terraform.ResourceState {
8+
9+
var result []*terraform.ResourceState
10+
11+
for _, rs := range state.RootModule().Resources {
12+
if rs.Type == resourceType {
13+
result = append(result, rs)
14+
}
15+
16+
}
17+
18+
return result
19+
}

0 commit comments

Comments
 (0)