@@ -26,17 +26,18 @@ import (
26
26
27
27
func TestNewHelper (t * testing.T ) {
28
28
tests := []struct {
29
- name string
30
- original * unstructured.Unstructured // current
31
- modified * unstructured.Unstructured // desired
32
- options []HelperOption
33
- wantHasChanges bool
34
- wantPatch []byte
29
+ name string
30
+ original * unstructured.Unstructured // current
31
+ modified * unstructured.Unstructured // desired
32
+ options []HelperOption
33
+ wantHasChanges bool
34
+ wantHasSpecChanges bool
35
+ wantPatch []byte
35
36
}{
36
37
// Field both in original and in modified --> align to modified
37
38
38
39
{
39
- name : "Field both in original and in modified, no-op when equal" ,
40
+ name : "Field (spec) both in original and in modified, no-op when equal" ,
40
41
original : & unstructured.Unstructured { // current
41
42
Object : map [string ]interface {}{
42
43
"spec" : map [string ]interface {}{
@@ -51,8 +52,9 @@ func TestNewHelper(t *testing.T) {
51
52
},
52
53
},
53
54
},
54
- wantHasChanges : false ,
55
- wantPatch : []byte ("{}" ),
55
+ wantHasChanges : false ,
56
+ wantHasSpecChanges : false ,
57
+ wantPatch : []byte ("{}" ),
56
58
},
57
59
{
58
60
name : "Field both in original and in modified, align to modified when different" ,
@@ -70,8 +72,33 @@ func TestNewHelper(t *testing.T) {
70
72
},
71
73
},
72
74
},
73
- wantHasChanges : true ,
74
- wantPatch : []byte ("{\" spec\" :{\" foo\" :\" bar\" }}" ),
75
+ wantHasChanges : true ,
76
+ wantHasSpecChanges : true ,
77
+ wantPatch : []byte ("{\" spec\" :{\" foo\" :\" bar\" }}" ),
78
+ },
79
+ {
80
+ name : "Field (metadata.label) both in original and in modified, align to modified when different" ,
81
+ original : & unstructured.Unstructured { // current
82
+ Object : map [string ]interface {}{
83
+ "metadata" : map [string ]interface {}{
84
+ "labels" : map [string ]interface {}{
85
+ "foo" : "bar-changed" ,
86
+ },
87
+ },
88
+ },
89
+ },
90
+ modified : & unstructured.Unstructured { // desired
91
+ Object : map [string ]interface {}{
92
+ "metadata" : map [string ]interface {}{
93
+ "labels" : map [string ]interface {}{
94
+ "foo" : "bar" ,
95
+ },
96
+ },
97
+ },
98
+ },
99
+ wantHasChanges : true ,
100
+ wantHasSpecChanges : false ,
101
+ wantPatch : []byte ("{\" metadata\" :{\" labels\" :{\" foo\" :\" bar\" }}}" ),
75
102
},
76
103
{
77
104
name : "Nested field both in original and in modified, no-op when equal" ,
@@ -97,8 +124,9 @@ func TestNewHelper(t *testing.T) {
97
124
},
98
125
},
99
126
},
100
- wantHasChanges : false ,
101
- wantPatch : []byte ("{}" ),
127
+ wantHasChanges : false ,
128
+ wantHasSpecChanges : false ,
129
+ wantPatch : []byte ("{}" ),
102
130
},
103
131
{
104
132
name : "Nested field both in original and in modified, align to modified when different" ,
@@ -124,8 +152,9 @@ func TestNewHelper(t *testing.T) {
124
152
},
125
153
},
126
154
},
127
- wantHasChanges : true ,
128
- wantPatch : []byte ("{\" spec\" :{\" template\" :{\" spec\" :{\" A\" :\" A\" }}}}" ),
155
+ wantHasChanges : true ,
156
+ wantHasSpecChanges : true ,
157
+ wantPatch : []byte ("{\" spec\" :{\" template\" :{\" spec\" :{\" A\" :\" A\" }}}}" ),
129
158
},
130
159
{
131
160
name : "Value of type map, enforces entries from modified, preserve entries only in original" ,
@@ -151,8 +180,9 @@ func TestNewHelper(t *testing.T) {
151
180
},
152
181
},
153
182
},
154
- wantHasChanges : true ,
155
- wantPatch : []byte ("{\" spec\" :{\" map\" :{\" A\" :\" A\" ,\" C\" :\" C\" }}}" ),
183
+ wantHasChanges : true ,
184
+ wantHasSpecChanges : true ,
185
+ wantPatch : []byte ("{\" spec\" :{\" map\" :{\" A\" :\" A\" ,\" C\" :\" C\" }}}" ),
156
186
},
157
187
{
158
188
name : "Value of type Array or Slice, align to modified" ,
@@ -178,8 +208,9 @@ func TestNewHelper(t *testing.T) {
178
208
},
179
209
},
180
210
},
181
- wantHasChanges : true ,
182
- wantPatch : []byte ("{\" spec\" :{\" slice\" :[\" A\" ,\" B\" ,\" C\" ]}}" ),
211
+ wantHasChanges : true ,
212
+ wantHasSpecChanges : true ,
213
+ wantPatch : []byte ("{\" spec\" :{\" slice\" :[\" A\" ,\" B\" ,\" C\" ]}}" ),
183
214
},
184
215
185
216
// Field only in modified (not existing in original) --> align to modified
@@ -196,8 +227,9 @@ func TestNewHelper(t *testing.T) {
196
227
},
197
228
},
198
229
},
199
- wantHasChanges : true ,
200
- wantPatch : []byte ("{\" spec\" :{\" foo\" :\" bar\" }}" ),
230
+ wantHasChanges : true ,
231
+ wantHasSpecChanges : true ,
232
+ wantPatch : []byte ("{\" spec\" :{\" foo\" :\" bar\" }}" ),
201
233
},
202
234
{
203
235
name : "Nested field only in modified, align to modified" ,
@@ -215,8 +247,9 @@ func TestNewHelper(t *testing.T) {
215
247
},
216
248
},
217
249
},
218
- wantHasChanges : true ,
219
- wantPatch : []byte ("{\" spec\" :{\" template\" :{\" spec\" :{\" A\" :\" A\" }}}}" ),
250
+ wantHasChanges : true ,
251
+ wantHasSpecChanges : true ,
252
+ wantPatch : []byte ("{\" spec\" :{\" template\" :{\" spec\" :{\" A\" :\" A\" }}}}" ),
220
253
},
221
254
222
255
// Field only in original (not existing in modified) --> preserve original
@@ -233,8 +266,9 @@ func TestNewHelper(t *testing.T) {
233
266
modified : & unstructured.Unstructured { // desired
234
267
Object : map [string ]interface {}{},
235
268
},
236
- wantHasChanges : false ,
237
- wantPatch : []byte ("{}" ),
269
+ wantHasChanges : false ,
270
+ wantHasSpecChanges : false ,
271
+ wantPatch : []byte ("{}" ),
238
272
},
239
273
{
240
274
name : "Nested field only in original, align to modified" ,
@@ -252,8 +286,9 @@ func TestNewHelper(t *testing.T) {
252
286
modified : & unstructured.Unstructured { // desired
253
287
Object : map [string ]interface {}{},
254
288
},
255
- wantHasChanges : false ,
256
- wantPatch : []byte ("{}" ),
289
+ wantHasChanges : false ,
290
+ wantHasSpecChanges : false ,
291
+ wantPatch : []byte ("{}" ),
257
292
},
258
293
259
294
// Diff for metadata fields computed by the system or in status are discarded
@@ -277,11 +312,12 @@ func TestNewHelper(t *testing.T) {
277
312
},
278
313
},
279
314
},
280
- wantHasChanges : false ,
281
- wantPatch : []byte ("{}" ),
315
+ wantHasChanges : false ,
316
+ wantHasSpecChanges : false ,
317
+ wantPatch : []byte ("{}" ),
282
318
},
283
319
{
284
- name : "Relevant Diff are preserved" ,
320
+ name : "Relevant Diff for metadata (labels and annotations) are preserved" ,
285
321
original : & unstructured.Unstructured { // current
286
322
Object : map [string ]interface {}{},
287
323
},
@@ -297,8 +333,9 @@ func TestNewHelper(t *testing.T) {
297
333
},
298
334
},
299
335
},
300
- wantHasChanges : true ,
301
- wantPatch : []byte ("{\" metadata\" :{\" annotations\" :{\" foo\" :\" bar\" },\" labels\" :{\" foo\" :\" bar\" }}}" ),
336
+ wantHasChanges : true ,
337
+ wantHasSpecChanges : false ,
338
+ wantPatch : []byte ("{\" metadata\" :{\" annotations\" :{\" foo\" :\" bar\" },\" labels\" :{\" foo\" :\" bar\" }}}" ),
302
339
},
303
340
304
341
// Ignore fields
@@ -318,9 +355,10 @@ func TestNewHelper(t *testing.T) {
318
355
},
319
356
},
320
357
},
321
- options : []HelperOption {IgnorePaths {contract.Path {"spec" , "controlPlaneEndpoint" }}},
322
- wantHasChanges : false ,
323
- wantPatch : []byte ("{}" ),
358
+ options : []HelperOption {IgnorePaths {contract.Path {"spec" , "controlPlaneEndpoint" }}},
359
+ wantHasChanges : false ,
360
+ wantHasSpecChanges : false ,
361
+ wantPatch : []byte ("{}" ),
324
362
},
325
363
326
364
// More tests
@@ -343,8 +381,9 @@ func TestNewHelper(t *testing.T) {
343
381
},
344
382
},
345
383
},
346
- wantHasChanges : false ,
347
- wantPatch : []byte ("{}" ),
384
+ wantHasChanges : false ,
385
+ wantHasSpecChanges : false ,
386
+ wantPatch : []byte ("{}" ),
348
387
},
349
388
{
350
389
name : "Many changes" ,
@@ -365,8 +404,9 @@ func TestNewHelper(t *testing.T) {
365
404
},
366
405
},
367
406
},
368
- wantHasChanges : true ,
369
- wantPatch : []byte ("{\" spec\" :{\" B\" :\" B\" }}" ),
407
+ wantHasChanges : true ,
408
+ wantHasSpecChanges : true ,
409
+ wantPatch : []byte ("{\" spec\" :{\" B\" :\" B\" }}" ),
370
410
},
371
411
}
372
412
for _ , tt := range tests {
@@ -377,6 +417,7 @@ func TestNewHelper(t *testing.T) {
377
417
g .Expect (err ).ToNot (HaveOccurred ())
378
418
379
419
g .Expect (patch .HasChanges ()).To (Equal (tt .wantHasChanges ))
420
+ g .Expect (patch .HasSpecChanges ()).To (Equal (tt .wantHasSpecChanges ))
380
421
g .Expect (patch .patch ).To (Equal (tt .wantPatch ))
381
422
})
382
423
}
0 commit comments