-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add edit fix
for patchesStrategicMerge to patches
#4733
Changes from 3 commits
64f42ea
bb7ebe0
3f0c213
b4d25b1
032bf33
cbb61fc
7c2e884
0d68e0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,8 +27,15 @@ func TestFix(t *testing.T) { | |
assert.Contains(t, string(content), "kind: Kustomization") | ||
} | ||
|
||
func TestFixOutdatedPatchesFieldTitle(t *testing.T) { | ||
kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` | ||
func TestFixCommand(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
name: "FixOutdatedPatchesFieldTitle", | ||
input: ` | ||
patchesJson6902: | ||
- path: patch1.yaml | ||
target: | ||
|
@@ -38,9 +45,8 @@ patchesJson6902: | |
group: apps | ||
kind: Deployment | ||
version: v1 | ||
`) | ||
|
||
expected := []byte(` | ||
`, | ||
expected: ` | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
patches: | ||
|
@@ -52,24 +58,11 @@ patches: | |
group: apps | ||
kind: Deployment | ||
version: v1 | ||
`) | ||
fSys := filesys.MakeFsInMemory() | ||
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) | ||
cmd := NewCmdFix(fSys, os.Stdout) | ||
assert.NoError(t, cmd.RunE(cmd, nil)) | ||
|
||
content, err := testutils_test.ReadTestKustomization(fSys) | ||
assert.NoError(t, err) | ||
assert.Contains(t, string(content), "apiVersion: ") | ||
assert.Contains(t, string(content), "kind: Kustomization") | ||
|
||
if diff := cmp.Diff(expected, content); diff != "" { | ||
t.Errorf("Mismatch (-expected, +actual):\n%s", diff) | ||
} | ||
} | ||
|
||
func TestRenameAndKeepOutdatedPatchesField(t *testing.T) { | ||
kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` | ||
`, | ||
}, | ||
{ | ||
name: "TestRenameAndKeepOutdatedPatchesField", | ||
input: ` | ||
patchesJson6902: | ||
- path: patch1.yaml | ||
target: | ||
|
@@ -81,9 +74,8 @@ patches: | |
- path: patch3.yaml | ||
target: | ||
kind: Service | ||
`) | ||
|
||
expected := []byte(` | ||
`, | ||
expected: ` | ||
patches: | ||
- path: patch2.yaml | ||
target: | ||
|
@@ -96,32 +88,154 @@ patches: | |
kind: Deployment | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
`) | ||
fSys := filesys.MakeFsInMemory() | ||
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) | ||
cmd := NewCmdFix(fSys, os.Stdout) | ||
assert.NoError(t, cmd.RunE(cmd, nil)) | ||
|
||
content, err := testutils_test.ReadTestKustomization(fSys) | ||
assert.NoError(t, err) | ||
assert.Contains(t, string(content), "apiVersion: ") | ||
assert.Contains(t, string(content), "kind: Kustomization") | ||
|
||
if diff := cmp.Diff(expected, content); diff != "" { | ||
t.Errorf("Mismatch (-expected, +actual):\n%s", diff) | ||
} | ||
} | ||
|
||
func TestFixOutdatedCommonLabels(t *testing.T) { | ||
kustomizationContentWithOutdatedCommonLabels := []byte(` | ||
`, | ||
}, | ||
{ | ||
name: "TestFixOutdatedPatchesStrategicMergeFieldTitle", | ||
input: ` | ||
patchesStrategicMerge: | ||
- |- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nignx:latest | ||
`, | ||
expected: ` | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
patches: | ||
- patch: |- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nignx:latest | ||
`, | ||
}, | ||
{ | ||
name: "TestFixAndMergeOutdatedPatchesStrategicMergeFieldTitle", | ||
input: ` | ||
patchesStrategicMerge: | ||
- |- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nignx:latest | ||
patches: | ||
- path: patch2.yaml | ||
target: | ||
kind: Deployment | ||
`, | ||
expected: ` | ||
patches: | ||
- path: patch2.yaml | ||
target: | ||
kind: Deployment | ||
- patch: |- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nignx:latest | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
`, | ||
}, | ||
{ | ||
name: "TestFixOutdatedPatchesStrategicMergeToPathFieldTitle", | ||
input: ` | ||
patchesStrategicMerge: | ||
- deploy.yaml | ||
`, | ||
expected: ` | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
patches: | ||
- path: deploy.yaml | ||
`, | ||
}, | ||
{ | ||
name: "TestFixOutdatedPatchesStrategicMergeToPathFieldYMLTitle", | ||
input: ` | ||
patchesStrategicMerge: | ||
- deploy.yml | ||
`, | ||
expected: ` | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
patches: | ||
- path: deploy.yml | ||
`, | ||
}, | ||
{ | ||
name: "TestFixOutdatedPatchesStrategicMergeFieldPatchEndOfYamlTitle", | ||
input: ` | ||
patchesStrategicMerge: | ||
- |- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: nginx | ||
env: | ||
- name: CONFIG_FILE_PATH | ||
value: home.yaml | ||
`, | ||
expected: ` | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
patches: | ||
- patch: |- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: nginx | ||
env: | ||
- name: CONFIG_FILE_PATH | ||
value: home.yaml | ||
`, | ||
}, | ||
{ | ||
name: "TestFixOutdatedCommonLabels", | ||
input: ` | ||
commonLabels: | ||
foo: bar | ||
labels: | ||
- pairs: | ||
a: b | ||
`) | ||
|
||
expected := []byte(` | ||
`, | ||
expected: ` | ||
labels: | ||
- pairs: | ||
a: b | ||
|
@@ -130,19 +244,23 @@ labels: | |
foo: bar | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
`) | ||
fSys := filesys.MakeFsInMemory() | ||
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedCommonLabels) | ||
cmd := NewCmdFix(fSys, os.Stdout) | ||
assert.NoError(t, cmd.RunE(cmd, nil)) | ||
`, | ||
}, | ||
} | ||
for _, test := range tests { | ||
koba1t marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we move the loop body into the body of a function passed to Very thorough testing! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fixed it at 7c2e884 |
||
fSys := filesys.MakeFsInMemory() | ||
testutils_test.WriteTestKustomizationWith(fSys, []byte(test.input)) | ||
cmd := NewCmdFix(fSys, os.Stdout) | ||
assert.NoError(t, cmd.RunE(cmd, nil)) | ||
|
||
content, err := testutils_test.ReadTestKustomization(fSys) | ||
assert.NoError(t, err) | ||
assert.Contains(t, string(content), "apiVersion: ") | ||
assert.Contains(t, string(content), "kind: Kustomization") | ||
content, err := testutils_test.ReadTestKustomization(fSys) | ||
assert.NoError(t, err) | ||
assert.Contains(t, string(content), "apiVersion: ") | ||
assert.Contains(t, string(content), "kind: Kustomization") | ||
koba1t marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if diff := cmp.Diff(expected, content); diff != "" { | ||
t.Errorf("Mismatch (-expected, +actual):\n%s", diff) | ||
if diff := cmp.Diff([]byte(test.expected), content); diff != "" { | ||
t.Errorf("%s: Mismatch (-expected, +actual):\n%s", test.name, diff) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional nit: We can replace these lines with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An easier and more reliable way to check if this is a filepath or not is to try to open it (i.e.
os.ReadFile
), and check for errorThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I rewrite at 032bf33.