Skip to content

Commit bc2479c

Browse files
committed
test(fip): harden destroy checks with RetryContext + update cassettes
1 parent df81909 commit bc2479c

File tree

4 files changed

+1258
-2869
lines changed

4 files changed

+1258
-2869
lines changed

internal/services/flexibleip/ip_test.go

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package flexibleip_test
22

33
import (
4+
"context"
45
"fmt"
56
"net"
67
"testing"
8+
"time"
79

10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
811
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
912
"github.com/hashicorp/terraform-plugin-testing/terraform"
1013
baremetalSDK "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
@@ -323,35 +326,38 @@ func testAccCheckFlexibleIPExists(tt *acctest.TestTools, name string) resource.T
323326
}
324327

325328
func testAccCheckFlexibleIPDestroy(tt *acctest.TestTools) resource.TestCheckFunc {
326-
return func(s *terraform.State) error {
327-
for _, rs := range s.RootModule().Resources {
328-
if rs.Type != "scaleway_flexible_ip" {
329-
continue
330-
}
331-
332-
fipAPI, zone, id, err := flexibleip.NewAPIWithZoneAndID(tt.Meta, rs.Primary.ID)
333-
if err != nil {
334-
return err
335-
}
336-
337-
_, err = fipAPI.WaitForFlexibleIP(&flexibleipSDK.WaitForFlexibleIPRequest{
338-
FipID: id,
339-
Zone: zone,
340-
})
341-
342-
// If no error resource still exist
343-
if err == nil {
344-
return fmt.Errorf("resource %s(%s) still exist", rs.Type, rs.Primary.ID)
329+
return func(state *terraform.State) error {
330+
ctx := context.Background()
331+
332+
return retry.RetryContext(ctx, 3*time.Minute, func() *retry.RetryError {
333+
for _, rs := range state.RootModule().Resources {
334+
if rs.Type != "scaleway_flexible_ip" {
335+
continue
336+
}
337+
338+
fipAPI, zone, id, err := flexibleip.NewAPIWithZoneAndID(tt.Meta, rs.Primary.ID)
339+
if err != nil {
340+
return retry.NonRetryableError(err)
341+
}
342+
343+
_, err = fipAPI.GetFlexibleIP(&flexibleipSDK.GetFlexibleIPRequest{
344+
FipID: id,
345+
Zone: zone,
346+
})
347+
348+
switch {
349+
case err == nil:
350+
return retry.RetryableError(fmt.Errorf("flexible IP (%s) still exists", rs.Primary.ID))
351+
// We check for 403 because instance API return 403 for deleted IP
352+
case httperrors.Is404(err) || httperrors.Is403(err):
353+
continue
354+
default:
355+
return retry.NonRetryableError(err)
356+
}
345357
}
346358

347-
// Unexpected api error we return it
348-
// We check for 403 because instance API return 403 for deleted IP
349-
if !httperrors.Is404(err) && !httperrors.Is403(err) {
350-
return err
351-
}
352-
}
353-
354-
return nil
359+
return nil
360+
})
355361
}
356362
}
357363

0 commit comments

Comments
 (0)