k8s.io/kubernetes@v1.29.3/pkg/apis/storage/v1beta1/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 v1beta1_test 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 storagev1beta1 "k8s.io/api/storage/v1beta1" 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(storagev1beta1.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 TestSetDefaultVolumeBindingMode(t *testing.T) { 55 class := &storagev1beta1.StorageClass{} 56 57 // field should be defaulted 58 defaultMode := storagev1beta1.VolumeBindingImmediate 59 output := roundTrip(t, runtime.Object(class)).(*storagev1beta1.StorageClass) 60 outMode := output.VolumeBindingMode 61 if outMode == nil { 62 t.Errorf("Expected VolumeBindingMode to be defaulted to: %+v, got: nil", defaultMode) 63 } else if *outMode != defaultMode { 64 t.Errorf("Expected VolumeBindingMode to be defaulted to: %+v, got: %+v", defaultMode, outMode) 65 } 66 } 67 68 func TestSetDefaultAttachRequired(t *testing.T) { 69 driver := &storagev1beta1.CSIDriver{} 70 71 // field should be defaulted 72 defaultAttach := true 73 defaultPodInfo := false 74 output := roundTrip(t, runtime.Object(driver)).(*storagev1beta1.CSIDriver) 75 outAttach := output.Spec.AttachRequired 76 if outAttach == nil { 77 t.Errorf("Expected AttachRequired to be defaulted to: %+v, got: nil", defaultAttach) 78 } else if *outAttach != defaultAttach { 79 t.Errorf("Expected AttachRequired to be defaulted to: %+v, got: %+v", defaultAttach, outAttach) 80 } 81 outPodInfo := output.Spec.PodInfoOnMount 82 if outPodInfo == nil { 83 t.Errorf("Expected PodInfoOnMount to be defaulted to: %+v, got: nil", defaultPodInfo) 84 } else if *outPodInfo != defaultPodInfo { 85 t.Errorf("Expected PodInfoOnMount to be defaulted to: %+v, got: %+v", defaultPodInfo, outPodInfo) 86 } 87 } 88 89 func TestSetDefaultStorageCapacityEnabled(t *testing.T) { 90 driver := &storagev1beta1.CSIDriver{} 91 92 // field should be defaulted 93 defaultStorageCapacity := false 94 output := roundTrip(t, runtime.Object(driver)).(*storagev1beta1.CSIDriver) 95 outStorageCapacity := output.Spec.StorageCapacity 96 if outStorageCapacity == nil { 97 t.Errorf("Expected StorageCapacity to be defaulted to: %+v, got: nil", defaultStorageCapacity) 98 } else if *outStorageCapacity != defaultStorageCapacity { 99 t.Errorf("Expected StorageCapacity to be defaulted to: %+v, got: %+v", defaultStorageCapacity, outStorageCapacity) 100 } 101 } 102 103 func TestSetDefaultVolumeLifecycleModesEnabled(t *testing.T) { 104 driver := &storagev1beta1.CSIDriver{} 105 106 // field should be defaulted 107 defaultMode := storagev1beta1.VolumeLifecyclePersistent 108 output := roundTrip(t, runtime.Object(driver)).(*storagev1beta1.CSIDriver) 109 outModes := output.Spec.VolumeLifecycleModes 110 if len(outModes) != 1 { 111 t.Errorf("Expected VolumeLifecycleModes to be defaulted to: %+v, got: %+v", defaultMode, outModes) 112 } else if outModes[0] != defaultMode { 113 t.Errorf("Expected VolumeLifecycleModes to be defaulted to: %+v, got: %+v", defaultMode, outModes) 114 } 115 } 116 117 func TestSetDefaultCSIDriver(t *testing.T) { 118 enabled := true 119 disabled := false 120 tests := []struct { 121 desc string 122 field string 123 wantSpec *storagev1beta1.CSIDriverSpec 124 }{ 125 { 126 desc: "AttachRequired default to true", 127 field: "AttachRequired", 128 wantSpec: &storagev1beta1.CSIDriverSpec{AttachRequired: &enabled}, 129 }, 130 { 131 desc: "PodInfoOnMount default to false", 132 field: "PodInfoOnMount", 133 wantSpec: &storagev1beta1.CSIDriverSpec{PodInfoOnMount: &disabled}, 134 }, 135 { 136 desc: "RequiresRepublish default to false", 137 field: "RequiresRepublish", 138 wantSpec: &storagev1beta1.CSIDriverSpec{RequiresRepublish: &disabled}, 139 }, 140 } 141 142 for _, test := range tests { 143 t.Run(test.desc, func(t *testing.T) { 144 gotSpec := roundTrip(t, runtime.Object(&storagev1beta1.CSIDriver{})).(*storagev1beta1.CSIDriver).Spec 145 got := reflect.Indirect(reflect.ValueOf(gotSpec)).FieldByName(test.field).Interface() 146 want := reflect.Indirect(reflect.ValueOf(test.wantSpec)).FieldByName(test.field).Interface() 147 if diff := cmp.Diff(want, got); diff != "" { 148 t.Errorf("CSIDriver defaults diff (-want +got):\n%s", diff) 149 } 150 }) 151 } 152 } 153 154 func TestSetDefaultSELinuxMountReadWriteOncePodEnabled(t *testing.T) { 155 defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.SELinuxMountReadWriteOncePod, true)() 156 driver := &storagev1beta1.CSIDriver{} 157 158 // field should be defaulted 159 defaultSELinuxMount := false 160 output := roundTrip(t, runtime.Object(driver)).(*storagev1beta1.CSIDriver) 161 outSELinuxMount := output.Spec.SELinuxMount 162 if outSELinuxMount == nil { 163 t.Errorf("Expected SELinuxMount to be defaulted to: %+v, got: nil", defaultSELinuxMount) 164 } else if *outSELinuxMount != defaultSELinuxMount { 165 t.Errorf("Expected SELinuxMount to be defaulted to: %+v, got: %+v", defaultSELinuxMount, outSELinuxMount) 166 } 167 } 168 169 func TestSetDefaultSELinuxMountReadWriteOncePodDisabled(t *testing.T) { 170 defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.SELinuxMountReadWriteOncePod, false)() 171 driver := &storagev1beta1.CSIDriver{} 172 173 // field should not be defaulted 174 output := roundTrip(t, runtime.Object(driver)).(*storagev1beta1.CSIDriver) 175 outSELinuxMount := output.Spec.SELinuxMount 176 if outSELinuxMount != nil { 177 t.Errorf("Expected SELinuxMount remain nil, got: %+v", outSELinuxMount) 178 } 179 }