1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-30 19:35:06 +00:00
kyverno/api/kyverno/v1/resource_description_test.go
Charles-Edouard Brétéché dc8a60a43e
feat: add operations support in match/exclude (#6658)
* feat: add operations support in match/exclude

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* clean

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* matching

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* operation

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* make operation mandatory

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* kuttl

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

---------

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2023-03-29 04:22:21 +00:00

70 lines
2 KiB
Go

package v1
import (
"testing"
"gotest.tools/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
)
func Test_ResourceDescription(t *testing.T) {
testCases := []struct {
name string
namespaced bool
subject ResourceDescription
errors []string
}{{
name: "valid",
namespaced: true,
subject: ResourceDescription{},
}, {
name: "name-names",
namespaced: true,
subject: ResourceDescription{
Name: "foo",
Names: []string{"bar", "baz"},
},
errors: []string{
`dummy: Invalid value: v1.ResourceDescription{Kinds:[]string(nil), Name:"foo", Names:[]string{"bar", "baz"}, Namespaces:[]string(nil), Annotations:map[string]string(nil), Selector:(*v1.LabelSelector)(nil), NamespaceSelector:(*v1.LabelSelector)(nil), Operations:[]v1.AdmissionOperation(nil)}: Both name and names can not be specified together`,
},
}, {
name: "selector",
namespaced: true,
subject: ResourceDescription{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app.type": "prod",
},
},
},
}, {
name: "bad-selector",
namespaced: true,
subject: ResourceDescription{
Kinds: []string{"Deployment"},
Selector: &metav1.LabelSelector{},
},
errors: []string{
`dummy.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string(nil), MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: The requirements are not specified in selector`,
},
}, {
name: "namespaces",
namespaced: true,
subject: ResourceDescription{
Namespaces: []string{"abc"},
},
errors: []string{
"dummy.namespaces: Forbidden: Filtering namespaces not allowed in namespaced policies",
},
}}
path := field.NewPath("dummy")
for _, testCase := range testCases {
errs := testCase.subject.Validate(path, testCase.namespaced, nil)
assert.Equal(t, len(errs), len(testCase.errors))
for i, err := range errs {
assert.Equal(t, err.Error(), testCase.errors[i])
}
}
}