2022-01-04 17:36:33 -08:00
|
|
|
package patch
|
2020-08-05 09:11:23 -07:00
|
|
|
|
|
|
|
import (
|
2021-01-19 11:08:06 -08:00
|
|
|
"fmt"
|
2020-08-05 09:11:23 -07:00
|
|
|
"testing"
|
|
|
|
|
2023-02-09 16:15:51 +01:00
|
|
|
"github.com/go-logr/logr"
|
2020-08-05 09:11:23 -07:00
|
|
|
assertnew "github.com/stretchr/testify/assert"
|
|
|
|
"gotest.tools/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_GeneratePatches(t *testing.T) {
|
2023-02-09 16:15:51 +01:00
|
|
|
out, err := strategicMergePatch(logr.Discard(), string(baseBytes), string(overlayBytes))
|
2020-08-05 09:11:23 -07:00
|
|
|
assert.NilError(t, err)
|
2021-02-25 18:02:52 -08:00
|
|
|
expectedPatches := map[string]bool{
|
|
|
|
`{"op":"add","path":"/spec/template/spec/initContainers","value":[{"command":["echo $(WORDPRESS_SERVICE)","echo $(MYSQL_SERVICE)"],"image":"debian","name":"init-command"}]}`: true,
|
2023-06-03 14:08:58 +02:00
|
|
|
`{"op":"add","path":"/spec/template/spec/containers/1","value":{"env":[{"name":"WORDPRESS_DB_HOST","value":"$(MYSQL_SERVICE)"},{"name":"WORDPRESS_DB_PASSWORD","valueFrom":{"secretKeyRef":{"key":"password","name":"mysql-pass"}}}],"image":"wordpress:4.8-apache","name":"wordpress","ports":[{"containerPort":80,"name":"wordpress"}],"volumeMounts":[{"mountPath":"/var/www/html","name":"wordpress-persistent-storage"}]}}`: true,
|
|
|
|
`{"op":"replace","path":"/spec/template/spec/containers/0/image","value":"nginx"}`: true,
|
|
|
|
`{"op":"replace","path":"/spec/template/spec/containers/0/name","value":"nginx"}`: true,
|
|
|
|
`{"op":"remove","path":"/spec/template/spec/containers/0/ports"}`: true,
|
|
|
|
`{"op":"remove","path":"/spec/template/spec/containers/0/volumeMounts"}`: true,
|
2021-02-25 15:21:55 -08:00
|
|
|
}
|
2020-08-05 09:11:23 -07:00
|
|
|
patches, err := generatePatches(baseBytes, out)
|
|
|
|
assert.NilError(t, err)
|
2021-02-25 18:02:52 -08:00
|
|
|
for _, p := range patches {
|
2023-04-28 09:31:12 +02:00
|
|
|
assertnew.Equal(t, expectedPatches[p.Json()], true)
|
2020-08-05 09:11:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
2021-01-08 16:45:39 -08:00
|
|
|
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": [{"name": "nginx","image": "nginx"},{"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"}}}]}],"volumes": [{"name": "wordpress-persistent-storage"}],"initContainers": [{"name": "init-command","image": "debian","command": ["echo $(WORDPRESS_SERVICE)","echo $(MYSQL_SERVICE)"]}]}}}}`)
|
2020-09-03 08:54:37 -07:00
|
|
|
|
2021-01-19 11:08:06 -08:00
|
|
|
func Test_ignorePath(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
ignore bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
path: "/",
|
|
|
|
ignore: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/metadata",
|
|
|
|
ignore: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/metadata/name",
|
|
|
|
ignore: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "spec/template/metadata/name",
|
|
|
|
ignore: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/metadata/namespace",
|
|
|
|
ignore: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/metadata/annotations",
|
|
|
|
ignore: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/metadata/labels",
|
|
|
|
ignore: false,
|
|
|
|
},
|
2022-10-25 02:34:07 -04:00
|
|
|
{
|
|
|
|
path: "/metadata/ownerReferences",
|
|
|
|
ignore: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/metadata/finalizers",
|
|
|
|
ignore: false,
|
|
|
|
},
|
2022-11-07 19:50:50 +05:30
|
|
|
{
|
|
|
|
path: "/metadata/generateName",
|
|
|
|
ignore: false,
|
|
|
|
},
|
2021-01-19 11:08:06 -08:00
|
|
|
{
|
|
|
|
path: "/metadata/creationTimestamp",
|
|
|
|
ignore: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "spec/template/metadata/creationTimestamp",
|
|
|
|
ignore: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/metadata/resourceVersion",
|
|
|
|
ignore: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/status",
|
2022-12-09 22:15:23 +05:30
|
|
|
ignore: false,
|
2021-01-19 11:08:06 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/spec",
|
|
|
|
ignore: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/kind",
|
|
|
|
ignore: false,
|
|
|
|
},
|
2022-11-28 10:55:42 +01:00
|
|
|
{
|
|
|
|
path: "/spec/triggers/0/metadata/serverAddress",
|
|
|
|
ignore: false,
|
|
|
|
},
|
2021-01-19 11:08:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
res := ignorePatch(test.path)
|
|
|
|
assertnew.Equal(t, test.ignore, res, fmt.Sprintf("test fails at %s", test.path))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_GeneratePatches_sortRemovalPatches(t *testing.T) {
|
|
|
|
base := []byte(`{"apiVersion": "apps/v1","kind": "Deployment","metadata": {"name": "nginx-deployment","labels": {"app": "nginx"}},"spec": {"selector": {"matchLabels": {"app": "nginx"}},"replicas": 1,"template": {"metadata": {"labels": {"app": "nginx"}},"spec": {"containers": [{"name": "nginx","image": "nginx:1.14.2","ports": [{"containerPort": 80}]}],"tolerations": [{"effect": "NoExecute","key": "node.kubernetes.io/not-ready","operator": "Exists","tolerationSeconds": 300},{"effect": "NoExecute","key": "node.kubernetes.io/unreachable","operator": "Exists","tolerationSeconds": 300}]}}}}`)
|
|
|
|
patchedResource := []byte(`{"apiVersion": "apps/v1","kind": "Deployment","metadata": {"name": "nginx-deployment","labels": {"app": "nginx"}},"spec": {"selector": {"matchLabels": {"app": "nginx"}},"replicas": 1,"template": {"metadata": {"labels": {"app": "nginx"}},"spec": {"containers": [{"name": "nginx","image": "nginx:1.14.2","ports": [{"containerPort": 80}]}],"tolerations": [{"effect": "NoSchedule","key": "networkzone","operator": "Equal","value": "dmz"}]}}}}`)
|
2023-06-03 14:08:58 +02:00
|
|
|
expectedPatches := [][]byte{
|
|
|
|
[]byte(`{"op":"remove","path":"/spec/template/spec/tolerations/1"}`),
|
|
|
|
[]byte(`{"op":"replace","path":"/spec/template/spec/tolerations/0/effect","value":"NoSchedule"}`),
|
|
|
|
[]byte(`{"op":"replace","path":"/spec/template/spec/tolerations/0/key","value":"networkzone"}`),
|
|
|
|
[]byte(`{"op":"replace","path":"/spec/template/spec/tolerations/0/operator","value":"Equal"}`),
|
|
|
|
[]byte(`{"op":"add","path":"/spec/template/spec/tolerations/0/value","value":"dmz"}`),
|
|
|
|
[]byte(`{"op":"remove","path":"/spec/template/spec/tolerations/0/tolerationSeconds"}`),
|
|
|
|
}
|
2021-01-19 11:08:06 -08:00
|
|
|
patches, err := generatePatches(base, patchedResource)
|
|
|
|
assertnew.Nil(t, err)
|
2023-06-03 14:08:58 +02:00
|
|
|
for _, patch := range patches {
|
|
|
|
fmt.Println(patch.Json())
|
2021-01-19 11:08:06 -08:00
|
|
|
}
|
2023-06-03 14:08:58 +02:00
|
|
|
assertnew.Equal(t, expectedPatches, ConvertPatches(patches...))
|
2021-01-19 11:08:06 -08:00
|
|
|
}
|