2022-04-25 20:20:40 +08:00
|
|
|
package utils
|
2019-10-07 18:31:14 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
2022-10-02 20:45:03 +01:00
|
|
|
"github.com/kyverno/kyverno/pkg/logging"
|
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"
|
2019-10-07 18:31:14 -07:00
|
|
|
)
|
|
|
|
|
2021-09-26 02:12:31 -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))
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:08:50 -08:00
|
|
|
return response.PolicyResponse{
|
2021-06-30 00:43:11 +03:00
|
|
|
Policy: response.PolicySpec{Name: 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,
|
2021-09-26 02:20:29 -07:00
|
|
|
Status: status,
|
2021-09-26 02:12:31 -07:00
|
|
|
},
|
2019-10-07 18:31:14 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 02:20:29 -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{}{
|
2021-09-20 21:56:19 +05:30
|
|
|
"annotations": annotation,
|
2019-11-11 19:08:46 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-09-26 02:12:31 -07: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" }`
|
2021-09-26 02:12:31 -07:00
|
|
|
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", []string{patchStr}, response.RuleStatusPass, nil)
|
2022-10-02 20:45:03 +01:00
|
|
|
annPatches := GenerateAnnotationPatches([]*response.EngineResponse{engineResponse}, logging.GlobalLogger())
|
2022-04-01 07:26:47 +02:00
|
|
|
expectedPatches := `{"path":"/metadata/annotations","op":"add","value":{"policies.kyverno.io/last-applied-patches":"default-imagepullpolicy.mutate-container.kyverno.io: replaced /spec/containers/0/imagePullPolicy\n"}}`
|
|
|
|
assert.Equal(t, string(annPatches[0]), expectedPatches)
|
2019-10-07 18:31:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_exist_annotation(t *testing.T) {
|
2021-09-20 21:56:19 +05:30
|
|
|
annotation := map[string]interface{}{
|
2019-10-07 18:31:14 -07:00
|
|
|
"test": "annotation",
|
|
|
|
}
|
|
|
|
patchStr := `{ "op": "replace", "path": "/spec/containers/0/imagePullPolicy", "value": "IfNotPresent" }`
|
2021-09-26 02:12:31 -07:00
|
|
|
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", []string{patchStr}, response.RuleStatusPass, annotation)
|
2022-10-02 20:45:03 +01:00
|
|
|
annPatches := GenerateAnnotationPatches([]*response.EngineResponse{engineResponse}, logging.GlobalLogger())
|
2022-04-01 07:26:47 +02:00
|
|
|
expectedPatches := `{"path":"/metadata/annotations/policies.kyverno.io~1last-applied-patches","op":"add","value":"default-imagepullpolicy.mutate-container.kyverno.io: replaced /spec/containers/0/imagePullPolicy\n"}`
|
|
|
|
assert.Equal(t, string(annPatches[0]), expectedPatches)
|
2019-10-07 18:31:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_exist_kyverno_annotation(t *testing.T) {
|
2021-09-20 21:56:19 +05:30
|
|
|
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" }`
|
2021-09-26 02:12:31 -07:00
|
|
|
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", []string{patchStr}, response.RuleStatusPass, annotation)
|
2022-10-02 20:45:03 +01:00
|
|
|
annPatches := GenerateAnnotationPatches([]*response.EngineResponse{engineResponse}, logging.GlobalLogger())
|
2022-04-01 07:26:47 +02:00
|
|
|
expectedPatches := `{"path":"/metadata/annotations/policies.kyverno.io~1last-applied-patches","op":"add","value":"default-imagepullpolicy.mutate-container.kyverno.io: replaced /spec/containers/0/imagePullPolicy\n"}`
|
|
|
|
assert.Equal(t, string(annPatches[0]), expectedPatches)
|
2019-10-07 18:31:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_annotation_nil_patch(t *testing.T) {
|
2021-09-20 21:56:19 +05:30
|
|
|
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
|
|
|
}
|
2021-09-26 02:12:31 -07:00
|
|
|
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", nil, response.RuleStatusPass, annotation)
|
2022-10-02 20:45:03 +01:00
|
|
|
annPatches := GenerateAnnotationPatches([]*response.EngineResponse{engineResponse}, logging.GlobalLogger())
|
2019-10-07 18:31:14 -07:00
|
|
|
assert.Assert(t, annPatches == nil)
|
2021-09-26 02:12:31 -07:00
|
|
|
engineResponseNew := newEngineResponse("mutate-container", "default-imagepullpolicy", []string{""}, response.RuleStatusPass, annotation)
|
2022-10-02 20:45:03 +01:00
|
|
|
annPatchesNew := GenerateAnnotationPatches([]*response.EngineResponse{engineResponseNew}, logging.GlobalLogger())
|
2019-10-07 18:31:14 -07:00
|
|
|
assert.Assert(t, annPatchesNew == nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_annotation_failed_Patch(t *testing.T) {
|
2021-09-20 21:56:19 +05:30
|
|
|
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
|
|
|
}
|
2021-09-26 02:12:31 -07:00
|
|
|
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", nil, response.RuleStatusFail, annotation)
|
2022-10-02 20:45:03 +01:00
|
|
|
annPatches := GenerateAnnotationPatches([]*response.EngineResponse{engineResponse}, logging.GlobalLogger())
|
2019-10-07 18:31:14 -07:00
|
|
|
assert.Assert(t, annPatches == nil)
|
|
|
|
}
|
2021-09-20 21:56:19 +05:30
|
|
|
|
|
|
|
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" }`
|
2021-09-26 02:20:29 -07:00
|
|
|
engineResponse := newEngineResponse("mutate-container", "default-imagepullpolicy", []string{patchStr}, response.RuleStatusPass, annotation)
|
2022-10-02 20:45:03 +01:00
|
|
|
annPatches := GenerateAnnotationPatches([]*response.EngineResponse{engineResponse}, logging.GlobalLogger())
|
2022-04-01 07:26:47 +02:00
|
|
|
expectedPatches1 := `{"path":"/metadata/annotations/policies.kyverno.io~1patches","op":"remove"}`
|
|
|
|
expectedPatches2 := `{"path":"/metadata/annotations/policies.kyverno.io~1last-applied-patches","op":"add","value":"default-imagepullpolicy.mutate-container.kyverno.io: replaced /spec/containers/0/imagePullPolicy\n"}`
|
|
|
|
assert.Equal(t, string(annPatches[0]), expectedPatches1)
|
|
|
|
assert.Equal(t, string(annPatches[1]), expectedPatches2)
|
2021-09-20 21:56:19 +05:30
|
|
|
}
|