mirror of
https://github.com/aquasecurity/kube-bench.git
synced 2024-12-23 23:18:33 +00:00
Remove os.exit When not needed (#631)
* Update test.go * Update test_test.go
This commit is contained in:
parent
52ebfa5b5a
commit
1b5b6c2afe
@ -23,6 +23,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
yaml "gopkg.in/yaml.v2"
|
yaml "gopkg.in/yaml.v2"
|
||||||
"k8s.io/client-go/util/jsonpath"
|
"k8s.io/client-go/util/jsonpath"
|
||||||
)
|
)
|
||||||
@ -123,8 +124,8 @@ func (t *testItem) execute(s string) *testOutput {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(os.Stderr, "invalid flag in testitem definition")
|
glog.V(1).Infof(fmt.Sprintf("invalid flag in testitem definition"))
|
||||||
os.Exit(1)
|
return failTestItem("error invalid flag in testitem definition")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,8 +171,8 @@ func compareOp(tCompareOp string, flagVal string, tCompareValue string) (string,
|
|||||||
case "gt", "gte", "lt", "lte":
|
case "gt", "gte", "lt", "lte":
|
||||||
a, b, err := toNumeric(flagVal, tCompareValue)
|
a, b, err := toNumeric(flagVal, tCompareValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Not numeric value - flag: %q - compareValue: %q %v\n", flagVal, tCompareValue, err)
|
glog.V(1).Infof(fmt.Sprintf("Not numeric value - flag: %q - compareValue: %q %v\n", flagVal, tCompareValue, err))
|
||||||
os.Exit(1)
|
return "Invalid Number(s) used for comparison", false
|
||||||
}
|
}
|
||||||
switch tCompareOp {
|
switch tCompareOp {
|
||||||
case "gt":
|
case "gt":
|
||||||
@ -213,10 +214,14 @@ func compareOp(tCompareOp string, flagVal string, tCompareValue string) (string,
|
|||||||
case "bitmask":
|
case "bitmask":
|
||||||
expectedResultPattern = "bitmask '%s' AND '%s'"
|
expectedResultPattern = "bitmask '%s' AND '%s'"
|
||||||
requested, err := strconv.ParseInt(flagVal, 8, 64)
|
requested, err := strconv.ParseInt(flagVal, 8, 64)
|
||||||
|
if err != nil {
|
||||||
|
glog.V(1).Infof(fmt.Sprintf("Not numeric value - flag: %q - compareValue: %q %v\n", flagVal, tCompareValue, err))
|
||||||
|
return fmt.Sprintf("Not numeric value - flag: %s", flagVal), false
|
||||||
|
}
|
||||||
max, err := strconv.ParseInt(tCompareValue, 8, 64)
|
max, err := strconv.ParseInt(tCompareValue, 8, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Not numeric value - flag: %q - compareValue: %q %v\n", flagVal, tCompareValue, err)
|
glog.V(1).Infof(fmt.Sprintf("Not numeric value - flag: %q - compareValue: %q %v\n", flagVal, tCompareValue, err))
|
||||||
os.Exit(1)
|
return fmt.Sprintf("Not numeric value - flag: %s", tCompareValue), false
|
||||||
}
|
}
|
||||||
testResult = (max & requested) == requested
|
testResult = (max & requested) == requested
|
||||||
}
|
}
|
||||||
@ -330,8 +335,9 @@ func (ts *tests) execute(s string) *testOutput {
|
|||||||
// If no binary operation is specified, default to AND
|
// If no binary operation is specified, default to AND
|
||||||
switch ts.BinOp {
|
switch ts.BinOp {
|
||||||
default:
|
default:
|
||||||
fmt.Fprintf(os.Stderr, "unknown binary operator for tests %s\n", ts.BinOp)
|
glog.V(2).Info(fmt.Sprintf("unknown binary operator for tests %s\n", ts.BinOp))
|
||||||
os.Exit(1)
|
finalOutput.actualResult = fmt.Sprintf("unknown binary operator for tests %s\n", ts.BinOp)
|
||||||
|
return finalOutput
|
||||||
case and, "":
|
case and, "":
|
||||||
result = true
|
result = true
|
||||||
for i := range res {
|
for i := range res {
|
||||||
|
@ -531,11 +531,9 @@ func TestCompareOp(t *testing.T) {
|
|||||||
testResult: true},
|
testResult: true},
|
||||||
|
|
||||||
// Test Op "gt"
|
// Test Op "gt"
|
||||||
// TODO: test for non-numeric values.
|
{label: "op=gt, both empty", op: "gt", flagVal: "",
|
||||||
// toNumeric function currently uses os.Exit, which stops tests.
|
compareValue: "", expectedResultPattern: "Invalid Number(s) used for comparison",
|
||||||
// {label: "op=gt, both empty", op: "gt", flagVal: "",
|
testResult: false},
|
||||||
// compareValue: "", expectedResultPattern: "'' is greater than ''",
|
|
||||||
// testResult: true},
|
|
||||||
{label: "op=gt, 0 > 0", op: "gt", flagVal: "0",
|
{label: "op=gt, 0 > 0", op: "gt", flagVal: "0",
|
||||||
compareValue: "0", expectedResultPattern: "0 is greater than 0",
|
compareValue: "0", expectedResultPattern: "0 is greater than 0",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
@ -548,109 +546,124 @@ func TestCompareOp(t *testing.T) {
|
|||||||
{label: "op=gt, 5 > 5", op: "gt", flagVal: "5",
|
{label: "op=gt, 5 > 5", op: "gt", flagVal: "5",
|
||||||
compareValue: "5", expectedResultPattern: "5 is greater than 5",
|
compareValue: "5", expectedResultPattern: "5 is greater than 5",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
|
{label: "op=gt, Pikachu > 5", op: "gt", flagVal: "Pikachu",
|
||||||
|
compareValue: "5", expectedResultPattern: "Invalid Number(s) used for comparison",
|
||||||
|
testResult: false},
|
||||||
|
{label: "op=gt, 5 > Bulbasaur", op: "gt", flagVal: "5",
|
||||||
|
compareValue: "Bulbasaur", expectedResultPattern: "Invalid Number(s) used for comparison",
|
||||||
|
testResult: false},
|
||||||
// Test Op "lt"
|
// Test Op "lt"
|
||||||
// TODO: test for non-numeric values.
|
{label: "op=lt, both empty", op: "lt", flagVal: "",
|
||||||
// toNumeric function currently uses os.Exit, which stops tests.
|
compareValue: "", expectedResultPattern: "Invalid Number(s) used for comparison",
|
||||||
// {label: "op=lt, both empty", op: "lt", flagVal: "",
|
testResult: false},
|
||||||
// compareValue: "", expectedResultPattern: "'' is lower than ''",
|
{label: "op=lt, 0 < 0", op: "lt", flagVal: "0",
|
||||||
// testResult: true},
|
|
||||||
{label: "op=gt, 0 < 0", op: "lt", flagVal: "0",
|
|
||||||
compareValue: "0", expectedResultPattern: "0 is lower than 0",
|
compareValue: "0", expectedResultPattern: "0 is lower than 0",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
{label: "op=gt, 4 < 5", op: "lt", flagVal: "4",
|
{label: "op=lt, 4 < 5", op: "lt", flagVal: "4",
|
||||||
compareValue: "5", expectedResultPattern: "4 is lower than 5",
|
compareValue: "5", expectedResultPattern: "4 is lower than 5",
|
||||||
testResult: true},
|
testResult: true},
|
||||||
{label: "op=gt, 5 < 4", op: "lt", flagVal: "5",
|
{label: "op=lt, 5 < 4", op: "lt", flagVal: "5",
|
||||||
compareValue: "4", expectedResultPattern: "5 is lower than 4",
|
compareValue: "4", expectedResultPattern: "5 is lower than 4",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
{label: "op=gt, 5 < 5", op: "lt", flagVal: "5",
|
{label: "op=lt, 5 < 5", op: "lt", flagVal: "5",
|
||||||
compareValue: "5", expectedResultPattern: "5 is lower than 5",
|
compareValue: "5", expectedResultPattern: "5 is lower than 5",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
|
{label: "op=lt, Charmander < 5", op: "lt", flagVal: "Charmander",
|
||||||
|
compareValue: "5", expectedResultPattern: "Invalid Number(s) used for comparison",
|
||||||
|
testResult: false},
|
||||||
|
{label: "op=lt, 5 < Charmeleon", op: "lt", flagVal: "5",
|
||||||
|
compareValue: "Charmeleon", expectedResultPattern: "Invalid Number(s) used for comparison",
|
||||||
|
testResult: false},
|
||||||
// Test Op "gte"
|
// Test Op "gte"
|
||||||
// TODO: test for non-numeric values.
|
{label: "op=gte, both empty", op: "gte", flagVal: "",
|
||||||
// toNumeric function currently uses os.Exit, which stops tests.
|
compareValue: "", expectedResultPattern: "Invalid Number(s) used for comparison",
|
||||||
// {label: "op=gt, both empty", op: "gte", flagVal: "",
|
testResult: false},
|
||||||
// compareValue: "", expectedResultPattern: "'' is greater or equal to ''",
|
{label: "op=gte, 0 >= 0", op: "gte", flagVal: "0",
|
||||||
// testResult: true},
|
|
||||||
{label: "op=gt, 0 >= 0", op: "gte", flagVal: "0",
|
|
||||||
compareValue: "0", expectedResultPattern: "0 is greater or equal to 0",
|
compareValue: "0", expectedResultPattern: "0 is greater or equal to 0",
|
||||||
testResult: true},
|
testResult: true},
|
||||||
{label: "op=gt, 4 >= 5", op: "gte", flagVal: "4",
|
{label: "op=gte, 4 >= 5", op: "gte", flagVal: "4",
|
||||||
compareValue: "5", expectedResultPattern: "4 is greater or equal to 5",
|
compareValue: "5", expectedResultPattern: "4 is greater or equal to 5",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
{label: "op=gt, 5 >= 4", op: "gte", flagVal: "5",
|
{label: "op=gte, 5 >= 4", op: "gte", flagVal: "5",
|
||||||
compareValue: "4", expectedResultPattern: "5 is greater or equal to 4",
|
compareValue: "4", expectedResultPattern: "5 is greater or equal to 4",
|
||||||
testResult: true},
|
testResult: true},
|
||||||
{label: "op=gt, 5 >= 5", op: "gte", flagVal: "5",
|
{label: "op=gte, 5 >= 5", op: "gte", flagVal: "5",
|
||||||
compareValue: "5", expectedResultPattern: "5 is greater or equal to 5",
|
compareValue: "5", expectedResultPattern: "5 is greater or equal to 5",
|
||||||
testResult: true},
|
testResult: true},
|
||||||
|
{label: "op=gte, Ekans >= 5", op: "gte", flagVal: "Ekans",
|
||||||
|
compareValue: "5", expectedResultPattern: "Invalid Number(s) used for comparison",
|
||||||
|
testResult: false},
|
||||||
|
{label: "op=gte, 4 >= Zubat", op: "gte", flagVal: "4",
|
||||||
|
compareValue: "Zubat", expectedResultPattern: "Invalid Number(s) used for comparison",
|
||||||
|
testResult: false},
|
||||||
// Test Op "lte"
|
// Test Op "lte"
|
||||||
// TODO: test for non-numeric values.
|
{label: "op=lte, both empty", op: "lte", flagVal: "",
|
||||||
// toNumeric function currently uses os.Exit, which stops tests.
|
compareValue: "", expectedResultPattern: "Invalid Number(s) used for comparison",
|
||||||
// {label: "op=gt, both empty", op: "lte", flagVal: "",
|
testResult: false},
|
||||||
// compareValue: "", expectedResultPattern: "'' is lower or equal to ''",
|
{label: "op=lte, 0 <= 0", op: "lte", flagVal: "0",
|
||||||
// testResult: true},
|
|
||||||
{label: "op=gt, 0 <= 0", op: "lte", flagVal: "0",
|
|
||||||
compareValue: "0", expectedResultPattern: "0 is lower or equal to 0",
|
compareValue: "0", expectedResultPattern: "0 is lower or equal to 0",
|
||||||
testResult: true},
|
testResult: true},
|
||||||
{label: "op=gt, 4 <= 5", op: "lte", flagVal: "4",
|
{label: "op=lte, 4 <= 5", op: "lte", flagVal: "4",
|
||||||
compareValue: "5", expectedResultPattern: "4 is lower or equal to 5",
|
compareValue: "5", expectedResultPattern: "4 is lower or equal to 5",
|
||||||
testResult: true},
|
testResult: true},
|
||||||
{label: "op=gt, 5 <= 4", op: "lte", flagVal: "5",
|
{label: "op=lte, 5 <= 4", op: "lte", flagVal: "5",
|
||||||
compareValue: "4", expectedResultPattern: "5 is lower or equal to 4",
|
compareValue: "4", expectedResultPattern: "5 is lower or equal to 4",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
{label: "op=gt, 5 <= 5", op: "lte", flagVal: "5",
|
{label: "op=lte, 5 <= 5", op: "lte", flagVal: "5",
|
||||||
compareValue: "5", expectedResultPattern: "5 is lower or equal to 5",
|
compareValue: "5", expectedResultPattern: "5 is lower or equal to 5",
|
||||||
testResult: true},
|
testResult: true},
|
||||||
|
{label: "op=lte, Venomoth <= 4", op: "lte", flagVal: "Venomoth",
|
||||||
|
compareValue: "4", expectedResultPattern: "Invalid Number(s) used for comparison",
|
||||||
|
testResult: false},
|
||||||
|
{label: "op=lte, 5 <= Meowth", op: "lte", flagVal: "5",
|
||||||
|
compareValue: "Meowth", expectedResultPattern: "Invalid Number(s) used for comparison",
|
||||||
|
testResult: false},
|
||||||
|
|
||||||
// Test Op "has"
|
// Test Op "has"
|
||||||
{label: "op=gt, both empty", op: "has", flagVal: "",
|
{label: "op=has, both empty", op: "has", flagVal: "",
|
||||||
compareValue: "", expectedResultPattern: "'' has ''",
|
compareValue: "", expectedResultPattern: "'' has ''",
|
||||||
testResult: true},
|
testResult: true},
|
||||||
{label: "op=gt, flagVal=empty", op: "has", flagVal: "",
|
{label: "op=has, flagVal=empty", op: "has", flagVal: "",
|
||||||
compareValue: "blah", expectedResultPattern: "'' has 'blah'",
|
compareValue: "blah", expectedResultPattern: "'' has 'blah'",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
{label: "op=gt, compareValue=empty", op: "has", flagVal: "blah",
|
{label: "op=has, compareValue=empty", op: "has", flagVal: "blah",
|
||||||
compareValue: "", expectedResultPattern: "'blah' has ''",
|
compareValue: "", expectedResultPattern: "'blah' has ''",
|
||||||
testResult: true},
|
testResult: true},
|
||||||
{label: "op=gt, 'blah' has 'la'", op: "has", flagVal: "blah",
|
{label: "op=has, 'blah' has 'la'", op: "has", flagVal: "blah",
|
||||||
compareValue: "la", expectedResultPattern: "'blah' has 'la'",
|
compareValue: "la", expectedResultPattern: "'blah' has 'la'",
|
||||||
testResult: true},
|
testResult: true},
|
||||||
{label: "op=gt, 'blah' has 'LA'", op: "has", flagVal: "blah",
|
{label: "op=has, 'blah' has 'LA'", op: "has", flagVal: "blah",
|
||||||
compareValue: "LA", expectedResultPattern: "'blah' has 'LA'",
|
compareValue: "LA", expectedResultPattern: "'blah' has 'LA'",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
{label: "op=gt, 'blah' has 'lo'", op: "has", flagVal: "blah",
|
{label: "op=has, 'blah' has 'lo'", op: "has", flagVal: "blah",
|
||||||
compareValue: "lo", expectedResultPattern: "'blah' has 'lo'",
|
compareValue: "lo", expectedResultPattern: "'blah' has 'lo'",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
|
|
||||||
// Test Op "nothave"
|
// Test Op "nothave"
|
||||||
{label: "op=gt, both empty", op: "nothave", flagVal: "",
|
{label: "op=nothave, both empty", op: "nothave", flagVal: "",
|
||||||
compareValue: "", expectedResultPattern: " '' not have ''",
|
compareValue: "", expectedResultPattern: " '' not have ''",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
{label: "op=gt, flagVal=empty", op: "nothave", flagVal: "",
|
{label: "op=nothave, flagVal=empty", op: "nothave", flagVal: "",
|
||||||
compareValue: "blah", expectedResultPattern: " '' not have 'blah'",
|
compareValue: "blah", expectedResultPattern: " '' not have 'blah'",
|
||||||
testResult: true},
|
testResult: true},
|
||||||
{label: "op=gt, compareValue=empty", op: "nothave", flagVal: "blah",
|
{label: "op=nothave, compareValue=empty", op: "nothave", flagVal: "blah",
|
||||||
compareValue: "", expectedResultPattern: " 'blah' not have ''",
|
compareValue: "", expectedResultPattern: " 'blah' not have ''",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
{label: "op=gt, 'blah' not have 'la'", op: "nothave", flagVal: "blah",
|
{label: "op=nothave, 'blah' not have 'la'", op: "nothave", flagVal: "blah",
|
||||||
compareValue: "la", expectedResultPattern: " 'blah' not have 'la'",
|
compareValue: "la", expectedResultPattern: " 'blah' not have 'la'",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
{label: "op=gt, 'blah' not have 'LA'", op: "nothave", flagVal: "blah",
|
{label: "op=nothave, 'blah' not have 'LA'", op: "nothave", flagVal: "blah",
|
||||||
compareValue: "LA", expectedResultPattern: " 'blah' not have 'LA'",
|
compareValue: "LA", expectedResultPattern: " 'blah' not have 'LA'",
|
||||||
testResult: true},
|
testResult: true},
|
||||||
{label: "op=gt, 'blah' not have 'lo'", op: "nothave", flagVal: "blah",
|
{label: "op=nothave, 'blah' not have 'lo'", op: "nothave", flagVal: "blah",
|
||||||
compareValue: "lo", expectedResultPattern: " 'blah' not have 'lo'",
|
compareValue: "lo", expectedResultPattern: " 'blah' not have 'lo'",
|
||||||
testResult: true},
|
testResult: true},
|
||||||
|
|
||||||
// Test Op "regex"
|
// Test Op "regex"
|
||||||
{label: "op=gt, both empty", op: "regex", flagVal: "",
|
{label: "op=regex, both empty", op: "regex", flagVal: "",
|
||||||
compareValue: "", expectedResultPattern: " '' matched by ''",
|
compareValue: "", expectedResultPattern: " '' matched by ''",
|
||||||
testResult: true},
|
testResult: true},
|
||||||
{label: "op=gt, flagVal=empty", op: "regex", flagVal: "",
|
{label: "op=regex, flagVal=empty", op: "regex", flagVal: "",
|
||||||
compareValue: "blah", expectedResultPattern: " '' matched by 'blah'",
|
compareValue: "blah", expectedResultPattern: " '' matched by 'blah'",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
|
|
||||||
@ -679,6 +692,12 @@ func TestCompareOp(t *testing.T) {
|
|||||||
{label: "op=bitmask, 644 AND 211", op: "bitmask", flagVal: "211",
|
{label: "op=bitmask, 644 AND 211", op: "bitmask", flagVal: "211",
|
||||||
compareValue: "644", expectedResultPattern: "bitmask '211' AND '644'",
|
compareValue: "644", expectedResultPattern: "bitmask '211' AND '644'",
|
||||||
testResult: false},
|
testResult: false},
|
||||||
|
{label: "op=bitmask, Harry AND 211", op: "bitmask", flagVal: "Harry",
|
||||||
|
compareValue: "644", expectedResultPattern: "Not numeric value - flag: Harry",
|
||||||
|
testResult: false},
|
||||||
|
{label: "op=bitmask, 644 AND Potter", op: "bitmask", flagVal: "211",
|
||||||
|
compareValue: "Potter", expectedResultPattern: "Not numeric value - flag: Potter",
|
||||||
|
testResult: false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
|
Loading…
Reference in New Issue
Block a user