refactor edit fix test cases

This commit is contained in:
yugo kobayashi
2022-08-06 20:25:00 +00:00
parent bb7ebe029c
commit 3f0c21304c

View File

@@ -27,8 +27,15 @@ func TestFix(t *testing.T) {
assert.Contains(t, string(content), "kind: Kustomization") assert.Contains(t, string(content), "kind: Kustomization")
} }
func TestFixOutdatedPatchesFieldTitle(t *testing.T) { func TestFixCommand(t *testing.T) {
kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` tests := []struct {
name string
input string
expected string
}{
{
name: "FixOutdatedPatchesFieldTitle",
input: `
patchesJson6902: patchesJson6902:
- path: patch1.yaml - path: patch1.yaml
target: target:
@@ -38,9 +45,8 @@ patchesJson6902:
group: apps group: apps
kind: Deployment kind: Deployment
version: v1 version: v1
`) `,
expected: `
expected := []byte(`
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
patches: patches:
@@ -52,24 +58,11 @@ patches:
group: apps group: apps
kind: Deployment kind: Deployment
version: v1 version: v1
`) `,
fSys := filesys.MakeFsInMemory() },
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) {
cmd := NewCmdFix(fSys, os.Stdout) name: "TestRenameAndKeepOutdatedPatchesField",
assert.NoError(t, cmd.RunE(cmd, nil)) input: `
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(`
patchesJson6902: patchesJson6902:
- path: patch1.yaml - path: patch1.yaml
target: target:
@@ -81,9 +74,8 @@ patches:
- path: patch3.yaml - path: patch3.yaml
target: target:
kind: Service kind: Service
`) `,
expected: `
expected := []byte(`
patches: patches:
- path: patch2.yaml - path: patch2.yaml
target: target:
@@ -96,24 +88,11 @@ patches:
kind: Deployment kind: Deployment
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
`) `,
fSys := filesys.MakeFsInMemory() },
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) {
cmd := NewCmdFix(fSys, os.Stdout) name: "TestFixOutdatedPatchesStrategicMergeFieldTitle",
assert.NoError(t, cmd.RunE(cmd, nil)) input: `
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 TestFixOutdatedPatchesStrategicMergeFieldTitle(t *testing.T) {
kustomizationContentWithOutdatedPatchesFieldTitle := []byte(`
patchesStrategicMerge: patchesStrategicMerge:
- |- - |-
apiVersion: apps/v1 apiVersion: apps/v1
@@ -126,9 +105,8 @@ patchesStrategicMerge:
containers: containers:
- name: nginx - name: nginx
image: nignx:latest image: nignx:latest
`) `,
expected: `
expected := []byte(`
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
patches: patches:
@@ -143,24 +121,11 @@ patches:
containers: containers:
- name: nginx - name: nginx
image: nignx:latest image: nignx:latest
`) `,
fSys := filesys.MakeFsInMemory() },
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) {
cmd := NewCmdFix(fSys, os.Stdout) name: "TestFixAndMergeOutdatedPatchesStrategicMergeFieldTitle",
assert.NoError(t, cmd.RunE(cmd, nil)) input: `
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 TestFixAndMergeOutdatedPatchesStrategicMergeFieldTitle(t *testing.T) {
kustomizationContentWithOutdatedPatchesFieldTitle := []byte(`
patchesStrategicMerge: patchesStrategicMerge:
- |- - |-
apiVersion: apps/v1 apiVersion: apps/v1
@@ -177,9 +142,8 @@ patches:
- path: patch2.yaml - path: patch2.yaml
target: target:
kind: Deployment kind: Deployment
`) `,
expected: `
expected := []byte(`
patches: patches:
- path: patch2.yaml - path: patch2.yaml
target: target:
@@ -197,78 +161,37 @@ patches:
image: nignx:latest image: nignx:latest
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
`) `,
fSys := filesys.MakeFsInMemory() },
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) {
cmd := NewCmdFix(fSys, os.Stdout) name: "TestFixOutdatedPatchesStrategicMergeToPathFieldTitle",
assert.NoError(t, cmd.RunE(cmd, nil)) input: `
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 TestFixOutdatedPatchesStrategicMergeToPathFieldTitle(t *testing.T) {
kustomizationContentWithOutdatedPatchesFieldTitle := []byte(`
patchesStrategicMerge: patchesStrategicMerge:
- deploy.yaml - deploy.yaml
`) `,
expected: `
expected := []byte(`
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
patches: patches:
- path: deploy.yaml - path: deploy.yaml
`) `,
fSys := filesys.MakeFsInMemory() },
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) {
cmd := NewCmdFix(fSys, os.Stdout) name: "TestFixOutdatedPatchesStrategicMergeToPathFieldYMLTitle",
assert.NoError(t, cmd.RunE(cmd, nil)) input: `
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 TestFixOutdatedPatchesStrategicMergeToPathFieldYMLTitle(t *testing.T) {
kustomizationContentWithOutdatedPatchesFieldTitle := []byte(`
patchesStrategicMerge: patchesStrategicMerge:
- deploy.yml - deploy.yml
`) `,
expected: `
expected := []byte(`
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
patches: patches:
- path: deploy.yml - path: deploy.yml
`) `,
fSys := filesys.MakeFsInMemory() },
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) {
cmd := NewCmdFix(fSys, os.Stdout) name: "TestFixOutdatedPatchesStrategicMergeFieldPatchEndOfYamlTitle",
assert.NoError(t, cmd.RunE(cmd, nil)) input: `
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 TestFixOutdatedPatchesStrategicMergeFieldPatchEndOfYamlTitle(t *testing.T) {
kustomizationContentWithOutdatedPatchesFieldTitle := []byte(`
patchesStrategicMerge: patchesStrategicMerge:
- |- - |-
apiVersion: apps/v1 apiVersion: apps/v1
@@ -283,9 +206,8 @@ patchesStrategicMerge:
env: env:
- name: CONFIG_FILE_PATH - name: CONFIG_FILE_PATH
value: home.yaml value: home.yaml
`) `,
expected: `
expected := []byte(`
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
patches: patches:
@@ -302,32 +224,18 @@ patches:
env: env:
- name: CONFIG_FILE_PATH - name: CONFIG_FILE_PATH
value: home.yaml value: home.yaml
`) `,
fSys := filesys.MakeFsInMemory() },
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) {
cmd := NewCmdFix(fSys, os.Stdout) name: "TestFixOutdatedCommonLabels",
assert.NoError(t, cmd.RunE(cmd, nil)) input: `
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(`
commonLabels: commonLabels:
foo: bar foo: bar
labels: labels:
- pairs: - pairs:
a: b a: b
`) `,
expected: `
expected := []byte(`
labels: labels:
- pairs: - pairs:
a: b a: b
@@ -336,19 +244,23 @@ labels:
foo: bar foo: bar
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
`) `,
fSys := filesys.MakeFsInMemory() },
testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedCommonLabels) }
cmd := NewCmdFix(fSys, os.Stdout) for _, test := range tests {
assert.NoError(t, cmd.RunE(cmd, nil)) 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) content, err := testutils_test.ReadTestKustomization(fSys)
assert.NoError(t, err) assert.NoError(t, err)
assert.Contains(t, string(content), "apiVersion: ") assert.Contains(t, string(content), "apiVersion: ")
assert.Contains(t, string(content), "kind: Kustomization") assert.Contains(t, string(content), "kind: Kustomization")
if diff := cmp.Diff(expected, content); diff != "" { if diff := cmp.Diff([]byte(test.expected), content); diff != "" {
t.Errorf("Mismatch (-expected, +actual):\n%s", diff) t.Errorf("%s: Mismatch (-expected, +actual):\n%s", test.name, diff)
}
} }
} }