Refactor config env var test and add string replacer for automatic env
Changes - to _ for environmental variables controlled by viper Refactors tests to temporarily reset minikube related environment variables to test them.pull/555/head
parent
3e7bd86044
commit
121f48a23c
|
@ -20,6 +20,7 @@ import (
|
||||||
goflag "flag"
|
goflag "flag"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/machine/libmachine/log"
|
"github.com/docker/machine/libmachine/log"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
|
@ -129,6 +130,9 @@ func initConfig() {
|
||||||
|
|
||||||
func setupViper() {
|
func setupViper() {
|
||||||
viper.SetEnvPrefix(constants.MinikubeEnvPrefix)
|
viper.SetEnvPrefix(constants.MinikubeEnvPrefix)
|
||||||
|
// Replaces '-' in flags with '_' in env variables
|
||||||
|
// e.g. show-libmachine-logs => $ENVPREFIX_SHOW_LIBMACHINE_LOGS
|
||||||
|
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
||||||
viper.AutomaticEnv()
|
viper.AutomaticEnv()
|
||||||
|
|
||||||
viper.SetDefault(config.WantUpdateNotification, true)
|
viper.SetDefault(config.WantUpdateNotification, true)
|
||||||
|
|
|
@ -97,6 +97,27 @@ func runCommand(f func(*cobra.Command, []string)) {
|
||||||
f(&cmd, args)
|
f(&cmd, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Temporarily unsets the env variables for the test cases
|
||||||
|
// returns a function to reset them to their initial values
|
||||||
|
func hideEnv(t *testing.T) func(t *testing.T) {
|
||||||
|
envs := make(map[string]string)
|
||||||
|
for _, env := range os.Environ() {
|
||||||
|
if strings.HasPrefix(env, constants.MinikubeEnvPrefix) {
|
||||||
|
line := strings.Split(env, "=")
|
||||||
|
key, val := line[0], line[1]
|
||||||
|
envs[key] = val
|
||||||
|
t.Logf("TestConfig: Unsetting %s=%s for unit test!", key, val)
|
||||||
|
os.Unsetenv(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return func(t *testing.T) {
|
||||||
|
for key, val := range envs {
|
||||||
|
t.Logf("TestConfig: Finished test, Resetting Env %s=%s", key, val)
|
||||||
|
os.Setenv(key, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestPreRunDirectories(t *testing.T) {
|
func TestPreRunDirectories(t *testing.T) {
|
||||||
// Make sure we create the required directories.
|
// Make sure we create the required directories.
|
||||||
tempDir := tests.MakeTempDir()
|
tempDir := tests.MakeTempDir()
|
||||||
|
@ -135,7 +156,8 @@ func setValues(t *testing.T, tt configTest) {
|
||||||
pflag.Set(tt.Name, tt.FlagValue)
|
pflag.Set(tt.Name, tt.FlagValue)
|
||||||
}
|
}
|
||||||
if tt.EnvValue != "" {
|
if tt.EnvValue != "" {
|
||||||
os.Setenv(getEnvVarName(tt.Name), tt.EnvValue)
|
s := strings.Replace(getEnvVarName(tt.Name), "-", "_", -1)
|
||||||
|
os.Setenv(s, tt.EnvValue)
|
||||||
}
|
}
|
||||||
if tt.ConfigValue != "" {
|
if tt.ConfigValue != "" {
|
||||||
err := initTestConfig(tt.ConfigValue)
|
err := initTestConfig(tt.ConfigValue)
|
||||||
|
@ -156,10 +178,16 @@ func unsetValues(tt configTest) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestViperAndFlags(t *testing.T) {
|
func TestViperAndFlags(t *testing.T) {
|
||||||
|
restore := hideEnv(t)
|
||||||
|
defer restore(t)
|
||||||
for _, tt := range configTests {
|
for _, tt := range configTests {
|
||||||
setValues(t, tt)
|
setValues(t, tt)
|
||||||
setupViper()
|
setupViper()
|
||||||
var actual = pflag.Lookup(tt.Name).Value.String()
|
f := pflag.Lookup(tt.Name)
|
||||||
|
if f == nil {
|
||||||
|
t.Fatalf("Could not find flag for %s", tt.Name)
|
||||||
|
}
|
||||||
|
actual := f.Value.String()
|
||||||
if actual != tt.ExpectedValue {
|
if actual != tt.ExpectedValue {
|
||||||
t.Errorf("pflag.Value(%s) => %s, wanted %s [%+v]", tt.Name, actual, tt.ExpectedValue, tt)
|
t.Errorf("pflag.Value(%s) => %s, wanted %s [%+v]", tt.Name, actual, tt.ExpectedValue, tt)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue