Skip to content

Commit f406ed3

Browse files
committed
chore: add support for vcr compression and trim k8s cassettes
1 parent 555a269 commit f406ed3

16 files changed

+7763
-102776
lines changed

cmd/vcr-compressor/main.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
7+
"gopkg.in/dnaeon/go-vcr.v3/cassette"
8+
"log"
9+
"os"
10+
)
11+
12+
var transientStates = map[string]bool{
13+
k8s.ClusterStatusCreating.String(): true,
14+
k8s.ClusterStatusDeleting.String(): true,
15+
k8s.ClusterStatusUpdating.String(): true,
16+
k8s.PoolStatusDeleting.String(): true,
17+
k8s.PoolStatusScaling.String(): true,
18+
k8s.PoolStatusUpgrading.String(): true,
19+
}
20+
21+
func main() {
22+
if len(os.Args) < 2 {
23+
log.Fatalf("Usage: %s <chemin_du_fichier_yaml>\n", os.Args[0])
24+
}
25+
26+
chemin := os.Args[1]
27+
inputCassette, err := cassette.Load(chemin)
28+
if err != nil {
29+
log.Fatalf("Erreur de lecture du fichier : %v\n", err)
30+
}
31+
32+
outputCassette := cassette.New(chemin)
33+
transitioning := false
34+
for i := 0; i < len(inputCassette.Interactions); i++ {
35+
interaction := inputCassette.Interactions[i]
36+
responseBody := interaction.Response.Body
37+
requestMethod := interaction.Request.Method
38+
39+
if requestMethod != "GET" {
40+
transitioning = false
41+
log.Printf("Interaction %d is not a GET request. Recording it\n", i)
42+
outputCassette.AddInteraction(interaction)
43+
continue
44+
}
45+
46+
if responseBody == "" {
47+
log.Printf("Interaction %d got an empty response body. Recording it\n", i)
48+
outputCassette.AddInteraction(interaction)
49+
continue
50+
}
51+
52+
var m map[string]interface{}
53+
err := json.Unmarshal([]byte(responseBody), &m)
54+
if err != nil {
55+
fmt.Printf("Interaction %d have an error with unmarshalling reponse body: %v. Recording it\n", i, err)
56+
outputCassette.AddInteraction(interaction)
57+
continue
58+
}
59+
60+
if m["status"] == nil {
61+
log.Printf("Interaction %d does not contain a status field. Recording it\n", i)
62+
outputCassette.AddInteraction(interaction)
63+
continue
64+
}
65+
66+
if m["status"] != nil {
67+
// We test if the state is transient
68+
if _, ok := transientStates[m["status"].(string)]; ok {
69+
if transitioning {
70+
fmt.Printf("Interaction %d is in a transient state while we are already in transitient state. No need to record it: %s\n", i, m["status"])
71+
continue
72+
} else {
73+
transitioning = true
74+
fmt.Printf("Interaction %d is in a transient state: %s, Recording it\n", i, m["status"])
75+
outputCassette.AddInteraction(interaction)
76+
}
77+
} else {
78+
if transitioning {
79+
fmt.Printf("Interaction %d is not in a transient state anymore: %s, Recording it\n", i, m["status"])
80+
outputCassette.AddInteraction(interaction)
81+
transitioning = false
82+
} else {
83+
fmt.Printf("Interaction %d is not in a transient state: %s, Recording it\n", i, m["status"])
84+
outputCassette.AddInteraction(interaction)
85+
}
86+
87+
}
88+
}
89+
}
90+
91+
outputCassette.Save()
92+
}

cmd/vcr-viewer/main.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"gopkg.in/dnaeon/go-vcr.v3/cassette"
7+
"log"
8+
"os"
9+
)
10+
11+
func main() {
12+
if len(os.Args) < 2 {
13+
log.Fatalf("Usage: %s <chemin_du_fichier_yaml>\n", os.Args[0])
14+
}
15+
16+
chemin := os.Args[1]
17+
data, err := cassette.Load(chemin)
18+
if err != nil {
19+
log.Fatalf("Erreur de lecture du fichier : %v\n", err)
20+
}
21+
22+
for i := 0; i < len(data.Interactions); i++ {
23+
interaction := data.Interactions[i]
24+
fmt.Println("--------------")
25+
fmt.Printf("Interaction %d:\n", i+1)
26+
fmt.Printf(" Request:\n")
27+
fmt.Printf(" Method: %s\n", interaction.Request.Method)
28+
fmt.Printf(" URL: %s\n", interaction.Request.URL)
29+
if interaction.Request.Body != "" {
30+
fmt.Printf(" Body: %s\n", interaction.Request.Body)
31+
}
32+
fmt.Printf(" Response:\n")
33+
fmt.Printf(" Status: %s\n", interaction.Response.Status)
34+
fmt.Printf(" Body: %s\n", interaction.Response.Body)
35+
36+
var m map[string]interface{}
37+
err := json.Unmarshal([]byte(interaction.Response.Body), &m)
38+
if err != nil {
39+
continue
40+
}
41+
if m["status"] != nil {
42+
fmt.Println("++++++++++++++++")
43+
fmt.Printf("status: %s\n", m["status"]) // Modifie le champ "status" pour qu'il soit "ok"
44+
fmt.Println("++++++++++++++++")
45+
}
46+
fmt.Println("--------------")
47+
}
48+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package acctest_test
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"github.com/stretchr/testify/require"
6+
"gopkg.in/dnaeon/go-vcr.v3/cassette"
7+
"testing"
8+
)
9+
10+
func TestAccCassettes_Compressed(t *testing.T) {
11+
paths, err := getTestFiles()
12+
require.NoError(t, err)
13+
14+
for path := range paths {
15+
c, err := cassette.Load(path)
16+
require.NoError(t, err)
17+
assert.NoError(t, checkCassetteCompressed(t, c))
18+
}
19+
}
20+
21+
func checkCassetteCompressed(t *testing.T, c *cassette.Cassette) error {
22+
for i := 0; i < len(c.Interactions)-1; i++ {
23+
if c.Interactions[i].Response.Body == c.Interactions[i+1].Response.Body {
24+
t.Errorf("cassette %s contains two consecutive interactions with the same response body. Interaction %d and %d have the same body", c.Name, i, i+1)
25+
}
26+
}
27+
return nil
28+
}

internal/services/k8s/testdata/cluster-multicloud.cassette.yaml

Lines changed: 165 additions & 6633 deletions
Large diffs are not rendered by default.

internal/services/k8s/testdata/cluster-pool-private-network.cassette.yaml

Lines changed: 2013 additions & 3506 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)