k8s.io/kubernetes@v1.29.3/pkg/volume/util/volumeattributesclass_test.go (about) 1 /* 2 Copyright 2023 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 util 18 19 import ( 20 "testing" 21 "time" 22 23 storagev1alpha1 "k8s.io/api/storage/v1alpha1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/client-go/informers" 26 "k8s.io/kubernetes/pkg/controller" 27 ) 28 29 func TestGetDefaultVolumeAttributesClass(t *testing.T) { 30 var ( 31 t1 = time.Now() 32 t2 = time.Now().Add(1 * time.Hour) 33 ) 34 35 dirverName1 := "my-driver1" 36 vac1 := &storagev1alpha1.VolumeAttributesClass{ 37 ObjectMeta: metav1.ObjectMeta{ 38 Name: "my-vac1", 39 Annotations: map[string]string{ 40 "a": "b", 41 }, 42 }, 43 DriverName: dirverName1, 44 } 45 vac2 := &storagev1alpha1.VolumeAttributesClass{ 46 ObjectMeta: metav1.ObjectMeta{ 47 Name: "my-vac2", 48 Annotations: map[string]string{ 49 "a": "b", 50 }, 51 }, 52 DriverName: dirverName1, 53 } 54 vac3 := &storagev1alpha1.VolumeAttributesClass{ 55 ObjectMeta: metav1.ObjectMeta{ 56 Name: "my-vac3", 57 Annotations: map[string]string{ 58 AlphaIsDefaultVolumeAttributesClassAnnotation: "true", 59 }, 60 CreationTimestamp: metav1.Time{Time: t1}, 61 }, 62 DriverName: dirverName1, 63 } 64 vac4 := &storagev1alpha1.VolumeAttributesClass{ 65 ObjectMeta: metav1.ObjectMeta{ 66 Name: "my-vac4", 67 Annotations: map[string]string{ 68 AlphaIsDefaultVolumeAttributesClassAnnotation: "true", 69 }, 70 CreationTimestamp: metav1.Time{Time: t2}, 71 }, 72 DriverName: dirverName1, 73 } 74 vac5 := &storagev1alpha1.VolumeAttributesClass{ 75 ObjectMeta: metav1.ObjectMeta{ 76 Name: "my-vac5", 77 Annotations: map[string]string{ 78 AlphaIsDefaultVolumeAttributesClassAnnotation: "true", 79 }, 80 CreationTimestamp: metav1.Time{Time: t2}, 81 }, 82 DriverName: dirverName1, 83 } 84 85 dirverName2 := "my-driver2" 86 vac6 := &storagev1alpha1.VolumeAttributesClass{ 87 ObjectMeta: metav1.ObjectMeta{ 88 Name: "my-vac6", 89 Annotations: map[string]string{ 90 "a": "b", 91 }, 92 }, 93 DriverName: dirverName2, 94 } 95 vac7 := &storagev1alpha1.VolumeAttributesClass{ 96 ObjectMeta: metav1.ObjectMeta{ 97 Name: "my-vac7", 98 Annotations: map[string]string{ 99 AlphaIsDefaultVolumeAttributesClassAnnotation: "true", 100 }, 101 }, 102 DriverName: dirverName2, 103 } 104 105 testCases := []struct { 106 name string 107 driverName string 108 classes []*storagev1alpha1.VolumeAttributesClass 109 expect *storagev1alpha1.VolumeAttributesClass 110 }{ 111 { 112 name: "no volume attributes class", 113 driverName: dirverName1, 114 }, 115 { 116 name: "no default volume attributes class", 117 driverName: dirverName1, 118 classes: []*storagev1alpha1.VolumeAttributesClass{vac1, vac2, vac6}, 119 expect: nil, 120 }, 121 { 122 name: "no default volume attributes class for the driverName1", 123 driverName: dirverName1, 124 classes: []*storagev1alpha1.VolumeAttributesClass{vac1, vac2, vac6, vac7}, 125 expect: nil, 126 }, 127 { 128 name: "one default volume attributes class for the driverName1", 129 driverName: dirverName1, 130 classes: []*storagev1alpha1.VolumeAttributesClass{vac1, vac2, vac3, vac6, vac7}, 131 expect: vac3, 132 }, 133 { 134 name: "two default volume attributes class with different creation timestamp for the driverName1", 135 driverName: dirverName1, 136 classes: []*storagev1alpha1.VolumeAttributesClass{vac3, vac4, vac6, vac7}, 137 expect: vac4, 138 }, 139 { 140 name: "two default volume attributes class with same creation timestamp for the driverName1", 141 driverName: dirverName1, 142 classes: []*storagev1alpha1.VolumeAttributesClass{vac4, vac5, vac6, vac7}, 143 expect: vac4, 144 }, 145 } 146 147 for _, tc := range testCases { 148 t.Run(tc.name, func(t *testing.T) { 149 informerFactory := informers.NewSharedInformerFactory(nil, controller.NoResyncPeriodFunc()) 150 for _, c := range tc.classes { 151 err := informerFactory.Storage().V1alpha1().VolumeAttributesClasses().Informer().GetStore().Add(c) 152 if err != nil { 153 t.Errorf("Expected no error, got %v", err) 154 return 155 } 156 } 157 lister := informerFactory.Storage().V1alpha1().VolumeAttributesClasses().Lister() 158 actual, err := GetDefaultVolumeAttributesClass(lister, tc.driverName) 159 if err != nil { 160 t.Errorf("Expected no error, got %v", err) 161 return 162 } 163 if tc.expect != actual { 164 t.Errorf("Expected %v, got %v", tc.expect, actual) 165 } 166 }) 167 } 168 } 169 170 func TestIsDefaultVolumeAttributesClassAnnotation(t *testing.T) { 171 testCases := []struct { 172 name string 173 class *storagev1alpha1.VolumeAttributesClass 174 expect bool 175 }{ 176 { 177 name: "no annotation", 178 class: &storagev1alpha1.VolumeAttributesClass{}, 179 expect: false, 180 }, 181 { 182 name: "annotation is not boolean", 183 class: &storagev1alpha1.VolumeAttributesClass{ 184 ObjectMeta: metav1.ObjectMeta{ 185 Annotations: map[string]string{ 186 AlphaIsDefaultVolumeAttributesClassAnnotation: "not-boolean", 187 }, 188 }, 189 }, 190 expect: false, 191 }, 192 { 193 name: "annotation is false", 194 class: &storagev1alpha1.VolumeAttributesClass{ 195 ObjectMeta: metav1.ObjectMeta{ 196 Annotations: map[string]string{ 197 AlphaIsDefaultVolumeAttributesClassAnnotation: "false", 198 }, 199 }, 200 }, 201 expect: false, 202 }, 203 { 204 name: "annotation is true", 205 class: &storagev1alpha1.VolumeAttributesClass{ 206 ObjectMeta: metav1.ObjectMeta{ 207 Annotations: map[string]string{ 208 AlphaIsDefaultVolumeAttributesClassAnnotation: "true", 209 }, 210 }, 211 }, 212 expect: true, 213 }, 214 } 215 216 for _, tc := range testCases { 217 t.Run(tc.name, func(t *testing.T) { 218 actual := IsDefaultVolumeAttributesClassAnnotation(tc.class.ObjectMeta) 219 if tc.expect != actual { 220 t.Errorf("Expected %v, got %v", tc.expect, actual) 221 } 222 }) 223 } 224 }