1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 09:26:54 +00:00
kyverno/pkg/webhooks/annotations_test.go

114 lines
5.1 KiB
Go
Raw Normal View History

2019-10-07 18:31:14 -07:00
package webhooks
import (
"testing"
"github.com/kyverno/kyverno/pkg/engine/response"
2019-10-07 18:31:14 -07:00
"gotest.tools/assert"
2019-11-11 19:08:46 -08:00
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2020-03-17 16:25:34 -07:00
"sigs.k8s.io/controller-runtime/pkg/log"
2019-10-07 18:31:14 -07:00
)
func newPolicyResponse(policy, rule string, patchesStr []string, status response.RuleStatus) response.PolicyResponse {
2019-10-07 18:31:14 -07:00
var patches [][]byte
for _, p := range patchesStr {
patches = append(patches, []byte(p))
}
return response.PolicyResponse{
Policy: response.PolicySpec{Name: policy},
Rules: []response.RuleResponse{
{
2019-10-07 18:31:14 -07:00
Name: rule,
Patches: patches,
Status: status,
},
2019-10-07 18:31:14 -07:00
},
}
}
func newEngineResponse(policy, rule string, patchesStr []string, status response.RuleStatus, annotation map[string]interface{}) *response.EngineResponse {
2020-12-23 15:10:07 -08:00
return &response.EngineResponse{
2019-11-11 19:08:46 -08:00
PatchedResource: unstructured.Unstructured{
Object: map[string]interface{}{
"metadata": map[string]interface{}{
"annotations": annotation,
2019-11-11 19:08:46 -08:00
},
},
},
PolicyResponse: newPolicyResponse(policy, rule, patchesStr, status),
2019-10-07 18:31:14 -07:00
}
}
func Test_empty_annotation(t *testing.T) {
patchStr := `{ "op": "replace", "path": "/spec/containers/0/imagePullPolicy", "value": "IfNotPresent" }`
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", []string{patchStr}, response.RuleStatusPass, nil)
2019-10-07 18:31:14 -07:00
2020-12-23 15:10:07 -08:00
annPatches := generateAnnotationPatches([]*response.EngineResponse{engineResponse}, log.Log)
expectedPatches := `{"op":"add","path":"/metadata/annotations","value":{"policies.kyverno.io/last-applied-patches":"default-imagepullpolicy.mutate-container.kyverno.io: replaced /spec/containers/0/imagePullPolicy\n"}}`
assert.Assert(t, string(annPatches[0]) == expectedPatches)
2019-10-07 18:31:14 -07:00
}
func Test_exist_annotation(t *testing.T) {
annotation := map[string]interface{}{
2019-10-07 18:31:14 -07:00
"test": "annotation",
}
patchStr := `{ "op": "replace", "path": "/spec/containers/0/imagePullPolicy", "value": "IfNotPresent" }`
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", []string{patchStr}, response.RuleStatusPass, annotation)
2020-12-23 15:10:07 -08:00
annPatches := generateAnnotationPatches([]*response.EngineResponse{engineResponse}, log.Log)
2019-10-07 18:31:14 -07:00
expectedPatches := `{"op":"add","path":"/metadata/annotations/policies.kyverno.io~1last-applied-patches","value":"default-imagepullpolicy.mutate-container.kyverno.io: replaced /spec/containers/0/imagePullPolicy\n"}`
assert.Assert(t, string(annPatches[0]) == expectedPatches)
2019-10-07 18:31:14 -07:00
}
func Test_exist_kyverno_annotation(t *testing.T) {
annotation := map[string]interface{}{
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" }`
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", []string{patchStr}, response.RuleStatusPass, annotation)
2020-12-23 15:10:07 -08:00
annPatches := generateAnnotationPatches([]*response.EngineResponse{engineResponse}, log.Log)
2019-10-07 18:31:14 -07:00
expectedPatches := `{"op":"add","path":"/metadata/annotations/policies.kyverno.io~1last-applied-patches","value":"default-imagepullpolicy.mutate-container.kyverno.io: replaced /spec/containers/0/imagePullPolicy\n"}`
assert.Assert(t, string(annPatches[0]) == expectedPatches)
2019-10-07 18:31:14 -07:00
}
func Test_annotation_nil_patch(t *testing.T) {
annotation := map[string]interface{}{
2019-11-11 19:08:46 -08:00
"policies.kyverno.patches": "old-annotation",
2019-10-07 18:31:14 -07:00
}
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", nil, response.RuleStatusPass, annotation)
2020-12-23 15:10:07 -08:00
annPatches := generateAnnotationPatches([]*response.EngineResponse{engineResponse}, log.Log)
2019-10-07 18:31:14 -07:00
assert.Assert(t, annPatches == nil)
engineResponseNew := newEngineResponse("mutate-container", "default-imagepullpolicy", []string{""}, response.RuleStatusPass, annotation)
2020-12-23 15:10:07 -08:00
annPatchesNew := generateAnnotationPatches([]*response.EngineResponse{engineResponseNew}, log.Log)
2019-10-07 18:31:14 -07:00
assert.Assert(t, annPatchesNew == nil)
}
func Test_annotation_failed_Patch(t *testing.T) {
annotation := map[string]interface{}{
2019-11-11 19:08:46 -08:00
"policies.kyverno.patches": "old-annotation",
2019-10-07 18:31:14 -07:00
}
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", nil, response.RuleStatusFail, annotation)
2020-12-23 15:10:07 -08:00
annPatches := generateAnnotationPatches([]*response.EngineResponse{engineResponse}, log.Log)
2019-10-07 18:31:14 -07:00
assert.Assert(t, annPatches == nil)
}
func Test_exist_patches(t *testing.T) {
annotation := map[string]interface{}{
"policies.kyverno.io/patches": "present",
}
patchStr := `{ "op": "replace", "path": "/spec/containers/0/imagePullPolicy", "value": "IfNotPresent" }`
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", []string{patchStr}, response.RuleStatusPass, annotation)
annPatches := generateAnnotationPatches([]*response.EngineResponse{engineResponse}, log.Log)
expectedPatches1 := `{"op":"remove","path":"/metadata/annotations/policies.kyverno.io~1patches","value":null}`
expectedPatches2 := `{"op":"add","path":"/metadata/annotations/policies.kyverno.io~1last-applied-patches","value":"default-imagepullpolicy.mutate-container.kyverno.io: replaced /spec/containers/0/imagePullPolicy\n"}`
assert.Assert(t, string(annPatches[0]) == expectedPatches1)
assert.Assert(t, string(annPatches[1]) == expectedPatches2)
}