Merge pull request #4944 from josedonizetti/refactor-extract-writeconfig
Refactor extract writeconfigpull/4986/head
commit
45e5265c35
|
@ -17,10 +17,6 @@ limitations under the License.
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
|
@ -302,7 +298,7 @@ func AddToConfigMap(name string, images []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Set the values
|
// Set the values
|
||||||
configFile, err := config.ReadConfig()
|
cfg, err := config.ReadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -310,16 +306,16 @@ func AddToConfigMap(name string, images []string) error {
|
||||||
for _, image := range images {
|
for _, image := range images {
|
||||||
newImages[image] = nil
|
newImages[image] = nil
|
||||||
}
|
}
|
||||||
if values, ok := configFile[name].(map[string]interface{}); ok {
|
if values, ok := cfg[name].(map[string]interface{}); ok {
|
||||||
for key := range values {
|
for key := range values {
|
||||||
newImages[key] = nil
|
newImages[key] = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err = s.setMap(configFile, name, newImages); err != nil {
|
if err = s.setMap(cfg, name, newImages); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Write the values
|
// Write the values
|
||||||
return WriteConfig(configFile)
|
return config.WriteConfig(constants.ConfigFile, cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteFromConfigMap deletes entries from a map in the config file
|
// DeleteFromConfigMap deletes entries from a map in the config file
|
||||||
|
@ -329,45 +325,20 @@ func DeleteFromConfigMap(name string, images []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Set the values
|
// Set the values
|
||||||
configFile, err := config.ReadConfig()
|
cfg, err := config.ReadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
values, ok := configFile[name]
|
values, ok := cfg[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
for _, image := range images {
|
for _, image := range images {
|
||||||
delete(values.(map[string]interface{}), image)
|
delete(values.(map[string]interface{}), image)
|
||||||
}
|
}
|
||||||
if err = s.setMap(configFile, name, values.(map[string]interface{})); err != nil {
|
if err = s.setMap(cfg, name, values.(map[string]interface{})); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Write the values
|
// Write the values
|
||||||
return WriteConfig(configFile)
|
return config.WriteConfig(constants.ConfigFile, cfg)
|
||||||
}
|
|
||||||
|
|
||||||
// WriteConfig writes a minikube config to the JSON file
|
|
||||||
func WriteConfig(m config.MinikubeConfig) error {
|
|
||||||
f, err := os.Create(constants.ConfigFile)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("create %s: %s", constants.ConfigFile, err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
err = encode(f, m)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("encode %s: %s", constants.ConfigFile, err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func encode(w io.Writer, m config.MinikubeConfig) error {
|
|
||||||
b, err := json.MarshalIndent(m, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = w.Write(b)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,46 +20,8 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"k8s.io/minikube/pkg/minikube/constants"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type configTestCase struct {
|
|
||||||
data string
|
|
||||||
config map[string]interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
var configTestCases = []configTestCase{
|
|
||||||
{
|
|
||||||
data: `{
|
|
||||||
"memory": 2
|
|
||||||
}`,
|
|
||||||
config: map[string]interface{}{
|
|
||||||
"memory": 2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
data: `{
|
|
||||||
"ReminderWaitPeriodInHours": 99,
|
|
||||||
"cpus": 4,
|
|
||||||
"disk-size": "20g",
|
|
||||||
"log_dir": "/etc/hosts",
|
|
||||||
"show-libmachine-logs": true,
|
|
||||||
"v": 5,
|
|
||||||
"vm-driver": "kvm2"
|
|
||||||
}`,
|
|
||||||
config: map[string]interface{}{
|
|
||||||
"vm-driver": constants.DriverKvm2,
|
|
||||||
"cpus": 4,
|
|
||||||
"disk-size": "20g",
|
|
||||||
"v": 5,
|
|
||||||
"show-libmachine-logs": true,
|
|
||||||
"log_dir": "/etc/hosts",
|
|
||||||
"ReminderWaitPeriodInHours": 99,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHiddenPrint(t *testing.T) {
|
func TestHiddenPrint(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
TestString string
|
TestString string
|
||||||
|
@ -90,17 +52,3 @@ func TestHiddenPrint(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWriteConfig(t *testing.T) {
|
|
||||||
var b bytes.Buffer
|
|
||||||
for _, tt := range configTestCases {
|
|
||||||
err := encode(&b, tt.config)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Error encoding: %v", err)
|
|
||||||
}
|
|
||||||
if b.String() != tt.data {
|
|
||||||
t.Errorf("Did not write config correctly, \n\n expected:\n %+v \n\n actual:\n %+v", tt.data, b.String())
|
|
||||||
}
|
|
||||||
b.Reset()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ package config
|
||||||
import (
|
import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
pkgConfig "k8s.io/minikube/pkg/minikube/config"
|
pkgConfig "k8s.io/minikube/pkg/minikube/config"
|
||||||
|
"k8s.io/minikube/pkg/minikube/constants"
|
||||||
"k8s.io/minikube/pkg/minikube/exit"
|
"k8s.io/minikube/pkg/minikube/exit"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -71,5 +72,5 @@ func Set(name string, value string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the value
|
// Write the value
|
||||||
return WriteConfig(config)
|
return pkgConfig.WriteConfig(constants.ConfigFile, config)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ package config
|
||||||
import (
|
import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
pkgConfig "k8s.io/minikube/pkg/minikube/config"
|
pkgConfig "k8s.io/minikube/pkg/minikube/config"
|
||||||
|
"k8s.io/minikube/pkg/minikube/constants"
|
||||||
"k8s.io/minikube/pkg/minikube/exit"
|
"k8s.io/minikube/pkg/minikube/exit"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -48,5 +49,5 @@ func Unset(name string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
delete(m, name)
|
delete(m, name)
|
||||||
return WriteConfig(m)
|
return pkgConfig.WriteConfig(constants.ConfigFile, m)
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,6 +74,20 @@ func get(name string, config MinikubeConfig) (string, error) {
|
||||||
return "", ErrKeyNotFound
|
return "", ErrKeyNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteConfig writes a minikube config to the JSON file
|
||||||
|
func WriteConfig(configFile string, m MinikubeConfig) error {
|
||||||
|
f, err := os.Create(configFile)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("create %s: %s", configFile, err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
err = encode(f, m)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("encode %s: %s", configFile, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// ReadConfig reads in the JSON minikube config
|
// ReadConfig reads in the JSON minikube config
|
||||||
func ReadConfig() (MinikubeConfig, error) {
|
func ReadConfig() (MinikubeConfig, error) {
|
||||||
return readConfig(constants.ConfigFile)
|
return readConfig(constants.ConfigFile)
|
||||||
|
@ -103,6 +117,17 @@ func decode(r io.Reader) (MinikubeConfig, error) {
|
||||||
return data, err
|
return data, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func encode(w io.Writer, m MinikubeConfig) error {
|
||||||
|
b, err := json.MarshalIndent(m, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = w.Write(b)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// GetMachineName gets the machine name for the VM
|
// GetMachineName gets the machine name for the VM
|
||||||
func GetMachineName() string {
|
func GetMachineName() string {
|
||||||
// REFACTOR NECESSARY: This function should not rely on globals.
|
// REFACTOR NECESSARY: This function should not rely on globals.
|
||||||
|
|
|
@ -18,6 +18,8 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -46,7 +48,7 @@ var configTestCases = []configTestCase{
|
||||||
"log_dir": "/etc/hosts",
|
"log_dir": "/etc/hosts",
|
||||||
"show-libmachine-logs": true,
|
"show-libmachine-logs": true,
|
||||||
"v": 5,
|
"v": 5,
|
||||||
"vm-driver": "kvm"
|
"vm-driver": "kvm2"
|
||||||
}`,
|
}`,
|
||||||
config: map[string]interface{}{
|
config: map[string]interface{}{
|
||||||
"vm-driver": constants.DriverKvm2,
|
"vm-driver": constants.DriverKvm2,
|
||||||
|
@ -141,3 +143,47 @@ func Test_readConfig(t *testing.T) {
|
||||||
t.Errorf("Did not read config correctly,\n\n wanted %+v, \n\n got %+v", expectedConfig, mkConfig)
|
t.Errorf("Did not read config correctly,\n\n wanted %+v, \n\n got %+v", expectedConfig, mkConfig)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteConfig(t *testing.T) {
|
||||||
|
configFile, err := ioutil.TempFile("/tmp", "configTest")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error not expected but got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg := map[string]interface{}{
|
||||||
|
"vm-driver": constants.DriverKvm2,
|
||||||
|
"cpus": 4,
|
||||||
|
"disk-size": "20g",
|
||||||
|
"show-libmachine-logs": true,
|
||||||
|
"log_dir": "/etc/hosts",
|
||||||
|
}
|
||||||
|
|
||||||
|
err = WriteConfig(configFile.Name(), cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error not expected but got %v", err)
|
||||||
|
}
|
||||||
|
defer os.Remove(configFile.Name())
|
||||||
|
|
||||||
|
mkConfig, err := readConfig(configFile.Name())
|
||||||
|
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 {
|
||||||
|
err := encode(&b, tt.config)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error encoding: %v", err)
|
||||||
|
}
|
||||||
|
if b.String() != tt.data {
|
||||||
|
t.Errorf("Did not write config correctly, \n\n expected:\n %+v \n\n actual:\n %+v", tt.data, b.String())
|
||||||
|
}
|
||||||
|
b.Reset()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue