Skip to content

Commit 7ee00a3

Browse files
committed
test(cockpit): harden destroy checks with RetryContext
1 parent ccc9b72 commit 7ee00a3

File tree

2 files changed

+118
-159
lines changed

2 files changed

+118
-159
lines changed

internal/services/cockpit/source_test.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package cockpit_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
cockpitSDK "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
@@ -231,30 +234,35 @@ func isSourcePresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
231234

232235
func isSourceDestroyed(tt *acctest.TestTools) resource.TestCheckFunc {
233236
return func(state *terraform.State) error {
234-
for _, rs := range state.RootModule().Resources {
235-
if rs.Type != "scaleway_cockpit_source" {
236-
continue
237-
}
237+
ctx := context.Background()
238238

239-
api, region, ID, err := cockpit.NewAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
240-
if err != nil {
241-
return err
242-
}
239+
return retry.RetryContext(ctx, 3*time.Minute, func() *retry.RetryError {
240+
for _, rs := range state.RootModule().Resources {
241+
if rs.Type != "scaleway_cockpit_source" {
242+
continue
243+
}
243244

244-
_, err = api.GetDataSource(&cockpitSDK.RegionalAPIGetDataSourceRequest{
245-
Region: region,
246-
DataSourceID: ID,
247-
})
245+
api, region, id, err := cockpit.NewAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
246+
if err != nil {
247+
return retry.NonRetryableError(err)
248+
}
248249

249-
if err == nil {
250-
return fmt.Errorf("cockpit source (%s) still exists", rs.Primary.ID)
251-
}
250+
_, err = api.GetDataSource(&cockpitSDK.RegionalAPIGetDataSourceRequest{
251+
Region: region,
252+
DataSourceID: id,
253+
})
252254

253-
if !httperrors.Is404(err) {
254-
return err
255+
switch {
256+
case err == nil:
257+
return retry.RetryableError(fmt.Errorf("cockpit source (%s) still exists", rs.Primary.ID))
258+
case httperrors.Is404(err):
259+
continue
260+
default:
261+
return retry.NonRetryableError(err)
262+
}
255263
}
256-
}
257264

258-
return nil
265+
return nil
266+
})
259267
}
260268
}

0 commit comments

Comments
 (0)