k8s.io/kubernetes@v1.29.3/pkg/apis/flowcontrol/v1beta3/conversion_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 v1beta3 18 19 import ( 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 "k8s.io/api/flowcontrol/v1beta3" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/kubernetes/pkg/apis/flowcontrol" 26 ) 27 28 func TestConvert_v1beta3_PriorityLevelConfiguration_To_flowcontrol_PriorityLevelConfiguration(t *testing.T) { 29 inObjFn := func(shares int32, annotations map[string]string) *v1beta3.PriorityLevelConfiguration { 30 return &v1beta3.PriorityLevelConfiguration{ 31 ObjectMeta: metav1.ObjectMeta{ 32 Annotations: annotations, 33 }, 34 Spec: v1beta3.PriorityLevelConfigurationSpec{ 35 Type: v1beta3.PriorityLevelEnablementLimited, 36 Limited: &v1beta3.LimitedPriorityLevelConfiguration{ 37 NominalConcurrencyShares: shares, 38 LimitResponse: v1beta3.LimitResponse{ 39 Type: v1beta3.LimitResponseTypeReject, 40 }, 41 }, 42 }, 43 } 44 } 45 46 outObjFn := func(shares int32, annotations map[string]string) *flowcontrol.PriorityLevelConfiguration { 47 return &flowcontrol.PriorityLevelConfiguration{ 48 ObjectMeta: metav1.ObjectMeta{ 49 Annotations: annotations, 50 }, 51 Spec: flowcontrol.PriorityLevelConfigurationSpec{ 52 Type: flowcontrol.PriorityLevelEnablementLimited, 53 Limited: &flowcontrol.LimitedPriorityLevelConfiguration{ 54 NominalConcurrencyShares: shares, 55 LimitResponse: flowcontrol.LimitResponse{ 56 Type: flowcontrol.LimitResponseTypeReject, 57 }, 58 }, 59 }, 60 } 61 } 62 63 tests := []struct { 64 name string 65 in *v1beta3.PriorityLevelConfiguration 66 expected *flowcontrol.PriorityLevelConfiguration 67 }{ 68 { 69 name: "v1beta3 object, the roundtrip annotation is set, NominalConcurrencyShares is zero; the internal object should not have the roundtrip annotation set", 70 in: inObjFn(0, map[string]string{ 71 "foo": "bar", 72 v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "", 73 }), 74 expected: outObjFn(0, map[string]string{ 75 "foo": "bar", 76 }), 77 }, 78 { 79 name: "v1beta3 object; the internal object should not have the roundtrip annotation set", 80 in: &v1beta3.PriorityLevelConfiguration{ 81 ObjectMeta: metav1.ObjectMeta{ 82 Annotations: map[string]string{ 83 "foo": "bar", 84 v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "", 85 }, 86 }, 87 }, 88 expected: &flowcontrol.PriorityLevelConfiguration{ 89 ObjectMeta: metav1.ObjectMeta{ 90 Annotations: map[string]string{ 91 "foo": "bar", 92 }, 93 }, 94 }, 95 }, 96 { 97 name: "v1beta3 object, the roundtrip annotation is not set, NominalConcurrencyShares is zero; the internal object should not have the roundtrip annotation set", 98 in: inObjFn(0, map[string]string{ 99 "foo": "bar", 100 }), 101 expected: outObjFn(0, map[string]string{ 102 "foo": "bar", 103 }), 104 }, 105 } 106 107 for _, test := range tests { 108 t.Run(test.name, func(t *testing.T) { 109 copy := test.in.DeepCopy() 110 111 out := &flowcontrol.PriorityLevelConfiguration{} 112 if err := Convert_v1beta3_PriorityLevelConfiguration_To_flowcontrol_PriorityLevelConfiguration(test.in, out, nil); err != nil { 113 t.Errorf("Expected no error, but got: %v", err) 114 } 115 if !cmp.Equal(test.expected, out) { 116 t.Errorf("Expected a match, diff: %s", cmp.Diff(test.expected, out)) 117 } 118 if want, got := copy.ObjectMeta.Annotations, test.in.ObjectMeta.Annotations; !cmp.Equal(want, got) { 119 t.Errorf("Did not expect the 'Annotations' field of the source to be mutated, diff: %s", cmp.Diff(want, got)) 120 } 121 }) 122 } 123 } 124 125 func TestConvert_flowcontrol_PriorityLevelConfiguration_To_v1beta3_PriorityLevelConfiguration(t *testing.T) { 126 inObjFn := func(shares int32, annotations map[string]string) *flowcontrol.PriorityLevelConfiguration { 127 return &flowcontrol.PriorityLevelConfiguration{ 128 ObjectMeta: metav1.ObjectMeta{ 129 Annotations: annotations, 130 }, 131 Spec: flowcontrol.PriorityLevelConfigurationSpec{ 132 Type: flowcontrol.PriorityLevelEnablementLimited, 133 Limited: &flowcontrol.LimitedPriorityLevelConfiguration{ 134 NominalConcurrencyShares: shares, 135 LimitResponse: flowcontrol.LimitResponse{ 136 Type: flowcontrol.LimitResponseTypeReject, 137 }, 138 }, 139 }, 140 } 141 } 142 143 outObjFn := func(shares int32, annotations map[string]string) *v1beta3.PriorityLevelConfiguration { 144 return &v1beta3.PriorityLevelConfiguration{ 145 ObjectMeta: metav1.ObjectMeta{ 146 Annotations: annotations, 147 }, 148 Spec: v1beta3.PriorityLevelConfigurationSpec{ 149 Type: v1beta3.PriorityLevelEnablementLimited, 150 Limited: &v1beta3.LimitedPriorityLevelConfiguration{ 151 NominalConcurrencyShares: shares, 152 LimitResponse: v1beta3.LimitResponse{ 153 Type: v1beta3.LimitResponseTypeReject, 154 }, 155 }, 156 }, 157 } 158 } 159 160 tests := []struct { 161 name string 162 in *flowcontrol.PriorityLevelConfiguration 163 expected *v1beta3.PriorityLevelConfiguration 164 }{ 165 { 166 name: "internal object, NominalConcurrencyShares is 0; v1beta3 object should have the roundtrip annotation set", 167 in: inObjFn(0, map[string]string{ 168 "foo": "bar", 169 }), 170 expected: outObjFn(0, map[string]string{ 171 "foo": "bar", 172 v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "", 173 }), 174 }, 175 { 176 name: "internal object, NominalConcurrencyShares is not 0; v1beta3 object should not have the roundtrip annotation set", 177 in: inObjFn(1, map[string]string{ 178 "foo": "bar", 179 }), 180 expected: outObjFn(1, map[string]string{ 181 "foo": "bar", 182 }), 183 }, 184 { 185 name: "internal object, the roundtrip annotation is set, NominalConcurrencyShares is 0, no error expected", 186 in: inObjFn(0, map[string]string{ 187 "foo": "bar", 188 v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "", 189 }), 190 expected: outObjFn(0, map[string]string{ 191 "foo": "bar", 192 v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "", 193 }), 194 }, 195 { 196 name: "internal object, the roundtrip annotation is set with a non-empty value, NominalConcurrencyShares is 0, the annotation value should be preserved", 197 in: inObjFn(0, map[string]string{ 198 "foo": "bar", 199 v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "non-empty", 200 }), 201 expected: outObjFn(0, map[string]string{ 202 "foo": "bar", 203 v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "non-empty", 204 }), 205 }, 206 { 207 name: "internal object, the roundtrip annotation is set with a non-empty value, NominalConcurrencyShares is not 0, the annotation value should be preserved", 208 in: inObjFn(1, map[string]string{ 209 "foo": "bar", 210 v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "non-empty", 211 }), 212 expected: outObjFn(1, map[string]string{ 213 "foo": "bar", 214 v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey: "non-empty", 215 }), 216 }, 217 } 218 219 for _, test := range tests { 220 t.Run(test.name, func(t *testing.T) { 221 copy := test.in.DeepCopy() 222 223 out := &v1beta3.PriorityLevelConfiguration{} 224 if err := Convert_flowcontrol_PriorityLevelConfiguration_To_v1beta3_PriorityLevelConfiguration(test.in, out, nil); err != nil { 225 t.Errorf("Expected no error, but got: %v", err) 226 } 227 228 if !cmp.Equal(test.expected, out) { 229 t.Errorf("Expected a match, diff: %s", cmp.Diff(test.expected, out)) 230 } 231 if want, got := copy.ObjectMeta.Annotations, test.in.ObjectMeta.Annotations; !cmp.Equal(want, got) { 232 t.Errorf("Did not expect the 'Annotations' field of the source to be mutated, diff: %s", cmp.Diff(want, got)) 233 } 234 }) 235 } 236 }