@@ -18,11 +18,13 @@ import (
1818 "flag"
1919 "os"
2020 "path"
21+ "path/filepath"
2122 "reflect"
2223 "testing"
2324
2425 "github.com/pkg/errors"
2526 "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
27+ "k8s.io/utils/strings/slices"
2628
2729 "github.com/intel/intel-device-plugins-for-kubernetes/cmd/gpu_plugin/rm"
2830 dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin"
@@ -43,6 +45,7 @@ type mockNotifier struct {
4345func (n * mockNotifier ) Notify (newDeviceTree dpapi.DeviceTree ) {
4446 n .monitorCount = len (newDeviceTree [monitorType ])
4547 n .devCount = len (newDeviceTree [deviceType ])
48+
4649 n .scanDone <- true
4750}
4851
@@ -190,7 +193,11 @@ func TestScan(t *testing.T) {
190193 sysfsfiles : map [string ][]byte {
191194 "card0/device/vendor" : []byte ("0x8086" ),
192195 },
193- devfsdirs : []string {"card0" },
196+ devfsdirs : []string {
197+ "card0" ,
198+ "by-path/pci-0000:00:00.0-card" ,
199+ "by-path/pci-0000:00:00.0-render" ,
200+ },
194201 expectedDevs : 1 ,
195202 },
196203 {
@@ -314,3 +321,126 @@ func TestScan(t *testing.T) {
314321 })
315322 }
316323}
324+
325+ // Would be nice to combine these with the overall Scan unit tests.
326+ func createBypathTestFiles (t * testing.T , card , root , linkFile string , bypathFiles []string ) (string , string ) {
327+ drmPath := path .Join (root , "sys/class/drm/" , card )
328+ devPath := path .Join (root , "sys" , linkFile )
329+ byPath := path .Join (root , "by-path" )
330+
331+ if linkFile != "" {
332+ if err := os .MkdirAll (filepath .Dir (devPath ), os .ModePerm ); err != nil {
333+ t .Fatal ("Couldn't create test dev dir" , err )
334+ }
335+
336+ if err := os .MkdirAll (filepath .Dir (drmPath ), os .ModePerm ); err != nil {
337+ t .Fatal ("Couldn't create test drm dir" , err )
338+ }
339+
340+ if err := os .WriteFile (devPath , []byte {0 }, os .ModePerm ); err != nil {
341+ t .Fatal ("Couldn't create card file" , err )
342+ }
343+
344+ if err := os .Symlink (devPath , drmPath ); err != nil {
345+ t .Fatal ("Couldn't create symlink between pci path and sysfs drm path" )
346+ }
347+ }
348+
349+ if len (bypathFiles ) > 0 {
350+ if err := os .MkdirAll (byPath , os .ModePerm ); err != nil {
351+ t .Fatal ("Mkdir failed:" , byPath )
352+ }
353+
354+ for _ , f := range bypathFiles {
355+ if err := os .WriteFile (path .Join (byPath , f ), []byte {1 }, os .ModePerm ); err != nil {
356+ t .Fatal ("WriteFile failed:" , path .Join (byPath , f ))
357+ }
358+ }
359+ }
360+
361+ return drmPath , byPath
362+ }
363+
364+ func TestBypath (t * testing.T ) {
365+ type testData struct {
366+ desc string
367+ linkpath string
368+ bypathFiles []string
369+ mountCount int
370+ }
371+
372+ const cardName string = "card0"
373+
374+ tds := []testData {
375+ {
376+ "card with two by-path files" ,
377+ "00.10.2/00.334.302/0.0.1.00/0000:0f:05.0/drm/" + cardName ,
378+ []string {"pci-0000:0f:05.0-card" , "pci-0000:0f:05.0-render" },
379+ 2 ,
380+ },
381+ {
382+ "different by-path files" ,
383+ "00.10.2/00.334.302/0.0.1.00/0000:ff:05.0/drm/" + cardName ,
384+ []string {"pci-0000:0f:05.0-card" , "pci-0000:0f:05.0-render" },
385+ 0 ,
386+ },
387+ {
388+ "invalid pci address" ,
389+ "00.10.2/00.334.302/0.0.1.00/000:ff:05.1/drm/" + cardName ,
390+ []string {"pci-0000:0f:05.0-card" , "pci-0000:0f:05.0-render" },
391+ 0 ,
392+ },
393+ {
394+ "symlink without card" ,
395+ "00.10.2/00.334.302/0.0.1.00/0000:0f:05.0/drm" ,
396+ []string {"pci-0000:0f:05.0-card" , "pci-0000:0f:05.0-render" },
397+ 0 ,
398+ },
399+ {
400+ "no symlink" ,
401+ "" ,
402+ []string {"pci-0000:0f:05.0-card" , "pci-0000:0f:05.0-render" },
403+ 0 ,
404+ },
405+ {
406+ "no by-path files" ,
407+ "00.10.2/00.334.302/0.0.1.00/0000:0f:05.0/drm/" + cardName ,
408+ []string {},
409+ 0 ,
410+ },
411+ }
412+
413+ for _ , td := range tds {
414+ root , err := os .MkdirTemp ("" , "test_bypath_mounting" )
415+ if err != nil {
416+ t .Fatalf ("can't create temporary directory: %+v" , err )
417+ }
418+ // dirs/files need to be removed for the next test
419+ defer os .RemoveAll (root )
420+
421+ plugin := newDevicePlugin ("/" , "/" , cliOptions {})
422+
423+ drmPath , byPath := createBypathTestFiles (t , cardName , root , td .linkpath , td .bypathFiles )
424+
425+ mounts := plugin .bypathMountsForPci (drmPath , cardName , byPath )
426+
427+ if len (mounts ) != td .mountCount {
428+ t .Errorf ("%s: Wrong number of mounts %d vs. %d" , td .desc , len (mounts ), td .mountCount )
429+ }
430+
431+ absPaths := []string {}
432+ for _ , link := range td .bypathFiles {
433+ absPaths = append (absPaths , path .Join (byPath , link ))
434+ }
435+
436+ for _ , mount := range mounts {
437+ if ! slices .Contains (absPaths , mount .ContainerPath ) {
438+ t .Errorf ("%s: containerpath is incorrect: %s" , td .desc , mount .ContainerPath )
439+ }
440+
441+ if ! slices .Contains (absPaths , mount .HostPath ) {
442+ t .Errorf ("%s: hostpath is incorrect: %s" , td .desc , mount .HostPath )
443+ }
444+ }
445+ }
446+ }
0 commit comments