mirror of
https://github.com/aquasecurity/kube-bench.git
synced 2025-06-02 14:19:09 +00:00
Adds Unit Tests for check/toNumeric (#401)
* fixes issue #364 * fixed unit test error text
This commit is contained in:
parent
050145f6b3
commit
4416e46967
@ -162,25 +162,29 @@ func compareOp(tCompareOp string, flagVal string, tCompareValue string) (string,
|
|||||||
testResult = !(flagVal == tCompareValue)
|
testResult = !(flagVal == tCompareValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "gt":
|
case "gt", "gte", "lt", "lte":
|
||||||
expectedResultPattern = "%s is greater than %s"
|
a, b, err := toNumeric(flagVal, tCompareValue)
|
||||||
a, b := toNumeric(flagVal, tCompareValue)
|
if err != nil {
|
||||||
testResult = a > b
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
switch tCompareOp {
|
||||||
|
case "gt":
|
||||||
|
expectedResultPattern = "%s is greater than %s"
|
||||||
|
testResult = a > b
|
||||||
|
|
||||||
case "gte":
|
case "gte":
|
||||||
expectedResultPattern = "%s is greater or equal to %s"
|
expectedResultPattern = "%s is greater or equal to %s"
|
||||||
a, b := toNumeric(flagVal, tCompareValue)
|
testResult = a >= b
|
||||||
testResult = a >= b
|
|
||||||
|
|
||||||
case "lt":
|
case "lt":
|
||||||
expectedResultPattern = "%s is lower than %s"
|
expectedResultPattern = "%s is lower than %s"
|
||||||
a, b := toNumeric(flagVal, tCompareValue)
|
testResult = a < b
|
||||||
testResult = a < b
|
|
||||||
|
|
||||||
case "lte":
|
case "lte":
|
||||||
expectedResultPattern = "%s is lower or equal to %s"
|
expectedResultPattern = "%s is lower or equal to %s"
|
||||||
a, b := toNumeric(flagVal, tCompareValue)
|
testResult = a <= b
|
||||||
testResult = a <= b
|
}
|
||||||
|
|
||||||
case "has":
|
case "has":
|
||||||
expectedResultPattern = "'%s' has '%s'"
|
expectedResultPattern = "'%s' has '%s'"
|
||||||
@ -342,18 +346,15 @@ func (ts *tests) execute(s string) *testOutput {
|
|||||||
return finalOutput
|
return finalOutput
|
||||||
}
|
}
|
||||||
|
|
||||||
func toNumeric(a, b string) (c, d int) {
|
func toNumeric(a, b string) (c, d int, err error) {
|
||||||
var err error
|
c, err = strconv.Atoi(strings.TrimSpace(a))
|
||||||
c, err = strconv.Atoi(a)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "error converting %s: %s\n", a, err)
|
return -1, -1, fmt.Errorf("toNumeric - error converting %s: %s", a, err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
d, err = strconv.Atoi(b)
|
d, err = strconv.Atoi(strings.TrimSpace(b))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "error converting %s: %s\n", b, err)
|
return -1, -1, fmt.Errorf("toNumeric - error converting %s: %s", b, err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c, d
|
return c, d, nil
|
||||||
}
|
}
|
||||||
|
@ -656,3 +656,38 @@ func TestCompareOp(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToNumeric(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
firstValue string
|
||||||
|
secondValue string
|
||||||
|
expectedToFail bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
firstValue: "a",
|
||||||
|
secondValue: "b",
|
||||||
|
expectedToFail: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
firstValue: "5",
|
||||||
|
secondValue: "b",
|
||||||
|
expectedToFail: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
firstValue: "5",
|
||||||
|
secondValue: "6",
|
||||||
|
expectedToFail: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
f, s, err := toNumeric(c.firstValue, c.secondValue)
|
||||||
|
if c.expectedToFail && err == nil {
|
||||||
|
t.Errorf("TestToNumeric - Expected error while converting %s and %s", c.firstValue, c.secondValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !c.expectedToFail && (f != 5 || s != 6) {
|
||||||
|
t.Errorf("TestToNumeric - Expected to return %d,%d , but instead got %d,%d", 5, 6, f, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user