Skip to content

Commit

Permalink
Correct lessOrEqual and greateOrEqual validators.
Browse files Browse the repository at this point in the history
  • Loading branch information
quintush committed Dec 31, 2024
1 parent 082c20e commit 71c159e
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 65 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
0.7.1 / 2024-12-31
==================
- Move custom MergeValues to HelmCharts MergeTables to align merging values with Helm (relates #471)
- Correct mix up test value with expectation in lessOrEqual and greaterOrEqual validators (resolves #515)
- Refactoring for improved modules update (credits @ivankatliarchuk, relates #501)
- Added extra tests for validation (credits @ivankatliarchuk)
- Update packages to latest patch versions
- Update documentation

0.7.0 / 2024-11-25
==================
- Fix template path is translated differently (credits @ivankatliarchuk, resolves #300)
Expand Down
43 changes: 10 additions & 33 deletions pkg/unittest/validators/equal_or_greater_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ func TestEqualOrGreaterValidatorOk(t *testing.T) {
name: "test case 1: int values",
doc: "spec: 4",
path: "spec",
value: 5,
value: 3,
expected: true,
},
{
name: "test case 2: float64 values",
doc: "cpu: 0.6",
path: "cpu",
value: 0.75,
value: 0.5,
expected: true,
},
{
Expand Down Expand Up @@ -68,52 +68,52 @@ func TestEqualOrGreaterValidatorFail(t *testing.T) {
name: "test case 1: int values",
doc: "value: 25",
path: "value",
value: 5,
value: 55,
errorMsg: []string{
"DocumentIndex:\t0",
"ValuesIndex:\t0",
"Path:\tvalue",
"Expected to be greater then or equal to, got:",
"\tthe expected '5' is not greater or equal to the actual '25'",
"\tthe actual '25' is not greater or equal to the expected '55'",
},
},
{
name: "test case 2: float64 values",
doc: "cpu: 1.7",
path: "cpu",
value: 1.31,
value: 1.91,
errorMsg: []string{
"DocumentIndex:\t0",
"ValuesIndex:\t0",
"Path:\tcpu",
"Expected to be greater then or equal to, got:",
"\tthe expected '1.31' is not greater or equal to the actual '1.7'",
"\tthe actual '1.7' is not greater or equal to the expected '1.91'",
},
},
{
name: "test case 3: float64 values",
doc: "cpu: 1.341",
path: "cpu",
value: 1.338,
value: 1.348,
errorMsg: []string{
"DocumentIndex:\t0",
"ValuesIndex:\t0",
"Path:\tcpu",
"Expected to be greater then or equal to, got:",
"\tthe expected '1.338' is not greater or equal to the actual '1.341'",
"\tthe actual '1.341' is not greater or equal to the expected '1.348'",
},
},
{
name: "test case 4: string values",
doc: "cpu: 600m",
path: "cpu",
value: "590m",
value: "690m",
errorMsg: []string{
"DocumentIndex:\t0",
"ValuesIndex:\t0",
"Path:\tcpu",
"Expected to be greater then or equal to, got:",
"\tthe expected '590m' is not greater or equal to the actual '600m'",
"\tthe actual '600m' is not greater or equal to the expected '690m'",
},
},
}
Expand Down Expand Up @@ -218,29 +218,6 @@ spec:
assert.Equal(t, []string{}, diff)
}

func TestEqualOrGreaterValidator_TODO(t *testing.T) {
var actual = `
kind: PodDisruptionBudget
metadata:
name: 'greaterorequal-test-pdb'
spec:
minAvailable: 2
unhealthyPodEvictionPolicy: AlwaysAllow
`
manifest := makeManifest(actual)

v := EqualOrGreaterValidator{
Path: "spec.minAvailable",
Value: 1,
}
pass, _ := v.Validate(&ValidateContext{
Docs: []common.K8sManifest{manifest},
Negative: false,
})

assert.False(t, pass)
}

func TestEqualOrGreaterValidatorWhenTypesDoNotMatch(t *testing.T) {
tests := []struct {
name, doc, path string
Expand Down
24 changes: 12 additions & 12 deletions pkg/unittest/validators/equal_or_less_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ func TestEqualOrLessValidatorOk(t *testing.T) {
name: "Test case 1: int ok",
doc: "spec: 4",
path: "spec",
value: 3,
value: 5,
expected: true,
},
{
name: "Test case 2: float64 ok",
doc: "cpu: 0.6",
path: "cpu",
value: 0.54,
value: 0.7,
expected: true,
},
{
name: "Test case 3: string ok",
doc: "cpu: 600m",
path: "cpu",
value: "580m",
value: "680m",
expected: true,
},
}
Expand Down Expand Up @@ -68,39 +68,39 @@ func TestEqualOrLessValidatorFail(t *testing.T) {
name: "Test case 1: int fail",
doc: "value: 6",
path: "value",
value: 7,
value: 5,
errorMsg: []string{
"DocumentIndex:\t0",
"ValuesIndex:\t0",
"Path:\tvalue",
"Expected to be less then or equal to, got:",
"\tthe expected '7' is not less or equal to the actual '6'",
"\tthe actual '6' is not less or equal to the expected '5'",
},
},
{
name: "Test case 2: float64 fail",
doc: "cpu: 1.7",
path: "cpu",
value: 1.71,
value: 1.69,
errorMsg: []string{
"DocumentIndex:\t0",
"ValuesIndex:\t0",
"Path:\tcpu",
"Expected to be less then or equal to, got:",
"\tthe expected '1.71' is not less or equal to the actual '1.7'",
"\tthe actual '1.7' is not less or equal to the expected '1.69'",
},
},
{
name: "Test case 3: float64 fail",
doc: "cpu: 1.341",
path: "cpu",
value: 1.342,
value: 1.34,
errorMsg: []string{
"DocumentIndex:\t0",
"ValuesIndex:\t0",
"Path:\tcpu",
"Expected to be less then or equal to, got:",
"\tthe expected '1.342' is not less or equal to the actual '1.341'",
"\tthe actual '1.341' is not less or equal to the expected '1.34'",
},
},
}
Expand Down Expand Up @@ -211,7 +211,7 @@ a:

v := EqualOrLessValidator{
Path: "a.*",
Value: 2,
Value: 0,
}
pass, diff := v.Validate(&ValidateContext{
FailFast: true,
Expand All @@ -224,7 +224,7 @@ a:
"ValuesIndex:\t0",
"Path:\ta.*",
"Expected to be less then or equal to, got:",
"\tthe expected '2' is not less or equal to the actual '1'",
"\tthe actual '1' is not less or equal to the expected '0'",
}, diff)
}

Expand All @@ -249,7 +249,7 @@ func TestEqualOrLessValidatorWhenNoManifestFail(t *testing.T) {
func TestEqualOrLessValidatorWhenNoManifestNegativeOk(t *testing.T) {
v := EqualOrLessValidator{
Path: "a.*",
Value: 2,
Value: 0,
}
pass, diff := v.Validate(&ValidateContext{
FailFast: true,
Expand Down
8 changes: 4 additions & 4 deletions pkg/unittest/validators/operator_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,28 @@ func (o operatorValidator) compareValues(expected, actual interface{}, compariso
}

if !result {
return false, []string{fmt.Sprintf("the expected '%s' is not %s or equal to the actual '%s'", expStr, comparisonType, actStr)}
return false, []string{fmt.Sprintf("the actual '%s' is not %s or equal to the expected '%s'", actStr, comparisonType, expStr)}
}

return true, nil
}

func (o operatorValidator) compareStringValues(expected, actual string, comparisonType string, negative bool) bool {
if (comparisonType == "greater" && expected >= actual) || (comparisonType == "less" && expected <= actual) == negative {
if (comparisonType == "greater" && actual >= expected) || (comparisonType == "less" && actual <= expected) == negative {
return true
}
return false
}

func (o operatorValidator) compareIntValues(expected, actual int, comparisonType string, negative bool) bool {
if (comparisonType == "greater" && expected >= actual) || (comparisonType == "less" && expected <= actual) == negative {
if (comparisonType == "greater" && actual >= expected) || (comparisonType == "less" && actual <= expected) == negative {
return true
}
return false
}

func (o operatorValidator) compareFloatValues(expected, actual float64, comparisonType string, negative bool) bool {
if (comparisonType == "greater" && expected >= actual) || (comparisonType == "less" && expected <= actual) == negative {
if (comparisonType == "greater" && actual >= expected) || (comparisonType == "less" && actual <= expected) == negative {
return true
}
return false
Expand Down
2 changes: 1 addition & 1 deletion plugin-dbg.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "unittest"
version: "0.7.0"
version: "0.7.1"
usage: "unittest for helm charts"
description: "Unit test for helm chart in YAML with ease to keep your chart functional and robust."
ignoreFlags: false
Expand Down
2 changes: 1 addition & 1 deletion plugin.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "unittest"
version: "0.7.0"
version: "0.7.1"
usage: "unittest for helm charts"
description: "Unit test for helm chart in YAML with ease to keep your chart functional and robust."
ignoreFlags: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
should pass all kinds of assertion:
1: |
replicas: 1
replicas: 2
template:
metadata:
labels:
Expand Down
20 changes: 10 additions & 10 deletions test/data/v3/basic/tests/deployment_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ tests:
asserts:
- greaterOrEqual:
path: metadata.labels.appVersion
value: 2.0.0
value: 0.0.1
- greaterOrEqual:
path: spec.template.spec.securityContext.runAsUser
value: 1286
value: 1000
- greaterOrEqual:
path: spec.template.spec.containers[?(@.name == "basic")].resources.limits.cpu
value: "1540m"
value: "1440m"
- greaterOrEqual:
path: spec.template.spec.containers[?(@.name == "basic")].resources.requests.cpu
value: 1.11
value: 1.0
- greaterOrEqual:
path: spec.template.spec.containers[?(@.name == "basic")].resources.requests.cpu
value: 1.1
Expand All @@ -125,25 +125,25 @@ tests:
value: 10.0.0
- lessOrEqual:
path: spec.template.spec.securityContext.runAsUser
value: 1281
value: 1285
- lessOrEqual:
path: spec.template.spec.containers[?(@.name == "basic")].resources.limits.cpu
value: "1280m"
value: "2000m"
- lessOrEqual:
path: spec.template.spec.containers[?(@.name == "basic")].resources.requests.cpu
value: 1.09
value: 1.9
- lessOrEqual:
path: spec.template.spec.containers[?(@.name == "basic")].resources.requests.cpu
value: 1.1
- notLessOrEqual:
path: spec.template.spec.containers[?(@.name == "basic")].resources.requests.cpu
value: 1.2
value: 1.0
- notGreaterOrEqual:
path: spec.template.spec.containers[?(@.name == "basic")].resources.requests.cpu
value: 11.0
value: 1.0
- notGreaterOrEqual:
path: spec.template.spec.containers[?(@.name == "basic")].resources.requests.cpu
value: 0.1
value: 1.2
- notGreaterOrEqual:
path: spec.template.spec.containers[?(@.name == "basic")].resources.requests.memory
value: "1Mi"
Expand Down
2 changes: 1 addition & 1 deletion test/data/v3/basic/tests/pdp_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ tests:
path: spec.minAvailable
- greaterOrEqual:
path: spec.minAvailable
value: 2
value: 1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
should fail all kinds of assertion:
1: |
replicas: 1
replicas: 2
template:
metadata:
labels:
Expand Down
2 changes: 1 addition & 1 deletion test/data/v3/basic/values.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Default values for basic.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
replicaCount: 2
image:
repository: nginx
tag: stable
Expand Down

0 comments on commit 71c159e

Please sign in to comment.