Skip to content

Commit 7284a9b

Browse files
committed
use sdk vcr (v4) if migrating package
1 parent 6278d71 commit 7284a9b

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ require (
8585
github.com/fsnotify/fsnotify v1.8.0 // indirect
8686
github.com/go-logr/logr v1.4.3 // indirect
8787
github.com/go-logr/stdr v1.2.2 // indirect
88+
github.com/goccy/go-yaml v1.18.0 // indirect
8889
github.com/golang/protobuf v1.5.4 // indirect
8990
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
9091
github.com/gookit/color v1.5.1 // indirect
@@ -171,6 +172,7 @@ require (
171172
google.golang.org/genproto/googleapis/rpc v0.0.0-20250825161204-c5933d9347a5 // indirect
172173
google.golang.org/grpc v1.75.1 // indirect
173174
google.golang.org/protobuf v1.36.9 // indirect
175+
gopkg.in/dnaeon/go-vcr.v4 v4.0.5 // indirect
174176
gopkg.in/ini.v1 v1.66.4 // indirect
175177
gopkg.in/yaml.v2 v2.4.0 // indirect
176178
gopkg.in/yaml.v3 v3.0.1 // indirect

internal/acctest/acctest.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package acctest
22

33
import (
4+
"net/http"
45
"os"
56
"strconv"
67
"strings"
@@ -9,6 +10,8 @@ import (
910

1011
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1112
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
13+
"github.com/scaleway/scaleway-sdk-go/scw"
14+
"github.com/scaleway/scaleway-sdk-go/vcr"
1215
"github.com/scaleway/terraform-provider-scaleway/v2/internal/env"
1316
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1417
"github.com/scaleway/terraform-provider-scaleway/v2/internal/transport"
@@ -25,6 +28,41 @@ type TestTools struct {
2528
Cleanup func()
2629
}
2730

31+
var foldersUsingVCRv4 = []string{
32+
"instance",
33+
}
34+
35+
func folderUsesVCRv4(fullFolderPath string) bool {
36+
fullPathSplit := strings.Split(fullFolderPath, "/")
37+
folder := fullPathSplit[len(fullPathSplit)-1]
38+
for _, migratedFolder := range foldersUsingVCRv4 {
39+
if migratedFolder == folder {
40+
return true
41+
}
42+
}
43+
return false
44+
}
45+
46+
func NewRecordedClient(t *testing.T, pkgFolder string, update bool) (client *http.Client, cleanup func(), err error) {
47+
t.Helper()
48+
49+
r, err := vcr.NewHTTPRecorder(t, pkgFolder, update, nil)
50+
if err != nil {
51+
return nil, nil, err
52+
}
53+
54+
retryOptions := transport.RetryableTransportOptions{}
55+
if !update {
56+
retryOptions.RetryWaitMax = scw.TimeDurationPtr(0)
57+
}
58+
59+
return &http.Client{
60+
Transport: transport.NewRetryableTransportWithOptions(r, retryOptions),
61+
}, func() {
62+
require.NoError(t, r.Stop()) // Make sure recorder is stopped once done with it
63+
}, nil
64+
}
65+
2866
func NewTestTools(t *testing.T) *TestTools {
2967
t.Helper()
3068

@@ -35,8 +73,15 @@ func NewTestTools(t *testing.T) *TestTools {
3573
t.Fatalf("cannot detect working directory for testing")
3674
}
3775

38-
// Create a http client with recording capabilities
39-
httpClient, cleanup, err := getHTTPRecoder(t, folder, *UpdateCassettes)
76+
// Create an HTTP client with recording capabilities
77+
var httpClient *http.Client
78+
var cleanup func()
79+
80+
if folderUsesVCRv4(folder) {
81+
httpClient, cleanup, err = NewRecordedClient(t, folder, *UpdateCassettes)
82+
} else {
83+
httpClient, cleanup, err = getHTTPRecoder(t, folder, *UpdateCassettes)
84+
}
4085
require.NoError(t, err)
4186

4287
// Create meta that will be passed in the provider config

0 commit comments

Comments
 (0)