diff --git a/pkg/minikube/localpath/localpath.go b/pkg/minikube/localpath/localpath.go index 6bc9ef1239..1ac1172b6a 100644 --- a/pkg/minikube/localpath/localpath.go +++ b/pkg/minikube/localpath/localpath.go @@ -38,13 +38,14 @@ func ConfigFile() string { // MiniPath returns the path to the user's minikube dir func MiniPath() string { - if os.Getenv(MinikubeHome) == "" { + minikubeHomeEnv := os.Getenv(MinikubeHome) + if minikubeHomeEnv == "" { return filepath.Join(homedir.HomeDir(), ".minikube") } - if filepath.Base(os.Getenv(MinikubeHome)) == ".minikube" { - return os.Getenv(MinikubeHome) + if filepath.Base(minikubeHomeEnv) == ".minikube" { + return minikubeHomeEnv } - return filepath.Join(os.Getenv(MinikubeHome), ".minikube") + return filepath.Join(minikubeHomeEnv, ".minikube") } // MakeMiniPath is a utility to calculate a relative path to our directory. diff --git a/pkg/minikube/localpath/localpath_test.go b/pkg/minikube/localpath/localpath_test.go index 173ca5df88..d8e6915a43 100644 --- a/pkg/minikube/localpath/localpath_test.go +++ b/pkg/minikube/localpath/localpath_test.go @@ -17,10 +17,15 @@ limitations under the License. package localpath import ( + "fmt" "io/ioutil" "os" + "path/filepath" "runtime" + "strings" "testing" + + "k8s.io/client-go/util/homedir" ) func TestReplaceWinDriveLetterToVolumeName(t *testing.T) { @@ -61,3 +66,95 @@ func TestHasWindowsDriveLetter(t *testing.T) { } } } + +func TestMiniPath(t *testing.T) { + var testCases = []struct { + env, basePath string + }{ + {"/tmp/.minikube", "/tmp/"}, + {"/tmp/", "/tmp"}, + {"", homedir.HomeDir()}, + } + originalEnv := os.Getenv(MinikubeHome) + defer func() { // revert to pre-test env var + err := os.Setenv(MinikubeHome, originalEnv) + if err != nil { + t.Fatalf("Error reverting env %s to its original value (%s) var after test ", MinikubeHome, originalEnv) + } + }() + for _, tc := range testCases { + t.Run(tc.env, func(t *testing.T) { + expectedPath := filepath.Join(tc.basePath, ".minikube") + os.Setenv(MinikubeHome, tc.env) + path := MiniPath() + if path != expectedPath { + t.Errorf("MiniPath expected to return '%s', but got '%s'", expectedPath, path) + } + }) + } +} + +func TestMachinePath(t *testing.T) { + var testCases = []struct { + miniHome []string + contains string + }{ + {[]string{"tmp", "foo", "bar", "baz"}, "tmp"}, + {[]string{"tmp"}, "tmp"}, + {[]string{}, MiniPath()}, + } + for _, tc := range testCases { + t.Run(fmt.Sprintf("%s", tc.miniHome), func(t *testing.T) { + machinePath := MachinePath("foo", tc.miniHome...) + if !strings.Contains(machinePath, tc.contains) { + t.Errorf("Function MachinePath returned (%v) which doesn't contain expected (%v)", machinePath, tc.contains) + } + }) + } +} + +type propertyFnWithArg func(string) string + +func TestPropertyWithNameArg(t *testing.T) { + var testCases = []struct { + propertyFunc propertyFnWithArg + name string + }{ + {Profile, "Profile"}, + {ClientCert, "ClientCert"}, + {ClientKey, "ClientKey"}, + } + miniPath := MiniPath() + mockedName := "foo" + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + if !strings.Contains(tc.propertyFunc(mockedName), MiniPath()) { + t.Errorf("Property %s(%v) doesn't contain miniPath %v", tc.name, tc.propertyFunc, miniPath) + } + if !strings.Contains(tc.propertyFunc(mockedName), mockedName) { + t.Errorf("Property %s(%v) doesn't contain passed name %v", tc.name, tc.propertyFunc, mockedName) + } + }) + + } +} + +type propertyFnWithoutArg func() string + +func TestPropertyWithoutNameArg(t *testing.T) { + var testCases = []struct { + propertyFunc propertyFnWithoutArg + name string + }{ + {ConfigFile, "ConfigFile"}, + {CACert, "CACert"}, + } + miniPath := MiniPath() + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + if !strings.Contains(tc.propertyFunc(), MiniPath()) { + t.Errorf("Property %s(%v) doesn't contain expected miniPath %v", tc.name, tc.propertyFunc, miniPath) + } + }) + } +}