Skip to content

test: have default region overwrite the configuration file #744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions internal/core/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ func getTestFilePath(t *testing.T, suffix string) string {
}

func getTestClient(t *testing.T, testConfig *TestConfig) (client *scw.Client, cleanup func()) {
var err error
cleanup = func() {}

// Init default options
clientOpts := []scw.ClientOption{
scw.WithDefaultRegion(scw.RegionFrPar),
scw.WithDefaultZone(scw.ZoneFrPar1),
Expand All @@ -124,39 +128,42 @@ func getTestClient(t *testing.T, testConfig *TestConfig) (client *scw.Client, cl
scw.WithDefaultOrganizationID("11111111-1111-1111-1111-111111111111"),
}

if testConfig.DefaultRegion != "" {
clientOpts = append(clientOpts, scw.WithDefaultRegion(testConfig.DefaultRegion))
}

if testConfig.DefaultZone != "" {
clientOpts = append(clientOpts, scw.WithDefaultZone(testConfig.DefaultZone))
}

// If client is NOT an E2E client we init http recorder and load configuration.
if !testConfig.UseE2EClient {
httpClient, cleanup, err := getHTTPRecoder(t, UpdateCassettes)
var httpClient *http.Client
httpClient, cleanup, err = getHTTPRecoder(t, UpdateCassettes)
require.NoError(t, err)
clientOpts = append(clientOpts, scw.WithHTTPClient(httpClient))

config, err := scw.LoadConfig()
if err == nil {
p, err := config.GetActiveProfile()
require.NoError(t, err)
clientOpts = append(clientOpts, scw.WithProfile(p))
}
}

client, err := scw.NewClient(clientOpts...)
require.NoError(t, err)
return client, cleanup
// We handle default zone and region configured specifically for a test
if testConfig.DefaultRegion != "" {
clientOpts = append(clientOpts, scw.WithDefaultRegion(testConfig.DefaultRegion))
}
if testConfig.DefaultZone != "" {
clientOpts = append(clientOpts, scw.WithDefaultZone(testConfig.DefaultZone))
}

client, err := scw.NewClient(clientOpts...)
require.NoError(t, err)
res, err := test.NewAPI(client).Register(&test.RegisterRequest{Username: "sidi"})
client, err = scw.NewClient(clientOpts...)
require.NoError(t, err)

client, err = scw.NewClient(append(clientOpts, scw.WithAuth(res.AccessKey, res.SecretKey))...)
require.NoError(t, err)
// If client is an E2E client we must register and use returned credential.
if testConfig.UseE2EClient {
res, err := test.NewAPI(client).Register(&test.RegisterRequest{Username: "sidi"})
require.NoError(t, err)

client, err = scw.NewClient(append(clientOpts, scw.WithAuth(res.AccessKey, res.SecretKey))...)
require.NoError(t, err)
}

return client, func() {}
return client, cleanup
}

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