Skip to content

Commit 8d9afd0

Browse files
authored
test: have default region overwrite the configuration file (#744)
* fix: Have default region overwrite the configuration file Co-authored-by: Jérôme Quéré <[email protected]>
1 parent 080706d commit 8d9afd0

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

internal/core/testing.go

+25-18
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ func getTestFilePath(t *testing.T, suffix string) string {
116116
}
117117

118118
func getTestClient(t *testing.T, testConfig *TestConfig) (client *scw.Client, cleanup func()) {
119+
var err error
120+
cleanup = func() {}
121+
122+
// Init default options
119123
clientOpts := []scw.ClientOption{
120124
scw.WithDefaultRegion(scw.RegionFrPar),
121125
scw.WithDefaultZone(scw.ZoneFrPar1),
@@ -124,39 +128,42 @@ func getTestClient(t *testing.T, testConfig *TestConfig) (client *scw.Client, cl
124128
scw.WithDefaultOrganizationID("11111111-1111-1111-1111-111111111111"),
125129
}
126130

127-
if testConfig.DefaultRegion != "" {
128-
clientOpts = append(clientOpts, scw.WithDefaultRegion(testConfig.DefaultRegion))
129-
}
130-
131-
if testConfig.DefaultZone != "" {
132-
clientOpts = append(clientOpts, scw.WithDefaultZone(testConfig.DefaultZone))
133-
}
134-
131+
// If client is NOT an E2E client we init http recorder and load configuration.
135132
if !testConfig.UseE2EClient {
136-
httpClient, cleanup, err := getHTTPRecoder(t, UpdateCassettes)
133+
var httpClient *http.Client
134+
httpClient, cleanup, err = getHTTPRecoder(t, UpdateCassettes)
137135
require.NoError(t, err)
138136
clientOpts = append(clientOpts, scw.WithHTTPClient(httpClient))
137+
139138
config, err := scw.LoadConfig()
140139
if err == nil {
141140
p, err := config.GetActiveProfile()
142141
require.NoError(t, err)
143142
clientOpts = append(clientOpts, scw.WithProfile(p))
144143
}
144+
}
145145

146-
client, err := scw.NewClient(clientOpts...)
147-
require.NoError(t, err)
148-
return client, cleanup
146+
// We handle default zone and region configured specifically for a test
147+
if testConfig.DefaultRegion != "" {
148+
clientOpts = append(clientOpts, scw.WithDefaultRegion(testConfig.DefaultRegion))
149+
}
150+
if testConfig.DefaultZone != "" {
151+
clientOpts = append(clientOpts, scw.WithDefaultZone(testConfig.DefaultZone))
149152
}
150153

151-
client, err := scw.NewClient(clientOpts...)
152-
require.NoError(t, err)
153-
res, err := test.NewAPI(client).Register(&test.RegisterRequest{Username: "sidi"})
154+
client, err = scw.NewClient(clientOpts...)
154155
require.NoError(t, err)
155156

156-
client, err = scw.NewClient(append(clientOpts, scw.WithAuth(res.AccessKey, res.SecretKey))...)
157-
require.NoError(t, err)
157+
// If client is an E2E client we must register and use returned credential.
158+
if testConfig.UseE2EClient {
159+
res, err := test.NewAPI(client).Register(&test.RegisterRequest{Username: "sidi"})
160+
require.NoError(t, err)
161+
162+
client, err = scw.NewClient(append(clientOpts, scw.WithAuth(res.AccessKey, res.SecretKey))...)
163+
require.NoError(t, err)
164+
}
158165

159-
return client, func() {}
166+
return client, cleanup
160167
}
161168

162169
// Run a CLI integration test. See TestConfig for configuration option

0 commit comments

Comments
 (0)