Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 99 additions & 6 deletions api/internal/generators/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
. "sigs.k8s.io/kustomize/api/internal/generators"
"sigs.k8s.io/kustomize/api/kv"
"sigs.k8s.io/kustomize/api/pkg/loader"
Expand Down Expand Up @@ -42,6 +43,94 @@ func TestMakeConfigMap(t *testing.T) {
args types.ConfigMapArgs
exp expected
}{
// Regression tests for https://github.com/kubernetes-sigs/kustomize/issues/4292
// ConfigMap data keys must be emitted in sorted (deterministic) order regardless
// of the order they are defined in the source.
"literal sources in non-alphabetical order produce sorted output": {
args: types.ConfigMapArgs{
GeneratorArgs: types.GeneratorArgs{
Name: "sortedLiterals",
KvPairSources: types.KvPairSources{
LiteralSources: []string{
"zebra=z",
"mango=m",
"apple=a",
"banana=b",
"kiwi=k",
},
},
},
},
exp: expected{
out: `apiVersion: v1
kind: ConfigMap
metadata:
name: sortedLiterals
data:
apple: a
banana: b
kiwi: k
mango: m
zebra: z
`,
},
},
"env file with non-alphabetical keys produces sorted output": {
args: types.ConfigMapArgs{
GeneratorArgs: types.GeneratorArgs{
Name: "sortedEnv",
KvPairSources: types.KvPairSources{
EnvSources: []string{
filepath.Join("configmap", "unsorted.env"),
},
},
},
},
exp: expected{
out: `apiVersion: v1
kind: ConfigMap
metadata:
name: sortedEnv
data:
APPLE: a
BANANA: b
KIWI: k
MANGO: m
ZEBRA: z
`,
},
},
"mixed literal and env sources produce sorted output": {
args: types.ConfigMapArgs{
GeneratorArgs: types.GeneratorArgs{
Name: "sortedMixed",
KvPairSources: types.KvPairSources{
LiteralSources: []string{
"zebra=z",
"apple=a",
},
EnvSources: []string{
filepath.Join("configmap", "unsorted.env"),
},
},
},
},
exp: expected{
out: `apiVersion: v1
kind: ConfigMap
metadata:
name: sortedMixed
data:
APPLE: a
BANANA: b
KIWI: k
MANGO: m
ZEBRA: z
apple: a
zebra: z
`,
},
},
"construct config map from env": {
args: types.ConfigMapArgs{
GeneratorArgs: types.GeneratorArgs{
Expand Down Expand Up @@ -190,15 +279,19 @@ immutable: true
},
}
fSys := filesys.MakeFsInMemory()
fSys.WriteFile(
require.NoError(t, fSys.WriteFile(
filesys.RootedPath("configmap", "app.env"),
[]byte("DB_USERNAME=admin\nDB_PASSWORD=qwerty\n"))
fSys.WriteFile(
[]byte("DB_USERNAME=admin\nDB_PASSWORD=qwerty\n")))
require.NoError(t, fSys.WriteFile(
filesys.RootedPath("configmap", "app-init.ini"),
[]byte("FOO=bar\nBAR=baz\n"))
fSys.WriteFile(
[]byte("FOO=bar\nBAR=baz\n")))
require.NoError(t, fSys.WriteFile(
filesys.RootedPath("configmap", "app.bin"),
manyHellos(30))
manyHellos(30)))
// unsorted.env is used by regression tests for issue #4292 (non-deterministic ordering)
require.NoError(t, fSys.WriteFile(
filesys.RootedPath("configmap", "unsorted.env"),
[]byte("ZEBRA=z\nMANGO=m\nAPPLE=a\nBANANA=b\nKIWI=k\n")))
kvLdr := kv.NewLoader(
loader.NewFileLoaderAtRoot(fSys),
valtest_test.MakeFakeValidator())
Expand Down
Loading