k8s.io/kubernetes@v1.29.3/pkg/apis/storage/v1/defaults_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package v1_test 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 storagev1 "k8s.io/api/storage/v1" 25 "k8s.io/apimachinery/pkg/runtime" 26 utilfeature "k8s.io/apiserver/pkg/util/feature" 27 featuregatetesting "k8s.io/component-base/featuregate/testing" 28 "k8s.io/kubernetes/pkg/api/legacyscheme" 29 _ "k8s.io/kubernetes/pkg/apis/storage/install" 30 "k8s.io/kubernetes/pkg/features" 31 ) 32 33 func roundTrip(t *testing.T, obj runtime.Object) runtime.Object { 34 codec := legacyscheme.Codecs.LegacyCodec(storagev1.SchemeGroupVersion) 35 data, err := runtime.Encode(codec, obj) 36 if err != nil { 37 t.Errorf("%v\n %#v", err, obj) 38 return nil 39 } 40 obj2, err := runtime.Decode(codec, data) 41 if err != nil { 42 t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj) 43 return nil 44 } 45 obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object) 46 err = legacyscheme.Scheme.Convert(obj2, obj3, nil) 47 if err != nil { 48 t.Errorf("%v\nSource: %#v", err, obj2) 49 return nil 50 } 51 return obj3 52 } 53 54 func TestSetDefaultStorageCapacityEnabled(t *testing.T) { 55 driver := &storagev1.CSIDriver{} 56 57 // field should be defaulted 58 defaultStorageCapacity := false 59 output := roundTrip(t, runtime.Object(driver)).(*storagev1.CSIDriver) 60 outStorageCapacity := output.Spec.StorageCapacity 61 if outStorageCapacity == nil { 62 t.Errorf("Expected StorageCapacity to be defaulted to: %+v, got: nil", defaultStorageCapacity) 63 } else if *outStorageCapacity != defaultStorageCapacity { 64 t.Errorf("Expected StorageCapacity to be defaulted to: %+v, got: %+v", defaultStorageCapacity, outStorageCapacity) 65 } 66 } 67 68 func TestSetDefaultVolumeBindingMode(t *testing.T) { 69 class := &storagev1.StorageClass{} 70 71 // field should be defaulted 72 defaultMode := storagev1.VolumeBindingImmediate 73 output := roundTrip(t, runtime.Object(class)).(*storagev1.StorageClass) 74 outMode := output.VolumeBindingMode 75 if outMode == nil { 76 t.Errorf("Expected VolumeBindingMode to be defaulted to: %+v, got: nil", defaultMode) 77 } else if *outMode != defaultMode { 78 t.Errorf("Expected VolumeBindingMode to be defaulted to: %+v, got: %+v", defaultMode, outMode) 79 } 80 } 81 82 func TestSetDefaultCSIDriver(t *testing.T) { 83 enabled := true 84 disabled := false 85 tests := []struct { 86 desc string 87 field string 88 wantSpec *storagev1.CSIDriverSpec 89 }{ 90 { 91 desc: "AttachRequired default to true", 92 field: "AttachRequired", 93 wantSpec: &storagev1.CSIDriverSpec{AttachRequired: &enabled}, 94 }, 95 { 96 desc: "PodInfoOnMount default to false", 97 field: "PodInfoOnMount", 98 wantSpec: &storagev1.CSIDriverSpec{PodInfoOnMount: &disabled}, 99 }, 100 { 101 desc: "VolumeLifecycleModes default to VolumeLifecyclePersistent", 102 field: "VolumeLifecycleModes", 103 wantSpec: &storagev1.CSIDriverSpec{VolumeLifecycleModes: []storagev1.VolumeLifecycleMode{storagev1.VolumeLifecyclePersistent}}, 104 }, 105 { 106 desc: "RequiresRepublish default to false", 107 field: "RequiresRepublish", 108 wantSpec: &storagev1.CSIDriverSpec{RequiresRepublish: &disabled}, 109 }, 110 } 111 112 for _, test := range tests { 113 t.Run(test.desc, func(t *testing.T) { 114 gotSpec := roundTrip(t, runtime.Object(&storagev1.CSIDriver{})).(*storagev1.CSIDriver).Spec 115 got := reflect.Indirect(reflect.ValueOf(gotSpec)).FieldByName(test.field).Interface() 116 want := reflect.Indirect(reflect.ValueOf(test.wantSpec)).FieldByName(test.field).Interface() 117 if diff := cmp.Diff(want, got); diff != "" { 118 t.Errorf("CSIDriver defaults diff (-want +got):\n%s", diff) 119 } 120 }) 121 } 122 } 123 124 func TestSetDefaultSELinuxMountReadWriteOncePodEnabled(t *testing.T) { 125 defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.SELinuxMountReadWriteOncePod, true)() 126 driver := &storagev1.CSIDriver{} 127 128 // field should be defaulted 129 defaultSELinuxMount := false 130 output := roundTrip(t, runtime.Object(driver)).(*storagev1.CSIDriver) 131 outSELinuxMount := output.Spec.SELinuxMount 132 if outSELinuxMount == nil { 133 t.Errorf("Expected SELinuxMount to be defaulted to: %+v, got: nil", defaultSELinuxMount) 134 } else if *outSELinuxMount != defaultSELinuxMount { 135 t.Errorf("Expected SELinuxMount to be defaulted to: %+v, got: %+v", defaultSELinuxMount, outSELinuxMount) 136 } 137 } 138 139 func TestSetDefaultSELinuxMountReadWriteOncePodDisabled(t *testing.T) { 140 defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.SELinuxMountReadWriteOncePod, false)() 141 driver := &storagev1.CSIDriver{} 142 143 // field should not be defaulted 144 output := roundTrip(t, runtime.Object(driver)).(*storagev1.CSIDriver) 145 outSELinuxMount := output.Spec.SELinuxMount 146 if outSELinuxMount != nil { 147 t.Errorf("Expected SELinuxMount to remain nil, got: %+v", outSELinuxMount) 148 } 149 }