Extract writeConfig

pull/4944/head
josedonizetti 2019-08-01 14:54:47 -03:00
parent 487c104ab9
commit 021eaa723d
2 changed files with 34 additions and 3 deletions

View File

@ -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
}

View File

@ -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 {