forked from openservicemesh/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests/e2e: Generalize GetEnv (openservicemesh#1844)
Addressing: openservicemesh#1828 (comment)
- Loading branch information
Showing
7 changed files
with
69 additions
and
39 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
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
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,13 @@ | ||
package utils | ||
|
||
import "os" | ||
|
||
// GetEnv is a convenience wrapper for os.Getenv() with additional default value return | ||
// when empty or unset | ||
func GetEnv(envVar string, defaultValue string) string { | ||
val := os.Getenv(envVar) | ||
if val == "" { | ||
return defaultValue | ||
} | ||
return val | ||
} |
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,32 @@ | ||
package utils | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetEnv(t *testing.T) { | ||
const ( | ||
EnvVarName = "TEST_VAR" | ||
EnvVarValue = "test_value" | ||
EnvVarDefaultValue = "default_value" | ||
) | ||
|
||
assert := assert.New(t) | ||
|
||
// make sure the variable is unset before starting the test | ||
assert.NoError(os.Unsetenv(EnvVarName)) | ||
|
||
// Expect Default when not set | ||
assert.Equal(EnvVarDefaultValue, GetEnv(EnvVarName, EnvVarDefaultValue)) | ||
|
||
// Set it, expect actual value when set | ||
assert.NoError(os.Setenv(EnvVarName, EnvVarValue)) | ||
assert.Equal(EnvVarValue, GetEnv(EnvVarName, EnvVarDefaultValue)) | ||
|
||
// Unset it, expect default again | ||
assert.NoError(os.Unsetenv(EnvVarName)) | ||
assert.Equal(EnvVarDefaultValue, GetEnv(EnvVarName, EnvVarDefaultValue)) | ||
} |
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