@@ -5,6 +5,7 @@ package kustfile
5
5
6
6
import (
7
7
"reflect"
8
+ "slices"
8
9
"strings"
9
10
"testing"
10
11
@@ -21,6 +22,7 @@ func TestFieldOrder(t *testing.T) {
21
22
"APIVersion" ,
22
23
"Kind" ,
23
24
"MetaData" ,
25
+ "SortOptions" ,
24
26
"Resources" ,
25
27
"Bases" ,
26
28
"NamePrefix" ,
@@ -46,6 +48,7 @@ func TestFieldOrder(t *testing.T) {
46
48
"Configurations" ,
47
49
"Generators" ,
48
50
"Transformers" ,
51
+ "Validators" ,
49
52
"Components" ,
50
53
"OpenAPI" ,
51
54
"BuildMetadata" ,
@@ -87,6 +90,154 @@ func TestWriteAndRead(t *testing.T) {
87
90
}
88
91
}
89
92
93
+ func TestReadAndWrite (t * testing.T ) {
94
+ kWrite := []byte (completeKustfileInOrder )
95
+
96
+ fSys := filesys .MakeFsInMemory ()
97
+ testutils_test .WriteTestKustomizationWith (fSys , kWrite )
98
+ mf , err := NewKustomizationFile (fSys )
99
+ if err != nil {
100
+ t .Fatalf ("Unexpected Error: %v" , err )
101
+ }
102
+
103
+ kustomization , err := mf .Read ()
104
+ if err != nil {
105
+ t .Fatalf ("Unexpected Error: %v" , err )
106
+ }
107
+
108
+ if err := mf .Write (kustomization ); err != nil {
109
+ t .Fatalf ("Couldn't write kustomization file: %v\n " , err )
110
+ }
111
+
112
+ kRead , err := testutils_test .ReadTestKustomization (fSys )
113
+ if err != nil {
114
+ t .Fatalf ("Unexpected Error: %v" , err )
115
+ }
116
+
117
+ if ! reflect .DeepEqual (kWrite , kRead ) {
118
+ t .Fatal ("Written kustomization is different from read kustomization" )
119
+ }
120
+ }
121
+
122
+ func TestReadAndWriteDummy (t * testing.T ) {
123
+ kWrite := & types.Kustomization {
124
+ TypeMeta : types.TypeMeta {
125
+ APIVersion : "kustomize.config.k8s.io/v1beta1" ,
126
+ Kind : "Kustomization" ,
127
+ },
128
+ MetaData : & types.ObjectMeta {
129
+ Name : "name" ,
130
+ Namespace : "namespace" ,
131
+ Labels : map [string ]string {"label" : "label" },
132
+ Annotations : map [string ]string {"annotation" : "annotation" },
133
+ },
134
+ OpenAPI : map [string ]string {"path" : "schema.json" },
135
+ NamePrefix : "prefix" ,
136
+ NameSuffix : "suffix" ,
137
+ Namespace : "namespace" ,
138
+ CommonLabels : map [string ]string {"commonLabel" : "commonLabel" },
139
+ Labels : []types.Label {{
140
+ Pairs : map [string ]string {"label" : "label" },
141
+ IncludeSelectors : true ,
142
+ IncludeTemplates : true ,
143
+ FieldSpecs : []types.FieldSpec {{
144
+ Path : "metadata.labels.label" ,
145
+ }},
146
+ }},
147
+ CommonAnnotations : map [string ]string {"commonAnnotation" : "commonAnnotation" },
148
+ Patches : []types.Patch {{
149
+ Path : "path" ,
150
+ }},
151
+ Images : []types.Image {{
152
+ Name : "name" ,
153
+ NewName : "newName" ,
154
+ TagSuffix : "tagSuffix" ,
155
+ NewTag : "newTag" ,
156
+ Digest : "digest" ,
157
+ }},
158
+ Replacements : []types.ReplacementField {{
159
+ Path : "path" ,
160
+ }},
161
+ Replicas : []types.Replica {{
162
+ Name : "name" ,
163
+ Count : 1 ,
164
+ }},
165
+ SortOptions : & types.SortOptions {
166
+ Order : types .LegacySortOrder ,
167
+ LegacySortOptions : & types.LegacySortOptions {
168
+ OrderFirst : []string {"orderFirst" },
169
+ OrderLast : []string {"orderLast" },
170
+ },
171
+ },
172
+ Resources : []string {"resource" },
173
+ Components : []string {"component" },
174
+ Crds : []string {"crd" },
175
+ ConfigMapGenerator : []types.ConfigMapArgs {{
176
+ GeneratorArgs : types.GeneratorArgs {
177
+ Namespace : "namespace" ,
178
+ Name : "name" ,
179
+ },
180
+ }},
181
+ SecretGenerator : []types.SecretArgs {{
182
+ GeneratorArgs : types.GeneratorArgs {
183
+ Namespace : "namespace" ,
184
+ Name : "name" ,
185
+ },
186
+ }},
187
+ HelmGlobals : & types.HelmGlobals {
188
+ ChartHome : "chartHome" ,
189
+ ConfigHome : "configHome" ,
190
+ },
191
+ HelmCharts : []types.HelmChart {{
192
+ Name : "name" ,
193
+ }},
194
+ GeneratorOptions : & types.GeneratorOptions {
195
+ Labels : map [string ]string {"label" : "label" },
196
+ },
197
+ Configurations : []string {"configuration" },
198
+ Generators : []string {"generator" },
199
+ Transformers : []string {"transformer" },
200
+ Validators : []string {"validator" },
201
+ BuildMetadata : []string {"buildMetadata" },
202
+ }
203
+
204
+ // this check is for forward compatibility: if this fails, add a dummy value to the Kustomization above
205
+ assertAllNonZeroExcept (t , kWrite , []string {"PatchesStrategicMerge" , "PatchesJson6902" , "ImageTags" , "Vars" , "Bases" , "HelmChartInflationGenerator" })
206
+
207
+ fSys := filesys .MakeFsInMemory ()
208
+ testutils_test .WriteTestKustomization (fSys )
209
+ mf , err := NewKustomizationFile (fSys )
210
+ if err != nil {
211
+ t .Fatalf ("Unexpected Error: %v" , err )
212
+ }
213
+
214
+ if err := mf .Write (kWrite ); err != nil {
215
+ t .Fatalf ("Couldn't write kustomization file: %v\n " , err )
216
+ }
217
+
218
+ kRead , err := mf .Read ()
219
+ if err != nil {
220
+ t .Fatalf ("Unexpected Error: %v" , err )
221
+ }
222
+
223
+ if ! reflect .DeepEqual (kWrite , kRead ) {
224
+ t .Fatal ("Written kustomization is different from read kustomization." )
225
+ }
226
+ }
227
+
228
+ func assertAllNonZeroExcept (t * testing.T , val * types.Kustomization , except []string ) {
229
+ fFor := reflect .ValueOf (val ).Elem ()
230
+ n := fFor .NumField ()
231
+ for i := 0 ; i < n ; i ++ {
232
+ key := fFor .Type ().Field (i ).Name
233
+ val := fFor .Field (i )
234
+ if val .IsZero () && ! slices .Contains (except , key ) {
235
+ t .Helper ()
236
+ t .Fatalf ("Key %s should not be empty" , key )
237
+ }
238
+ }
239
+ }
240
+
90
241
func TestGetPath (t * testing.T ) {
91
242
fSys := filesys .MakeEmptyDirInMemory ()
92
243
testutils_test .WriteTestKustomization (fSys )
@@ -386,3 +537,80 @@ foo:
386
537
t .Fatalf ("Expect an unknown field error but got: %v" , err )
387
538
}
388
539
}
540
+
541
+ const completeKustfileInOrder = `
542
+ kind: Kustomization
543
+ apiVersion: kustomize.config.k8s.io/v1beta1
544
+ metadata:
545
+ annotations:
546
+ annotation: annotation
547
+ labels:
548
+ label: label
549
+ name: name
550
+ namespace: namespace
551
+ openapi:
552
+ path: schema.json
553
+ namePrefix: prefix
554
+ nameSuffix: suffix
555
+ namespace: namespace
556
+ commonLabels:
557
+ commonLabel: commonLabel
558
+ labels:
559
+ - fields:
560
+ - path: metadata.labels.label
561
+ includeSelectors: true
562
+ includeTemplates: true
563
+ pairs:
564
+ label: label
565
+ commonAnnotations:
566
+ commonAnnotation: commonAnnotation
567
+ patches:
568
+ - path: path
569
+ images:
570
+ - digest: digest
571
+ name: name
572
+ newName: newName
573
+ newTag: newTag
574
+ tagSuffix: tagSuffix
575
+ replacements:
576
+ - path: path
577
+ replicas:
578
+ - count: 1
579
+ name: name
580
+ sortOptions:
581
+ legacySortOptions:
582
+ orderFirst:
583
+ - orderFirst
584
+ orderLast:
585
+ - orderLast
586
+ order: legacy
587
+ resources:
588
+ - resource
589
+ components:
590
+ - component
591
+ crds:
592
+ - crd
593
+ configMapGenerator:
594
+ - name: name
595
+ namespace: namespace
596
+ secretGenerator:
597
+ - name: name
598
+ namespace: namespace
599
+ helmGlobals:
600
+ chartHome: chartHome
601
+ configHome: configHome
602
+ helmCharts:
603
+ - name: name
604
+ - name: chartName
605
+ generatorOptions:
606
+ labels:
607
+ label: label
608
+ configurations:
609
+ - configuration
610
+ generators:
611
+ - generator
612
+ transformers:
613
+ - transformer
614
+ buildMetadata:
615
+ - buildMetadata
616
+ `
0 commit comments