@@ -332,3 +332,247 @@ func TestName(t *testing.T) {
332
332
})
333
333
}
334
334
}
335
+
336
+ func TestBaseRequestRef (t * testing.T ) {
337
+ testcases := map [string ]struct {
338
+ requestRef string
339
+ expectedBaseRequestRef string
340
+ }{
341
+ "valid-no-subrequest" : {
342
+ requestRef : "foo" ,
343
+ expectedBaseRequestRef : "foo" ,
344
+ },
345
+ "valid-subrequest" : {
346
+ requestRef : "foo/bar" ,
347
+ expectedBaseRequestRef : "foo" ,
348
+ },
349
+ }
350
+
351
+ for name , tc := range testcases {
352
+ t .Run (name , func (t * testing.T ) {
353
+ baseRequestRef := BaseRequestRef (tc .requestRef )
354
+ assert .Equal (t , tc .expectedBaseRequestRef , baseRequestRef )
355
+ })
356
+ }
357
+ }
358
+
359
+ func TestConfigForResult (t * testing.T ) {
360
+ testcases := map [string ]struct {
361
+ deviceConfigurations []resourceapi.DeviceAllocationConfiguration
362
+ result resourceapi.DeviceRequestAllocationResult
363
+ expectedConfigs []resourceapi.DeviceAllocationConfiguration
364
+ }{
365
+ "opaque-nil" : {
366
+ deviceConfigurations : []resourceapi.DeviceAllocationConfiguration {
367
+ {
368
+ Source : resourceapi .AllocationConfigSourceClass ,
369
+ Requests : []string {},
370
+ DeviceConfiguration : resourceapi.DeviceConfiguration {
371
+ Opaque : nil ,
372
+ },
373
+ },
374
+ },
375
+ result : resourceapi.DeviceRequestAllocationResult {
376
+ Request : "foo" ,
377
+ Device : "device-1" ,
378
+ },
379
+ expectedConfigs : nil ,
380
+ },
381
+ "empty-requests-match-all" : {
382
+ deviceConfigurations : []resourceapi.DeviceAllocationConfiguration {
383
+ {
384
+ Source : resourceapi .AllocationConfigSourceClass ,
385
+ Requests : []string {},
386
+ DeviceConfiguration : resourceapi.DeviceConfiguration {
387
+ Opaque : & resourceapi.OpaqueDeviceConfiguration {
388
+ Driver : "driver-a" ,
389
+ },
390
+ },
391
+ },
392
+ },
393
+ result : resourceapi.DeviceRequestAllocationResult {
394
+ Request : "foo" ,
395
+ Device : "device-1" ,
396
+ },
397
+ expectedConfigs : []resourceapi.DeviceAllocationConfiguration {
398
+ {
399
+ Source : resourceapi .AllocationConfigSourceClass ,
400
+ Requests : []string {},
401
+ DeviceConfiguration : resourceapi.DeviceConfiguration {
402
+ Opaque : & resourceapi.OpaqueDeviceConfiguration {
403
+ Driver : "driver-a" ,
404
+ },
405
+ },
406
+ },
407
+ },
408
+ },
409
+ "match-regular-request" : {
410
+ deviceConfigurations : []resourceapi.DeviceAllocationConfiguration {
411
+ {
412
+ Source : resourceapi .AllocationConfigSourceClass ,
413
+ Requests : []string {
414
+ "foo" ,
415
+ },
416
+ DeviceConfiguration : resourceapi.DeviceConfiguration {
417
+ Opaque : & resourceapi.OpaqueDeviceConfiguration {
418
+ Driver : "driver-a" ,
419
+ },
420
+ },
421
+ },
422
+ },
423
+ result : resourceapi.DeviceRequestAllocationResult {
424
+ Request : "foo" ,
425
+ Device : "device-1" ,
426
+ },
427
+ expectedConfigs : []resourceapi.DeviceAllocationConfiguration {
428
+ {
429
+ Source : resourceapi .AllocationConfigSourceClass ,
430
+ Requests : []string {
431
+ "foo" ,
432
+ },
433
+ DeviceConfiguration : resourceapi.DeviceConfiguration {
434
+ Opaque : & resourceapi.OpaqueDeviceConfiguration {
435
+ Driver : "driver-a" ,
436
+ },
437
+ },
438
+ },
439
+ },
440
+ },
441
+ "match-parent-request-for-subrequest" : {
442
+ deviceConfigurations : []resourceapi.DeviceAllocationConfiguration {
443
+ {
444
+ Source : resourceapi .AllocationConfigSourceClass ,
445
+ Requests : []string {
446
+ "foo" ,
447
+ },
448
+ DeviceConfiguration : resourceapi.DeviceConfiguration {
449
+ Opaque : & resourceapi.OpaqueDeviceConfiguration {
450
+ Driver : "driver-a" ,
451
+ },
452
+ },
453
+ },
454
+ },
455
+ result : resourceapi.DeviceRequestAllocationResult {
456
+ Request : "foo/bar" ,
457
+ Device : "device-1" ,
458
+ },
459
+ expectedConfigs : []resourceapi.DeviceAllocationConfiguration {
460
+ {
461
+ Source : resourceapi .AllocationConfigSourceClass ,
462
+ Requests : []string {
463
+ "foo" ,
464
+ },
465
+ DeviceConfiguration : resourceapi.DeviceConfiguration {
466
+ Opaque : & resourceapi.OpaqueDeviceConfiguration {
467
+ Driver : "driver-a" ,
468
+ },
469
+ },
470
+ },
471
+ },
472
+ },
473
+ "match-subrequest" : {
474
+ deviceConfigurations : []resourceapi.DeviceAllocationConfiguration {
475
+ {
476
+ Source : resourceapi .AllocationConfigSourceClass ,
477
+ Requests : []string {
478
+ "foo/bar" ,
479
+ },
480
+ DeviceConfiguration : resourceapi.DeviceConfiguration {
481
+ Opaque : & resourceapi.OpaqueDeviceConfiguration {
482
+ Driver : "driver-a" ,
483
+ },
484
+ },
485
+ },
486
+ {
487
+ Source : resourceapi .AllocationConfigSourceClass ,
488
+ Requests : []string {
489
+ "foo/not-bar" ,
490
+ },
491
+ DeviceConfiguration : resourceapi.DeviceConfiguration {
492
+ Opaque : & resourceapi.OpaqueDeviceConfiguration {
493
+ Driver : "driver-a" ,
494
+ },
495
+ },
496
+ },
497
+ },
498
+ result : resourceapi.DeviceRequestAllocationResult {
499
+ Request : "foo/bar" ,
500
+ Device : "device-1" ,
501
+ },
502
+ expectedConfigs : []resourceapi.DeviceAllocationConfiguration {
503
+ {
504
+ Source : resourceapi .AllocationConfigSourceClass ,
505
+ Requests : []string {
506
+ "foo/bar" ,
507
+ },
508
+ DeviceConfiguration : resourceapi.DeviceConfiguration {
509
+ Opaque : & resourceapi.OpaqueDeviceConfiguration {
510
+ Driver : "driver-a" ,
511
+ },
512
+ },
513
+ },
514
+ },
515
+ },
516
+ "match-both-source-class-and-claim" : {
517
+ deviceConfigurations : []resourceapi.DeviceAllocationConfiguration {
518
+ {
519
+ Source : resourceapi .AllocationConfigSourceClass ,
520
+ Requests : []string {
521
+ "foo" ,
522
+ },
523
+ DeviceConfiguration : resourceapi.DeviceConfiguration {
524
+ Opaque : & resourceapi.OpaqueDeviceConfiguration {
525
+ Driver : "driver-a" ,
526
+ },
527
+ },
528
+ },
529
+ {
530
+ Source : resourceapi .AllocationConfigSourceClaim ,
531
+ Requests : []string {
532
+ "foo/bar" ,
533
+ },
534
+ DeviceConfiguration : resourceapi.DeviceConfiguration {
535
+ Opaque : & resourceapi.OpaqueDeviceConfiguration {
536
+ Driver : "driver-a" ,
537
+ },
538
+ },
539
+ },
540
+ },
541
+ result : resourceapi.DeviceRequestAllocationResult {
542
+ Request : "foo/bar" ,
543
+ Device : "device-1" ,
544
+ },
545
+ expectedConfigs : []resourceapi.DeviceAllocationConfiguration {
546
+ {
547
+ Source : resourceapi .AllocationConfigSourceClass ,
548
+ Requests : []string {
549
+ "foo" ,
550
+ },
551
+ DeviceConfiguration : resourceapi.DeviceConfiguration {
552
+ Opaque : & resourceapi.OpaqueDeviceConfiguration {
553
+ Driver : "driver-a" ,
554
+ },
555
+ },
556
+ },
557
+ {
558
+ Source : resourceapi .AllocationConfigSourceClaim ,
559
+ Requests : []string {
560
+ "foo/bar" ,
561
+ },
562
+ DeviceConfiguration : resourceapi.DeviceConfiguration {
563
+ Opaque : & resourceapi.OpaqueDeviceConfiguration {
564
+ Driver : "driver-a" ,
565
+ },
566
+ },
567
+ },
568
+ },
569
+ },
570
+ }
571
+
572
+ for name , tc := range testcases {
573
+ t .Run (name , func (t * testing.T ) {
574
+ configs := ConfigForResult (tc .deviceConfigurations , tc .result )
575
+ assert .Equal (t , tc .expectedConfigs , configs )
576
+ })
577
+ }
578
+ }
0 commit comments