2022-01-04 17:36:33 -08:00
|
|
|
package patch
|
2020-08-05 09:11:23 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2023-02-09 16:15:51 +01:00
|
|
|
"github.com/go-logr/logr"
|
2021-10-29 18:13:20 +02:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2022-03-28 16:01:27 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/autogen"
|
2020-08-05 09:11:23 -07:00
|
|
|
assertnew "github.com/stretchr/testify/assert"
|
|
|
|
"gotest.tools/assert"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMergePatch(t *testing.T) {
|
2021-01-08 16:45:39 -08:00
|
|
|
testCases := []struct {
|
|
|
|
rawPolicy []byte
|
|
|
|
rawResource []byte
|
|
|
|
expected []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
rawPolicy: overlayBytes,
|
|
|
|
rawResource: baseBytes,
|
|
|
|
expected: expectBytes,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// condition matches the first element of the array
|
2021-09-13 18:59:28 +03:00
|
|
|
rawPolicy: []byte(`{
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"(image)": "gcr.io/google-containers/busybox:*"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"imagePullSecrets": [
|
|
|
|
{
|
|
|
|
"name": "regcred"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`),
|
|
|
|
rawResource: []byte(`{
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"kind": "Pod",
|
|
|
|
"metadata": {
|
|
|
|
"name": "hello"
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"name": "hello",
|
|
|
|
"image": "gcr.io/google-containers/busybox:latest"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "hello2",
|
|
|
|
"image": "gcr.io/google-containers/busybox:latest"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "hello3",
|
|
|
|
"image": "gcr.io/google-containers/nginx:latest"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`),
|
|
|
|
expected: []byte(`{
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"kind": "Pod",
|
|
|
|
"metadata": {
|
|
|
|
"name": "hello"
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"image": "gcr.io/google-containers/busybox:latest",
|
|
|
|
"name": "hello"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"image": "gcr.io/google-containers/busybox:latest",
|
|
|
|
"name": "hello2"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"image": "gcr.io/google-containers/nginx:latest",
|
|
|
|
"name": "hello3"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"imagePullSecrets": [
|
|
|
|
{
|
|
|
|
"name": "regcred"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`),
|
2021-01-08 16:45:39 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// condition matches the third element of the array
|
2021-09-13 18:59:28 +03:00
|
|
|
rawPolicy: []byte(`{
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"(image)": "gcr.io/google-containers/nginx:*"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"imagePullSecrets": [
|
|
|
|
{
|
|
|
|
"name": "regcred"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`),
|
|
|
|
rawResource: []byte(`{
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"kind": "Pod",
|
|
|
|
"metadata": {
|
|
|
|
"name": "hello"
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"name": "hello",
|
|
|
|
"image": "gcr.io/google-containers/busybox:latest"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "hello2",
|
|
|
|
"image": "gcr.io/google-containers/busybox:latest"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "hello3",
|
|
|
|
"image": "gcr.io/google-containers/nginx:latest"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`),
|
|
|
|
expected: []byte(`{
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"kind": "Pod",
|
|
|
|
"metadata": {
|
|
|
|
"name": "hello"
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"image": "gcr.io/google-containers/busybox:latest",
|
|
|
|
"name": "hello"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"image": "gcr.io/google-containers/busybox:latest",
|
|
|
|
"name": "hello2"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"image": "gcr.io/google-containers/nginx:latest",
|
|
|
|
"name": "hello3"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"imagePullSecrets": [
|
|
|
|
{
|
|
|
|
"name": "regcred"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`),
|
2021-01-08 16:45:39 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range testCases {
|
2021-09-13 18:59:28 +03:00
|
|
|
t.Logf("Running test %d...", i+1)
|
2023-02-09 16:15:51 +01:00
|
|
|
out, err := strategicMergePatch(logr.Discard(), string(test.rawResource), string(test.rawPolicy))
|
2021-01-08 16:45:39 -08:00
|
|
|
assert.NilError(t, err)
|
2022-01-23 05:54:22 -08:00
|
|
|
assert.DeepEqual(t, toJSON(t, test.expected), toJSON(t, out))
|
2020-08-05 09:11:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_PolicyDeserilize(t *testing.T) {
|
|
|
|
rawPolicy := []byte(`
|
|
|
|
{
|
|
|
|
"apiVersion": "kyverno.io/v1",
|
|
|
|
"kind": "ClusterPolicy",
|
|
|
|
"metadata": {
|
|
|
|
"name": "set-image-pull-policy"
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"validationFailureAction": "enforce",
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"name": "set-image-pull-policy",
|
|
|
|
"match": {
|
|
|
|
"resources": {
|
|
|
|
"kinds": [
|
|
|
|
"Pod"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"mutate": {
|
|
|
|
"patchStrategicMerge": {
|
|
|
|
"spec": {
|
|
|
|
"template": {
|
|
|
|
"spec": {
|
|
|
|
"containers": [
|
|
|
|
{
|
|
|
|
"name": "nginx",
|
|
|
|
"image": "nginx"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "wordpress",
|
|
|
|
"env": [
|
|
|
|
{
|
|
|
|
"name": "WORDPRESS_DB_HOST",
|
|
|
|
"value": "$(MYSQL_SERVICE)"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "WORDPRESS_DB_PASSWORD",
|
|
|
|
"valueFrom": {
|
|
|
|
"secretKeyRef": {
|
|
|
|
"name": "mysql-pass",
|
|
|
|
"key": "password"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"initContainers": [
|
|
|
|
{
|
|
|
|
"name": "init-command",
|
|
|
|
"image": "debian",
|
|
|
|
"command": [
|
|
|
|
"echo $(WORDPRESS_SERVICE)",
|
|
|
|
"echo $(MYSQL_SERVICE)"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
var policy kyvernov1.ClusterPolicy
|
|
|
|
err := json.Unmarshal(rawPolicy, &policy)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2022-03-28 16:01:27 +02:00
|
|
|
overlayPatches := autogen.ComputeRules(&policy)[0].Mutation.GetPatchStrategicMerge()
|
2020-08-05 09:11:23 -07:00
|
|
|
patchString, err := json.Marshal(overlayPatches)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2023-02-09 16:15:51 +01:00
|
|
|
out, err := strategicMergePatch(logr.Discard(), string(baseBytes), string(patchString))
|
2020-08-05 09:11:23 -07:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
var ep unstructured.Unstructured
|
|
|
|
err = json.Unmarshal(expectBytes, &ep)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
eb, err := json.Marshal(ep.Object)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
if !assertnew.Equal(t, string(eb), string(out)) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|