From f42572f29ce69f7b015cd0d00c623dafe9c13a88 Mon Sep 17 00:00:00 2001 From: Marcin Niemira Date: Wed, 25 Mar 2020 22:44:40 +1100 Subject: [PATCH 1/6] Add TestMiniPath test --- pkg/minikube/localpath/localpath.go | 9 +++--- pkg/minikube/localpath/localpath_test.go | 39 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) 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..81b40722dc 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,37 @@ func TestHasWindowsDriveLetter(t *testing.T) { } } } + +func TestConfigFile(t *testing.T) { + configFile := ConfigFile() + if !strings.Contains(configFile, "config.json") { + t.Errorf("ConfigFile returned path without 'config.json': %s", configFile) + } +} + +func TestMiniPath(t *testing.T) { + var testCases = []struct { + env, basePath string + }{ + {"/tmp/.minikube", "/tmp/"}, + {"/tmp/", "/tmp"}, + {"", homedir.HomeDir()}, + } + for _, tc := range testCases { + 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) + } + }() + t.Run(fmt.Sprintf("%s", 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) + } + }) + } +} From 31c6c5f36eb4390d671792cb90ccde015d0179a8 Mon Sep 17 00:00:00 2001 From: Marcin Niemira Date: Sat, 28 Mar 2020 12:11:41 +1100 Subject: [PATCH 2/6] use table testing for property testing --- pkg/minikube/localpath/localpath_test.go | 50 ++++++++++++++++++++---- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/pkg/minikube/localpath/localpath_test.go b/pkg/minikube/localpath/localpath_test.go index 81b40722dc..bd78eb2ef7 100644 --- a/pkg/minikube/localpath/localpath_test.go +++ b/pkg/minikube/localpath/localpath_test.go @@ -67,13 +67,6 @@ func TestHasWindowsDriveLetter(t *testing.T) { } } -func TestConfigFile(t *testing.T) { - configFile := ConfigFile() - if !strings.Contains(configFile, "config.json") { - t.Errorf("ConfigFile returned path without 'config.json': %s", configFile) - } -} - func TestMiniPath(t *testing.T) { var testCases = []struct { env, basePath string @@ -100,3 +93,46 @@ func TestMiniPath(t *testing.T) { }) } } + +type propertyFnWithArg func(string) string +type propertyFnWithoutArg func() string + +func TestPropertyWithNameArg(t *testing.T) { + var testCases = []struct { + propertyFunc propertyFnWithArg + }{ + {Profile}, + {ClientCert}, + {ClientKey}, + } + miniPath := MiniPath() + mockedName := "foo" + for _, tc := range testCases { + t.Run(fmt.Sprintf("%v", tc.propertyFunc), func(t *testing.T) { + if !strings.Contains(tc.propertyFunc(mockedName), MiniPath()) { + t.Errorf("Propert %v doesn't contain miniPat %v", tc.propertyFunc, miniPath) + } + if !strings.Contains(tc.propertyFunc(mockedName), mockedName) { + t.Errorf("Propert %v doesn't contain passed name inpath %v", tc.propertyFunc, mockedName) + } + }) + + } +} + +func TestPropertyWithoutNameArg(t *testing.T) { + var testCases = []struct { + propertyFunc propertyFnWithoutArg + }{ + {ConfigFile}, + {CACert}, + } + miniPath := MiniPath() + for _, tc := range testCases { + t.Run(fmt.Sprintf("%v", tc.propertyFunc), func(t *testing.T) { + if !strings.Contains(tc.propertyFunc(), MiniPath()) { + t.Errorf("Propert %v doesn't contain miniPat %v", tc.propertyFunc, miniPath) + } + }) + } +} From c985a7287a08b5c35d32613eb15f87019820ac7f Mon Sep 17 00:00:00 2001 From: Marcin Niemira Date: Sat, 28 Mar 2020 13:46:16 +1100 Subject: [PATCH 3/6] test properties --- pkg/minikube/localpath/localpath_test.go | 46 +++++++++++++++++------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/pkg/minikube/localpath/localpath_test.go b/pkg/minikube/localpath/localpath_test.go index bd78eb2ef7..7ce6d68a3e 100644 --- a/pkg/minikube/localpath/localpath_test.go +++ b/pkg/minikube/localpath/localpath_test.go @@ -83,7 +83,7 @@ func TestMiniPath(t *testing.T) { t.Fatalf("Error reverting env %s to its original value (%s) var after test ", MinikubeHome, originalEnv) } }() - t.Run(fmt.Sprintf("%s", tc.env), func(t *testing.T) { + t.Run(tc.env, func(t *testing.T) { expectedPath := filepath.Join(tc.basePath, ".minikube") os.Setenv(MinikubeHome, tc.env) path := MiniPath() @@ -94,44 +94,66 @@ func TestMiniPath(t *testing.T) { } } +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 -type propertyFnWithoutArg func() string func TestPropertyWithNameArg(t *testing.T) { var testCases = []struct { propertyFunc propertyFnWithArg + name string }{ - {Profile}, - {ClientCert}, - {ClientKey}, + {Profile, "Profile"}, + {ClientCert, "ClientCert"}, + {ClientKey, "ClientKey"}, } miniPath := MiniPath() mockedName := "foo" for _, tc := range testCases { - t.Run(fmt.Sprintf("%v", tc.propertyFunc), func(t *testing.T) { + t.Run(tc.name, func(t *testing.T) { if !strings.Contains(tc.propertyFunc(mockedName), MiniPath()) { - t.Errorf("Propert %v doesn't contain miniPat %v", tc.propertyFunc, miniPath) + t.Errorf("Propert %s(%v) doesn't contain miniPat %v", tc.name, tc.propertyFunc, miniPath) } if !strings.Contains(tc.propertyFunc(mockedName), mockedName) { - t.Errorf("Propert %v doesn't contain passed name inpath %v", tc.propertyFunc, mockedName) + t.Errorf("Propert %s(%v) doesn't contain passed name inpath %v", tc.name, tc.propertyFunc, mockedName) } }) } } +type propertyFnWithoutArg func() string + func TestPropertyWithoutNameArg(t *testing.T) { var testCases = []struct { propertyFunc propertyFnWithoutArg + name string }{ - {ConfigFile}, - {CACert}, + {ConfigFile, "ConfigFile"}, + {CACert, "CACert"}, } miniPath := MiniPath() for _, tc := range testCases { - t.Run(fmt.Sprintf("%v", tc.propertyFunc), func(t *testing.T) { + t.Run(tc.name, func(t *testing.T) { if !strings.Contains(tc.propertyFunc(), MiniPath()) { - t.Errorf("Propert %v doesn't contain miniPat %v", tc.propertyFunc, miniPath) + t.Errorf("Propert %s(%v) doesn't contain miniPat %v", tc.name, tc.propertyFunc, miniPath) } }) } From 93d6829905446838def9f3e9ee9e24361663937b Mon Sep 17 00:00:00 2001 From: Marcin Niemira Date: Sat, 28 Mar 2020 13:49:39 +1100 Subject: [PATCH 4/6] typofix --- pkg/minikube/localpath/localpath_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/minikube/localpath/localpath_test.go b/pkg/minikube/localpath/localpath_test.go index 7ce6d68a3e..6a4ee4e51b 100644 --- a/pkg/minikube/localpath/localpath_test.go +++ b/pkg/minikube/localpath/localpath_test.go @@ -129,10 +129,10 @@ func TestPropertyWithNameArg(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { if !strings.Contains(tc.propertyFunc(mockedName), MiniPath()) { - t.Errorf("Propert %s(%v) doesn't contain miniPat %v", tc.name, tc.propertyFunc, 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("Propert %s(%v) doesn't contain passed name inpath %v", tc.name, tc.propertyFunc, mockedName) + t.Errorf("Property %s(%v) doesn't contain passed name %v", tc.name, tc.propertyFunc, mockedName) } }) @@ -153,7 +153,7 @@ func TestPropertyWithoutNameArg(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { if !strings.Contains(tc.propertyFunc(), MiniPath()) { - t.Errorf("Propert %s(%v) doesn't contain miniPat %v", tc.name, tc.propertyFunc, miniPath) + t.Errorf("Property %s(%v) doesn't contain expected miniPath %v", tc.name, tc.propertyFunc, miniPath) } }) } From 546b26e695a8c9af8530959ffe7d52443382fe93 Mon Sep 17 00:00:00 2001 From: Marcin Niemira Date: Sat, 28 Mar 2020 20:16:48 +1100 Subject: [PATCH 5/6] implement CR suggestions --- pkg/minikube/localpath/localpath_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/minikube/localpath/localpath_test.go b/pkg/minikube/localpath/localpath_test.go index 6a4ee4e51b..4753752ee2 100644 --- a/pkg/minikube/localpath/localpath_test.go +++ b/pkg/minikube/localpath/localpath_test.go @@ -75,8 +75,8 @@ func TestMiniPath(t *testing.T) { {"/tmp/", "/tmp"}, {"", homedir.HomeDir()}, } + originalEnv := os.Getenv(MinikubeHome) for _, tc := range testCases { - originalEnv := os.Getenv(MinikubeHome) defer func() { // revert to pre-test env var err := os.Setenv(MinikubeHome, originalEnv) if err != nil { From 417081aeb672e242ed31955d63a1f4b64b8f131b Mon Sep 17 00:00:00 2001 From: Marcin Niemira Date: Sun, 29 Mar 2020 15:08:30 +1100 Subject: [PATCH 6/6] move defer outside of for loop --- pkg/minikube/localpath/localpath_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/minikube/localpath/localpath_test.go b/pkg/minikube/localpath/localpath_test.go index 4753752ee2..d8e6915a43 100644 --- a/pkg/minikube/localpath/localpath_test.go +++ b/pkg/minikube/localpath/localpath_test.go @@ -76,13 +76,13 @@ func TestMiniPath(t *testing.T) { {"", 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 { - 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) - } - }() t.Run(tc.env, func(t *testing.T) { expectedPath := filepath.Join(tc.basePath, ".minikube") os.Setenv(MinikubeHome, tc.env)