Skip to content

Commit

Permalink
chore: add extra tests as requested in code review
Browse files Browse the repository at this point in the history
Include tests to cover more scenarios as requested via code review.
  • Loading branch information
stormqueen1990 committed Nov 28, 2023
1 parent 4fa894e commit 8dd9328
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 60 deletions.
110 changes: 81 additions & 29 deletions kustomize/commands/edit/remove/removeconfigmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import (
)

func TestRemoveConfigMap(t *testing.T) {
const configMapName01 = "example-configmap-01"
const configMapName02 = "example-configmap-02"

tests := map[string]struct {
input string
args []string
Expand All @@ -28,11 +25,11 @@ func TestRemoveConfigMap(t *testing.T) {
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- name: %s
- name: test-cm-1
files:
- application.properties
`, configMapName01),
args: []string{configMapName01},
`),
args: []string{"test-cm-1"},
expectedOutput: `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
Expand All @@ -43,54 +40,57 @@ kind: Kustomization
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- name: %s
- name: test-cm-1
files:
- application.properties
- name: %s
- name: test-cm-2
files:
- application.properties
`, configMapName01, configMapName02),
`),
args: []string{
fmt.Sprintf("%s,%s", configMapName01, configMapName02),
fmt.Sprintf("test-cm-1,test-cm-2"),

Check failure on line 51 in kustomize/commands/edit/remove/removeconfigmap_test.go

View workflow job for this annotation

GitHub Actions / Lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
},
expectedOutput: `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
`,
},
"fails when single configmap name is specified and doesn't exist": {
"removes a configmap successfully when single configmap name is specified, it exists, but is not the only configmap present": {
input: fmt.Sprintf(`

Check failure on line 59 in kustomize/commands/edit/remove/removeconfigmap_test.go

View workflow job for this annotation

GitHub Actions / Lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- name: %s
- name: test-cm-1
namespace: test-ns
files:
- application.properties
`, configMapName01),
args: []string{"foo"},
wantErr: true,
expectedErr: "no specified configmap(s) were found",
},
"fails when no configmap name is specified": {
args: []string{},
wantErr: true,
expectedErr: "at least one configmap name must be specified",
},
"fails when too many configmap names are specified": {
args: []string{"test1", "test2"},
wantErr: true,
expectedErr: "too many arguments",
- name: test-cm-2
namespace: default
literals:
- test-key=test-value
`),
args: []string{"test-cm-1", "--namespace=test-ns"},
wantErr: false,
expectedOutput: `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- literals:
- test-key=test-value
name: test-cm-2
namespace: default
`,
},
"succeeds when one configmap name exists and one doesn't exist": {
input: fmt.Sprintf(`

Check failure on line 85 in kustomize/commands/edit/remove/removeconfigmap_test.go

View workflow job for this annotation

GitHub Actions / Lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- name: %s
- name: test-cm-1
files:
- application.properties
`, configMapName01),
args: []string{fmt.Sprintf("%s,%s", configMapName01, "foo")},
`),
args: []string{fmt.Sprintf("test-cm-1,foo")},

Check failure on line 93 in kustomize/commands/edit/remove/removeconfigmap_test.go

View workflow job for this annotation

GitHub Actions / Lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
expectedOutput: `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
Expand Down Expand Up @@ -146,6 +146,58 @@ kind: Kustomization
wantErr: false,
args: []string{"test-cm", "--namespace=default"},
},
"fails when single configmap name is specified and doesn't exist": {
input: fmt.Sprintf(`

Check failure on line 150 in kustomize/commands/edit/remove/removeconfigmap_test.go

View workflow job for this annotation

GitHub Actions / Lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- name: test-cm-1
files:
- application.properties
`),
args: []string{"foo"},
wantErr: true,
expectedErr: "no specified configmap(s) were found",
},
"fails when single configmap name is specified and doesn't exist in the specified namespace": {
input: fmt.Sprintf(`

Check failure on line 163 in kustomize/commands/edit/remove/removeconfigmap_test.go

View workflow job for this annotation

GitHub Actions / Lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- name: test-cm-1
namespace: test-ns
files:
- application.properties
`),
args: []string{"test-cm-1"},
wantErr: true,
expectedErr: "no specified configmap(s) were found",
},

"fails when single configmap name is specified and doesn't exist in the specified namespace, and neither namespace is the default one": {
input: fmt.Sprintf(`

Check failure on line 178 in kustomize/commands/edit/remove/removeconfigmap_test.go

View workflow job for this annotation

GitHub Actions / Lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- name: test-cm-1
namespace: test-ns
files:
- application.properties
`),
args: []string{"test-cm-1", "--namespace=other-ns"},
wantErr: true,
expectedErr: "no specified configmap(s) were found",
},
"fails when no configmap name is specified": {
args: []string{},
wantErr: true,
expectedErr: "at least one configmap name must be specified",
},
"fails when too many configmap names are specified": {
args: []string{"test1", "test2"},
wantErr: true,
expectedErr: "too many arguments",
},
}

for name, tc := range tests {
Expand Down
112 changes: 81 additions & 31 deletions kustomize/commands/edit/remove/removesecret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import (
)

func TestRemoveSecret(t *testing.T) {
const secretName01 = "example-secret-01"
const secretName02 = "example-secret-02"

tests := map[string]struct {
input string
args []string
Expand All @@ -28,11 +25,11 @@ func TestRemoveSecret(t *testing.T) {
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- name: %s
- name: test-secret-1
files:
- longsecret.txt
`, secretName01),
args: []string{secretName01},
`),
args: []string{"test-secret-1"},
expectedOutput: `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
Expand All @@ -43,54 +40,55 @@ kind: Kustomization
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- name: %s
- name: test-secret-1
files:
- longsecret.txt
- name: %s
- name: test-secret-2
files:
- longsecret.txt
`, secretName01, secretName02),
args: []string{
fmt.Sprintf("%s,%s", secretName01, secretName02),
},
`),
args: []string{"test-secret-1,test-secret-2"},
expectedOutput: `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
`,
},
"fails when single secret name is specified and doesn't exist": {
"removes a secret successfully when single secret name is specified, it exists, but is not the only secret present": {
input: fmt.Sprintf(`
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- name: %s
- name: test-secret-1
namespace: test-ns
files:
- longsecret.txt
`, secretName01),
args: []string{"foo"},
wantErr: true,
expectedErr: "no specified secret(s) were found",
},
"fails when no secret name is specified": {
args: []string{},
wantErr: true,
expectedErr: "at least one secret name must be specified",
},
"fails when too many secret names are specified": {
args: []string{"test1", "test2"},
wantErr: true,
expectedErr: "too many arguments",
- name: test-secret-2
namespace: default
literals:
- test-key=test-secret
`),
args: []string{"test-secret-1", "--namespace=test-ns"},
wantErr: false,
expectedOutput: `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- literals:
- test-key=test-secret
name: test-secret-2
namespace: default
`,
},
"succeeds when one secret name exists and one doesn't exist": {
input: fmt.Sprintf(`
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- name: %s
- name: test-secret-1
files:
- application.properties
`, secretName01),
args: []string{fmt.Sprintf("%s,%s", secretName01, "foo")},
`),
args: []string{"test-secret-1,test-secret-2"},
expectedOutput: `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
Expand Down Expand Up @@ -145,6 +143,58 @@ kind: Kustomization
wantErr: false,
args: []string{"test-secret", "--namespace=default"},
},
"fails when single secret name is specified and doesn't exist": {
input: fmt.Sprintf(`
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- name: test-secret-1
files:
- longsecret.txt
`),
args: []string{"foo"},
wantErr: true,
expectedErr: "no specified secret(s) were found",
},
"fails when single secret name is specified and doesn't exist in the specified namespace": {
input: fmt.Sprintf(`
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- name: test-secret-1
namespace: test-ns
files:
- longsecret.txt
`),
args: []string{"test-secret-1"},
wantErr: true,
expectedErr: "no specified secret(s) were found",
},

"fails when single secret name is specified and doesn't exist in the specified namespace, and neither namespace is the default one": {
input: fmt.Sprintf(`
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- name: test-secret-1
namespace: test-ns
files:
- longsecret.txt
`),
args: []string{"test-secret-1", "--namespace=other-ns"},
wantErr: true,
expectedErr: "no specified secret(s) were found",
},
"fails when no secret name is specified": {
args: []string{},
wantErr: true,
expectedErr: "at least one secret name must be specified",
},
"fails when too many secret names are specified": {
args: []string{"test1", "test2"},
wantErr: true,
expectedErr: "too many arguments",
},
}

for name, tc := range tests {
Expand Down

0 comments on commit 8dd9328

Please sign in to comment.