mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-06 16:06:56 +00:00
232 lines
4.7 KiB
Go
232 lines
4.7 KiB
Go
|
package mutate
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/nirmata/kyverno/pkg/engine/utils"
|
||
|
assertnew "github.com/stretchr/testify/assert"
|
||
|
"gotest.tools/assert"
|
||
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||
|
)
|
||
|
|
||
|
func Test_GeneratePatches(t *testing.T) {
|
||
|
|
||
|
out, err := strategicMergePatch(string(baseBytes), string(overlayBytes))
|
||
|
assert.NilError(t, err)
|
||
|
|
||
|
patches, err := generatePatches(baseBytes, out)
|
||
|
assert.NilError(t, err)
|
||
|
t.Logf("patches\n%v", patches)
|
||
|
|
||
|
var overlay unstructured.Unstructured
|
||
|
err = json.Unmarshal(baseBytes, &overlay)
|
||
|
assert.NilError(t, err)
|
||
|
|
||
|
bb, err := json.Marshal(overlay.Object)
|
||
|
assert.NilError(t, err)
|
||
|
|
||
|
res, err := utils.ApplyPatches(bb, patches)
|
||
|
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(res)) {
|
||
|
t.FailNow()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var baseBytes = []byte(`
|
||
|
{
|
||
|
"apiVersion": "apps/v1",
|
||
|
"kind": "Deployment",
|
||
|
"metadata": {
|
||
|
"name": "wordpress",
|
||
|
"labels": {
|
||
|
"app": "wordpress"
|
||
|
}
|
||
|
},
|
||
|
"spec": {
|
||
|
"selector": {
|
||
|
"matchLabels": {
|
||
|
"app": "wordpress"
|
||
|
}
|
||
|
},
|
||
|
"strategy": {
|
||
|
"type": "Recreate"
|
||
|
},
|
||
|
"template": {
|
||
|
"metadata": {
|
||
|
"labels": {
|
||
|
"app": "wordpress"
|
||
|
}
|
||
|
},
|
||
|
"spec": {
|
||
|
"containers": [
|
||
|
{
|
||
|
"image": "wordpress:4.8-apache",
|
||
|
"name": "wordpress",
|
||
|
"ports": [
|
||
|
{
|
||
|
"containerPort": 80,
|
||
|
"name": "wordpress"
|
||
|
}
|
||
|
],
|
||
|
"volumeMounts": [
|
||
|
{
|
||
|
"name": "wordpress-persistent-storage",
|
||
|
"mountPath": "/var/www/html"
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"volumes": [
|
||
|
{
|
||
|
"name": "wordpress-persistent-storage"
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
`)
|
||
|
|
||
|
var overlayBytes = []byte(`
|
||
|
{
|
||
|
"apiVersion": "apps/v1",
|
||
|
"kind": "Deployment",
|
||
|
"metadata": {
|
||
|
"name": "wordpress"
|
||
|
},
|
||
|
"spec": {
|
||
|
"template": {
|
||
|
"spec": {
|
||
|
"initContainers": [
|
||
|
{
|
||
|
"name": "init-command",
|
||
|
"image": "debian",
|
||
|
"command": [
|
||
|
"echo $(WORDPRESS_SERVICE)",
|
||
|
"echo $(MYSQL_SERVICE)"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"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"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
`)
|
||
|
|
||
|
var expectBytes = []byte(`
|
||
|
{
|
||
|
"apiVersion": "apps/v1",
|
||
|
"kind": "Deployment",
|
||
|
"metadata": {
|
||
|
"name": "wordpress",
|
||
|
"labels": {
|
||
|
"app": "wordpress"
|
||
|
}
|
||
|
},
|
||
|
"spec": {
|
||
|
"selector": {
|
||
|
"matchLabels": {
|
||
|
"app": "wordpress"
|
||
|
}
|
||
|
},
|
||
|
"strategy": {
|
||
|
"type": "Recreate"
|
||
|
},
|
||
|
"template": {
|
||
|
"metadata": {
|
||
|
"labels": {
|
||
|
"app": "wordpress"
|
||
|
}
|
||
|
},
|
||
|
"spec": {
|
||
|
"containers": [
|
||
|
{
|
||
|
"image": "wordpress:4.8-apache",
|
||
|
"name": "wordpress",
|
||
|
"ports": [
|
||
|
{
|
||
|
"containerPort": 80,
|
||
|
"name": "wordpress"
|
||
|
}
|
||
|
],
|
||
|
"volumeMounts": [
|
||
|
{
|
||
|
"name": "wordpress-persistent-storage",
|
||
|
"mountPath": "/var/www/html"
|
||
|
}
|
||
|
],
|
||
|
"env": [
|
||
|
{
|
||
|
"name": "WORDPRESS_DB_HOST",
|
||
|
"value": "$(MYSQL_SERVICE)"
|
||
|
},
|
||
|
{
|
||
|
"name": "WORDPRESS_DB_PASSWORD",
|
||
|
"valueFrom": {
|
||
|
"secretKeyRef": {
|
||
|
"name": "mysql-pass",
|
||
|
"key": "password"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"name": "nginx",
|
||
|
"image": "nginx"
|
||
|
}
|
||
|
],
|
||
|
"volumes": [
|
||
|
{
|
||
|
"name": "wordpress-persistent-storage"
|
||
|
}
|
||
|
],
|
||
|
"initContainers": [
|
||
|
{
|
||
|
"name": "init-command",
|
||
|
"image": "debian",
|
||
|
"command": [
|
||
|
"echo $(WORDPRESS_SERVICE)",
|
||
|
"echo $(MYSQL_SERVICE)"
|
||
|
]
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
`)
|