@@ -361,6 +361,66 @@ func TestScan(t *testing.T) {
361361 expectedI915Devs : 1 ,
362362 expectedI915Monitors : 1 ,
363363 },
364+ {
365+ name : "two devices with only one whitelisted" ,
366+ sysfsdirs : []string {"card0/device/drm/card0" , "card0/device/drm/controlD64" , "card1/device/drm/card1" },
367+ sysfsfiles : map [string ][]byte {
368+ "card0/device/vendor" : []byte ("0x8086" ),
369+ "card0/device/device" : []byte ("0x1234" ),
370+ "card1/device/vendor" : []byte ("0x8086" ),
371+ "card1/device/device" : []byte ("0x9876" ),
372+ },
373+ symlinkfiles : map [string ]string {
374+ "card0/device/driver" : "drivers/xe" ,
375+ "card1/device/driver" : "drivers/i915" ,
376+ },
377+ devfsdirs : []string {
378+ "card0" ,
379+ "by-path/pci-0000:00:00.0-card" ,
380+ "by-path/pci-0000:00:00.0-render" ,
381+ "card1" ,
382+ "by-path/pci-0000:00:01.0-card" ,
383+ "by-path/pci-0000:00:01.0-render" ,
384+ },
385+ options : cliOptions {enableMonitoring : true , whitelistIDs : "0x1234" },
386+ expectedXeDevs : 1 ,
387+ expectedXeMonitors : 1 ,
388+ expectedI915Devs : 0 ,
389+ expectedI915Monitors : 0 ,
390+ },
391+ {
392+ name : "three devices with two whitelisted" ,
393+ sysfsdirs : []string {"card0/device/drm/card0" , "card0/device/drm/controlD64" , "card1/device/drm/card1" , "card2/device/drm/card2" },
394+ sysfsfiles : map [string ][]byte {
395+ "card0/device/vendor" : []byte ("0x8086" ),
396+ "card0/device/device" : []byte ("0x1234" ),
397+ "card1/device/vendor" : []byte ("0x8086" ),
398+ "card1/device/device" : []byte ("0x9876" ),
399+ "card2/device/vendor" : []byte ("0x8086" ),
400+ "card2/device/device" : []byte ("0x0101" ),
401+ },
402+ symlinkfiles : map [string ]string {
403+ "card0/device/driver" : "drivers/xe" ,
404+ "card1/device/driver" : "drivers/i915" ,
405+ "card2/device/driver" : "drivers/i915" ,
406+ },
407+ devfsdirs : []string {
408+ "card0" ,
409+ "by-path/pci-0000:00:00.0-card" ,
410+ "by-path/pci-0000:00:00.0-render" ,
411+ "card1" ,
412+ "by-path/pci-0000:00:01.0-card" ,
413+ "by-path/pci-0000:00:01.0-render" ,
414+ "card2" ,
415+ "by-path/pci-0000:00:02.0-card" ,
416+ "by-path/pci-0000:00:02.0-render" ,
417+ },
418+ options : cliOptions {enableMonitoring : true , whitelistIDs : "0x1234,0x9876" },
419+ expectedXeDevs : 1 ,
420+ expectedXeMonitors : 1 ,
421+ expectedI915Devs : 1 ,
422+ expectedI915Monitors : 1 ,
423+ },
364424 {
365425 name : "sriov-1-pf-no-vfs + monitoring" ,
366426 sysfsdirs : []string {"card0/device/drm/card0" , "card0/device/drm/controlD64" },
@@ -1048,3 +1108,73 @@ func TestCDIDeviceInclusion(t *testing.T) {
10481108 t .Error ("Invalid count for device (xe)" )
10491109 }
10501110}
1111+
1112+ func TestParsePCIDeviceIDs (t * testing.T ) {
1113+ tests := []struct {
1114+ name string
1115+ input string
1116+ want []string
1117+ wantError bool
1118+ }{
1119+ {
1120+ name : "valid single ID" ,
1121+ input : "0x1234" ,
1122+ want : []string {"0x1234" },
1123+ wantError : false ,
1124+ },
1125+ {
1126+ name : "valid multiple IDs" ,
1127+ input : "0x1234,0x5678,0x9abc" ,
1128+ want : []string {"0x1234" , "0x5678" , "0x9abc" },
1129+ wantError : false ,
1130+ },
1131+ {
1132+ name : "valid IDs with spaces" ,
1133+ input : " 0x1234 , 0x5678 " ,
1134+ want : []string {"0x1234" , "0x5678" },
1135+ wantError : false ,
1136+ },
1137+ {
1138+ name : "empty string" ,
1139+ input : "" ,
1140+ want : []string {},
1141+ wantError : true ,
1142+ },
1143+ {
1144+ name : "invalid ID format" ,
1145+ input : "0x1234,abcd" ,
1146+ want : []string {},
1147+ wantError : true ,
1148+ },
1149+ {
1150+ name : "invalid hex length" ,
1151+ input : "0x123,0x5678" ,
1152+ want : []string {},
1153+ wantError : true ,
1154+ },
1155+ {
1156+ name : "extra comma" ,
1157+ input : "0x1234," ,
1158+ want : []string {},
1159+ wantError : true ,
1160+ },
1161+ {
1162+ name : "capita hex" ,
1163+ input : "0xAA12," ,
1164+ want : []string {},
1165+ wantError : true ,
1166+ },
1167+ }
1168+
1169+ for _ , tt := range tests {
1170+ t .Run (tt .name , func (t * testing.T ) {
1171+ got , err := parsePCIDeviceIDs (tt .input )
1172+ if (err != nil ) != tt .wantError {
1173+ t .Errorf ("parsePCIDeviceIDs() error = %v, wantError %v" , err , tt .wantError )
1174+ }
1175+ if ! reflect .DeepEqual (got , tt .want ) {
1176+ t .Errorf ("parsePCIDeviceIDs() = %v, want %v" , got , tt .want )
1177+ }
1178+ })
1179+ }
1180+ }
0 commit comments