From f889ca8885c56e36794fae64bcb7f22a86476c2d Mon Sep 17 00:00:00 2001 From: Eyob Tefera Date: Fri, 11 Sep 2020 18:48:48 +0000 Subject: [PATCH 1/3] Add set image test with patchesJson6902. --- .../commands/edit/set/setimage_test.go | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/kustomize/internal/commands/edit/set/setimage_test.go b/kustomize/internal/commands/edit/set/setimage_test.go index 8187a0aee1..c1f298d812 100644 --- a/kustomize/internal/commands/edit/set/setimage_test.go +++ b/kustomize/internal/commands/edit/set/setimage_test.go @@ -145,6 +145,47 @@ func TestSetImage(t *testing.T) { " newTag: my-tag2", }}, }, + { + description: "override file with patch", + given: given{ + args: []string{"image1=foo.bar.foo:8800/foo/image1:foo-bar"}, + infileImages: []string{ + "images:", + "- name: image1", + " newName: my-image1", + " newTag: my-tag", + "- name: image2", + " newName: my-image2", + " newTag: my-tag2", + "patchesJson6902:", + "- patch: |-", + " - op: remove", + " path: /spec/selector", + " target:", + " kind: Service", + " name: foo", + " version: v1", + }, + }, + expected: expected{ + fileOutput: []string{ + "images:", + "- name: image1", + " newName: foo.bar.foo:8800/foo/image1", + " newTag: foo-bar", + "- name: image2", + " newName: my-image2", + " newTag: my-tag2", + "patchesJson6902:", + "- patch: |-", + " - op: remove", + " path: /spec/selector", + " target:", + " kind: Service", + " name: foo", + " version: v1", + }}, + }, { description: "override new tag and new name with just a new tag", given: given{ From dda3984a8f76daf8d3f360c0f95006b997712439 Mon Sep 17 00:00:00 2001 From: Eyob Tefera Date: Fri, 11 Sep 2020 18:57:14 +0000 Subject: [PATCH 2/3] Move kustomization fixing to before Write step. --- kustomize/internal/commands/edit/fix/fix.go | 1 + kustomize/internal/commands/kustfile/kustomizationfile.go | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/kustomize/internal/commands/edit/fix/fix.go b/kustomize/internal/commands/edit/fix/fix.go index 13d9a31368..6600f7849f 100644 --- a/kustomize/internal/commands/edit/fix/fix.go +++ b/kustomize/internal/commands/edit/fix/fix.go @@ -39,5 +39,6 @@ func RunFix(fSys filesys.FileSystem) error { return err } + m.FixKustomizationPreMarshalling() return mf.Write(m) } diff --git a/kustomize/internal/commands/kustfile/kustomizationfile.go b/kustomize/internal/commands/kustfile/kustomizationfile.go index 900d748e28..f347e368a0 100644 --- a/kustomize/internal/commands/kustfile/kustomizationfile.go +++ b/kustomize/internal/commands/kustfile/kustomizationfile.go @@ -170,7 +170,6 @@ func (mf *kustomizationFile) Write(kustomization *types.Kustomization) error { if kustomization == nil { return errors.New("util: kustomization file arg is nil") } - kustomization.FixKustomizationPreMarshalling() data, err := mf.marshal(kustomization) if err != nil { return err From d3c46d3f7cf40427f63ada24f7e46c000e79ac5a Mon Sep 17 00:00:00 2001 From: Eyob Tefera Date: Fri, 11 Sep 2020 19:19:54 +0000 Subject: [PATCH 3/3] Move offending Write() tests to tests for fix. --- .../internal/commands/edit/fix/fix_test.go | 102 ++++++++++++++++++ .../kustfile/kustomizationfile_test.go | 97 ----------------- 2 files changed, 102 insertions(+), 97 deletions(-) diff --git a/kustomize/internal/commands/edit/fix/fix_test.go b/kustomize/internal/commands/edit/fix/fix_test.go index d0cd1b940e..3c1b52210d 100644 --- a/kustomize/internal/commands/edit/fix/fix_test.go +++ b/kustomize/internal/commands/edit/fix/fix_test.go @@ -7,6 +7,7 @@ import ( "strings" "testing" + "github.com/google/go-cmp/cmp" "sigs.k8s.io/kustomize/api/filesys" testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils" ) @@ -31,3 +32,104 @@ func TestFix(t *testing.T) { t.Errorf("expected kind in kustomization") } } + +func TestFixOutdatedPatchesFieldTitle(t *testing.T) { + kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +patchesJson6902: +- path: patch1.yaml + target: + kind: Service +- path: patch2.yaml + target: + group: apps + kind: Deployment + version: v1 +`) + + expected := []byte(` +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +patches: +- path: patch1.yaml + target: + kind: Service +- path: patch2.yaml + target: + group: apps + kind: Deployment + version: v1 +`) + fSys := filesys.MakeFsInMemory() + testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) + cmd := NewCmdFix(fSys) + err := cmd.RunE(cmd, nil) + if err != nil { + t.Errorf("unexpected cmd error: %v", err) + } + content, err := testutils_test.ReadTestKustomization(fSys) + if err != nil { + t.Errorf("unexpected read error: %v", err) + } + if !strings.Contains(string(content), "apiVersion: ") { + t.Errorf("expected apiVersion in kustomization") + } + if !strings.Contains(string(content), "kind: Kustomization") { + t.Errorf("expected kind in kustomization") + } + + if diff := cmp.Diff(expected, content); diff != "" { + t.Errorf("Mismatch (-expected, +actual):\n%s", diff) + } +} + +func TestRenameAndKeepOutdatedPatchesField(t *testing.T) { + kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +patchesJson6902: +- path: patch1.yaml + target: + kind: Deployment +patches: +- path: patch2.yaml + target: + kind: Deployment +- path: patch3.yaml + target: + kind: Service +`) + + expected := []byte(` +patches: +- path: patch2.yaml + target: + kind: Deployment +- path: patch3.yaml + target: + kind: Service +- path: patch1.yaml + target: + kind: Deployment +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +`) + fSys := filesys.MakeFsInMemory() + testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) + cmd := NewCmdFix(fSys) + err := cmd.RunE(cmd, nil) + if err != nil { + t.Errorf("unexpected cmd error: %v", err) + } + content, err := testutils_test.ReadTestKustomization(fSys) + if err != nil { + t.Errorf("unexpected read error: %v", err) + } + if !strings.Contains(string(content), "apiVersion: ") { + t.Errorf("expected apiVersion in kustomization") + } + if !strings.Contains(string(content), "kind: Kustomization") { + t.Errorf("expected kind in kustomization") + } + + if diff := cmp.Diff(expected, content); diff != "" { + t.Errorf("Mismatch (-expected, +actual):\n%s", diff) + } +} diff --git a/kustomize/internal/commands/kustfile/kustomizationfile_test.go b/kustomize/internal/commands/kustfile/kustomizationfile_test.go index 97bcc76550..36c28969f2 100644 --- a/kustomize/internal/commands/kustfile/kustomizationfile_test.go +++ b/kustomize/internal/commands/kustfile/kustomizationfile_test.go @@ -338,103 +338,6 @@ kind: Kustomization } } -func TestRenameAndKeepOutdatedPatchesField(t *testing.T) { - kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` -patchesJson6902: -- path: patch1.yaml - target: - kind: Deployment -patches: -- path: patch2.yaml - target: - kind: Deployment -- path: patch3.yaml - target: - kind: Service -`) - - expected := []byte(` -patches: -- path: patch2.yaml - target: - kind: Deployment -- path: patch3.yaml - target: - kind: Service -- path: patch1.yaml - target: - kind: Deployment -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization -`) - fSys := filesys.MakeFsInMemory() - testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) - mf, err := NewKustomizationFile(fSys) - if err != nil { - t.Fatalf("Unexpected Error: %v", err) - } - - kustomization, err := mf.Read() - if err != nil { - t.Fatalf("Unexpected Error: %v", err) - } - if err = mf.Write(kustomization); err != nil { - t.Fatalf("Unexpected Error: %v", err) - } - bytes, _ := fSys.ReadFile(mf.path) - - if diff := cmp.Diff(expected, bytes); diff != "" { - t.Errorf("Mismatch (-expected, +actual):\n%s", diff) - } -} - -func TestFixOutdatedPatchesFieldTitle(t *testing.T) { - kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` -patchesJson6902: -- path: patch1.yaml - target: - kind: Service -- path: patch2.yaml - target: - group: apps - kind: Deployment - version: v1 -`) - - expected := []byte(` -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization -patches: -- path: patch1.yaml - target: - kind: Service -- path: patch2.yaml - target: - group: apps - kind: Deployment - version: v1 -`) - fSys := filesys.MakeFsInMemory() - testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) - mf, err := NewKustomizationFile(fSys) - if err != nil { - t.Fatalf("Unexpected Error: %v", err) - } - - kustomization, err := mf.Read() - if err != nil { - t.Fatalf("Unexpected Error: %v", err) - } - if err = mf.Write(kustomization); err != nil { - t.Fatalf("Unexpected Error: %v", err) - } - bytes, _ := fSys.ReadFile(mf.path) - - if diff := cmp.Diff(expected, bytes); diff != "" { - t.Errorf("Mismatch (-expected, +actual):\n%s", diff) - } -} - func TestUnknownFieldInKustomization(t *testing.T) { kContent := []byte(` foo: