1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 07:57:07 +00:00
kyverno/pkg/webhooks/annotations_test.go

100 lines
4 KiB
Go
Raw Normal View History

2019-10-07 18:31:14 -07:00
package webhooks
import (
"testing"
"github.com/nirmata/kyverno/pkg/engine"
"gotest.tools/assert"
2019-11-11 19:08:46 -08:00
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2019-10-07 18:31:14 -07:00
)
func newPolicyResponse(policy, rule string, patchesStr []string, success bool) engine.PolicyResponse {
var patches [][]byte
for _, p := range patchesStr {
patches = append(patches, []byte(p))
}
return engine.PolicyResponse{
Policy: policy,
Rules: []engine.RuleResponse{
engine.RuleResponse{
Name: rule,
Patches: patches,
Success: success},
},
}
}
2019-11-11 19:08:46 -08:00
func newEngineResponse(policy, rule string, patchesStr []string, success bool, annotation map[string]string) engine.EngineResponse {
return engine.EngineResponse{
2019-11-11 19:08:46 -08:00
PatchedResource: unstructured.Unstructured{
Object: map[string]interface{}{
"metadata": map[string]interface{}{
"annotation": annotation,
},
},
},
2019-10-07 18:31:14 -07:00
PolicyResponse: newPolicyResponse(policy, rule, patchesStr, success),
}
}
func Test_empty_annotation(t *testing.T) {
patchStr := `{ "op": "replace", "path": "/spec/containers/0/imagePullPolicy", "value": "IfNotPresent" }`
2019-11-11 19:08:46 -08:00
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", []string{patchStr}, true, nil)
2019-10-07 18:31:14 -07:00
2019-11-11 19:08:46 -08:00
annPatches := generateAnnotationPatches([]engine.EngineResponse{engineResponse})
expectedPatches := `{"op":"add","path":"/metadata/annotations","value":{"policies.kyverno.patches":"[{\"policyname\":\"mutate-container\",\"patches\":[{\"rulename\":\"default-imagepullpolicy\",\"op\":\"replace\",\"path\":\"/spec/containers/0/imagePullPolicy\"}]}]"}}`
2019-10-07 18:31:14 -07:00
assert.Assert(t, string(annPatches) == expectedPatches)
}
func Test_exist_annotation(t *testing.T) {
annotation := map[string]string{
"test": "annotation",
}
patchStr := `{ "op": "replace", "path": "/spec/containers/0/imagePullPolicy", "value": "IfNotPresent" }`
2019-11-11 19:08:46 -08:00
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", []string{patchStr}, true, annotation)
annPatches := generateAnnotationPatches([]engine.EngineResponse{engineResponse})
2019-10-07 18:31:14 -07:00
2019-11-11 19:08:46 -08:00
expectedPatches := `{"op":"add","path":"/metadata/annotations","value":{"policies.kyverno.patches":"[{\"policyname\":\"mutate-container\",\"patches\":[{\"rulename\":\"default-imagepullpolicy\",\"op\":\"replace\",\"path\":\"/spec/containers/0/imagePullPolicy\"}]}]"}}`
2019-10-07 18:31:14 -07:00
assert.Assert(t, string(annPatches) == expectedPatches)
}
func Test_exist_kyverno_annotation(t *testing.T) {
annotation := map[string]string{
2019-11-11 19:08:46 -08:00
"policies.kyverno.patches": "old-annotation",
2019-10-07 18:31:14 -07:00
}
patchStr := `{ "op": "replace", "path": "/spec/containers/0/imagePullPolicy", "value": "IfNotPresent" }`
2019-11-11 19:08:46 -08:00
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", []string{patchStr}, true, annotation)
annPatches := generateAnnotationPatches([]engine.EngineResponse{engineResponse})
2019-10-07 18:31:14 -07:00
2019-11-11 19:08:46 -08:00
expectedPatches := `{"op":"add","path":"/metadata/annotations","value":{"policies.kyverno.patches":"[{\"policyname\":\"mutate-container\",\"patches\":[{\"rulename\":\"default-imagepullpolicy\",\"op\":\"replace\",\"path\":\"/spec/containers/0/imagePullPolicy\"}]}]"}}`
2019-10-07 18:31:14 -07:00
assert.Assert(t, string(annPatches) == expectedPatches)
}
func Test_annotation_nil_patch(t *testing.T) {
annotation := map[string]string{
2019-11-11 19:08:46 -08:00
"policies.kyverno.patches": "old-annotation",
2019-10-07 18:31:14 -07:00
}
2019-11-11 19:08:46 -08:00
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", nil, true, annotation)
annPatches := generateAnnotationPatches([]engine.EngineResponse{engineResponse})
2019-10-07 18:31:14 -07:00
assert.Assert(t, annPatches == nil)
2019-11-11 19:08:46 -08:00
engineResponseNew := newEngineResponse("mutate-container", "default-imagepullpolicy", []string{""}, true, annotation)
annPatchesNew := generateAnnotationPatches([]engine.EngineResponse{engineResponseNew})
2019-10-07 18:31:14 -07:00
assert.Assert(t, annPatchesNew == nil)
}
func Test_annotation_failed_Patch(t *testing.T) {
annotation := map[string]string{
2019-11-11 19:08:46 -08:00
"policies.kyverno.patches": "old-annotation",
2019-10-07 18:31:14 -07:00
}
2019-11-11 19:08:46 -08:00
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", nil, false, annotation)
annPatches := generateAnnotationPatches([]engine.EngineResponse{engineResponse})
2019-10-07 18:31:14 -07:00
assert.Assert(t, annPatches == nil)
}