Skip to content

Commit

Permalink
added test cases
Browse files Browse the repository at this point in the history
Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>
  • Loading branch information
NoSkillGirl committed Apr 22, 2021
1 parent b98f111 commit 8f9c974
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions pkg/kyverno/validate/commmand_test.go
@@ -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)
}
}
}

0 comments on commit 8f9c974

Please sign in to comment.