mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-16 17:33:14 +00:00
Incorporate feedback from original PR
* Incorporate feedback left on original PR. * Add more test cases. * Minor refactoring and update to copyright notice.
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// Copyright 2022 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package remove //nolint:testpackage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
testutils_test "sigs.k8s.io/kustomize/kustomize/v4/commands/internal/testutils"
|
||||
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
)
|
||||
|
||||
@@ -18,9 +19,10 @@ func TestRemoveConfigMap(t *testing.T) {
|
||||
const configMapName02 = "example-configmap-02"
|
||||
|
||||
tests := map[string]struct {
|
||||
input string
|
||||
args []string
|
||||
expectedErr string
|
||||
input string
|
||||
args []string
|
||||
expectedOutput string
|
||||
expectedErr string
|
||||
}{
|
||||
"happy path": {
|
||||
input: fmt.Sprintf(`
|
||||
@@ -32,6 +34,10 @@ configMapGenerator:
|
||||
- application.properties
|
||||
`, configMapName01),
|
||||
args: []string{configMapName01},
|
||||
expectedOutput: `
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
`,
|
||||
},
|
||||
"multiple": {
|
||||
input: fmt.Sprintf(`
|
||||
@@ -48,6 +54,10 @@ configMapGenerator:
|
||||
args: []string{
|
||||
fmt.Sprintf("%s,%s", configMapName01, configMapName02),
|
||||
},
|
||||
expectedOutput: `
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
`,
|
||||
},
|
||||
"miss": {
|
||||
input: fmt.Sprintf(`
|
||||
@@ -58,7 +68,31 @@ configMapGenerator:
|
||||
files:
|
||||
- application.properties
|
||||
`, configMapName01),
|
||||
args: []string{"foo"},
|
||||
args: []string{"foo"},
|
||||
expectedErr: "no specified configmap(s) were found",
|
||||
},
|
||||
"no configmap name specified": {
|
||||
args: []string{},
|
||||
expectedErr: "at least one configmap name must be specified",
|
||||
},
|
||||
"too many configmap names specified": {
|
||||
args: []string{"test1", "test2"},
|
||||
expectedErr: "too many arguments",
|
||||
},
|
||||
"one existing and one non-existing": {
|
||||
input: fmt.Sprintf(`
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
configMapGenerator:
|
||||
- name: %s
|
||||
files:
|
||||
- application.properties
|
||||
`, configMapName01),
|
||||
args: []string{fmt.Sprintf("%s,%s", configMapName01, "foo")},
|
||||
expectedOutput: `
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -68,17 +102,17 @@ configMapGenerator:
|
||||
testutils_test.WriteTestKustomizationWith(fSys, []byte(tc.input))
|
||||
cmd := newCmdRemoveConfigMap(fSys)
|
||||
err := cmd.RunE(cmd, tc.args)
|
||||
|
||||
if tc.expectedErr != "" {
|
||||
assert.Error(t, err)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), tc.expectedErr)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
content, err := testutils_test.ReadTestKustomization(fSys)
|
||||
assert.NoError(t, err)
|
||||
for _, opt := range strings.Split(tc.args[0], ",") {
|
||||
assert.NotContains(t, string(content), opt)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
content, err := testutils_test.ReadTestKustomization(fSys)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expectedOutput, string(content))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user