Skip to content

Commit 6fd860d

Browse files
authored
tests(instance): use sdk vcr (go-vcr.v4) for instance package (#5089)
1 parent a8e4496 commit 6fd860d

File tree

159 files changed

+146461
-134851
lines changed

Some content is hidden

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

159 files changed

+146461
-134851
lines changed

core/testing.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,23 @@ func createTestClient(
306306
// because they will be executed without waiting.
307307
var DefaultRetryInterval *time.Duration
308308

309+
var foldersUsingVCRv4 = []string{
310+
"instance",
311+
}
312+
313+
func folderUsesVCRv4(fullFolderPath string) bool {
314+
fullPathSplit := strings.Split(fullFolderPath, "/")
315+
316+
folder := fullPathSplit[len(fullPathSplit)-1]
317+
for _, migratedFolder := range foldersUsingVCRv4 {
318+
if migratedFolder == folder {
319+
return true
320+
}
321+
}
322+
323+
return false
324+
}
325+
309326
// Run a CLI integration test. See TestConfig for configuration option
310327
func Test(config *TestConfig) func(t *testing.T) {
311328
return func(t *testing.T) {
@@ -348,7 +365,23 @@ func Test(config *TestConfig) func(t *testing.T) {
348365
ctx = interactive.InjectMockResponseToContext(ctx, config.PromptResponseMocks)
349366
}
350367

351-
httpClient, cleanup, err := getHTTPRecoder(t, *UpdateCassettes)
368+
folder, err := os.Getwd()
369+
if err != nil {
370+
t.Fatalf("cannot detect working directory for testing")
371+
}
372+
373+
// Create an HTTP client with recording capabilities
374+
var (
375+
httpClient *http.Client
376+
cleanup func()
377+
)
378+
379+
if folderUsesVCRv4(folder) {
380+
httpClient, cleanup, err = newHTTPRecorder(t, folder, *UpdateCassettes)
381+
} else {
382+
httpClient, cleanup, err = getHTTPRecoder(t, *UpdateCassettes)
383+
}
384+
352385
require.NoError(t, err)
353386
defer cleanup()
354387

core/testing_recorder.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/dnaeon/go-vcr/cassette"
1414
"github.com/dnaeon/go-vcr/recorder"
15+
"github.com/scaleway/scaleway-sdk-go/vcr"
1516
"github.com/stretchr/testify/assert"
1617
)
1718

@@ -155,3 +156,16 @@ func getHTTPRecoder(t *testing.T, update bool) (client *http.Client, cleanup fun
155156
assert.NoError(t, r.Stop()) // Make sure recorder is stopped once done with it
156157
}, nil
157158
}
159+
160+
func newHTTPRecorder(t *testing.T, folder string, update bool) (*http.Client, func(), error) {
161+
t.Helper()
162+
163+
r, err := vcr.NewHTTPRecorder(t, folder, update, &SocketPassthroughTransport{})
164+
if err != nil {
165+
return nil, nil, err
166+
}
167+
168+
return &http.Client{Transport: &retryableHTTPTransport{transport: r}}, func() {
169+
assert.NoError(t, r.Stop()) // Make sure recorder is stopped once done with it
170+
}, nil
171+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ require (
119119
github.com/go-logr/logr v1.4.3 // indirect
120120
github.com/go-logr/stdr v1.2.2 // indirect
121121
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
122+
github.com/goccy/go-yaml v1.18.0 // indirect
122123
github.com/gofrs/flock v0.12.1 // indirect
123124
github.com/gogo/protobuf v1.3.2 // indirect
124125
github.com/golang-jwt/jwt/v4 v4.5.2 // indirect
@@ -207,6 +208,7 @@ require (
207208
google.golang.org/genproto/googleapis/rpc v0.0.0-20250721164621-a45f3dfb1074 // indirect
208209
google.golang.org/grpc v1.74.2 // indirect
209210
google.golang.org/protobuf v1.36.9 // indirect
211+
gopkg.in/dnaeon/go-vcr.v4 v4.0.5 // indirect
210212
gopkg.in/warnings.v0 v0.1.2 // indirect
211213
gopkg.in/yaml.v2 v2.4.0 // indirect
212214
gotest.tools/gotestsum v1.12.3 // indirect

internal/namespaces/instance/v1/custom_server_action_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func Test_ServerTerminate(t *testing.T) {
7474
_, err := api.GetIP(&instanceSDK.GetIPRequest{
7575
IP: server.PublicIP.ID,
7676
})
77-
require.ErrorAs(t, err, new(*scw.PermissionsDeniedError))
77+
require.ErrorAs(t, err, new(*scw.ResourceNotFoundError))
7878
},
7979
),
8080
DisableParallel: true,

internal/namespaces/instance/v1/testdata/test-attach-filesystem-attach-filesystem.cassette.yaml

Lines changed: 506 additions & 680 deletions
Large diffs are not rendered by default.

internal/namespaces/instance/v1/testdata/test-attach-filesystem-attach-filesystem.golden

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
22
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3-
Server.ID e127680a-3606-4118-b785-d76e87c16931
4-
Server.Name cli-srv-eager-tereshkova
5-
Server.Organization d3520a52-2c75-4ba0-bda8-82dd087f07f2
6-
Server.Project d3520a52-2c75-4ba0-bda8-82dd087f07f2
3+
Server.ID 8b0cc204-0bd7-4ddb-b57e-f0cd4378ab0f
4+
Server.Name cli-srv-funny-curran
5+
Server.Organization fa1e3217-dc80-42ac-85c3-3f034b78b552
6+
Server.Project fa1e3217-dc80-42ac-85c3-3f034b78b552
77
Server.AllowedActions.0 poweron
88
Server.AllowedActions.1 backup
99
Server.CommercialType POP2-2C-8G
1010
Server.CreationDate few seconds ago
1111
Server.DynamicIPRequired false
1212
Server.RoutedIPEnabled true
1313
Server.EnableIPv6 false
14-
Server.Hostname cli-srv-eager-tereshkova
15-
Server.Image.ID 63d40353-5519-46d0-9172-ccf4885954e1
14+
Server.Hostname cli-srv-funny-curran
15+
Server.Image.ID 6d3c053e-c728-4294-b23a-560b62a4d592
1616
Server.Image.Name Ubuntu 22.04 Jammy Jellyfish
1717
Server.Image.Arch x86_64
1818
Server.Image.CreationDate few seconds ago
@@ -21,34 +21,35 @@ Server.Image.ExtraVolumes 0
2121
Server.Image.FromServer -
2222
Server.Image.Organization 51b656e3-4865-41e8-adbc-0c45bdd780db
2323
Server.Image.Public true
24-
Server.Image.RootVolume 905845fc-a6eb-4401-8e9d-5810071b7119
24+
Server.Image.RootVolume 36b4ce54-c67a-4f68-ab74-839515834352
2525
Server.Image.State available
2626
Server.Image.Project 51b656e3-4865-41e8-adbc-0c45bdd780db
2727
Server.Image.Zone fr-par-1
2828
Server.Protected false
29-
Server.MacAddress de:00:00:b5:91:c3
29+
Server.MacAddress de:00:00:d0:04:a1
3030
Server.ModificationDate few seconds ago
3131
Server.State archived
3232
Server.BootType local
33-
Server.Volumes.0.ID 26f21c8e-6de6-443e-8d64-d039c9e2dfa6
33+
Server.Volumes.0.ID d98ba8fa-0e20-45df-99de-ed186191d104
3434
Server.Volumes.0.VolumeType sbs_volume
35+
Server.Volumes.0.State available
3536
Server.Volumes.0.Boot false
3637
Server.Volumes.0.Zone fr-par-1
37-
Server.SecurityGroup.ID 1c1fc99f-b2de-408d-9eb0-da801a3e2744
38+
Server.SecurityGroup.ID da505169-540e-4c2b-b0da-c854139224e0
3839
Server.SecurityGroup.Name Default security group
3940
Server.StateDetail -
4041
Server.Arch x86_64
4142
Server.Zone fr-par-1
42-
Server.Filesystems.0.FilesystemID aab9e768-c1e9-407a-829c-4be5a38787dd
43+
Server.Filesystems.0.FilesystemID 76ec2ddd-9383-4898-bf7d-d3cbf2daac0f
4344
Server.Filesystems.0.State available
4445
Server.EndOfService false
4546
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
4647
{
4748
"server": {
48-
"id": "e127680a-3606-4118-b785-d76e87c16931",
49-
"name": "cli-srv-eager-tereshkova",
50-
"organization": "d3520a52-2c75-4ba0-bda8-82dd087f07f2",
51-
"project": "d3520a52-2c75-4ba0-bda8-82dd087f07f2",
49+
"id": "8b0cc204-0bd7-4ddb-b57e-f0cd4378ab0f",
50+
"name": "cli-srv-funny-curran",
51+
"organization": "fa1e3217-dc80-42ac-85c3-3f034b78b552",
52+
"project": "fa1e3217-dc80-42ac-85c3-3f034b78b552",
5253
"allowed_actions": [
5354
"poweron",
5455
"backup"
@@ -59,9 +60,9 @@ Server.EndOfService false
5960
"dynamic_ip_required": false,
6061
"routed_ip_enabled": true,
6162
"enable_ipv6": false,
62-
"hostname": "cli-srv-eager-tereshkova",
63+
"hostname": "cli-srv-funny-curran",
6364
"image": {
64-
"id": "63d40353-5519-46d0-9172-ccf4885954e1",
65+
"id": "6d3c053e-c728-4294-b23a-560b62a4d592",
6566
"name": "Ubuntu 22.04 Jammy Jellyfish",
6667
"arch": "x86_64",
6768
"creation_date": "1970-01-01T00:00:00.0Z",
@@ -72,7 +73,7 @@ Server.EndOfService false
7273
"organization": "51b656e3-4865-41e8-adbc-0c45bdd780db",
7374
"public": true,
7475
"root_volume": {
75-
"id": "905845fc-a6eb-4401-8e9d-5810071b7119",
76+
"id": "36b4ce54-c67a-4f68-ab74-839515834352",
7677
"name": "",
7778
"size": 0,
7879
"volume_type": "sbs_snapshot"
@@ -86,15 +87,15 @@ Server.EndOfService false
8687
"private_ip": null,
8788
"public_ip": null,
8889
"public_ips": [],
89-
"mac_address": "de:00:00:b5:91:c3",
90+
"mac_address": "de:00:00:d0:04:a1",
9091
"modification_date": "1970-01-01T00:00:00.0Z",
9192
"state": "stopped",
9293
"location": null,
9394
"ipv6": null,
9495
"boot_type": "local",
9596
"volumes": {
9697
"0": {
97-
"id": "26f21c8e-6de6-443e-8d64-d039c9e2dfa6",
98+
"id": "d98ba8fa-0e20-45df-99de-ed186191d104",
9899
"name": null,
99100
"export_uri": null,
100101
"organization": null,
@@ -103,14 +104,14 @@ Server.EndOfService false
103104
"volume_type": "sbs_volume",
104105
"creation_date": null,
105106
"modification_date": null,
106-
"state": null,
107+
"state": "available",
107108
"project": null,
108109
"boot": false,
109110
"zone": "fr-par-1"
110111
}
111112
},
112113
"security_group": {
113-
"id": "1c1fc99f-b2de-408d-9eb0-da801a3e2744",
114+
"id": "da505169-540e-4c2b-b0da-c854139224e0",
114115
"name": "Default security group"
115116
},
116117
"maintenances": [],
@@ -123,7 +124,7 @@ Server.EndOfService false
123124
"admin_password_encrypted_value": null,
124125
"filesystems": [
125126
{
126-
"filesystem_id": "aab9e768-c1e9-407a-829c-4be5a38787dd",
127+
"filesystem_id": "76ec2ddd-9383-4898-bf7d-d3cbf2daac0f",
127128
"state": "available"
128129
}
129130
],

0 commit comments

Comments
 (0)