@@ -417,39 +417,93 @@ func TestParseWithExpose(t *testing.T) {
417
417
418
418
func TestParseDevice (t * testing.T ) {
419
419
skip .If (t , runtime .GOOS != "linux" ) // Windows and macOS validate server-side
420
- valids := map [string ]container.DeviceMapping {
421
- "/dev/snd" : {
422
- PathOnHost : "/dev/snd" ,
423
- PathInContainer : "/dev/snd" ,
424
- CgroupPermissions : "rwm" ,
425
- },
426
- "/dev/snd:rw" : {
427
- PathOnHost : "/dev/snd" ,
428
- PathInContainer : "/dev/snd" ,
429
- CgroupPermissions : "rw" ,
430
- },
431
- "/dev/snd:/something" : {
432
- PathOnHost : "/dev/snd" ,
433
- PathInContainer : "/something" ,
434
- CgroupPermissions : "rwm" ,
435
- },
436
- "/dev/snd:/something:rw" : {
437
- PathOnHost : "/dev/snd" ,
438
- PathInContainer : "/something" ,
439
- CgroupPermissions : "rw" ,
440
- },
441
- }
442
- for device , deviceMapping := range valids {
443
- _ , hostconfig , _ , err := parseRun ([]string {fmt .Sprintf ("--device=%v" , device ), "img" , "cmd" })
444
- if err != nil {
445
- t .Fatal (err )
446
- }
447
- if len (hostconfig .Devices ) != 1 {
448
- t .Fatalf ("Expected 1 devices, got %v" , hostconfig .Devices )
449
- }
450
- if hostconfig .Devices [0 ] != deviceMapping {
451
- t .Fatalf ("Expected %v, got %v" , deviceMapping , hostconfig .Devices )
452
- }
420
+ testCases := []struct {
421
+ devices []string
422
+ deviceMapping * container.DeviceMapping
423
+ deviceRequests []container.DeviceRequest
424
+ }{
425
+ {
426
+ devices : []string {"/dev/snd" },
427
+ deviceMapping : & container.DeviceMapping {
428
+ PathOnHost : "/dev/snd" ,
429
+ PathInContainer : "/dev/snd" ,
430
+ CgroupPermissions : "rwm" ,
431
+ },
432
+ },
433
+ {
434
+ devices : []string {"/dev/snd:rw" },
435
+ deviceMapping : & container.DeviceMapping {
436
+ PathOnHost : "/dev/snd" ,
437
+ PathInContainer : "/dev/snd" ,
438
+ CgroupPermissions : "rw" ,
439
+ },
440
+ },
441
+ {
442
+ devices : []string {"/dev/snd:/something" },
443
+ deviceMapping : & container.DeviceMapping {
444
+ PathOnHost : "/dev/snd" ,
445
+ PathInContainer : "/something" ,
446
+ CgroupPermissions : "rwm" ,
447
+ },
448
+ },
449
+ {
450
+ devices : []string {"/dev/snd:/something:rw" },
451
+ deviceMapping : & container.DeviceMapping {
452
+ PathOnHost : "/dev/snd" ,
453
+ PathInContainer : "/something" ,
454
+ CgroupPermissions : "rw" ,
455
+ },
456
+ },
457
+ {
458
+ devices : []string {"vendor.com/class=name" },
459
+ deviceMapping : nil ,
460
+ deviceRequests : []container.DeviceRequest {
461
+ {
462
+ Driver : "cdi" ,
463
+ Capabilities : [][]string {{"cdi" }},
464
+ DeviceIDs : []string {"vendor.com/class=name" },
465
+ },
466
+ },
467
+ },
468
+ {
469
+ devices : []string {"vendor.com/class=name" , "/dev/snd:/something:rw" },
470
+ deviceMapping : & container.DeviceMapping {
471
+ PathOnHost : "/dev/snd" ,
472
+ PathInContainer : "/something" ,
473
+ CgroupPermissions : "rw" ,
474
+ },
475
+ deviceRequests : []container.DeviceRequest {
476
+ {
477
+ Driver : "cdi" ,
478
+ Capabilities : [][]string {{"cdi" }},
479
+ DeviceIDs : []string {"vendor.com/class=name" },
480
+ },
481
+ },
482
+ },
483
+ }
484
+
485
+ for _ , tc := range testCases {
486
+ t .Run (fmt .Sprintf ("%s" , tc .devices ), func (t * testing.T ) {
487
+ var args []string
488
+ for _ , d := range tc .devices {
489
+ args = append (args , fmt .Sprintf ("--device=%v" , d ))
490
+ }
491
+ args = append (args , "img" , "cmd" )
492
+
493
+ _ , hostconfig , _ , err := parseRun (args )
494
+
495
+ assert .NilError (t , err )
496
+
497
+ if tc .deviceMapping != nil {
498
+ if assert .Check (t , is .Len (hostconfig .Devices , 1 )) {
499
+ assert .Check (t , is .DeepEqual (* tc .deviceMapping , hostconfig .Devices [0 ]))
500
+ }
501
+ } else {
502
+ assert .Check (t , is .Len (hostconfig .Devices , 0 ))
503
+ }
504
+
505
+ assert .Check (t , is .DeepEqual (tc .deviceRequests , hostconfig .DeviceRequests ))
506
+ })
453
507
}
454
508
}
455
509
0 commit comments