Merge pull request #5116 from josedonizetti/refactor-read-config
Refactor config.ReadConfig to accept a filepull/5162/head
commit
b9a88eb52b
|
|
@ -67,7 +67,7 @@ var deleteCacheCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
func imagesInConfigFile() ([]string, error) {
|
||||
configFile, err := config.ReadConfig()
|
||||
configFile, err := config.ReadConfig(constants.ConfigFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -290,7 +290,7 @@ func configurableFields() string {
|
|||
|
||||
// ListConfigMap list entries from config file
|
||||
func ListConfigMap(name string) ([]string, error) {
|
||||
configFile, err := config.ReadConfig()
|
||||
configFile, err := config.ReadConfig(constants.ConfigFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -310,7 +310,7 @@ func AddToConfigMap(name string, images []string) error {
|
|||
return err
|
||||
}
|
||||
// Set the values
|
||||
cfg, err := config.ReadConfig()
|
||||
cfg, err := config.ReadConfig(constants.ConfigFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -337,7 +337,7 @@ func DeleteFromConfigMap(name string, images []string) error {
|
|||
return err
|
||||
}
|
||||
// Set the values
|
||||
cfg, err := config.ReadConfig()
|
||||
cfg, err := config.ReadConfig(constants.ConfigFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ func Set(name string, value string) error {
|
|||
}
|
||||
|
||||
// Set the value
|
||||
config, err := pkgConfig.ReadConfig()
|
||||
config, err := pkgConfig.ReadConfig(constants.ConfigFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ func init() {
|
|||
|
||||
// Unset unsets a property
|
||||
func Unset(name string) error {
|
||||
m, err := pkgConfig.ReadConfig()
|
||||
m, err := pkgConfig.ReadConfig(constants.ConfigFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ For the list of accessible variables for the template, see the struct values her
|
|||
|
||||
// View displays the current config
|
||||
func View() error {
|
||||
cfg, err := config.ReadConfig()
|
||||
cfg, err := config.ReadConfig(constants.ConfigFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ type MinikubeConfig map[string]interface{}
|
|||
|
||||
// Get gets a named value from config
|
||||
func Get(name string) (string, error) {
|
||||
m, err := ReadConfig()
|
||||
m, err := ReadConfig(constants.ConfigFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
@ -88,11 +88,7 @@ func WriteConfig(configFile string, m MinikubeConfig) error {
|
|||
}
|
||||
|
||||
// ReadConfig reads in the JSON minikube config
|
||||
func ReadConfig() (MinikubeConfig, error) {
|
||||
return readConfig(constants.ConfigFile)
|
||||
}
|
||||
|
||||
func readConfig(configFile string) (MinikubeConfig, error) {
|
||||
func ReadConfig(configFile string) (MinikubeConfig, error) {
|
||||
f, err := os.Open(configFile)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
|
|
|
|||
|
|
@ -104,9 +104,9 @@ func Test_get(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func Test_readConfig(t *testing.T) {
|
||||
func TestReadConfig(t *testing.T) {
|
||||
// non existing file
|
||||
mkConfig, err := readConfig("non_existing_file")
|
||||
mkConfig, err := ReadConfig("non_existing_file")
|
||||
if err != nil {
|
||||
t.Fatalf("Error not exepected but got %v", err)
|
||||
}
|
||||
|
|
@ -116,7 +116,7 @@ func Test_readConfig(t *testing.T) {
|
|||
}
|
||||
|
||||
// invalid config file
|
||||
mkConfig, err = readConfig("./testdata/.minikube/config/invalid_config.json")
|
||||
mkConfig, err = ReadConfig("./testdata/.minikube/config/invalid_config.json")
|
||||
if err == nil {
|
||||
t.Fatalf("Error expected but got none")
|
||||
}
|
||||
|
|
@ -126,7 +126,7 @@ func Test_readConfig(t *testing.T) {
|
|||
}
|
||||
|
||||
// valid config file
|
||||
mkConfig, err = readConfig("./testdata/.minikube/config/valid_config.json")
|
||||
mkConfig, err = ReadConfig("./testdata/.minikube/config/valid_config.json")
|
||||
if err != nil {
|
||||
t.Fatalf("Error not expected but got %v", err)
|
||||
}
|
||||
|
|
@ -164,7 +164,7 @@ func TestWriteConfig(t *testing.T) {
|
|||
}
|
||||
defer os.Remove(configFile.Name())
|
||||
|
||||
mkConfig, err := readConfig(configFile.Name())
|
||||
mkConfig, err := ReadConfig(configFile.Name())
|
||||
if err != nil {
|
||||
t.Fatalf("Error not expected but got %v", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,81 @@
|
|||
// +build integration
|
||||
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package integration
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := profileName(t)
|
||||
mk := NewMinikubeRunner(t, p, "--wait=false")
|
||||
|
||||
tests := []struct {
|
||||
cmd string
|
||||
stdout string
|
||||
stderr string
|
||||
}{
|
||||
{
|
||||
cmd: "config unset cpus",
|
||||
stdout: "",
|
||||
stderr: "",
|
||||
},
|
||||
{
|
||||
cmd: "config get cpus",
|
||||
stdout: "",
|
||||
stderr: "Error: specified key could not be found in config",
|
||||
},
|
||||
{
|
||||
cmd: "config set cpus 2",
|
||||
stdout: "! These changes will take effect upon a minikube delete and then a minikube start",
|
||||
stderr: "",
|
||||
},
|
||||
{
|
||||
cmd: "config get cpus",
|
||||
stdout: "2",
|
||||
stderr: "",
|
||||
},
|
||||
{
|
||||
cmd: "config unset cpus",
|
||||
stdout: "",
|
||||
stderr: ""},
|
||||
{
|
||||
cmd: "config get cpus",
|
||||
stdout: "",
|
||||
stderr: "Error: specified key could not be found in config",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
stdout, stderr, _ := mk.RunCommandRetriable(tc.cmd)
|
||||
|
||||
if !compare(tc.stdout, stdout) {
|
||||
t.Fatalf("Expected stdout to be: %s. Stdout was: %s Stderr: %s", tc.stdout, stdout, stderr)
|
||||
}
|
||||
if !compare(tc.stderr, stderr) {
|
||||
t.Fatalf("Expected stderr to be: %s. Stdout was: %s Stderr: %s", tc.stderr, stdout, stderr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func compare(s1, s2 string) bool {
|
||||
return strings.TrimSpace(s1) == strings.TrimSpace(s2)
|
||||
}
|
||||
|
|
@ -124,7 +124,7 @@ func (m *MinikubeRunner) RunCommand(cmdStr string, failError bool, waitForRun ..
|
|||
return stdout, stderr
|
||||
}
|
||||
|
||||
// RunCommandRetriable Error executes a command, returns error
|
||||
// RunCommandRetriable executes a command, returns error
|
||||
// the purpose of this command is to make it retriable and
|
||||
// better logging for retrying
|
||||
func (m *MinikubeRunner) RunCommandRetriable(cmdStr string, waitForRun ...bool) (stdout string, stderr string, err error) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue