Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
dbe18ee
bump go-vcr.v4 + move matching logic to the sdk-go
Mia-Cross Aug 28, 2025
8229a31
ns ok: webhosting
Mia-Cross Aug 28, 2025
54dd583
ns ok: vc + vpcgw
Mia-Cross Aug 28, 2025
ec95019
ns ok: rdb + registry + sdb
Mia-Cross Aug 28, 2025
95ba4fe
ns ok: jobs + keymanager + marketplace + mnq
Mia-Cross Aug 28, 2025
93206bf
ns ok: edge-services + file + flexible-ip + inference
Mia-Cross Aug 28, 2025
0bc28cd
ns ok: autoscaling + az + billing + domain
Mia-Cross Aug 28, 2025
2559111
ns ok: applesilicon + baremetal
Mia-Cross Aug 29, 2025
9d24b13
ns ok: iot + k8s + mongodb
Mia-Cross Sep 1, 2025
9573aa6
ns ok: ipam + redis + secret
Mia-Cross Sep 1, 2025
f13d1de
ns ok: lb + tem
Mia-Cross Sep 1, 2025
c627b79
ns ok: container + function
Mia-Cross Sep 1, 2025
56c16a7
ns ok: block + cockpit + object
Mia-Cross Sep 3, 2025
e7fe572
ns ok: account + iam + instance
Mia-Cross Sep 5, 2025
7939007
customize vcr with additional hooks
Mia-Cross Sep 22, 2025
10c8876
flexibleip ok
Mia-Cross Sep 23, 2025
f5e51d9
vpc ok ? (route=ok, ds-vpc-pn-vpc-id=flaky)
Mia-Cross Sep 23, 2025
091e33d
secret ok
Mia-Cross Sep 23, 2025
a1d2c62
registry ok
Mia-Cross Sep 23, 2025
7456d46
rdb ok
Mia-Cross Sep 23, 2025
891887d
object ok
Mia-Cross Sep 23, 2025
be80f39
mnq ok
Mia-Cross Sep 23, 2025
f7605fe
lb ok
Mia-Cross Sep 23, 2025
b6be8ab
instance ok
Mia-Cross Sep 23, 2025
b0411c8
iam ok
Mia-Cross Sep 23, 2025
f2a1264
container ok
Mia-Cross Sep 23, 2025
4a145b2
fix drift mongodb
Mia-Cross Sep 23, 2025
ea1314f
bump go-vcr.v4 + move matching logic to the sdk-go
Mia-Cross Aug 28, 2025
baa2b7a
lint ok
Mia-Cross Sep 24, 2025
4779c65
fixes after rebase
Mia-Cross Oct 23, 2025
28fcc11
fixes after rebase 2
Mia-Cross Oct 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion cmd/vcr-viewer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"os"

"gopkg.in/dnaeon/go-vcr.v3/cassette"
"gopkg.in/dnaeon/go-vcr.v4/pkg/cassette"
)

func main() {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
golang.org/x/crypto v0.43.0
golang.org/x/sync v0.17.0
gopkg.in/dnaeon/go-vcr.v3 v3.2.0
gopkg.in/dnaeon/go-vcr.v4 v4.0.5
)

require (
Expand Down Expand Up @@ -85,6 +86,7 @@ require (
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/goccy/go-yaml v1.18.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/gookit/color v1.5.1 // indirect
Expand Down
109 changes: 66 additions & 43 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package acctest

import (
"encoding/base64"
"encoding/json"
"encoding/xml"
"flag"
"net/http"
"os"
"strconv"
"strings"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/scaleway-sdk-go/vcr"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/env"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/transport"
"github.com/scaleway/terraform-provider-scaleway/v2/provider"
"github.com/stretchr/testify/require"
"gopkg.in/dnaeon/go-vcr.v4/pkg/cassette"
"gopkg.in/dnaeon/go-vcr.v4/pkg/recorder"
)

// UpdateCassettes will update all cassettes of a given test
var UpdateCassettes = flag.Bool("cassettes", os.Getenv(env.UpdateCassettes) == "true", "Record Cassettes")

func PreCheck(_ *testing.T) {}

type TestTools struct {
Expand All @@ -25,6 +36,58 @@ type TestTools struct {
Cleanup func()
}

// s3Encoder encodes binary payloads as base64 because serialization changed on go-vcr.v4
func s3Encoder(i *cassette.Interaction) error {
if strings.HasSuffix(i.Request.Host, "scw.cloud") {
if i.Request.Body != "" && i.Request.Headers.Get("Content-Type") == "application/octet-stream" {
requestBody := []byte(i.Request.Body)
if !json.Valid(requestBody) {
err := xml.Unmarshal(requestBody, new(any))
if err != nil {
i.Request.Body = base64.StdEncoding.EncodeToString(requestBody)
}
}
}

if i.Response.Body != "" && i.Response.Headers.Get("Content-Type") == "binary/octet-stream" {
responseBody := []byte(i.Response.Body)
if !json.Valid(responseBody) {
err := xml.Unmarshal(responseBody, new(any))
if err != nil {
i.Response.Body = base64.StdEncoding.EncodeToString(responseBody)
}
}
}
}

return nil
}

func NewRecordedClient(t *testing.T, pkgFolder string, update bool) (client *http.Client, cleanup func(), err error) {
t.Helper()

s3EncoderHook := vcr.AdditionalHook{
HookFunc: s3Encoder,
Kind: recorder.AfterCaptureHook,
}

r, err := vcr.NewHTTPRecorder(t, pkgFolder, update, nil, s3EncoderHook)
if err != nil {
return nil, nil, err
}

retryOptions := transport.RetryableTransportOptions{}
if !update {
retryOptions.RetryWaitMax = scw.TimeDurationPtr(0)
}

return &http.Client{
Transport: transport.NewRetryableTransportWithOptions(r, retryOptions),
}, func() {
require.NoError(t, r.Stop()) // Make sure recorder is stopped once done with it
}, nil
}

func NewTestTools(t *testing.T) *TestTools {
t.Helper()

Expand All @@ -35,8 +98,8 @@ func NewTestTools(t *testing.T) *TestTools {
t.Fatalf("cannot detect working directory for testing")
}

// Create a http client with recording capabilities
httpClient, cleanup, err := getHTTPRecoder(t, folder, *UpdateCassettes)
// Create an HTTP client with recording capabilities
httpClient, cleanup, err := NewRecordedClient(t, folder, *UpdateCassettes)
require.NoError(t, err)

// Create meta that will be passed in the provider config
Expand Down Expand Up @@ -84,46 +147,6 @@ func NewTestTools(t *testing.T) *TestTools {
}
}

// Test Generated name has format: "{prefix}-{generated_number}
// example: test-acc-scaleway-project-3723338038624371236
func extractTestGeneratedNamePrefix(name string) string {
// {prefix}-{generated}
// ^
dashIndex := strings.LastIndex(name, "-")

generated := name[dashIndex+1:]
_, generatedToIntErr := strconv.ParseInt(generated, 10, 64)

if dashIndex == -1 || generatedToIntErr != nil {
// some are only {name}
return name
}

// {prefix}
return name[:dashIndex]
}

// Generated names have format: "tf-{prefix}-{generated1}-{generated2}"
// example: tf-sg-gifted-yonath
func extractGeneratedNamePrefix(name string) string {
if strings.Count(name, "-") < 3 {
return name
}
// tf-{prefix}-gifted-yonath
name = strings.TrimPrefix(name, "tf-")

// {prefix}-gifted-yonath
// ^
dashIndex := strings.LastIndex(name, "-")
name = name[:dashIndex]
// {prefix}-gifted
// ^
dashIndex = strings.LastIndex(name, "-")
name = name[:dashIndex]

return name
}

// IsTestResource returns true if given resource identifier is from terraform test
// identifier should be resource name but some resource don't have names
// return true if identifier match regex "tf[-_]test"
Expand Down
6 changes: 3 additions & 3 deletions internal/acctest/acctest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"
"testing"

"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
"gopkg.in/dnaeon/go-vcr.v3/cassette"
"github.com/scaleway/scaleway-sdk-go/vcr"
"gopkg.in/dnaeon/go-vcr.v4/pkg/cassette"
)

var barMemberCreationBody = `{
Expand Down Expand Up @@ -488,7 +488,7 @@ var testBodyMatcherCases = []struct {

func TestCassetteMatcher(t *testing.T) {
for i, test := range testBodyMatcherCases {
shouldMatch := acctest.CassetteMatcher(test.requestBody, *test.cassetteBody)
shouldMatch := vcr.CassetteMatcher(test.requestBody, *test.cassetteBody)
if shouldMatch != test.shouldMatch {
t.Errorf("test %d: expected %v, got %v", i, test.shouldMatch, shouldMatch)
t.Errorf("requestBody: %s", test.requestBody.Body)
Expand Down
Loading
Loading