From 730871f33048422abeda7cb73c8cb7b3815aff71 Mon Sep 17 00:00:00 2001 From: Liz Rice Date: Tue, 21 Nov 2017 13:19:09 +0000 Subject: [PATCH] Fix kubeVersion regex tests --- cmd/common.go | 2 ++ cmd/util.go | 27 +++++++++------------------ cmd/util_test.go | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/cmd/common.go b/cmd/common.go index 2bcf30e..c552f86 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -62,6 +62,8 @@ func runChecks(t check.NodeType) { } ver := getKubeVersion() + glog.V(1).Info(fmt.Sprintf("Running tests for Kubernetes version: %s", ver)) + path := fmt.Sprintf("%s/%s/%s", cfgDir, ver, file) in, err := ioutil.ReadFile(path) if err != nil { diff --git a/cmd/util.go b/cmd/util.go index aa2baa0..f2c6b29 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -214,14 +214,10 @@ func multiWordReplace(s string, subname string, sub string) string { } func getKubeVersion() string { - var ver string - var matched bool - - failmsg := "kubernetes version check failed" // These executables might not be on the user's path. _, err := exec.LookPath("kubectl") if err != nil { - continueWithError(err, failmsg) + exitWithError(fmt.Errorf("kubernetes version check failed: %v", err)) } cmd := exec.Command("kubectl", "version", "--short") @@ -230,22 +226,17 @@ func getKubeVersion() string { continueWithError(fmt.Errorf("%s", out), "") } + return getVersionFromKubectlOutput(string(out)) +} + +func getVersionFromKubectlOutput(s string) string { serverVersionRe := regexp.MustCompile(`Server Version: v(\d+.\d+)`) - subs := serverVersionRe.FindStringSubmatch(string(out)) - if len(subs) == 2 { - ver = string(subs[1]) - validVersionPttn := `\d.\d.\d` - if matched, _ = regexp.MatchString(validVersionPttn, ver); !matched { - continueWithError(fmt.Errorf("%s: invalid server version ", ver), failmsg) - } - } - - if ver == "" || !matched { + subs := serverVersionRe.FindStringSubmatch(s) + if len(subs) < 2 { printlnWarn(fmt.Sprintf("Unable to get kubectl version, using default version: %s", defaultKubeVersion)) - ver = defaultKubeVersion + return defaultKubeVersion } - - return ver + return subs[1] } func makeSubstitutions(s string, ext string, m map[string]string) string { diff --git a/cmd/util_test.go b/cmd/util_test.go index 646a75e..7bf6a20 100644 --- a/cmd/util_test.go +++ b/cmd/util_test.go @@ -191,6 +191,20 @@ func TestGetKubeVersion(t *testing.T) { } +func TestKubeVersionRegex(t *testing.T) { + ver := getVersionFromKubectlOutput(`Client Version: v1.8.0 + Server Version: v1.8.12 + `) + if ver != "1.8" { + t.Fatalf("Expected 1.8 got %s", ver) + } + + ver = getVersionFromKubectlOutput("Something completely different") + if ver != "1.6" { + t.Fatalf("Expected 1.6 got %s", ver) + } +} + func TestFindConfigFile(t *testing.T) { cases := []struct { input []string