Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion cmd/vcr-compressor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"log"
"os"
"strings"

"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
)
Expand All @@ -13,8 +14,19 @@ func main() {
}

path := os.Args[1]
folder := strings.Split(path, "/")[2]

var (
report acctest.CompressReport
err error
)

if acctest.FolderUsesVCRv4(folder) {
report, err = acctest.CompressCassetteV4(path)
} else {
report, err = acctest.CompressCassetteV3(path)
}

report, err := acctest.CompressCassette(path)
if err != nil {
log.Fatalf("%s", err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ type TestTools struct {

var foldersUsingVCRv4 = []string{
"instance",
"k8s",
}

func folderUsesVCRv4(fullFolderPath string) bool {
func FolderUsesVCRv4(fullFolderPath string) bool {
fullPathSplit := strings.Split(fullFolderPath, "/")

folder := fullPathSplit[len(fullPathSplit)-1]
Expand Down Expand Up @@ -120,7 +121,7 @@ func NewTestTools(t *testing.T) *TestTools {
cleanup func()
)

if folderUsesVCRv4(folder) {
if FolderUsesVCRv4(folder) {
httpClient, cleanup, err = NewRecordedClient(t, folder, *UpdateCassettes)
} else {
httpClient, cleanup, err = getHTTPRecoder(t, folder, *UpdateCassettes)
Expand Down
14 changes: 13 additions & 1 deletion internal/acctest/compress_cassettes_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package acctest_test

import (
"strings"
"testing"

"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
Expand All @@ -16,7 +17,18 @@ func TestAccCassettes_Compressed(t *testing.T) {

for path := range paths {
g.Go(func() error {
report, errCompression := acctest.CompressCassette(path)
var (
report acctest.CompressReport
errCompression error
)

folder := strings.Split(path, "/")[2]
if acctest.FolderUsesVCRv4(folder) {
report, errCompression = acctest.CompressCassetteV4(path)
} else {
report, errCompression = acctest.CompressCassetteV3(path)
}

require.NoError(t, errCompression)
require.Zero(t, report.SkippedInteraction, "Issue with cassette: %s", report.Path)

Expand Down
97 changes: 93 additions & 4 deletions internal/acctest/vcr_compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
"github.com/scaleway/scaleway-sdk-go/api/rdb/v1"
"github.com/scaleway/scaleway-sdk-go/api/redis/v1"
tem "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1"
"gopkg.in/dnaeon/go-vcr.v3/cassette"
cassetteV3 "gopkg.in/dnaeon/go-vcr.v3/cassette"
cassetteV4 "gopkg.in/dnaeon/go-vcr.v4/pkg/cassette"
)

var transientStates = map[string]bool{
Expand Down Expand Up @@ -113,13 +114,101 @@ func (report *CompressReport) Print() {
}
}

func CompressCassette(path string) (CompressReport, error) {
inputCassette, err := cassette.Load(path)
func CompressCassetteV3(path string) (CompressReport, error) {
inputCassette, err := cassetteV3.Load(path)
if err != nil {
log.Fatalf("Error while reading file : %v\n", err)
}

outputCassette := cassette.New(path)
outputCassette := cassetteV3.New(path)
transitioning := false

report := CompressReport{
SkippedInteraction: 0,
Path: path,
ErrorLogs: []string{},
Logs: []string{},
}

for i := range len(inputCassette.Interactions) {
interaction := inputCassette.Interactions[i]
responseBody := interaction.Response.Body
requestMethod := interaction.Request.Method

if requestMethod != "GET" {
transitioning = false

report.AddLog(fmt.Sprintf("Interaction %d in test %s is not a GET request. Recording it\n", i, path))
outputCassette.AddInteraction(interaction)

continue
}

if responseBody == "" {
report.AddLog(fmt.Sprintf("Interaction %d in test %s got an empty response body. Recording it\n", i, path))
outputCassette.AddInteraction(interaction)

continue
}

var m map[string]any

err := json.Unmarshal([]byte(responseBody), &m)
if err != nil {
report.AddErrorLog(fmt.Sprintf("Interaction %d in test %s have an error with unmarshalling response body: %v. Recording it\n", i, path, err))
outputCassette.AddInteraction(interaction)

continue
}

if m["status"] == nil {
report.AddLog(fmt.Sprintf("Interaction %d in test %s does not contain a status field. Recording it\n", i, path))
outputCassette.AddInteraction(interaction)

continue
}

status := m["status"].(string)
// We test if the state is transient
if _, ok := transientStates[status]; ok {
if transitioning {
report.AddLog(fmt.Sprintf("Interaction %d in test %s is in a transient state while we are already in transitient state. No need to record it: %s\n", i, path, status))
report.SkippedInteraction++
} else {
report.AddLog(fmt.Sprintf("Interaction %d in test %s is in a transient state: %s, Recording it\n", i, path, status))

transitioning = true

outputCassette.AddInteraction(interaction)
}
} else {
if transitioning {
report.AddLog(fmt.Sprintf("Interaction %d in test %s is not in a transient state anymore: %s, Recording it\n", i, path, status))
outputCassette.AddInteraction(interaction)

transitioning = false
} else {
report.AddLog(fmt.Sprintf("Interaction %d in test %s is not in a transient state: %s, Recording it\n", i, path, status))
outputCassette.AddInteraction(interaction)
}
}
}

err = outputCassette.Save()
if err != nil {
return report, fmt.Errorf("error while saving file: %w", err)
}

return report, nil
}

func CompressCassetteV4(path string) (CompressReport, error) {
inputCassette, err := cassetteV4.Load(path)
if err != nil {
log.Fatalf("Error while reading file : %v\n", err)
}

outputCassette := cassetteV4.New(path)
transitioning := false

report := CompressReport{
Expand Down
Loading
Loading