@@ -20,6 +20,7 @@ import (
20
20
"reflect"
21
21
"testing"
22
22
23
+ "github.com/mohae/deepcopy"
23
24
specs "github.com/opencontainers/runtime-spec/specs-go"
24
25
)
25
26
@@ -52,11 +53,10 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
52
53
}
53
54
54
55
for _ , test := range []struct {
55
- name string
56
- spec * specs.Spec
57
- expected * specs.Spec
58
- expectErr bool
59
- expectUpdate bool
56
+ name string
57
+ spec * specs.Spec
58
+ expected * specs.Spec // If nil, the spec is expected to be unchanged.
59
+ expectErr bool
60
60
}{
61
61
{
62
62
name : "volume annotations for sandbox" ,
@@ -79,7 +79,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
79
79
volumeKeyPrefix + testVolumeName + ".source" : testVolumePath ,
80
80
},
81
81
},
82
- expectUpdate : true ,
83
82
},
84
83
{
85
84
name : "volume annotations for sandbox with legacy log path" ,
@@ -102,7 +101,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
102
101
volumeKeyPrefix + testVolumeName + ".source" : testVolumePath ,
103
102
},
104
103
},
105
- expectUpdate : true ,
106
104
},
107
105
{
108
106
name : "tmpfs: volume annotations for container" ,
@@ -150,7 +148,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
150
148
volumeKeyPrefix + testVolumeName + ".options" : "ro" ,
151
149
},
152
150
},
153
- expectUpdate : true ,
154
151
},
155
152
{
156
153
name : "non-empty volume for sandbox" ,
@@ -173,7 +170,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
173
170
volumeKeyPrefix + testNonEmptyVolumeName + ".source" : testNonEmptyVolumePath ,
174
171
},
175
172
},
176
- expectUpdate : true ,
177
173
},
178
174
{
179
175
name : "non-empty volume for container" ,
@@ -193,22 +189,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
193
189
volumeKeyPrefix + testNonEmptyVolumeName + ".options" : "ro" ,
194
190
},
195
191
},
196
- expected : & specs.Spec {
197
- Mounts : []specs.Mount {
198
- {
199
- Destination : "/test" ,
200
- Type : "bind" ,
201
- Source : testNonEmptyVolumePath ,
202
- Options : []string {"ro" },
203
- },
204
- },
205
- Annotations : map [string ]string {
206
- ContainerTypeAnnotation : ContainerTypeContainer ,
207
- volumeKeyPrefix + testNonEmptyVolumeName + ".share" : "pod" ,
208
- volumeKeyPrefix + testNonEmptyVolumeName + ".type" : "tmpfs" ,
209
- volumeKeyPrefix + testNonEmptyVolumeName + ".options" : "ro" ,
210
- },
211
- },
212
192
},
213
193
{
214
194
name : "bind: volume annotations for sandbox" ,
@@ -231,7 +211,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
231
211
volumeKeyPrefix + testVolumeName + ".source" : testVolumePath ,
232
212
},
233
213
},
234
- expectUpdate : true ,
235
214
},
236
215
{
237
216
name : "bind: volume annotations for container" ,
@@ -251,23 +230,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
251
230
volumeKeyPrefix + testVolumeName + ".options" : "ro" ,
252
231
},
253
232
},
254
- expected : & specs.Spec {
255
- Mounts : []specs.Mount {
256
- {
257
- Destination : "/test" ,
258
- Type : "bind" ,
259
- Source : testVolumePath ,
260
- Options : []string {"ro" },
261
- },
262
- },
263
- Annotations : map [string ]string {
264
- ContainerTypeAnnotation : ContainerTypeContainer ,
265
- volumeKeyPrefix + testVolumeName + ".share" : "container" ,
266
- volumeKeyPrefix + testVolumeName + ".type" : "bind" ,
267
- volumeKeyPrefix + testVolumeName + ".options" : "ro" ,
268
- },
269
- },
270
- expectUpdate : true ,
271
233
},
272
234
{
273
235
name : "should not return error without pod log directory" ,
@@ -279,14 +241,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
279
241
volumeKeyPrefix + testVolumeName + ".options" : "ro" ,
280
242
},
281
243
},
282
- expected : & specs.Spec {
283
- Annotations : map [string ]string {
284
- ContainerTypeAnnotation : containerTypeSandbox ,
285
- volumeKeyPrefix + testVolumeName + ".share" : "pod" ,
286
- volumeKeyPrefix + testVolumeName + ".type" : "tmpfs" ,
287
- volumeKeyPrefix + testVolumeName + ".options" : "ro" ,
288
- },
289
- },
290
244
},
291
245
{
292
246
name : "should return error if volume path does not exist" ,
@@ -309,12 +263,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
309
263
ContainerTypeAnnotation : containerTypeSandbox ,
310
264
},
311
265
},
312
- expected : & specs.Spec {
313
- Annotations : map [string ]string {
314
- sandboxLogDirAnnotation : testLogDirPath ,
315
- ContainerTypeAnnotation : containerTypeSandbox ,
316
- },
317
- },
318
266
},
319
267
{
320
268
name : "no volume annotations for container" ,
@@ -337,25 +285,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
337
285
ContainerTypeAnnotation : ContainerTypeContainer ,
338
286
},
339
287
},
340
- expected : & specs.Spec {
341
- Mounts : []specs.Mount {
342
- {
343
- Destination : "/test" ,
344
- Type : "bind" ,
345
- Source : "/test" ,
346
- Options : []string {"ro" },
347
- },
348
- {
349
- Destination : "/random" ,
350
- Type : "bind" ,
351
- Source : "/random" ,
352
- Options : []string {"ro" },
353
- },
354
- },
355
- Annotations : map [string ]string {
356
- ContainerTypeAnnotation : ContainerTypeContainer ,
357
- },
358
- },
359
288
},
360
289
{
361
290
name : "bind options removed" ,
@@ -393,7 +322,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
393
322
},
394
323
},
395
324
},
396
- expectUpdate : true ,
397
325
},
398
326
{
399
327
name : "shm-sandbox" ,
@@ -429,7 +357,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
429
357
},
430
358
},
431
359
},
432
- expectUpdate : true ,
433
360
},
434
361
{
435
362
name : "shm-container" ,
@@ -459,7 +386,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
459
386
},
460
387
},
461
388
},
462
- expectUpdate : true ,
463
389
},
464
390
{
465
391
name : "shm-duplicate" ,
@@ -505,10 +431,13 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
505
431
},
506
432
},
507
433
},
508
- expectUpdate : true ,
509
434
},
510
435
} {
511
436
t .Run (test .name , func (t * testing.T ) {
437
+ expectUpdate := test .expected != nil
438
+ if test .expected == nil && ! test .expectErr {
439
+ test .expected = deepcopy .Copy (test .spec ).(* specs.Spec )
440
+ }
512
441
updated , err := UpdateVolumeAnnotations (test .spec )
513
442
if test .expectErr {
514
443
if err == nil {
@@ -519,8 +448,8 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
519
448
if err != nil {
520
449
t .Fatalf ("UpdateVolumeAnnotations(spec): %v" , err )
521
450
}
522
- if test . expectUpdate != updated {
523
- t .Errorf ("want: %v, got: %v" , test . expectUpdate , updated )
451
+ if expectUpdate != updated {
452
+ t .Errorf ("want: %v, got: %v" , expectUpdate , updated )
524
453
}
525
454
if ! reflect .DeepEqual (test .expected , test .spec ) {
526
455
t .Fatalf ("want: %+v, got: %+v" , test .expected , test .spec )
0 commit comments