Skip to content

Commit 9716430

Browse files
committed
test(iot): harden destroy checks with RetryContext
1 parent 869a656 commit 9716430

File tree

2 files changed

+196
-337
lines changed

2 files changed

+196
-337
lines changed

internal/services/iot/hub_test.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package iot_test
22

33
import (
4+
"context"
45
"fmt"
56
"testing"
7+
"time"
68

9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
710
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
811
"github.com/hashicorp/terraform-plugin-testing/terraform"
912
iotSDK "github.com/scaleway/scaleway-sdk-go/api/iot/v1"
@@ -109,33 +112,36 @@ func TestAccHub_Dedicated(t *testing.T) {
109112

110113
func isHubDestroyed(tt *acctest.TestTools) resource.TestCheckFunc {
111114
return func(state *terraform.State) error {
112-
for _, rs := range state.RootModule().Resources {
113-
if rs.Type != "scaleway_iot_hub" {
114-
continue
115-
}
115+
ctx := context.Background()
116116

117-
iotAPI, region, hubID, err := iot.NewAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
118-
if err != nil {
119-
return err
120-
}
117+
return retry.RetryContext(ctx, 3*time.Minute, func() *retry.RetryError {
118+
for _, rs := range state.RootModule().Resources {
119+
if rs.Type != "scaleway_iot_hub" {
120+
continue
121+
}
121122

122-
_, err = iotAPI.GetHub(&iotSDK.GetHubRequest{
123-
Region: region,
124-
HubID: hubID,
125-
})
123+
iotAPI, region, hubID, err := iot.NewAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
124+
if err != nil {
125+
return retry.NonRetryableError(err)
126+
}
126127

127-
// If no error resource still exist
128-
if err == nil {
129-
return fmt.Errorf("hub (%s) still exists", rs.Primary.ID)
130-
}
128+
_, err = iotAPI.GetHub(&iotSDK.GetHubRequest{
129+
Region: region,
130+
HubID: hubID,
131+
})
131132

132-
// Unexpected api error we return it
133-
if !httperrors.Is404(err) {
134-
return err
133+
switch {
134+
case err == nil:
135+
return retry.RetryableError(fmt.Errorf("hub (%s) still exists", rs.Primary.ID))
136+
case httperrors.Is404(err):
137+
continue
138+
default:
139+
return retry.NonRetryableError(err)
140+
}
135141
}
136-
}
137142

138-
return nil
143+
return nil
144+
})
139145
}
140146
}
141147

0 commit comments

Comments
 (0)