@@ -17,10 +17,12 @@ limitations under the License.
17
17
package pod
18
18
19
19
import (
20
+ "fmt"
20
21
"reflect"
21
22
"strings"
22
23
"testing"
23
24
25
+ "k8s.io/apimachinery/pkg/util/diff"
24
26
"k8s.io/apimachinery/pkg/util/sets"
25
27
"k8s.io/apimachinery/pkg/util/validation/field"
26
28
utilfeature "k8s.io/apiserver/pkg/util/feature"
@@ -307,7 +309,7 @@ func TestDropAlphaVolumeDevices(t *testing.T) {
307
309
defer utilfeaturetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .BlockVolume , true )()
308
310
309
311
// now test dropping the fields - should not be dropped
310
- DropDisabledAlphaFields (& testPod .Spec )
312
+ DropDisabledFields (& testPod .Spec , nil )
311
313
312
314
// check to make sure VolumeDevices is still present
313
315
// if featureset is set to true
@@ -322,14 +324,108 @@ func TestDropAlphaVolumeDevices(t *testing.T) {
322
324
defer utilfeaturetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .BlockVolume , false )()
323
325
324
326
// now test dropping the fields
325
- DropDisabledAlphaFields (& testPod .Spec )
327
+ DropDisabledFields (& testPod .Spec , nil )
326
328
327
329
// check to make sure VolumeDevices is nil
328
330
// if featureset is set to false
329
331
if testPod .Spec .Containers [0 ].VolumeDevices != nil {
330
- t .Error ("DropDisabledAlphaFields for Containers failed" )
332
+ t .Error ("DropDisabledFields for Containers failed" )
331
333
}
332
334
if testPod .Spec .InitContainers [0 ].VolumeDevices != nil {
333
- t .Error ("DropDisabledAlphaFields for InitContainers failed" )
335
+ t .Error ("DropDisabledFields for InitContainers failed" )
336
+ }
337
+ }
338
+
339
+ func TestDropSubPath (t * testing.T ) {
340
+ podWithSubpaths := func () * api.Pod {
341
+ return & api.Pod {
342
+ Spec : api.PodSpec {
343
+ RestartPolicy : api .RestartPolicyNever ,
344
+ Containers : []api.Container {{Name : "container1" , Image : "testimage" , VolumeMounts : []api.VolumeMount {{Name : "a" , SubPath : "foo" }, {Name : "a" , SubPath : "foo2" }, {Name : "a" , SubPath : "foo3" }}}},
345
+ InitContainers : []api.Container {{Name : "container1" , Image : "testimage" , VolumeMounts : []api.VolumeMount {{Name : "a" , SubPath : "foo" }, {Name : "a" , SubPath : "foo2" }}}},
346
+ Volumes : []api.Volume {{Name : "a" , VolumeSource : api.VolumeSource {HostPath : & api.HostPathVolumeSource {Path : "/dev/xvdc" }}}},
347
+ },
348
+ }
349
+ }
350
+ podWithoutSubpaths := func () * api.Pod {
351
+ return & api.Pod {
352
+ Spec : api.PodSpec {
353
+ RestartPolicy : api .RestartPolicyNever ,
354
+ Containers : []api.Container {{Name : "container1" , Image : "testimage" , VolumeMounts : []api.VolumeMount {{Name : "a" , SubPath : "" }, {Name : "a" , SubPath : "" }, {Name : "a" , SubPath : "" }}}},
355
+ InitContainers : []api.Container {{Name : "container1" , Image : "testimage" , VolumeMounts : []api.VolumeMount {{Name : "a" , SubPath : "" }, {Name : "a" , SubPath : "" }}}},
356
+ Volumes : []api.Volume {{Name : "a" , VolumeSource : api.VolumeSource {HostPath : & api.HostPathVolumeSource {Path : "/dev/xvdc" }}}},
357
+ },
358
+ }
359
+ }
360
+
361
+ podInfo := []struct {
362
+ description string
363
+ hasSubpaths bool
364
+ pod func () * api.Pod
365
+ }{
366
+ {
367
+ description : "has subpaths" ,
368
+ hasSubpaths : true ,
369
+ pod : podWithSubpaths ,
370
+ },
371
+ {
372
+ description : "does not have subpaths" ,
373
+ hasSubpaths : false ,
374
+ pod : podWithoutSubpaths ,
375
+ },
376
+ {
377
+ description : "is nil" ,
378
+ hasSubpaths : false ,
379
+ pod : func () * api.Pod { return nil },
380
+ },
381
+ }
382
+
383
+ for _ , enabled := range []bool {true , false } {
384
+ for _ , oldPodInfo := range podInfo {
385
+ for _ , newPodInfo := range podInfo {
386
+ oldPodHasSubpaths , oldPod := oldPodInfo .hasSubpaths , oldPodInfo .pod ()
387
+ newPodHasSubpaths , newPod := newPodInfo .hasSubpaths , newPodInfo .pod ()
388
+ if newPod == nil {
389
+ continue
390
+ }
391
+
392
+ t .Run (fmt .Sprintf ("feature enabled=%v, old pod %v, new pod %v" , enabled , oldPodInfo .description , newPodInfo .description ), func (t * testing.T ) {
393
+ defer utilfeaturetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .VolumeSubpath , enabled )()
394
+
395
+ var oldPodSpec * api.PodSpec
396
+ if oldPod != nil {
397
+ oldPodSpec = & oldPod .Spec
398
+ }
399
+ DropDisabledFields (& newPod .Spec , oldPodSpec )
400
+
401
+ // old pod should never be changed
402
+ if ! reflect .DeepEqual (oldPod , oldPodInfo .pod ()) {
403
+ t .Errorf ("old pod changed: %v" , diff .ObjectReflectDiff (oldPod , oldPodInfo .pod ()))
404
+ }
405
+
406
+ switch {
407
+ case enabled || oldPodHasSubpaths :
408
+ // new pod should not be changed if the feature is enabled, or if the old pod had subpaths
409
+ if ! reflect .DeepEqual (newPod , newPodInfo .pod ()) {
410
+ t .Errorf ("new pod changed: %v" , diff .ObjectReflectDiff (newPod , newPodInfo .pod ()))
411
+ }
412
+ case newPodHasSubpaths :
413
+ // new pod should be changed
414
+ if reflect .DeepEqual (newPod , newPodInfo .pod ()) {
415
+ t .Errorf ("new pod was not changed" )
416
+ }
417
+ // new pod should not have subpaths
418
+ if ! reflect .DeepEqual (newPod , podWithoutSubpaths ()) {
419
+ t .Errorf ("new pod had subpaths: %v" , diff .ObjectReflectDiff (newPod , podWithoutSubpaths ()))
420
+ }
421
+ default :
422
+ // new pod should not need to be changed
423
+ if ! reflect .DeepEqual (newPod , newPodInfo .pod ()) {
424
+ t .Errorf ("new pod changed: %v" , diff .ObjectReflectDiff (newPod , newPodInfo .pod ()))
425
+ }
426
+ }
427
+ })
428
+ }
429
+ }
334
430
}
335
431
}
0 commit comments