-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: config file non-fatal edge cases, tests (#216)
* fix: non-fatal error possible in Load() * test: test all config file edge cases
- Loading branch information
1 parent
4a681e9
commit 9835179
Showing
4 changed files
with
179 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,19 +12,172 @@ import ( | |
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUsingJustTokenEnvVar(t *testing.T) { | ||
os.Clearenv() | ||
viper.Reset() | ||
|
||
viper.SetConfigFile(os.DevNull) | ||
viper.Set(constants.ArgConfig, os.DevNull) | ||
|
||
assert.NoError(t, os.Setenv(sdk.IonosTokenEnvVar, "tok")) | ||
assert.NoError(t, Load()) | ||
assert.Equal(t, "tok", viper.GetString(Token)) | ||
assert.Equal(t, "", viper.GetString(Username)) | ||
assert.Equal(t, "", viper.GetString(Password)) | ||
assert.Equal(t, "", viper.GetString(ServerUrl)) | ||
} | ||
|
||
func TestTokEnvWithUserPassConfigBackup(t *testing.T) { | ||
// Useful for API routes which don't accept bearer tokens, or custom IonosCTL commands (Image Upload) | ||
os.Clearenv() | ||
viper.Reset() | ||
|
||
assert.NoError(t, os.Setenv(sdk.IonosTokenEnvVar, "tok")) | ||
path := filepath.Join("..", "testdata", "config_user_pass.json") // TODO: These files should be created and then destroyed by the tests | ||
viper.SetConfigFile(path) | ||
viper.Set(constants.ArgConfig, path) | ||
assert.NoError(t, os.Chmod(path, 0600)) | ||
assert.NoError(t, Load()) | ||
|
||
assert.Equal(t, "tok", viper.GetString(Token)) | ||
assert.Equal(t, "[email protected]", viper.GetString(Username)) | ||
assert.Equal(t, "test", viper.GetString(Password)) | ||
assert.Equal(t, "", viper.GetString(ServerUrl)) | ||
} | ||
|
||
func TestTokEnvWithFullConfig(t *testing.T) { | ||
// Config token should not override env var token | ||
os.Clearenv() | ||
viper.Reset() | ||
|
||
assert.NoError(t, os.Setenv(sdk.IonosTokenEnvVar, "tok")) | ||
path := filepath.Join("..", "testdata", "config.json") // TODO: These files should be created and then destroyed by the tests | ||
viper.SetConfigFile(path) | ||
viper.Set(constants.ArgConfig, path) | ||
assert.NoError(t, os.Chmod(path, 0600)) | ||
assert.NoError(t, Load()) | ||
|
||
assert.Equal(t, "tok", viper.GetString(Token)) | ||
assert.Equal(t, "[email protected]", viper.GetString(Username)) | ||
assert.Equal(t, "test", viper.GetString(Password)) | ||
assert.Equal(t, "https://api.ionos.com", viper.GetString(ServerUrl)) | ||
} | ||
|
||
func TestEnvVarsHavePriority(t *testing.T) { | ||
// Make sure env vars not overriden by config file | ||
os.Clearenv() | ||
viper.Reset() | ||
|
||
assert.NoError(t, os.Setenv(sdk.IonosTokenEnvVar, "env_tok")) | ||
assert.NoError(t, os.Setenv(sdk.IonosUsernameEnvVar, "env_user")) | ||
assert.NoError(t, os.Setenv(sdk.IonosPasswordEnvVar, "env_pass")) | ||
assert.NoError(t, os.Setenv(sdk.IonosApiUrlEnvVar, "env_url")) | ||
path := filepath.Join("..", "testdata", "config.json") // TODO: These files should be created and then destroyed by the tests | ||
viper.SetConfigFile(path) | ||
viper.Set(constants.ArgConfig, path) | ||
assert.NoError(t, os.Chmod(path, 0600)) | ||
assert.NoError(t, Load()) | ||
|
||
assert.Equal(t, "env_tok", viper.GetString(Token)) | ||
assert.Equal(t, "env_user", viper.GetString(Username)) | ||
assert.Equal(t, "env_pass", viper.GetString(Password)) | ||
assert.Equal(t, "env_url", viper.GetString(ServerUrl)) | ||
} | ||
|
||
func TestAuthErr(t *testing.T) { | ||
os.Clearenv() | ||
viper.Reset() | ||
|
||
viper.SetConfigFile(os.DevNull) | ||
viper.Set(constants.ArgConfig, os.DevNull) | ||
|
||
assert.NoError(t, os.Setenv(sdk.IonosUsernameEnvVar, "env_user")) | ||
assert.NoError(t, os.Setenv(sdk.IonosApiUrlEnvVar, "env_url")) | ||
|
||
assert.Error(t, Load()) // Need password or token | ||
|
||
assert.Equal(t, "env_user", viper.GetString(Username)) | ||
assert.Equal(t, "env_url", viper.GetString(ServerUrl)) | ||
} | ||
|
||
func TestUsingJustUsernameAndPasswordEnvVar(t *testing.T) { | ||
os.Clearenv() | ||
viper.Reset() | ||
|
||
viper.SetConfigFile(os.DevNull) | ||
viper.Set(constants.ArgConfig, os.DevNull) | ||
|
||
assert.NoError(t, os.Setenv(sdk.IonosUsernameEnvVar, "user")) | ||
assert.NoError(t, os.Setenv(sdk.IonosPasswordEnvVar, "pass")) | ||
assert.NoError(t, Load()) | ||
assert.Equal(t, "", viper.GetString(Token)) | ||
assert.Equal(t, "user", viper.GetString(Username)) | ||
assert.Equal(t, "pass", viper.GetString(Password)) | ||
assert.Equal(t, "", viper.GetString(ServerUrl)) | ||
} | ||
|
||
func TestBadConfigPerms(t *testing.T) { | ||
os.Clearenv() | ||
viper.Reset() | ||
|
||
path := filepath.Join("..", "testdata", "config.json") // TODO: These files should be created and then destroyed by the tests | ||
viper.SetConfigFile(path) | ||
viper.Set(constants.ArgConfig, path) | ||
assert.NoError(t, os.Chmod(path, 0000)) // no read perms | ||
assert.Error(t, Load()) | ||
|
||
assert.Equal(t, "", viper.GetString(Token)) | ||
assert.Equal(t, "", viper.GetString(Username)) | ||
assert.Equal(t, "", viper.GetString(Password)) | ||
assert.Equal(t, "", viper.GetString(ServerUrl)) | ||
} | ||
|
||
func TestUsingJustTokenConfig(t *testing.T) { | ||
os.Clearenv() | ||
viper.Reset() | ||
|
||
path := filepath.Join("..", "testdata", "config_just_token.json") // TODO: These files should be created and then destroyed by the tests | ||
viper.SetConfigFile(path) | ||
viper.Set(constants.ArgConfig, path) | ||
assert.NoError(t, os.Chmod(path, 0600)) | ||
assert.NoError(t, Load()) | ||
|
||
assert.Equal(t, "tok", viper.GetString(Token)) | ||
assert.Equal(t, "", viper.GetString(Username)) | ||
assert.Equal(t, "", viper.GetString(Password)) | ||
assert.Equal(t, "", viper.GetString(ServerUrl)) | ||
} | ||
|
||
func TestUsingJustUsernameAndPasswordConfig(t *testing.T) { | ||
os.Clearenv() | ||
viper.Reset() | ||
|
||
path := filepath.Join("..", "testdata", "config_user_pass.json") // TODO: These files should be created and then destroyed by the tests | ||
viper.SetConfigFile(path) | ||
viper.Set(constants.ArgConfig, path) | ||
assert.NoError(t, os.Chmod(path, 0600)) | ||
assert.NoError(t, Load()) | ||
|
||
assert.Equal(t, "", viper.GetString(Token)) | ||
assert.Equal(t, "[email protected]", viper.GetString(Username)) | ||
assert.Equal(t, "test", viper.GetString(Password)) | ||
assert.Equal(t, "", viper.GetString(ServerUrl)) | ||
} | ||
|
||
func TestGetServerUrl(t *testing.T) { | ||
os.Clearenv() | ||
viper.Reset() | ||
|
||
// use env | ||
assert.NoError(t, os.Setenv(sdk.IonosApiUrlEnvVar, "url")) | ||
_ = Load() // ignore error since we just want to load the URL | ||
assert.Equal(t, "url", GetServerUrl()) | ||
err := Load() | ||
assert.Error(t, err) // Error because neither token nor user & pass set | ||
assert.Equal(t, "url", viper.GetString(ServerUrl)) | ||
|
||
// from config | ||
os.Clearenv() | ||
viper.Reset() | ||
viper.SetConfigFile(filepath.Join("..", "testdata", "config.json")) | ||
viper.SetConfigFile(filepath.Join("..", "testdata", "config.json")) // TODO: These files should be created and then destroyed by the tests | ||
viper.Set(constants.ArgConfig, filepath.Join("..", "testdata", "config.json")) | ||
assert.NoError(t, os.Chmod(filepath.Join("..", "testdata", "config.json"), 0600)) | ||
assert.NoError(t, Load()) | ||
|
@@ -44,7 +197,7 @@ func TestLoadFile(t *testing.T) { | |
os.Clearenv() | ||
viper.Reset() | ||
|
||
viper.SetConfigFile(filepath.Join("..", "testdata", "config.json")) | ||
viper.SetConfigFile(filepath.Join("..", "testdata", "config.json")) // TODO: These files should be created and then destroyed by the tests | ||
viper.Set(constants.ArgConfig, filepath.Join("..", "testdata", "config.json")) | ||
assert.NoError(t, os.Chmod(filepath.Join("..", "testdata", "config.json"), 0600)) | ||
assert.NoError(t, LoadFile()) | ||
|
@@ -58,7 +211,7 @@ func TestLoadEnvFallback(t *testing.T) { | |
os.Clearenv() | ||
viper.Reset() | ||
|
||
viper.SetConfigFile(filepath.Join("..", "testdata", "config.json")) | ||
viper.SetConfigFile(filepath.Join("..", "testdata", "config.json")) // TODO: These files should be created and then destroyed by the tests | ||
viper.Set(constants.ArgConfig, filepath.Join("..", "testdata", "config.json")) | ||
assert.NoError(t, os.Setenv(sdk.IonosUsernameEnvVar, "user")) | ||
assert.NoError(t, os.Setenv(sdk.IonosPasswordEnvVar, "pass")) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"userdata.token": "tok" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"userdata.name": "[email protected]", | ||
"userdata.password": "test" | ||
} |