Merge pull request #935 from dlorenc/cfgtest

Add some unit tests for config.
pull/939/head
dlorenc 2016-12-19 10:47:20 -08:00 committed by GitHub
commit f8d4a1c429
2 changed files with 37 additions and 1 deletions

View File

@ -53,7 +53,11 @@ func Get(name string) (string, error) {
if err != nil {
return "", err
}
if val, ok := m[name]; ok {
return get(name, m)
}
func get(name string, config MinikubeConfig) (string, error) {
if val, ok := config[name]; ok {
return fmt.Sprintf("%v", val), nil
} else {
return "", errors.New("specified key could not be found in config")

View File

@ -67,3 +67,35 @@ func TestReadConfig(t *testing.T) {
}
}
}
func TestGet(t *testing.T) {
cfg := `{
"key": "val"
}`
config, err := decode(bytes.NewBufferString(cfg))
if err != nil {
t.Fatalf("Error decoding config : %v", err)
}
var testcases = []struct {
key string
val string
err bool
}{
{"key", "val", false},
{"badkey", "", true},
}
for _, tt := range testcases {
val, err := get(tt.key, config)
if (err != nil) && !tt.err {
t.Errorf("Error fetching key: %s. Err: %v", tt.key, err)
continue
}
if val != tt.val {
t.Errorf("Expected %s, got %s", tt.val, val)
continue
}
}
}