Skip to content

Commit 02e77c4

Browse files
committed
wip
1 parent f9082eb commit 02e77c4

File tree

5 files changed

+72
-10
lines changed

5 files changed

+72
-10
lines changed

internal/acctest/acctest.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func NewTestTools(t *testing.T) *TestTools {
3535
}
3636

3737
// Create a http client with recording capabilities
38-
httpClient, cleanup, err := getHTTPRecoder(t, folder, *UpdateCassettes)
38+
httpClient, cleanup, err := getHTTPRecoder(t, folder, *UpdateCassettes, cassetteMatcher)
3939
require.NoError(t, err)
4040

4141
// Create meta that will be passed in the provider config
@@ -65,6 +65,45 @@ func NewTestTools(t *testing.T) *TestTools {
6565
}
6666
}
6767

68+
func NewTestToolsWithoutRequestProjectID(t *testing.T) *TestTools {
69+
t.Helper()
70+
71+
ctx := t.Context()
72+
73+
folder, err := os.Getwd()
74+
if err != nil {
75+
t.Fatalf("cannot detect working directory for testing")
76+
}
77+
78+
// Create a http client with recording capabilities
79+
httpClient, cleanup, err := getHTTPRecoder(t, folder, *UpdateCassettes, cassetteMatcherWithoutProjectID)
80+
require.NoError(t, err)
81+
82+
// Create meta that will be passed in the provider config
83+
m, err := meta.NewMeta(ctx, &meta.Config{
84+
ProviderSchema: nil,
85+
TerraformVersion: "terraform-tests",
86+
HTTPClient: httpClient,
87+
})
88+
require.NoError(t, err)
89+
90+
if !*UpdateCassettes {
91+
tmp := 0 * time.Second
92+
transport.DefaultWaitRetryInterval = &tmp
93+
}
94+
95+
return &TestTools{
96+
T: t,
97+
Meta: m,
98+
ProviderFactories: map[string]func() (*schema.Provider, error){
99+
"scaleway": func() (*schema.Provider, error) {
100+
return provider.Provider(&provider.Config{Meta: m})(), nil
101+
},
102+
},
103+
Cleanup: cleanup,
104+
}
105+
}
106+
68107
// Test Generated name has format: "{prefix}-{generated_number}
69108
// example: test-acc-scaleway-project-3723338038624371236
70109
func extractTestGeneratedNamePrefix(name string) string {

internal/acctest/vcr.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ var SensitiveFields = map[string]interface{}{
3535
// QueryMatcherIgnore contains the list of query value that should be ignored when matching requests with cassettes
3636
var QueryMatcherIgnore = []string{
3737
"organization_id",
38-
"project_id",
3938
}
4039

4140
// BodyMatcherIgnore contains the list of json body keys that should be ignored when matching requests with cassettes
@@ -236,6 +235,30 @@ func cassetteMatcher(actual *http.Request, expected cassette.Request) bool {
236235
cassetteBodyMatcher(actual, expected)
237236
}
238237

238+
// cassetteMatcherWithoutProjectID is a custom matcher that check equivalence of a played request against a recorded one
239+
// It compares method, path and query but will remove unwanted values from query
240+
func cassetteMatcherWithoutProjectID(actual *http.Request, expected cassette.Request) bool {
241+
expectedURL, _ := url.Parse(expected.URL)
242+
actualURL := actual.URL
243+
actualURLValues := actualURL.Query()
244+
expectedURLValues := expectedURL.Query()
245+
246+
for _, query := range QueryMatcherIgnore {
247+
actualURLValues.Del(query)
248+
expectedURLValues.Del(query)
249+
}
250+
actualURLValues.Del("project_id")
251+
expectedURLValues.Del("project_id")
252+
253+
actualURL.RawQuery = actualURLValues.Encode()
254+
expectedURL.RawQuery = expectedURLValues.Encode()
255+
256+
return actual.Method == expected.Method &&
257+
actual.URL.Path == expectedURL.Path &&
258+
actualURL.RawQuery == expectedURL.RawQuery &&
259+
cassetteBodyMatcher(actual, expected)
260+
}
261+
239262
func cassetteSensitiveFieldsAnonymizer(i *cassette.Interaction) error {
240263
var jsonBody map[string]interface{}
241264

@@ -267,7 +290,7 @@ func cassetteSensitiveFieldsAnonymizer(i *cassette.Interaction) error {
267290
//
268291
// It is important to add a `defer cleanup()` so the given cassette files are correctly
269292
// closed and saved after the requests.
270-
func getHTTPRecoder(t *testing.T, pkgFolder string, update bool) (client *http.Client, cleanup func(), err error) {
293+
func getHTTPRecoder(t *testing.T, pkgFolder string, update bool, matcherFunc cassette.MatcherFunc) (client *http.Client, cleanup func(), err error) {
271294
t.Helper()
272295

273296
recorderMode := recorder.ModeReplayOnly
@@ -298,7 +321,7 @@ func getHTTPRecoder(t *testing.T, pkgFolder string, update bool) (client *http.C
298321
}(r)
299322

300323
// Add custom matcher for requests and cassettes
301-
r.SetMatcher(cassetteMatcher)
324+
r.SetMatcher(matcherFunc)
302325

303326
// Add a filter which removes Authorization headers from all requests:
304327
r.AddHook(func(i *cassette.Interaction) error {

internal/services/container/container_data_source_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestAccDataSourceContainer_Basic(t *testing.T) {
11-
tt := acctest.NewTestTools(t)
11+
tt := acctest.NewTestToolsWithoutRequestProjectID(t)
1212
defer tt.Cleanup()
1313

1414
resource.ParallelTest(t, resource.TestCase{
@@ -30,7 +30,7 @@ func TestAccDataSourceContainer_Basic(t *testing.T) {
3030
namespace_id = scaleway_container_namespace.main.id
3131
name = scaleway_container.main.name
3232
}
33-
33+
3434
data "scaleway_container" "by_id" {
3535
namespace_id = scaleway_container_namespace.main.id
3636
container_id = scaleway_container.main.id

internal/services/container/container_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func TestAccContainer_Env(t *testing.T) {
208208
}
209209

210210
func TestAccContainer_WithIMG(t *testing.T) {
211-
tt := acctest.NewTestTools(t)
211+
tt := acctest.NewTestToolsWithoutRequestProjectID(t)
212212
defer tt.Cleanup()
213213

214214
containerNamespace := "test-cr-ns-02"

internal/services/container/namespace_data_source_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestAccDataSourceNamespace_Basic(t *testing.T) {
11-
tt := acctest.NewTestTools(t)
11+
tt := acctest.NewTestToolsWithoutRequestProjectID(t)
1212
defer tt.Cleanup()
1313

1414
resource.ParallelTest(t, resource.TestCase{
@@ -21,11 +21,11 @@ func TestAccDataSourceNamespace_Basic(t *testing.T) {
2121
resource "scaleway_container_namespace" "main" {
2222
name = "test-cr-data"
2323
}
24-
24+
2525
data "scaleway_container_namespace" "by_name" {
2626
name = scaleway_container_namespace.main.name
2727
}
28-
28+
2929
data "scaleway_container_namespace" "by_id" {
3030
namespace_id = scaleway_container_namespace.main.id
3131
}

0 commit comments

Comments
 (0)