Skip to content

Fix test runner for sdk #1306

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 5 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion core/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const (
privateKeyPathCredentialType credentialType = "private_key_path"
)

var userHomeDir = os.UserHomeDir

// SetupAuth sets up authentication based on the configuration. The different options are
// custom authentication, no authentication, explicit key flow, explicit token flow or default authentication
func SetupAuth(cfg *config.Configuration) (rt http.RoundTripper, err error) {
Expand Down Expand Up @@ -195,7 +197,7 @@ func readCredentialsFile(path string) (*Credentials, error) {
customPath, customPathSet := os.LookupEnv("STACKIT_CREDENTIALS_PATH")
if !customPathSet || customPath == "" {
path = credentialsFilePath
home, err := os.UserHomeDir()
home, err := userHomeDir()
if err != nil {
return nil, fmt.Errorf("getting home directory: %w", err)
}
Expand Down
20 changes: 19 additions & 1 deletion core/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ import (
"github.com/stackitcloud/stackit-sdk-go/core/config"
)

func setTemporaryHome(t *testing.T) {
old := userHomeDir
t.Cleanup(func() {
userHomeDir = old
})
userHomeDir = func() (string, error) {
return t.TempDir(), nil
}
}

func fixtureServiceAccountKey(mods ...func(*clients.ServiceAccountKeyResponse)) *clients.ServiceAccountKeyResponse {
validUntil := time.Now().Add(time.Hour)
serviceAccountKeyResponse := &clients.ServiceAccountKeyResponse{
Expand Down Expand Up @@ -150,6 +160,7 @@ func TestSetupAuth(t *testing.T) {
},
} {
t.Run(test.desc, func(t *testing.T) {
setTemporaryHome(t)
if test.setKeys {
t.Setenv("STACKIT_SERVICE_ACCOUNT_KEY_PATH", saKeyFile.Name())
t.Setenv("STACKIT_PRIVATE_KEY_PATH", privateKeyFile.Name())
Expand Down Expand Up @@ -232,6 +243,7 @@ func TestReadCredentials(t *testing.T) {
},
} {
t.Run(test.desc, func(t *testing.T) {
setTemporaryHome(t)
t.Setenv("STACKIT_CREDENTIALS_PATH", test.pathEnv)

var credential string
Expand Down Expand Up @@ -342,6 +354,7 @@ func TestDefaultAuth(t *testing.T) {
},
} {
t.Run(test.desc, func(t *testing.T) {
setTemporaryHome(t)
if test.setKeys {
t.Setenv("STACKIT_SERVICE_ACCOUNT_KEY_PATH", saKeyFile.Name())
t.Setenv("STACKIT_PRIVATE_KEY_PATH", privateKeyFile.Name())
Expand Down Expand Up @@ -406,6 +419,7 @@ func TestTokenAuth(t *testing.T) {
},
} {
t.Run(test.desc, func(t *testing.T) {
setTemporaryHome(t)
t.Setenv("STACKIT_SERVICE_ACCOUNT_TOKEN", "")
t.Setenv("STACKIT_CREDENTIALS_PATH", "test-path")

Expand Down Expand Up @@ -499,6 +513,7 @@ func TestKeyAuth(t *testing.T) {
},
} {
t.Run(test.desc, func(t *testing.T) {
setTemporaryHome(t)
t.Setenv("STACKIT_SERVICE_ACCOUNT_KEY", "")
t.Setenv("STACKIT_SERVICE_ACCOUNT_KEY_PATH", "")
t.Setenv("STACKIT_PRIVATE_KEY", "")
Expand Down Expand Up @@ -557,7 +572,7 @@ func TestNoAuth(t *testing.T) {
},
} {
t.Run(test.desc, func(t *testing.T) {
// Get the default authentication client and ensure that it's not nil
setTemporaryHome(t) // Get the default authentication client and ensure that it's not nil
authClient, err := NoAuth()
if err != nil {
t.Fatalf("Test returned error on valid test case: %v", err)
Expand Down Expand Up @@ -624,6 +639,7 @@ func TestGetServiceAccountEmail(t *testing.T) {
},
} {
t.Run(test.description, func(t *testing.T) {
setTemporaryHome(t)
if test.envEmailSet {
t.Setenv("STACKIT_SERVICE_ACCOUNT_EMAIL", "env_email")
} else {
Expand Down Expand Up @@ -742,6 +758,7 @@ func TestGetPrivateKey(t *testing.T) {
},
} {
t.Run(test.name, func(t *testing.T) {
setTemporaryHome(t)
t.Setenv("STACKIT_CREDENTIALS_PATH", test.credentialsFilePath)

if test.envPrivateKeyPathSet {
Expand Down Expand Up @@ -843,6 +860,7 @@ func TestGetServiceAccountKey(t *testing.T) {
},
} {
t.Run(test.name, func(t *testing.T) {
setTemporaryHome(t)
t.Setenv("STACKIT_CREDENTIALS_PATH", test.credentialsFilePath)

if test.envServiceAccountKeyPathSet {
Expand Down
Loading