mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-06 16:06:56 +00:00
- Change in namespace for test-generate example - Change cloneResource to cloneSourceResource - Add support for namespaced Policy and fix log messages - Add test-generate in Makefile and an example of namespaced Policy - Fix namespaced policy issue and add comments - Refactor according to new generate controller - Add json tag to GeneratedResource field of RuleResponse struct Signed-off-by: Shubham Nazare <shubham4443@gmail.com> Co-authored-by: Prateek Pandey <prateek.pandey@nirmata.com> Co-authored-by: Vyankatesh Kudtarkar <vyankateshkd@gmail.com>
115 lines
2.7 KiB
Go
115 lines
2.7 KiB
Go
package common
|
|
|
|
import (
|
|
"testing"
|
|
|
|
v1beta1 "github.com/kyverno/kyverno/api/kyverno/v1beta1"
|
|
"github.com/kyverno/kyverno/pkg/toggle"
|
|
ut "github.com/kyverno/kyverno/pkg/utils"
|
|
"gotest.tools/assert"
|
|
)
|
|
|
|
var policyNamespaceSelector = []byte(`{
|
|
"apiVersion": "kyverno.io/v1",
|
|
"kind": "ClusterPolicy",
|
|
"metadata": {
|
|
"name": "enforce-pod-name"
|
|
},
|
|
"spec": {
|
|
"validationFailureAction": "audit",
|
|
"background": true,
|
|
"rules": [
|
|
{
|
|
"name": "validate-name",
|
|
"match": {
|
|
"resources": {
|
|
"kinds": [
|
|
"Pod"
|
|
],
|
|
"namespaceSelector": {
|
|
"matchExpressions": [
|
|
{
|
|
"key": "foo.com/managed-state",
|
|
"operator": "In",
|
|
"values": [
|
|
"managed"
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}
|
|
},
|
|
"validate": {
|
|
"message": "The Pod must end with -nginx",
|
|
"pattern": {
|
|
"metadata": {
|
|
"name": "*-nginx"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
`)
|
|
|
|
func Test_NamespaceSelector(t *testing.T) {
|
|
type TestCase struct {
|
|
policy []byte
|
|
resource []byte
|
|
namespaceSelectorMap map[string]map[string]string
|
|
result ResultCounts
|
|
}
|
|
|
|
testcases := []TestCase{
|
|
{
|
|
policy: policyNamespaceSelector,
|
|
resource: []byte(`{"apiVersion":"v1","kind":"Pod","metadata":{"name":"nginx","namespace":"test1"},"spec":{"containers":[{"image":"nginx:latest","name":"test-fail"}]}}`),
|
|
namespaceSelectorMap: map[string]map[string]string{
|
|
"test1": {
|
|
"foo.com/managed-state": "managed",
|
|
},
|
|
},
|
|
result: ResultCounts{
|
|
Pass: 0,
|
|
Fail: 1,
|
|
Warn: 0,
|
|
Error: 0,
|
|
Skip: 2,
|
|
},
|
|
},
|
|
{
|
|
policy: policyNamespaceSelector,
|
|
resource: []byte(`{"apiVersion":"v1","kind":"Pod","metadata":{"name":"test-nginx","namespace":"test1"},"spec":{"containers":[{"image":"nginx:latest","name":"test-pass"}]}}`),
|
|
namespaceSelectorMap: map[string]map[string]string{
|
|
"test1": {
|
|
"foo.com/managed-state": "managed",
|
|
},
|
|
},
|
|
result: ResultCounts{
|
|
Pass: 1,
|
|
Fail: 1,
|
|
Warn: 0,
|
|
Error: 0,
|
|
Skip: 4,
|
|
},
|
|
},
|
|
}
|
|
|
|
rc := &ResultCounts{}
|
|
for _, tc := range testcases {
|
|
policyArray, _ := ut.GetPolicy(tc.policy)
|
|
resourceArray, _ := GetResource(tc.resource)
|
|
ApplyPolicyOnResource(policyArray[0], resourceArray[0], "", false, nil, v1beta1.RequestInfo{}, false, tc.namespaceSelectorMap, false, rc, false, nil)
|
|
assert.Equal(t, int64(rc.Pass), int64(tc.result.Pass))
|
|
assert.Equal(t, int64(rc.Fail), int64(tc.result.Fail))
|
|
// TODO: autogen rules seem to not be present when autogen internals is disabled
|
|
if toggle.AutogenInternals() {
|
|
assert.Equal(t, int64(rc.Skip), int64(tc.result.Skip))
|
|
} else {
|
|
assert.Equal(t, int64(rc.Skip), int64(0))
|
|
}
|
|
assert.Equal(t, int64(rc.Warn), int64(tc.result.Warn))
|
|
assert.Equal(t, int64(rc.Error), int64(tc.result.Error))
|
|
}
|
|
}
|