Skip to content

Commit

Permalink
fix: unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ved Ratan <[email protected]>
  • Loading branch information
VedRatan committed Jul 23, 2024
1 parent 5b7effc commit b21ecd0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
37 changes: 13 additions & 24 deletions pkg/engine/mutate/patch/patchesUtils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"testing"

"github.com/go-logr/logr"
assertnew "github.com/stretchr/testify/assert"
"gotest.tools/assert"
engineutils "github.com/kyverno/kyverno/pkg/engine/utils"
"github.com/stretchr/testify/assert"
)

func Test_GeneratePatches(t *testing.T) {
out, err := strategicMergePatch(logr.Discard(), string(baseBytes), string(overlayBytes))
assert.NilError(t, err)
assert.NoError(t, err)
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,
`{"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,
Expand All @@ -21,9 +21,9 @@ func Test_GeneratePatches(t *testing.T) {
`{"op":"remove","path":"/spec/template/spec/containers/0/volumeMounts"}`: true,
}
patches, err := generatePatches(baseBytes, out)
assert.NilError(t, err)
assert.NoError(t, err)
for _, p := range patches {
assertnew.Equal(t, expectedPatches[p.Json()], true)
assert.Equal(t, expectedPatches[p.Json()], true)
}
}

Expand Down Expand Up @@ -132,8 +132,6 @@ var overlayBytes = []byte(`
}
`)

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)"]}]}}}}`)

func Test_ignorePath(t *testing.T) {
tests := []struct {
path string
Expand Down Expand Up @@ -211,25 +209,16 @@ func Test_ignorePath(t *testing.T) {

for _, test := range tests {
res := ignorePatch(test.path)
assertnew.Equal(t, test.ignore, res, fmt.Sprintf("test fails at %s", test.path))
assert.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"}]}}}}`)
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"}`),
}
patches, err := generatePatches(base, patchedResource)
assertnew.Nil(t, err)
for _, patch := range patches {
fmt.Println(patch.Json())
}
assertnew.Equal(t, expectedPatches, ConvertPatches(patches...))
}
expectedResource := []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"}]}}}}`)
generatedPatches, err := generatePatches(base, expectedResource)
assert.NoError(t, err)
patchedResource, err := engineutils.ApplyPatches(base, ConvertPatches(generatedPatches...))
assert.NoError(t, err)
assert.JSONEq(t, string(expectedResource), string(patchedResource))
}
4 changes: 3 additions & 1 deletion pkg/engine/mutate/patch/strategicMergePatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

func TestMergePatch(t *testing.T) {
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)"]}]}}}}`)
testCases := []struct {
rawPolicy []byte
rawResource []byte
Expand Down Expand Up @@ -170,6 +171,7 @@ func TestMergePatch(t *testing.T) {
}

func Test_PolicyDeserilize(t *testing.T) {
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)"]}]}}}}`)
rawPolicy := []byte(`
{
"apiVersion": "kyverno.io/v1",
Expand Down Expand Up @@ -260,4 +262,4 @@ func Test_PolicyDeserilize(t *testing.T) {
if !assertnew.Equal(t, string(eb), string(out)) {
t.FailNow()
}
}
}

0 comments on commit b21ecd0

Please sign in to comment.