github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/meta/priority_test.go (about) 1 /* 2 Copyright 2016 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 meta 18 19 import ( 20 "errors" 21 "reflect" 22 "strings" 23 "testing" 24 25 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema" 26 ) 27 28 func TestPriorityRESTMapperResourceForErrorHandling(t *testing.T) { 29 tcs := []struct { 30 name string 31 32 delegate RESTMapper 33 resourcePatterns []schema.GroupVersionResource 34 result schema.GroupVersionResource 35 err string 36 }{ 37 { 38 name: "error", 39 delegate: fixedRESTMapper{err: errors.New("delegateError")}, 40 err: "delegateError", 41 }, 42 { 43 name: "single hit + error", 44 delegate: fixedRESTMapper{resourcesFor: []schema.GroupVersionResource{{Resource: "single-hit"}}, err: errors.New("delegateError")}, 45 result: schema.GroupVersionResource{Resource: "single-hit"}, 46 err: "delegateError", 47 }, 48 { 49 name: "group selection + error", 50 delegate: fixedRESTMapper{resourcesFor: []schema.GroupVersionResource{ 51 {Group: "one", Version: "a", Resource: "first"}, 52 {Group: "two", Version: "b", Resource: "second"}, 53 }, err: errors.New("delegateError")}, 54 resourcePatterns: []schema.GroupVersionResource{ 55 {Group: "one", Version: AnyVersion, Resource: AnyResource}, 56 }, 57 result: schema.GroupVersionResource{Group: "one", Version: "a", Resource: "first"}, 58 err: "delegateError", 59 }, 60 61 { 62 name: "single hit", 63 delegate: fixedRESTMapper{resourcesFor: []schema.GroupVersionResource{{Resource: "single-hit"}}}, 64 result: schema.GroupVersionResource{Resource: "single-hit"}, 65 }, 66 { 67 name: "ambiguous match", 68 delegate: fixedRESTMapper{resourcesFor: []schema.GroupVersionResource{ 69 {Group: "one", Version: "a", Resource: "first"}, 70 {Group: "two", Version: "b", Resource: "second"}, 71 }}, 72 err: "matches multiple resources", 73 }, 74 { 75 name: "group selection", 76 delegate: fixedRESTMapper{resourcesFor: []schema.GroupVersionResource{ 77 {Group: "one", Version: "a", Resource: "first"}, 78 {Group: "two", Version: "b", Resource: "second"}, 79 }}, 80 resourcePatterns: []schema.GroupVersionResource{ 81 {Group: "one", Version: AnyVersion, Resource: AnyResource}, 82 }, 83 result: schema.GroupVersionResource{Group: "one", Version: "a", Resource: "first"}, 84 }, 85 { 86 name: "empty match continues", 87 delegate: fixedRESTMapper{resourcesFor: []schema.GroupVersionResource{ 88 {Group: "one", Version: "a", Resource: "first"}, 89 {Group: "two", Version: "b", Resource: "second"}, 90 }}, 91 resourcePatterns: []schema.GroupVersionResource{ 92 {Group: "fail", Version: AnyVersion, Resource: AnyResource}, 93 {Group: "one", Version: AnyVersion, Resource: AnyResource}, 94 }, 95 result: schema.GroupVersionResource{Group: "one", Version: "a", Resource: "first"}, 96 }, 97 { 98 name: "group followed by version selection", 99 delegate: fixedRESTMapper{resourcesFor: []schema.GroupVersionResource{ 100 {Group: "one", Version: "a", Resource: "first"}, 101 {Group: "two", Version: "b", Resource: "second"}, 102 {Group: "one", Version: "c", Resource: "third"}, 103 }}, 104 resourcePatterns: []schema.GroupVersionResource{ 105 {Group: "one", Version: AnyVersion, Resource: AnyResource}, 106 {Group: AnyGroup, Version: "a", Resource: AnyResource}, 107 }, 108 result: schema.GroupVersionResource{Group: "one", Version: "a", Resource: "first"}, 109 }, 110 { 111 name: "resource selection", 112 delegate: fixedRESTMapper{resourcesFor: []schema.GroupVersionResource{ 113 {Group: "one", Version: "a", Resource: "first"}, 114 {Group: "one", Version: "a", Resource: "second"}, 115 }}, 116 resourcePatterns: []schema.GroupVersionResource{ 117 {Group: AnyGroup, Version: AnyVersion, Resource: "second"}, 118 }, 119 result: schema.GroupVersionResource{Group: "one", Version: "a", Resource: "second"}, 120 }, 121 } 122 123 for _, tc := range tcs { 124 mapper := PriorityRESTMapper{Delegate: tc.delegate, ResourcePriority: tc.resourcePatterns} 125 126 actualResult, actualErr := mapper.ResourceFor(schema.GroupVersionResource{}) 127 if e, a := tc.result, actualResult; e != a { 128 t.Errorf("%s: expected %v, got %v", tc.name, e, a) 129 } 130 if len(tc.err) == 0 && actualErr == nil { 131 continue 132 } 133 if len(tc.err) == 0 && actualErr != nil { 134 t.Errorf("%s: unexpected err: %v", tc.name, actualErr) 135 continue 136 } 137 if len(tc.err) > 0 && actualErr == nil { 138 t.Errorf("%s: missing expected err: %v", tc.name, tc.err) 139 continue 140 } 141 if !strings.Contains(actualErr.Error(), tc.err) { 142 t.Errorf("%s: expected %v, got %v", tc.name, tc.err, actualErr) 143 } 144 } 145 } 146 147 func TestPriorityRESTMapperKindForErrorHandling(t *testing.T) { 148 tcs := []struct { 149 name string 150 151 delegate RESTMapper 152 kindPatterns []schema.GroupVersionKind 153 result schema.GroupVersionKind 154 err string 155 }{ 156 { 157 name: "error", 158 delegate: fixedRESTMapper{err: errors.New("delegateErr")}, 159 err: "delegateErr", 160 }, 161 { 162 name: "single hit + error", 163 delegate: fixedRESTMapper{kindsFor: []schema.GroupVersionKind{{Kind: "single-hit"}}, err: errors.New("delegateErr")}, 164 result: schema.GroupVersionKind{Kind: "single-hit"}, 165 err: "delegateErr", 166 }, 167 { 168 name: "group selection + error", 169 delegate: fixedRESTMapper{kindsFor: []schema.GroupVersionKind{ 170 {Group: "one", Version: "a", Kind: "first"}, 171 {Group: "two", Version: "b", Kind: "second"}, 172 }, err: errors.New("delegateErr")}, 173 kindPatterns: []schema.GroupVersionKind{ 174 {Group: "one", Version: AnyVersion, Kind: AnyKind}, 175 }, 176 result: schema.GroupVersionKind{Group: "one", Version: "a", Kind: "first"}, 177 err: "delegateErr", 178 }, 179 180 { 181 name: "single hit", 182 delegate: fixedRESTMapper{kindsFor: []schema.GroupVersionKind{{Kind: "single-hit"}}}, 183 result: schema.GroupVersionKind{Kind: "single-hit"}, 184 }, 185 { 186 name: "ambiguous match", 187 delegate: fixedRESTMapper{kindsFor: []schema.GroupVersionKind{ 188 {Group: "one", Version: "a", Kind: "first"}, 189 {Group: "two", Version: "b", Kind: "second"}, 190 }}, 191 err: "matches multiple kinds", 192 }, 193 { 194 name: "group selection", 195 delegate: fixedRESTMapper{kindsFor: []schema.GroupVersionKind{ 196 {Group: "one", Version: "a", Kind: "first"}, 197 {Group: "two", Version: "b", Kind: "second"}, 198 }}, 199 kindPatterns: []schema.GroupVersionKind{ 200 {Group: "one", Version: AnyVersion, Kind: AnyKind}, 201 }, 202 result: schema.GroupVersionKind{Group: "one", Version: "a", Kind: "first"}, 203 }, 204 { 205 name: "empty match continues", 206 delegate: fixedRESTMapper{kindsFor: []schema.GroupVersionKind{ 207 {Group: "one", Version: "a", Kind: "first"}, 208 {Group: "two", Version: "b", Kind: "second"}, 209 }}, 210 kindPatterns: []schema.GroupVersionKind{ 211 {Group: "fail", Version: AnyVersion, Kind: AnyKind}, 212 {Group: "one", Version: AnyVersion, Kind: AnyKind}, 213 }, 214 result: schema.GroupVersionKind{Group: "one", Version: "a", Kind: "first"}, 215 }, 216 { 217 name: "group followed by version selection", 218 delegate: fixedRESTMapper{kindsFor: []schema.GroupVersionKind{ 219 {Group: "one", Version: "a", Kind: "first"}, 220 {Group: "two", Version: "b", Kind: "second"}, 221 {Group: "one", Version: "c", Kind: "third"}, 222 }}, 223 kindPatterns: []schema.GroupVersionKind{ 224 {Group: "one", Version: AnyVersion, Kind: AnyKind}, 225 {Group: AnyGroup, Version: "a", Kind: AnyKind}, 226 }, 227 result: schema.GroupVersionKind{Group: "one", Version: "a", Kind: "first"}, 228 }, 229 { 230 name: "kind selection", 231 delegate: fixedRESTMapper{kindsFor: []schema.GroupVersionKind{ 232 {Group: "one", Version: "a", Kind: "first"}, 233 {Group: "one", Version: "a", Kind: "second"}, 234 }}, 235 kindPatterns: []schema.GroupVersionKind{ 236 {Group: AnyGroup, Version: AnyVersion, Kind: "second"}, 237 }, 238 result: schema.GroupVersionKind{Group: "one", Version: "a", Kind: "second"}, 239 }, 240 } 241 242 for _, tc := range tcs { 243 mapper := PriorityRESTMapper{Delegate: tc.delegate, KindPriority: tc.kindPatterns} 244 245 actualResult, actualErr := mapper.KindFor(schema.GroupVersionResource{}) 246 if e, a := tc.result, actualResult; e != a { 247 t.Errorf("%s: expected %v, got %v", tc.name, e, a) 248 } 249 if len(tc.err) == 0 && actualErr == nil { 250 continue 251 } 252 if len(tc.err) == 0 && actualErr != nil { 253 t.Errorf("%s: unexpected err: %v", tc.name, actualErr) 254 continue 255 } 256 if len(tc.err) > 0 && actualErr == nil { 257 t.Errorf("%s: missing expected err: %v", tc.name, tc.err) 258 continue 259 } 260 if !strings.Contains(actualErr.Error(), tc.err) { 261 t.Errorf("%s: expected %v, got %v", tc.name, tc.err, actualErr) 262 } 263 } 264 } 265 266 func TestPriorityRESTMapperRESTMapping(t *testing.T) { 267 mapping1 := &RESTMapping{ 268 GroupVersionKind: schema.GroupVersionKind{Kind: "Foo", Version: "v1alpha1"}, 269 } 270 mapping2 := &RESTMapping{ 271 GroupVersionKind: schema.GroupVersionKind{Kind: "Foo", Version: "v1"}, 272 } 273 mapping3 := &RESTMapping{ 274 GroupVersionKind: schema.GroupVersionKind{Group: "other", Kind: "Foo", Version: "v1"}, 275 } 276 allMappers := MultiRESTMapper{ 277 fixedRESTMapper{mappings: []*RESTMapping{mapping1}}, 278 fixedRESTMapper{mappings: []*RESTMapping{mapping2}}, 279 fixedRESTMapper{mappings: []*RESTMapping{mapping3}}, 280 } 281 tcs := []struct { 282 name string 283 284 mapper PriorityRESTMapper 285 input schema.GroupKind 286 result *RESTMapping 287 err error 288 }{ 289 { 290 name: "empty", 291 mapper: PriorityRESTMapper{Delegate: MultiRESTMapper{}}, 292 input: schema.GroupKind{Kind: "Foo"}, 293 err: &NoKindMatchError{GroupKind: schema.GroupKind{Kind: "Foo"}}, 294 }, 295 { 296 name: "ignore not found", 297 mapper: PriorityRESTMapper{Delegate: MultiRESTMapper{fixedRESTMapper{err: &NoKindMatchError{GroupKind: schema.GroupKind{Kind: "IGNORE_THIS"}}}}}, 298 input: schema.GroupKind{Kind: "Foo"}, 299 err: &NoKindMatchError{GroupKind: schema.GroupKind{Kind: "Foo"}}, 300 }, 301 { 302 name: "accept first failure", 303 mapper: PriorityRESTMapper{Delegate: MultiRESTMapper{fixedRESTMapper{err: errors.New("fail on this")}, fixedRESTMapper{mappings: []*RESTMapping{mapping1}}}}, 304 input: schema.GroupKind{Kind: "Foo"}, 305 err: errors.New("fail on this"), 306 }, 307 { 308 name: "result + error", 309 mapper: PriorityRESTMapper{Delegate: fixedRESTMapper{mappings: []*RESTMapping{mapping1}, err: errors.New("fail on this")}}, 310 input: schema.GroupKind{Kind: "Foo"}, 311 result: mapping1, 312 err: errors.New("fail on this"), 313 }, 314 { 315 name: "return error for ambiguous", 316 mapper: PriorityRESTMapper{ 317 Delegate: allMappers, 318 }, 319 input: schema.GroupKind{Kind: "Foo"}, 320 err: &AmbiguousKindError{ 321 PartialKind: schema.GroupVersionKind{Kind: "Foo"}, 322 MatchingKinds: []schema.GroupVersionKind{ 323 {Kind: "Foo", Version: "v1alpha1"}, 324 {Kind: "Foo", Version: "v1"}, 325 {Group: "other", Kind: "Foo", Version: "v1"}, 326 }, 327 }, 328 }, 329 { 330 name: "accept only item", 331 mapper: PriorityRESTMapper{ 332 Delegate: fixedRESTMapper{mappings: []*RESTMapping{mapping1}}, 333 }, 334 input: schema.GroupKind{Kind: "Foo"}, 335 result: mapping1, 336 }, 337 { 338 name: "return single priority", 339 mapper: PriorityRESTMapper{ 340 Delegate: allMappers, 341 KindPriority: []schema.GroupVersionKind{{Version: "v1", Kind: AnyKind}, {Version: "v1alpha1", Kind: AnyKind}}, 342 }, 343 input: schema.GroupKind{Kind: "Foo"}, 344 result: mapping2, 345 }, 346 { 347 name: "return out of group match", 348 mapper: PriorityRESTMapper{ 349 Delegate: allMappers, 350 KindPriority: []schema.GroupVersionKind{{Group: AnyGroup, Version: "v1", Kind: AnyKind}, {Group: "other", Version: AnyVersion, Kind: AnyKind}}, 351 }, 352 input: schema.GroupKind{Kind: "Foo"}, 353 result: mapping3, 354 }, 355 } 356 357 for _, tc := range tcs { 358 actualResult, actualErr := tc.mapper.RESTMapping(tc.input) 359 if e, a := tc.result, actualResult; !reflect.DeepEqual(e, a) { 360 t.Errorf("%s: expected %v, got %v", tc.name, e, a) 361 } 362 switch { 363 case tc.err == nil && actualErr == nil: 364 case tc.err == nil: 365 t.Errorf("%s: unexpected error: %v", tc.name, actualErr) 366 case actualErr == nil: 367 t.Errorf("%s: expected error: %v got nil", tc.name, tc.err) 368 case tc.err.Error() != actualErr.Error(): 369 t.Errorf("%s: expected %v, got %v", tc.name, tc.err, actualErr) 370 } 371 } 372 } 373 374 func TestPriorityRESTMapperRESTMappingHonorsUserVersion(t *testing.T) { 375 mappingV2alpha1 := &RESTMapping{ 376 GroupVersionKind: schema.GroupVersionKind{Group: "Bar", Kind: "Foo", Version: "v2alpha1"}, 377 } 378 mappingV1 := &RESTMapping{ 379 GroupVersionKind: schema.GroupVersionKind{Group: "Bar", Kind: "Foo", Version: "v1"}, 380 } 381 382 allMappers := MultiRESTMapper{ 383 fixedRESTMapper{mappings: []*RESTMapping{mappingV2alpha1}}, 384 fixedRESTMapper{mappings: []*RESTMapping{mappingV1}}, 385 } 386 387 mapper := PriorityRESTMapper{ 388 Delegate: allMappers, 389 KindPriority: []schema.GroupVersionKind{{Group: "Bar", Version: "v2alpha1", Kind: AnyKind}, {Group: "Bar", Version: AnyVersion, Kind: AnyKind}}, 390 } 391 392 outMapping1, err := mapper.RESTMapping(schema.GroupKind{Group: "Bar", Kind: "Foo"}, "v1") 393 if err != nil { 394 t.Errorf("unexpected error: %v", err) 395 } 396 397 if outMapping1 != mappingV1 { 398 t.Errorf("asked for version %v, expected mapping for %v, got mapping for %v", "v1", mappingV1.GroupVersionKind, outMapping1.GroupVersionKind) 399 } 400 401 outMapping2, err := mapper.RESTMapping(schema.GroupKind{Group: "Bar", Kind: "Foo"}, "v2alpha1") 402 if err != nil { 403 t.Errorf("unexpected error: %v", err) 404 } 405 406 if outMapping2 != mappingV2alpha1 { 407 t.Errorf("asked for version %v, expected mapping for %v, got mapping for %v", "v2alpha1", mappingV2alpha1.GroupVersionKind, outMapping2.GroupVersionKind) 408 } 409 }