2019-10-07 18:31:14 -07:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-10-07 11:12:31 -07:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2019-12-30 17:08:50 -08:00
|
|
|
func newPolicyResponse(policy, rule string, patchesStr []string, success bool) response.PolicyResponse {
|
2019-10-07 18:31:14 -07:00
|
|
|
var patches [][]byte
|
|
|
|
for _, p := range patchesStr {
|
|
|
|
patches = append(patches, []byte(p))
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:08:50 -08:00
|
|
|
return response.PolicyResponse{
|
2019-10-07 18:31:14 -07:00
|
|
|
Policy: policy,
|
2019-12-30 17:08:50 -08:00
|
|
|
Rules: []response.RuleResponse{
|
2020-02-03 13:38:24 -08:00
|
|
|
{
|
2019-10-07 18:31:14 -07:00
|
|
|
Name: rule,
|
|
|
|
Patches: patches,
|
|
|
|
Success: success},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:08:50 -08:00
|
|
|
func newEngineResponse(policy, rule string, patchesStr []string, success bool, annotation map[string]string) response.EngineResponse {
|
|
|
|
return response.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
|
|
|
|
2020-03-17 11:05:20 -07:00
|
|
|
annPatches := generateAnnotationPatches([]response.EngineResponse{engineResponse}, log.Log)
|
2020-02-06 08:05:27 +05:30
|
|
|
expectedPatches := `{"op":"add","path":"/metadata/annotations","value":{"policies.kyverno.io/patches":"default-imagepullpolicy.mutate-container.kyverno.io: replaced /spec/containers/0/imagePullPolicy\n"}}`
|
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)
|
2020-03-17 11:05:20 -07:00
|
|
|
annPatches := generateAnnotationPatches([]response.EngineResponse{engineResponse}, log.Log)
|
2019-10-07 18:31:14 -07:00
|
|
|
|
2020-02-06 08:05:27 +05:30
|
|
|
expectedPatches := `{"op":"add","path":"/metadata/annotations","value":{"policies.kyverno.io/patches":"default-imagepullpolicy.mutate-container.kyverno.io: replaced /spec/containers/0/imagePullPolicy\n"}}`
|
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)
|
2020-03-17 11:05:20 -07:00
|
|
|
annPatches := generateAnnotationPatches([]response.EngineResponse{engineResponse}, log.Log)
|
2019-10-07 18:31:14 -07:00
|
|
|
|
2020-02-06 08:05:27 +05:30
|
|
|
expectedPatches := `{"op":"add","path":"/metadata/annotations","value":{"policies.kyverno.io/patches":"default-imagepullpolicy.mutate-container.kyverno.io: replaced /spec/containers/0/imagePullPolicy\n"}}`
|
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)
|
2020-03-17 11:05:20 -07:00
|
|
|
annPatches := generateAnnotationPatches([]response.EngineResponse{engineResponse}, log.Log)
|
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)
|
2020-03-17 11:05:20 -07: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]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)
|
2020-03-17 11:05:20 -07:00
|
|
|
annPatches := generateAnnotationPatches([]response.EngineResponse{engineResponse}, log.Log)
|
2019-10-07 18:31:14 -07:00
|
|
|
|
|
|
|
assert.Assert(t, annPatches == nil)
|
|
|
|
}
|