@@ -70,6 +70,202 @@ metadata:
70
70
}
71
71
}
72
72
73
+ func TestDemandOneMatchForId (t * testing.T ) {
74
+ rm1 := ResMap {
75
+ resid .NewResIdWithPrefixNamespace (cmap , "cm1" , "prefix1" , "ns1" ): resource .NewResourceFromMap (
76
+ map [string ]interface {}{
77
+ "apiVersion" : "v1" ,
78
+ "kind" : "ConfigMap" ,
79
+ "metadata" : map [string ]interface {}{
80
+ "name" : "cm1" ,
81
+ },
82
+ }),
83
+ resid .NewResIdWithPrefixNamespace (cmap , "cm2" , "prefix1" , "ns1" ): resource .NewResourceFromMap (
84
+ map [string ]interface {}{
85
+ "apiVersion" : "v1" ,
86
+ "kind" : "ConfigMap" ,
87
+ "metadata" : map [string ]interface {}{
88
+ "name" : "cm2" ,
89
+ },
90
+ }),
91
+ }
92
+
93
+ _ , ok := rm1 .DemandOneMatchForId (resid .NewResIdWithPrefixNamespace (cmap , "cm2" , "prefix1" , "ns1" ))
94
+ if ! ok {
95
+ t .Fatal ("Expected single map entry but got none" )
96
+ }
97
+
98
+ // confirm that ns and prefix are not included in match
99
+ _ , ok = rm1 .DemandOneMatchForId (resid .NewResIdWithPrefixNamespace (cmap , "cm2" , "prefix" , "ns" ))
100
+ if ! ok {
101
+ t .Fatal ("Expected single map entry but got none" )
102
+ }
103
+
104
+ // confirm that name is matched correctly
105
+ result , ok := rm1 .DemandOneMatchForId (resid .NewResIdWithPrefixNamespace (cmap , "cm3" , "prefix1" , "ns1" ))
106
+ if ok {
107
+ t .Fatalf ("Expected no map entries but got %v" , result )
108
+ }
109
+
110
+ cmap2 := gvk.Gvk {Version : "v2" , Kind : "ConfigMap" }
111
+
112
+ // confirm that gvk is matched correctly
113
+ result , ok = rm1 .DemandOneMatchForId (resid .NewResIdWithPrefixNamespace (cmap2 , "cm2" , "prefix1" , "ns1" ))
114
+ if ok {
115
+ t .Fatalf ("Expected no map entries but got %v" , result )
116
+ }
117
+
118
+ }
119
+
120
+ func TestFilterBy (t * testing.T ) {
121
+ rm := ResMap {resid .NewResIdWithPrefixNamespace (cmap , "cm1" , "prefix1" , "ns1" ): resource .NewResourceFromMap (
122
+ map [string ]interface {}{
123
+ "apiVersion" : "v1" ,
124
+ "kind" : "ConfigMap" ,
125
+ "metadata" : map [string ]interface {}{
126
+ "name" : "cm1" ,
127
+ },
128
+ }),
129
+ resid .NewResIdWithPrefixNamespace (cmap , "cm2" , "prefix1" , "ns1" ): resource .NewResourceFromMap (
130
+ map [string ]interface {}{
131
+ "apiVersion" : "v1" ,
132
+ "kind" : "ConfigMap" ,
133
+ "metadata" : map [string ]interface {}{
134
+ "name" : "cm2" ,
135
+ },
136
+ }),
137
+ }
138
+ rm1 := ResMap {
139
+ resid .NewResIdWithPrefixNamespace (cmap , "cm3" , "prefix1" , "ns2" ): resource .NewResourceFromMap (
140
+ map [string ]interface {}{
141
+ "apiVersion" : "v1" ,
142
+ "kind" : "ConfigMap" ,
143
+ "metadata" : map [string ]interface {}{
144
+ "name" : "cm2" ,
145
+ },
146
+ }),
147
+ }
148
+
149
+ for k , v := range rm {
150
+ rm1 [k ] = v
151
+ }
152
+
153
+ empty := rm1 .FilterBy (resid .NewResIdWithPrefixNamespace (cmap , "cm4" , "prefix1" , "ns3" ))
154
+ if len (empty ) != 0 {
155
+ t .Fatalf ("Expected empty filtered map but got %v" , empty )
156
+ }
157
+
158
+ ns1map := rm1 .FilterBy (resid .NewResIdWithPrefixNamespace (cmap , "cm4" , "prefix1" , "ns1" ))
159
+ if ! reflect .DeepEqual (rm , ns1map ) {
160
+ t .Fatalf ("Expected %v but got back %v" , rm , ns1map )
161
+ }
162
+ }
163
+ func TestDeepCopy (t * testing.T ) {
164
+ rm1 := ResMap {
165
+ resid .NewResId (cmap , "cm1" ): resource .NewResourceFromMap (
166
+ map [string ]interface {}{
167
+ "apiVersion" : "v1" ,
168
+ "kind" : "ConfigMap" ,
169
+ "metadata" : map [string ]interface {}{
170
+ "name" : "cm1" ,
171
+ },
172
+ }),
173
+ resid .NewResId (cmap , "cm2" ): resource .NewResourceFromMap (
174
+ map [string ]interface {}{
175
+ "apiVersion" : "v1" ,
176
+ "kind" : "ConfigMap" ,
177
+ "metadata" : map [string ]interface {}{
178
+ "name" : "cm2" ,
179
+ },
180
+ }),
181
+ }
182
+
183
+ rm2 := rm1 .DeepCopy ()
184
+
185
+ if ! reflect .DeepEqual (rm1 , rm2 ) {
186
+ t .Fatalf ("%v doesn't equal it's deep copy %v" , rm1 , rm2 )
187
+ }
188
+ }
189
+
190
+ func TestErrorIfNotEqual (t * testing.T ) {
191
+
192
+ rm1 := ResMap {
193
+ resid .NewResId (cmap , "cm1" ): resource .NewResourceFromMap (
194
+ map [string ]interface {}{
195
+ "apiVersion" : "v1" ,
196
+ "kind" : "ConfigMap" ,
197
+ "metadata" : map [string ]interface {}{
198
+ "name" : "cm1" ,
199
+ },
200
+ }),
201
+ resid .NewResId (cmap , "cm2" ): resource .NewResourceFromMap (
202
+ map [string ]interface {}{
203
+ "apiVersion" : "v1" ,
204
+ "kind" : "ConfigMap" ,
205
+ "metadata" : map [string ]interface {}{
206
+ "name" : "cm2" ,
207
+ },
208
+ }),
209
+ }
210
+
211
+ err := rm1 .ErrorIfNotEqual (rm1 )
212
+ if err != nil {
213
+ t .Fatalf ("%v should equal itself %v" , rm1 , err )
214
+ }
215
+
216
+ rm2 := ResMap {
217
+ resid .NewResId (cmap , "cm1" ): resource .NewResourceFromMap (
218
+ map [string ]interface {}{
219
+ "apiVersion" : "v1" ,
220
+ "kind" : "ConfigMap" ,
221
+ "metadata" : map [string ]interface {}{
222
+ "name" : "cm1" ,
223
+ },
224
+ }),
225
+ }
226
+
227
+ // test the different number of keys path
228
+ err = rm1 .ErrorIfNotEqual (rm2 )
229
+ if err == nil {
230
+ t .Fatalf ("%v should not equal %v %v" , rm1 , rm2 , err )
231
+ }
232
+
233
+ rm3 := ResMap {
234
+ resid .NewResId (cmap , "cm2" ): resource .NewResourceFromMap (
235
+ map [string ]interface {}{
236
+ "apiVersion" : "v1" ,
237
+ "kind" : "ConfigMap" ,
238
+ "metadata" : map [string ]interface {}{
239
+ "name" : "cm1" ,
240
+ },
241
+ }),
242
+ }
243
+
244
+ // test the different key values path
245
+ err = rm2 .ErrorIfNotEqual (rm3 )
246
+ if err == nil {
247
+ t .Fatalf ("%v should not equal %v %v" , rm1 , rm2 , err )
248
+ }
249
+
250
+ rm4 := ResMap {
251
+ resid .NewResId (cmap , "cm1" ): resource .NewResourceFromMap (
252
+ map [string ]interface {}{
253
+ "apiVersion" : "v1" ,
254
+ "kind" : "ConfigMap" ,
255
+ "metadata" : map [string ]interface {}{
256
+ "name" : "cm3" ,
257
+ },
258
+ }),
259
+ }
260
+
261
+ // test the deepcopy path
262
+ err = rm2 .ErrorIfNotEqual (rm4 )
263
+ if err == nil {
264
+ t .Fatalf ("%v should not equal %v %v" , rm1 , rm2 , err )
265
+ }
266
+
267
+ }
268
+
73
269
func TestNewMapFromFiles (t * testing.T ) {
74
270
75
271
resourceStr := `apiVersion: apps/v1
@@ -225,7 +421,8 @@ func TestMergeWithoutOverride(t *testing.T) {
225
421
}
226
422
}
227
423
228
- func TestMergeWithOverride (t * testing.T ) {
424
+ func generateMergeFixtures (b ifc.GenerationBehavior ) []ResMap {
425
+
229
426
input1 := ResMap {
230
427
resid .NewResId (cmap , "cmap" ): resource .NewResourceFromMap (
231
428
map [string ]interface {}{
@@ -256,8 +453,11 @@ func TestMergeWithOverride(t *testing.T) {
256
453
}),
257
454
}
258
455
input1 [resid .NewResId (cmap , "cmap" )].SetBehavior (ifc .BehaviorCreate )
259
- input2 [resid .NewResId (cmap , "cmap" )].SetBehavior (ifc .BehaviorMerge )
260
- input := []ResMap {input1 , input2 }
456
+ input2 [resid .NewResId (cmap , "cmap" )].SetBehavior (b )
457
+ return []ResMap {input1 , input2 }
458
+ }
459
+
460
+ func TestMergeWithOverride (t * testing.T ) {
261
461
expected := ResMap {
262
462
resid .NewResId (cmap , "cmap" ): resource .NewResourceFromMap (
263
463
map [string ]interface {}{
@@ -276,7 +476,7 @@ func TestMergeWithOverride(t *testing.T) {
276
476
}),
277
477
}
278
478
expected [resid .NewResId (cmap , "cmap" )].SetBehavior (ifc .BehaviorCreate )
279
- merged , err := MergeWithOverride (input ... )
479
+ merged , err := MergeWithOverride (generateMergeFixtures ( ifc . BehaviorMerge ) ... )
280
480
if err != nil {
281
481
t .Fatalf ("unexpected error: %v" , err )
282
482
}
@@ -299,4 +499,19 @@ func TestMergeWithOverride(t *testing.T) {
299
499
if ! reflect .DeepEqual (merged2 , expected ) {
300
500
t .Fatalf ("%#v doesn't equal expected %#v" , merged2 , expected )
301
501
}
502
+
503
+ inputs := generateMergeFixtures (ifc .BehaviorReplace )
504
+ replaced , err := MergeWithOverride (inputs ... )
505
+ if err != nil {
506
+ t .Fatalf ("unexpected error: %v" , err )
507
+ }
508
+ expectedReplaced := inputs [1 ]
509
+ if ! reflect .DeepEqual (replaced , expectedReplaced ) {
510
+ t .Fatalf ("%#v doesn't equal expected %#v" , replaced , expectedReplaced )
511
+ }
512
+
513
+ _ , err = MergeWithOverride (generateMergeFixtures (ifc .BehaviorUnspecified )... )
514
+ if err == nil {
515
+ t .Fatal ("Merging with GenerationBehavior BehaviorUnspecified should return an error but does not" )
516
+ }
302
517
}
0 commit comments