Skip to content

Commit cbf408e

Browse files
authored
tests: handle v4 cassettes in vcr-compressor (#3430)
* handle v4 cassettes in vcr-compressor * new cassettes for instance
1 parent ed9d79b commit cbf408e

File tree

78 files changed

+149500
-187818
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+149500
-187818
lines changed

cmd/vcr-compressor/main.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"log"
55
"os"
6+
"strings"
67

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

1516
path := os.Args[1]
17+
folder := strings.Split(path, "/")[2]
18+
19+
var (
20+
report acctest.CompressReport
21+
err error
22+
)
23+
24+
if acctest.FolderUsesVCRv4(folder) {
25+
report, err = acctest.CompressCassetteV4(path)
26+
} else {
27+
report, err = acctest.CompressCassetteV3(path)
28+
}
1629

17-
report, err := acctest.CompressCassette(path)
1830
if err != nil {
1931
log.Fatalf("%s", err)
2032
}

internal/acctest/acctest.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ type TestTools struct {
3535

3636
var foldersUsingVCRv4 = []string{
3737
"instance",
38+
"k8s",
3839
}
3940

40-
func folderUsesVCRv4(fullFolderPath string) bool {
41+
func FolderUsesVCRv4(fullFolderPath string) bool {
4142
fullPathSplit := strings.Split(fullFolderPath, "/")
4243

4344
folder := fullPathSplit[len(fullPathSplit)-1]
@@ -120,7 +121,7 @@ func NewTestTools(t *testing.T) *TestTools {
120121
cleanup func()
121122
)
122123

123-
if folderUsesVCRv4(folder) {
124+
if FolderUsesVCRv4(folder) {
124125
httpClient, cleanup, err = NewRecordedClient(t, folder, *UpdateCassettes)
125126
} else {
126127
httpClient, cleanup, err = getHTTPRecoder(t, folder, *UpdateCassettes)

internal/acctest/compress_cassettes_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package acctest_test
22

33
import (
4+
"strings"
45
"testing"
56

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

1718
for path := range paths {
1819
g.Go(func() error {
19-
report, errCompression := acctest.CompressCassette(path)
20+
var (
21+
report acctest.CompressReport
22+
errCompression error
23+
)
24+
25+
folder := strings.Split(path, "/")[2]
26+
if acctest.FolderUsesVCRv4(folder) {
27+
report, errCompression = acctest.CompressCassetteV4(path)
28+
} else {
29+
report, errCompression = acctest.CompressCassetteV3(path)
30+
}
31+
2032
require.NoError(t, errCompression)
2133
require.Zero(t, report.SkippedInteraction, "Issue with cassette: %s", report.Path)
2234

internal/acctest/vcr_compress.go

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import (
1818
"github.com/scaleway/scaleway-sdk-go/api/rdb/v1"
1919
"github.com/scaleway/scaleway-sdk-go/api/redis/v1"
2020
tem "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1"
21-
"gopkg.in/dnaeon/go-vcr.v3/cassette"
21+
cassetteV3 "gopkg.in/dnaeon/go-vcr.v3/cassette"
22+
cassetteV4 "gopkg.in/dnaeon/go-vcr.v4/pkg/cassette"
2223
)
2324

2425
var transientStates = map[string]bool{
@@ -113,13 +114,101 @@ func (report *CompressReport) Print() {
113114
}
114115
}
115116

116-
func CompressCassette(path string) (CompressReport, error) {
117-
inputCassette, err := cassette.Load(path)
117+
func CompressCassetteV3(path string) (CompressReport, error) {
118+
inputCassette, err := cassetteV3.Load(path)
118119
if err != nil {
119120
log.Fatalf("Error while reading file : %v\n", err)
120121
}
121122

122-
outputCassette := cassette.New(path)
123+
outputCassette := cassetteV3.New(path)
124+
transitioning := false
125+
126+
report := CompressReport{
127+
SkippedInteraction: 0,
128+
Path: path,
129+
ErrorLogs: []string{},
130+
Logs: []string{},
131+
}
132+
133+
for i := range len(inputCassette.Interactions) {
134+
interaction := inputCassette.Interactions[i]
135+
responseBody := interaction.Response.Body
136+
requestMethod := interaction.Request.Method
137+
138+
if requestMethod != "GET" {
139+
transitioning = false
140+
141+
report.AddLog(fmt.Sprintf("Interaction %d in test %s is not a GET request. Recording it\n", i, path))
142+
outputCassette.AddInteraction(interaction)
143+
144+
continue
145+
}
146+
147+
if responseBody == "" {
148+
report.AddLog(fmt.Sprintf("Interaction %d in test %s got an empty response body. Recording it\n", i, path))
149+
outputCassette.AddInteraction(interaction)
150+
151+
continue
152+
}
153+
154+
var m map[string]any
155+
156+
err := json.Unmarshal([]byte(responseBody), &m)
157+
if err != nil {
158+
report.AddErrorLog(fmt.Sprintf("Interaction %d in test %s have an error with unmarshalling response body: %v. Recording it\n", i, path, err))
159+
outputCassette.AddInteraction(interaction)
160+
161+
continue
162+
}
163+
164+
if m["status"] == nil {
165+
report.AddLog(fmt.Sprintf("Interaction %d in test %s does not contain a status field. Recording it\n", i, path))
166+
outputCassette.AddInteraction(interaction)
167+
168+
continue
169+
}
170+
171+
status := m["status"].(string)
172+
// We test if the state is transient
173+
if _, ok := transientStates[status]; ok {
174+
if transitioning {
175+
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))
176+
report.SkippedInteraction++
177+
} else {
178+
report.AddLog(fmt.Sprintf("Interaction %d in test %s is in a transient state: %s, Recording it\n", i, path, status))
179+
180+
transitioning = true
181+
182+
outputCassette.AddInteraction(interaction)
183+
}
184+
} else {
185+
if transitioning {
186+
report.AddLog(fmt.Sprintf("Interaction %d in test %s is not in a transient state anymore: %s, Recording it\n", i, path, status))
187+
outputCassette.AddInteraction(interaction)
188+
189+
transitioning = false
190+
} else {
191+
report.AddLog(fmt.Sprintf("Interaction %d in test %s is not in a transient state: %s, Recording it\n", i, path, status))
192+
outputCassette.AddInteraction(interaction)
193+
}
194+
}
195+
}
196+
197+
err = outputCassette.Save()
198+
if err != nil {
199+
return report, fmt.Errorf("error while saving file: %w", err)
200+
}
201+
202+
return report, nil
203+
}
204+
205+
func CompressCassetteV4(path string) (CompressReport, error) {
206+
inputCassette, err := cassetteV4.Load(path)
207+
if err != nil {
208+
log.Fatalf("Error while reading file : %v\n", err)
209+
}
210+
211+
outputCassette := cassetteV4.New(path)
123212
transitioning := false
124213

125214
report := CompressReport{

0 commit comments

Comments
 (0)