@@ -42,6 +42,94 @@ func TestMakeConfigMap(t *testing.T) {
4242 args types.ConfigMapArgs
4343 exp expected
4444 }{
45+ // Regression tests for https://github.com/kubernetes-sigs/kustomize/issues/4292
46+ // ConfigMap data keys must be emitted in sorted (deterministic) order regardless
47+ // of the order they are defined in the source.
48+ "literal sources in non-alphabetical order produce sorted output" : {
49+ args : types.ConfigMapArgs {
50+ GeneratorArgs : types.GeneratorArgs {
51+ Name : "sortedLiterals" ,
52+ KvPairSources : types.KvPairSources {
53+ LiteralSources : []string {
54+ "zebra=z" ,
55+ "mango=m" ,
56+ "apple=a" ,
57+ "banana=b" ,
58+ "kiwi=k" ,
59+ },
60+ },
61+ },
62+ },
63+ exp : expected {
64+ out : `apiVersion: v1
65+ kind: ConfigMap
66+ metadata:
67+ name: sortedLiterals
68+ data:
69+ apple: a
70+ banana: b
71+ kiwi: k
72+ mango: m
73+ zebra: z
74+ ` ,
75+ },
76+ },
77+ "env file with non-alphabetical keys produces sorted output" : {
78+ args : types.ConfigMapArgs {
79+ GeneratorArgs : types.GeneratorArgs {
80+ Name : "sortedEnv" ,
81+ KvPairSources : types.KvPairSources {
82+ EnvSources : []string {
83+ filepath .Join ("configmap" , "unsorted.env" ),
84+ },
85+ },
86+ },
87+ },
88+ exp : expected {
89+ out : `apiVersion: v1
90+ kind: ConfigMap
91+ metadata:
92+ name: sortedEnv
93+ data:
94+ APPLE: a
95+ BANANA: b
96+ KIWI: k
97+ MANGO: m
98+ ZEBRA: z
99+ ` ,
100+ },
101+ },
102+ "mixed literal and env sources produce sorted output" : {
103+ args : types.ConfigMapArgs {
104+ GeneratorArgs : types.GeneratorArgs {
105+ Name : "sortedMixed" ,
106+ KvPairSources : types.KvPairSources {
107+ LiteralSources : []string {
108+ "zebra=z" ,
109+ "apple=a" ,
110+ },
111+ EnvSources : []string {
112+ filepath .Join ("configmap" , "unsorted.env" ),
113+ },
114+ },
115+ },
116+ },
117+ exp : expected {
118+ out : `apiVersion: v1
119+ kind: ConfigMap
120+ metadata:
121+ name: sortedMixed
122+ data:
123+ APPLE: a
124+ BANANA: b
125+ KIWI: k
126+ MANGO: m
127+ ZEBRA: z
128+ apple: a
129+ zebra: z
130+ ` ,
131+ },
132+ },
45133 "construct config map from env" : {
46134 args : types.ConfigMapArgs {
47135 GeneratorArgs : types.GeneratorArgs {
@@ -199,6 +287,10 @@ immutable: true
199287 fSys .WriteFile (
200288 filesys .RootedPath ("configmap" , "app.bin" ),
201289 manyHellos (30 ))
290+ // unsorted.env is used by regression tests for issue #4292 (non-deterministic ordering)
291+ fSys .WriteFile (
292+ filesys .RootedPath ("configmap" , "unsorted.env" ),
293+ []byte ("ZEBRA=z\n MANGO=m\n APPLE=a\n BANANA=b\n KIWI=k\n " ))
202294 kvLdr := kv .NewLoader (
203295 loader .NewFileLoaderAtRoot (fSys ),
204296 valtest_test .MakeFakeValidator ())
0 commit comments