1
0
mirror of https://github.com/aquasecurity/kube-bench.git synced 2024-11-25 17:38:21 +00:00

Fix kubeVersion regex tests

This commit is contained in:
Liz Rice 2017-11-21 13:19:09 +00:00
parent 471c02f4d7
commit 730871f330
3 changed files with 25 additions and 18 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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