diff --git a/pkg/minikube/config/config.go b/pkg/minikube/config/config.go index ff202726a9..3200342edb 100644 --- a/pkg/minikube/config/config.go +++ b/pkg/minikube/config/config.go @@ -76,14 +76,18 @@ func get(name string, config MinikubeConfig) (string, error) { // WriteConfig writes a minikube config to the JSON file func WriteConfig(m MinikubeConfig) error { - f, err := os.Create(constants.ConfigFile) + return writeConfig(constants.ConfigFile, m) +} + +func writeConfig(configFile string, m MinikubeConfig) error { + f, err := os.Create(configFile) if err != nil { - return fmt.Errorf("create %s: %s", constants.ConfigFile, err) + return fmt.Errorf("create %s: %s", configFile, err) } defer f.Close() err = encode(f, m) if err != nil { - return fmt.Errorf("encode %s: %s", constants.ConfigFile, err) + return fmt.Errorf("encode %s: %s", configFile, err) } return nil } diff --git a/pkg/minikube/config/config_test.go b/pkg/minikube/config/config_test.go index 5ffdaab035..e8dfea7dae 100644 --- a/pkg/minikube/config/config_test.go +++ b/pkg/minikube/config/config_test.go @@ -18,6 +18,7 @@ package config import ( "bytes" + "os" "reflect" "testing" @@ -142,6 +143,32 @@ func Test_readConfig(t *testing.T) { } } +func Test_writeConfig(t *testing.T) { + configFile := "./testdata/configTest" + cfg := map[string]interface{}{ + "vm-driver": constants.DriverKvm2, + "cpus": 4, + "disk-size": "20g", + "show-libmachine-logs": true, + "log_dir": "/etc/hosts", + } + + err := writeConfig(configFile, cfg) + if err != nil { + t.Fatalf("Error not expected but got %v", err) + } + defer os.Remove(configFile) + + mkConfig, err := readConfig("./testdata/.minikube/config/valid_config.json") + if err != nil { + t.Fatalf("Error not expected but got %v", err) + } + + if reflect.DeepEqual(cfg, mkConfig) || err != nil { + t.Errorf("Did not read config correctly,\n\n wanted %+v, \n\n got %+v", cfg, mkConfig) + } +} + func Test_encode(t *testing.T) { var b bytes.Buffer for _, tt := range configTestCases {