11package acctest
22
33import (
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+
2866func 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