Skip to content

Commit 6fb6fd1

Browse files
authored
Fix test runner for sdk (#1306)
* fix: reset user directory for tests to avoid automatic lookup of real keys during test * fix: encapsulated homedir retrieval to allow testing without interfering with real user home * fix: Add review findings * fixed linting issue * fix: save home directory for every testcase
1 parent e3fcbdd commit 6fb6fd1

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

core/auth/auth.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const (
2727
privateKeyPathCredentialType credentialType = "private_key_path"
2828
)
2929

30+
var userHomeDir = os.UserHomeDir
31+
3032
// SetupAuth sets up authentication based on the configuration. The different options are
3133
// custom authentication, no authentication, explicit key flow, explicit token flow or default authentication
3234
func SetupAuth(cfg *config.Configuration) (rt http.RoundTripper, err error) {
@@ -195,7 +197,7 @@ func readCredentialsFile(path string) (*Credentials, error) {
195197
customPath, customPathSet := os.LookupEnv("STACKIT_CREDENTIALS_PATH")
196198
if !customPathSet || customPath == "" {
197199
path = credentialsFilePath
198-
home, err := os.UserHomeDir()
200+
home, err := userHomeDir()
199201
if err != nil {
200202
return nil, fmt.Errorf("getting home directory: %w", err)
201203
}

core/auth/auth_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ import (
1616
"github.com/stackitcloud/stackit-sdk-go/core/config"
1717
)
1818

19+
func setTemporaryHome(t *testing.T) {
20+
old := userHomeDir
21+
t.Cleanup(func() {
22+
userHomeDir = old
23+
})
24+
userHomeDir = func() (string, error) {
25+
return t.TempDir(), nil
26+
}
27+
}
28+
1929
func fixtureServiceAccountKey(mods ...func(*clients.ServiceAccountKeyResponse)) *clients.ServiceAccountKeyResponse {
2030
validUntil := time.Now().Add(time.Hour)
2131
serviceAccountKeyResponse := &clients.ServiceAccountKeyResponse{
@@ -150,6 +160,7 @@ func TestSetupAuth(t *testing.T) {
150160
},
151161
} {
152162
t.Run(test.desc, func(t *testing.T) {
163+
setTemporaryHome(t)
153164
if test.setKeys {
154165
t.Setenv("STACKIT_SERVICE_ACCOUNT_KEY_PATH", saKeyFile.Name())
155166
t.Setenv("STACKIT_PRIVATE_KEY_PATH", privateKeyFile.Name())
@@ -232,6 +243,7 @@ func TestReadCredentials(t *testing.T) {
232243
},
233244
} {
234245
t.Run(test.desc, func(t *testing.T) {
246+
setTemporaryHome(t)
235247
t.Setenv("STACKIT_CREDENTIALS_PATH", test.pathEnv)
236248

237249
var credential string
@@ -342,6 +354,7 @@ func TestDefaultAuth(t *testing.T) {
342354
},
343355
} {
344356
t.Run(test.desc, func(t *testing.T) {
357+
setTemporaryHome(t)
345358
if test.setKeys {
346359
t.Setenv("STACKIT_SERVICE_ACCOUNT_KEY_PATH", saKeyFile.Name())
347360
t.Setenv("STACKIT_PRIVATE_KEY_PATH", privateKeyFile.Name())
@@ -406,6 +419,7 @@ func TestTokenAuth(t *testing.T) {
406419
},
407420
} {
408421
t.Run(test.desc, func(t *testing.T) {
422+
setTemporaryHome(t)
409423
t.Setenv("STACKIT_SERVICE_ACCOUNT_TOKEN", "")
410424
t.Setenv("STACKIT_CREDENTIALS_PATH", "test-path")
411425

@@ -499,6 +513,7 @@ func TestKeyAuth(t *testing.T) {
499513
},
500514
} {
501515
t.Run(test.desc, func(t *testing.T) {
516+
setTemporaryHome(t)
502517
t.Setenv("STACKIT_SERVICE_ACCOUNT_KEY", "")
503518
t.Setenv("STACKIT_SERVICE_ACCOUNT_KEY_PATH", "")
504519
t.Setenv("STACKIT_PRIVATE_KEY", "")
@@ -557,7 +572,7 @@ func TestNoAuth(t *testing.T) {
557572
},
558573
} {
559574
t.Run(test.desc, func(t *testing.T) {
560-
// Get the default authentication client and ensure that it's not nil
575+
setTemporaryHome(t) // Get the default authentication client and ensure that it's not nil
561576
authClient, err := NoAuth()
562577
if err != nil {
563578
t.Fatalf("Test returned error on valid test case: %v", err)
@@ -624,6 +639,7 @@ func TestGetServiceAccountEmail(t *testing.T) {
624639
},
625640
} {
626641
t.Run(test.description, func(t *testing.T) {
642+
setTemporaryHome(t)
627643
if test.envEmailSet {
628644
t.Setenv("STACKIT_SERVICE_ACCOUNT_EMAIL", "env_email")
629645
} else {
@@ -742,6 +758,7 @@ func TestGetPrivateKey(t *testing.T) {
742758
},
743759
} {
744760
t.Run(test.name, func(t *testing.T) {
761+
setTemporaryHome(t)
745762
t.Setenv("STACKIT_CREDENTIALS_PATH", test.credentialsFilePath)
746763

747764
if test.envPrivateKeyPathSet {
@@ -843,6 +860,7 @@ func TestGetServiceAccountKey(t *testing.T) {
843860
},
844861
} {
845862
t.Run(test.name, func(t *testing.T) {
863+
setTemporaryHome(t)
846864
t.Setenv("STACKIT_CREDENTIALS_PATH", test.credentialsFilePath)
847865

848866
if test.envServiceAccountKeyPathSet {

0 commit comments

Comments
 (0)