Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>
- Loading branch information
1 parent
b98f111
commit 8f9c974
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package validate | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1" | ||
"gotest.tools/assert" | ||
) | ||
|
||
func Test_validateUsingPolicyCRD(t *testing.T) { | ||
type TestCase struct { | ||
rawPolicy []byte | ||
errorDetail string | ||
} | ||
|
||
testcases := []TestCase{ | ||
{ | ||
rawPolicy: []byte(` | ||
{ | ||
"apiVersion": "kyverno.io/v1", | ||
"kind": "ClusterPolicy", | ||
"metadata": { | ||
"name": "add-requests" | ||
}, | ||
"spec": { | ||
"rules": [ | ||
{ | ||
"name": "Set memory and/or cpu requests for all pods in namespaces labeled 'myprivatelabel'", | ||
"match": { | ||
"resources": { | ||
"kinds": [ | ||
"Pod" | ||
] | ||
} | ||
}, | ||
"mutate": { | ||
"overlay": { | ||
"spec": { | ||
"containers": [ | ||
{ | ||
"(name)": "*", | ||
"resources": { | ||
"requests": { | ||
"cpu": "1000m" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
`), | ||
errorDetail: "spec.rules.name in body should be at most 63 chars long", | ||
}, | ||
{ | ||
rawPolicy: []byte(` | ||
{ | ||
"apiVersion": "kyverno.io/v1", | ||
"kind": "ClusterPolicy", | ||
"metadata": { | ||
"name": "min-replicas-clusterpolicy" | ||
}, | ||
"spec": { | ||
"validationFailureAction": "audit", | ||
"rules": [ | ||
{ | ||
"name": "check-min-replicas", | ||
"match": { | ||
"resources": { | ||
"kinds": [ | ||
"Deployment" | ||
] | ||
} | ||
}, | ||
"validate": { | ||
"message": "should have at least 2 replicas", | ||
"pattern": { | ||
"spec": { | ||
"replicas": 2 | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
`), | ||
errorDetail: "", | ||
}, | ||
} | ||
|
||
v1crd, err := getPolicyCRD() | ||
assert.NilError(t, err) | ||
|
||
var policy kyverno.ClusterPolicy | ||
for _, tc := range testcases { | ||
err = json.Unmarshal(tc.rawPolicy, &policy) | ||
assert.NilError(t, err) | ||
|
||
_, errorList := validatePolicyAccordingToPolicyCRD(&policy, v1crd) | ||
fmt.Println("errorList: ", errorList) | ||
for _, e := range errorList { | ||
assert.Assert(t, tc.errorDetail == e.Detail) | ||
} | ||
} | ||
} |