Skip to content

Commit 0a07e13

Browse files
committed
test(account): harden destroy checks with RetryContext
1 parent d92c894 commit 0a07e13

File tree

2 files changed

+162
-304
lines changed

2 files changed

+162
-304
lines changed

internal/services/account/project_test.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package account_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
accountSDK "github.com/scaleway/scaleway-sdk-go/api/account/v3"
@@ -101,29 +104,31 @@ func isProjectPresent(tt *acctest.TestTools, name string) resource.TestCheckFunc
101104
}
102105

103106
func isProjectDestroyed(tt *acctest.TestTools) resource.TestCheckFunc {
104-
return func(s *terraform.State) error {
105-
for _, rs := range s.RootModule().Resources {
106-
if rs.Type != "scaleway_account_project" {
107-
continue
108-
}
107+
return func(state *terraform.State) error {
108+
ctx := context.Background()
109+
api := account.NewProjectAPI(tt.Meta)
109110

110-
accountAPI := account.NewProjectAPI(tt.Meta)
111+
return retry.RetryContext(ctx, 3*time.Minute, func() *retry.RetryError {
112+
for _, rs := range state.RootModule().Resources {
113+
if rs.Type != "scaleway_account_project" {
114+
continue
115+
}
111116

112-
_, err := accountAPI.GetProject(&accountSDK.ProjectAPIGetProjectRequest{
113-
ProjectID: rs.Primary.ID,
114-
})
117+
_, err := api.GetProject(&accountSDK.ProjectAPIGetProjectRequest{
118+
ProjectID: rs.Primary.ID,
119+
})
115120

116-
// If no error resource still exist
117-
if err == nil {
118-
return fmt.Errorf("resource %s(%s) still exist", rs.Type, rs.Primary.ID)
121+
switch {
122+
case err == nil:
123+
return retry.RetryableError(fmt.Errorf("resource %s(%s) still exists", rs.Type, rs.Primary.ID))
124+
case httperrors.Is404(err):
125+
continue
126+
default:
127+
return retry.NonRetryableError(err)
128+
}
119129
}
120130

121-
// Unexpected api error we return it
122-
if !httperrors.Is404(err) {
123-
return err
124-
}
125-
}
126-
127-
return nil
131+
return nil
132+
})
128133
}
129134
}

0 commit comments

Comments
 (0)