From e13f8803eba57f3bb3d8b108ffffd264d50cdb5d Mon Sep 17 00:00:00 2001 From: Mikhail Nikitin Date: Fri, 15 Jan 2021 00:08:49 +0300 Subject: [PATCH 1/2] Add test case of deleting not existing elements --- api/krusty/patchdelete_test.go | 96 ++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 api/krusty/patchdelete_test.go diff --git a/api/krusty/patchdelete_test.go b/api/krusty/patchdelete_test.go new file mode 100644 index 000000000..65a6954cd --- /dev/null +++ b/api/krusty/patchdelete_test.go @@ -0,0 +1,96 @@ +package krusty_test + +import ( + "testing" + + kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" +) + +func TestPatchDeleteOfNotExistingAttributesShouldNotAddExtraElements(t *testing.T) { + th := kusttest_test.MakeEnhancedHarness(t) + defer th.Reset() + + th.WriteF("/app/resource.yaml", ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: whatever +spec: + template: + spec: + containers: + - env: + - name: EXISTING + value: EXISTING_VALUE + - name: FOR_REMOVAL + value: FOR_REMOVAL_VALUE + name: whatever + image: helloworld +`) + th.WriteF("/app/patch.yml", ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: whatever +spec: + template: + spec: + containers: + - name: whatever + env: + - name: FOR_REMOVAL + $patch: delete + - name: NOT_EXISTING_FOR_REMOVAL + $patch: delete +`) + th.WriteK("/app", ` +resources: +- resource.yaml +patches: +- path: patch.yml + target: + kind: Deployment +`) + + /* + // It's expected that removal of not existing elements should not introduce extra values, + // as a patch can be applied to multipe resources, not all of them can have all deleted elements. + expected := ` + apiVersion: apps/v1 + kind: Deployment + metadata: + name: whatever + spec: + template: + spec: + containers: + - env: + - name: EXISTING + value: EXISTING_VALUE + image: helloworld + name: whatever + ` + */ + + // Currently kustomize inserts $patch: delete elements into the resulting resources + erroneousActual := ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: whatever +spec: + template: + spec: + containers: + - env: + - $patch: delete + name: NOT_EXISTING_FOR_REMOVAL + - name: EXISTING + value: EXISTING_VALUE + image: helloworld + name: whatever +` + + m := th.Run("/app", th.MakeDefaultOptions()) + th.AssertActualEqualsExpected(m, erroneousActual) +} From f6ddea435c5ecac3dca6d0d8932b8ee0d1d64194 Mon Sep 17 00:00:00 2001 From: Mikhail Nikitin Date: Fri, 15 Jan 2021 22:50:23 +0300 Subject: [PATCH 2/2] Make test file paths consistent and relative Signed-off-by: Mikhail Nikitin --- api/krusty/patchdelete_test.go | 50 +++++++++++++++++----------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/api/krusty/patchdelete_test.go b/api/krusty/patchdelete_test.go index 65a6954cd..e0245c125 100644 --- a/api/krusty/patchdelete_test.go +++ b/api/krusty/patchdelete_test.go @@ -10,7 +10,7 @@ func TestPatchDeleteOfNotExistingAttributesShouldNotAddExtraElements(t *testing. th := kusttest_test.MakeEnhancedHarness(t) defer th.Reset() - th.WriteF("/app/resource.yaml", ` + th.WriteF("resource.yaml", ` apiVersion: apps/v1 kind: Deployment metadata: @@ -27,7 +27,7 @@ spec: name: whatever image: helloworld `) - th.WriteF("/app/patch.yml", ` + th.WriteF("patch.yaml", ` apiVersion: apps/v1 kind: Deployment metadata: @@ -43,36 +43,36 @@ spec: - name: NOT_EXISTING_FOR_REMOVAL $patch: delete `) - th.WriteK("/app", ` + th.WriteK(".", ` resources: - resource.yaml patches: -- path: patch.yml +- path: patch.yaml target: kind: Deployment `) - /* - // It's expected that removal of not existing elements should not introduce extra values, - // as a patch can be applied to multipe resources, not all of them can have all deleted elements. - expected := ` - apiVersion: apps/v1 - kind: Deployment - metadata: - name: whatever - spec: - template: - spec: - containers: - - env: - - name: EXISTING - value: EXISTING_VALUE - image: helloworld - name: whatever - ` - */ + // It's expected that removal of not existing elements should not introduce extra values, + // as a patch can be applied to multiple resources, not all of them can have all the elements being deleted. + expected := ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: whatever +spec: + template: + spec: + containers: + - env: + - name: EXISTING + value: EXISTING_VALUE + image: helloworld + name: whatever +` + // Allow expected variable to be unused + _ = expected - // Currently kustomize inserts $patch: delete elements into the resulting resources + // Currently, kustomize inserts $patch: delete elements into the resulting resources erroneousActual := ` apiVersion: apps/v1 kind: Deployment @@ -91,6 +91,6 @@ spec: name: whatever ` - m := th.Run("/app", th.MakeDefaultOptions()) + m := th.Run(".", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, erroneousActual) }