k8s.io/kubernetes@v1.29.3/pkg/apis/flowcontrol/v1beta3/defaults_test.go (about) 1 /* 2 Copyright 2022 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 v1beta3 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 25 flowcontrolv1beta3 "k8s.io/api/flowcontrol/v1beta3" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/runtime" 28 "k8s.io/utils/pointer" 29 ) 30 31 func TestDefaultWithPriorityLevelConfiguration(t *testing.T) { 32 tests := []struct { 33 name string 34 original runtime.Object 35 expected runtime.Object 36 }{ 37 { 38 name: "Defaulting for Exempt", 39 original: &flowcontrolv1beta3.PriorityLevelConfiguration{ 40 Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ 41 Type: flowcontrolv1beta3.PriorityLevelEnablementExempt, 42 Exempt: &flowcontrolv1beta3.ExemptPriorityLevelConfiguration{}, 43 }, 44 }, 45 expected: &flowcontrolv1beta3.PriorityLevelConfiguration{ 46 Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ 47 Type: flowcontrolv1beta3.PriorityLevelEnablementExempt, 48 Exempt: &flowcontrolv1beta3.ExemptPriorityLevelConfiguration{ 49 NominalConcurrencyShares: pointer.Int32(0), 50 LendablePercent: pointer.Int32(0), 51 }, 52 }, 53 }, 54 }, 55 { 56 name: "LendablePercent is not specified in Limited, should default to zero", 57 original: &flowcontrolv1beta3.PriorityLevelConfiguration{ 58 Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ 59 Type: flowcontrolv1beta3.PriorityLevelEnablementLimited, 60 Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{ 61 NominalConcurrencyShares: 5, 62 LimitResponse: flowcontrolv1beta3.LimitResponse{ 63 Type: flowcontrolv1beta3.LimitResponseTypeReject, 64 }, 65 }, 66 }, 67 }, 68 expected: &flowcontrolv1beta3.PriorityLevelConfiguration{ 69 Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ 70 Type: flowcontrolv1beta3.PriorityLevelEnablementLimited, 71 Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{ 72 NominalConcurrencyShares: 5, 73 LendablePercent: pointer.Int32(0), 74 LimitResponse: flowcontrolv1beta3.LimitResponse{ 75 Type: flowcontrolv1beta3.LimitResponseTypeReject, 76 }, 77 }, 78 }, 79 }, 80 }, 81 { 82 name: "Defaulting for queuing configuration", 83 original: &flowcontrolv1beta3.PriorityLevelConfiguration{ 84 Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ 85 Type: flowcontrolv1beta3.PriorityLevelEnablementLimited, 86 Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{ 87 NominalConcurrencyShares: 5, 88 LimitResponse: flowcontrolv1beta3.LimitResponse{ 89 Type: flowcontrolv1beta3.LimitResponseTypeQueue, 90 Queuing: &flowcontrolv1beta3.QueuingConfiguration{}, 91 }, 92 }, 93 }, 94 }, 95 expected: &flowcontrolv1beta3.PriorityLevelConfiguration{ 96 Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ 97 Type: flowcontrolv1beta3.PriorityLevelEnablementLimited, 98 Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{ 99 NominalConcurrencyShares: 5, 100 LendablePercent: pointer.Int32(0), 101 LimitResponse: flowcontrolv1beta3.LimitResponse{ 102 Type: flowcontrolv1beta3.LimitResponseTypeQueue, 103 Queuing: &flowcontrolv1beta3.QueuingConfiguration{ 104 HandSize: PriorityLevelConfigurationDefaultHandSize, 105 Queues: PriorityLevelConfigurationDefaultQueues, 106 QueueLengthLimit: PriorityLevelConfigurationDefaultQueueLengthLimit, 107 }, 108 }, 109 }, 110 }, 111 }, 112 }, 113 { 114 name: "NominalConcurrencyShares is 0, roundtrip annotation is not set, default value should be applied", 115 original: &flowcontrolv1beta3.PriorityLevelConfiguration{ 116 Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ 117 Type: flowcontrolv1beta3.PriorityLevelEnablementLimited, 118 Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{ 119 NominalConcurrencyShares: 0, 120 LimitResponse: flowcontrolv1beta3.LimitResponse{ 121 Type: flowcontrolv1beta3.LimitResponseTypeReject, 122 }, 123 }, 124 }, 125 }, 126 expected: &flowcontrolv1beta3.PriorityLevelConfiguration{ 127 Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ 128 Type: flowcontrolv1beta3.PriorityLevelEnablementLimited, 129 Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{ 130 NominalConcurrencyShares: PriorityLevelConfigurationDefaultNominalConcurrencyShares, 131 LendablePercent: pointer.Int32(0), 132 LimitResponse: flowcontrolv1beta3.LimitResponse{ 133 Type: flowcontrolv1beta3.LimitResponseTypeReject, 134 }, 135 }, 136 }, 137 }, 138 }, 139 { 140 name: "NominalConcurrencyShares is 0, roundtrip annotation is set, default value should not be applied", 141 original: &flowcontrolv1beta3.PriorityLevelConfiguration{ 142 ObjectMeta: metav1.ObjectMeta{ 143 Annotations: map[string]string{ 144 flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "", 145 }, 146 }, 147 Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ 148 Type: flowcontrolv1beta3.PriorityLevelEnablementLimited, 149 Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{ 150 NominalConcurrencyShares: 0, 151 LimitResponse: flowcontrolv1beta3.LimitResponse{ 152 Type: flowcontrolv1beta3.LimitResponseTypeReject, 153 }, 154 }, 155 }, 156 }, 157 expected: &flowcontrolv1beta3.PriorityLevelConfiguration{ 158 ObjectMeta: metav1.ObjectMeta{ 159 Annotations: map[string]string{ 160 flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "", 161 }, 162 }, 163 Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ 164 Type: flowcontrolv1beta3.PriorityLevelEnablementLimited, 165 Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{ 166 NominalConcurrencyShares: 0, 167 LendablePercent: pointer.Int32(0), 168 LimitResponse: flowcontrolv1beta3.LimitResponse{ 169 Type: flowcontrolv1beta3.LimitResponseTypeReject, 170 }, 171 }, 172 }, 173 }, 174 }, 175 { 176 name: "NominalConcurrencyShares is 0, roundtrip annotation is set with a non-empty string, default value should not be applied", 177 original: &flowcontrolv1beta3.PriorityLevelConfiguration{ 178 ObjectMeta: metav1.ObjectMeta{ 179 Annotations: map[string]string{ 180 flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "", 181 }, 182 }, 183 Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ 184 Type: flowcontrolv1beta3.PriorityLevelEnablementLimited, 185 Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{ 186 NominalConcurrencyShares: 0, 187 LimitResponse: flowcontrolv1beta3.LimitResponse{ 188 Type: flowcontrolv1beta3.LimitResponseTypeReject, 189 }, 190 }, 191 }, 192 }, 193 expected: &flowcontrolv1beta3.PriorityLevelConfiguration{ 194 ObjectMeta: metav1.ObjectMeta{ 195 Annotations: map[string]string{ 196 flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "", 197 }, 198 }, 199 Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ 200 Type: flowcontrolv1beta3.PriorityLevelEnablementLimited, 201 Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{ 202 NominalConcurrencyShares: 0, 203 LendablePercent: pointer.Int32(0), 204 LimitResponse: flowcontrolv1beta3.LimitResponse{ 205 Type: flowcontrolv1beta3.LimitResponseTypeReject, 206 }, 207 }, 208 }, 209 }, 210 }, 211 { 212 name: "NominalConcurrencyShares is positive, roundtrip annotation is set, annotation should be ignored", 213 original: &flowcontrolv1beta3.PriorityLevelConfiguration{ 214 ObjectMeta: metav1.ObjectMeta{ 215 Annotations: map[string]string{ 216 flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "", 217 }, 218 }, 219 Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ 220 Type: flowcontrolv1beta3.PriorityLevelEnablementLimited, 221 Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{ 222 NominalConcurrencyShares: 1, 223 LimitResponse: flowcontrolv1beta3.LimitResponse{ 224 Type: flowcontrolv1beta3.LimitResponseTypeReject, 225 }, 226 }, 227 }, 228 }, 229 expected: &flowcontrolv1beta3.PriorityLevelConfiguration{ 230 ObjectMeta: metav1.ObjectMeta{ 231 Annotations: map[string]string{ 232 flowcontrolv1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "", 233 }, 234 }, 235 Spec: flowcontrolv1beta3.PriorityLevelConfigurationSpec{ 236 Type: flowcontrolv1beta3.PriorityLevelEnablementLimited, 237 Limited: &flowcontrolv1beta3.LimitedPriorityLevelConfiguration{ 238 NominalConcurrencyShares: 1, 239 LendablePercent: pointer.Int32(0), 240 LimitResponse: flowcontrolv1beta3.LimitResponse{ 241 Type: flowcontrolv1beta3.LimitResponseTypeReject, 242 }, 243 }, 244 }, 245 }, 246 }, 247 } 248 249 scheme := runtime.NewScheme() 250 if err := AddToScheme(scheme); err != nil { 251 t.Fatalf("Failed to add to scheme: %v", err) 252 } 253 254 for _, test := range tests { 255 t.Run(test.name, func(t *testing.T) { 256 original := test.original 257 expected := test.expected 258 259 scheme.Default(original) 260 if !reflect.DeepEqual(expected, original) { 261 t.Errorf("Expected defaulting to work - diff: %s", cmp.Diff(expected, original)) 262 } 263 }) 264 } 265 }