1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00

644 added test to verify usage

This commit is contained in:
shravan 2020-02-09 12:34:59 +05:30
parent c7bed6f3ff
commit b32b52224d

View file

@ -18,20 +18,36 @@ func TestMatchesResourceDescription(t *testing.T) {
Description string Description string
AdmissionInfo kyverno.RequestInfo AdmissionInfo kyverno.RequestInfo
Resource []byte Resource []byte
Rule []byte Policy []byte
areErrorsExpected bool areErrorsExpected bool
}{ }{
{ {
Description: "",
AdmissionInfo: kyverno.RequestInfo{
ClusterRoles: []string{"admin"},
},
Resource: []byte(`{"apiVersion":"v1","kind":"Pod","metadata":{"name":"hello-world","labels":{"name":"hello-world"}},"spec":{"containers":[{"name":"hello-world","image":"hello-world","ports":[{"containerPort":81}],"resources":{"limits":{"memory":"30Mi","cpu":"0.2"},"requests":{"memory":"20Mi","cpu":"0.1"}}}]}}`), Resource: []byte(`{"apiVersion":"v1","kind":"Pod","metadata":{"name":"hello-world","labels":{"name":"hello-world"}},"spec":{"containers":[{"name":"hello-world","image":"hello-world","ports":[{"containerPort":81}],"resources":{"limits":{"memory":"30Mi","cpu":"0.2"},"requests":{"memory":"20Mi","cpu":"0.1"}}}]}}`),
Rule: []byte(`{"name":"hello-world-policy","match":{"resources":{"kinds":["Pod"]}},"exclude":{"resources":{"name":"hello-world"},"clusterroles":["system:node"]},"mutate":{"overlay":{"spec":{"containers":[{"(image)":"*","imagePullPolicy":"IfNotPresent"}]}}}}`), Policy: []byte(`{"apiVersion":"kyverno.io/v1","kind":"ClusterPolicy","metadata":{"name":"hello-world-policy"},"spec":{"background":false,"rules":[{"name":"hello-world-policy","match":{"resources":{"kinds":["Pod"]}},"exclude":{"resources":{"name":"hello-world"},"clusterroles":["system:node"]},"mutate":{"overlay":{"spec":{"containers":[{"(image)":"*","imagePullPolicy":"IfNotPresent"}]}}}}]}}`),
areErrorsExpected: false, areErrorsExpected: false,
}, },
} }
for range tcs { for _, tc := range tcs {
} var policy kyverno.Policy
json.Unmarshal(tc.Policy, &policy)
resource, _ := utils.ConvertToUnstructured(tc.Resource)
for _, rule := range policy.Spec.Rules {
err := MatchesResourceDescription(*resource, rule, tc.AdmissionInfo)
if err != nil && !tc.areErrorsExpected {
t.Errorf("Unexpected error: %v", err)
} else {
if tc.areErrorsExpected {
t.Errorf("Expected Error but recievd no error")
}
}
}
}
} }
// Match multiple kinds // Match multiple kinds