github.com/jaypipes/ghw@v0.21.1/pkg/linuxpath/path_linux_test.go (about) 1 // 2 // Use and distribution licensed under the Apache license version 2. 3 // 4 // See the COPYING file in the root project directory for full text. 5 // 6 7 //go:build linux 8 // +build linux 9 10 package linuxpath_test 11 12 import ( 13 "os" 14 "path/filepath" 15 "slices" 16 "sort" 17 "testing" 18 19 "github.com/jaypipes/ghw/pkg/context" 20 "github.com/jaypipes/ghw/pkg/gpu" 21 "github.com/jaypipes/ghw/pkg/linuxpath" 22 "github.com/jaypipes/ghw/pkg/option" 23 ) 24 25 func TestPathRoot(t *testing.T) { 26 orig, origExists := os.LookupEnv("GHW_CHROOT") 27 if origExists { 28 // For tests, save the original, test an override and then at the end 29 // of the test, reset to the original 30 defer os.Setenv("GHW_CHROOT", orig) 31 os.Unsetenv("GHW_CHROOT") 32 } else { 33 defer os.Unsetenv("GHW_CHROOT") 34 } 35 36 ctx := context.FromEnv() 37 paths := linuxpath.New(ctx) 38 39 // No environment variable is set for GHW_CHROOT, so pathProcCpuinfo() should 40 // return the default "/proc/cpuinfo" 41 path := paths.ProcCpuinfo 42 if path != "/proc/cpuinfo" { 43 t.Fatalf("Expected pathProcCpuInfo() to return '/proc/cpuinfo' but got %s", path) 44 } 45 46 // Now set the GHW_CHROOT environ variable and verify that pathRoot() 47 // returns that value 48 os.Setenv("GHW_CHROOT", "/host") 49 50 ctx = context.FromEnv() 51 paths = linuxpath.New(ctx) 52 53 path = paths.ProcCpuinfo 54 if path != "/host/proc/cpuinfo" { 55 t.Fatalf("Expected path.ProcCpuinfo to return '/host/proc/cpuinfo' but got %s", path) 56 } 57 } 58 59 func TestPathSpecificRoots(t *testing.T) { 60 ctx := context.New(option.WithPathOverrides(option.PathOverrides{ 61 "/proc": "/host-proc", 62 "/sys": "/host-sys", 63 })) 64 65 paths := linuxpath.New(ctx) 66 67 path := paths.ProcCpuinfo 68 expectedPath := "/host-proc/cpuinfo" 69 if path != expectedPath { 70 t.Fatalf("Expected path.ProcCpuInfo to return %q but got %q", expectedPath, path) 71 } 72 73 path = paths.SysBusPciDevices 74 expectedPath = "/host-sys/bus/pci/devices" 75 if path != expectedPath { 76 t.Fatalf("Expected path.SysBusPciDevices to return %q but got %q", expectedPath, path) 77 } 78 } 79 80 func TestPathChrootAndSpecifics(t *testing.T) { 81 ctx := context.New( 82 option.WithPathOverrides(option.PathOverrides{ 83 "/proc": "/host2-proc", 84 "/sys": "/host2-sys", 85 }), 86 option.WithChroot("/redirect"), 87 ) 88 89 paths := linuxpath.New(ctx) 90 91 path := paths.ProcCpuinfo 92 expectedPath := "/redirect/host2-proc/cpuinfo" 93 if path != expectedPath { 94 t.Fatalf("Expected path.ProcCpuInfo to return %q but got %q", expectedPath, path) 95 } 96 97 path = paths.SysBusPciDevices 98 expectedPath = "/redirect/host2-sys/bus/pci/devices" 99 if path != expectedPath { 100 t.Fatalf("Expected path.SysBusPciDevices to return %q but got %q", expectedPath, path) 101 } 102 } 103 104 func TestGpuPathRegexp(t *testing.T) { 105 tmp := t.TempDir() 106 107 // Make sure that last element of path is unique across other paths. 108 var paths = []string{ 109 "../../devices/pci0000:00/0000:00:03.1/0000:07:00.0/drm/card0", 110 "../../devices/pci0000:00/0000:00:0d.0/0000:01:00.1/drm/card1", 111 "../../devices/pci0000:25/0000:25:01.0/0000:26:00.0/drm/card2", 112 "../../devices/pci0000:89/0000:89:01.0/0000:8a:00.0/drm/card3", 113 } 114 115 // Expecting third element from paths above. 116 var expectedAddrs = []string{ 117 "0000:07:00.0", "0000:01:00.1", "0000:26:00.0", "0000:8a:00.0", 118 } 119 120 drmPath := filepath.Join(tmp, "/sys/class/drm") 121 err := os.MkdirAll(drmPath, 0755) 122 if err != nil { 123 t.Fatal(err) 124 } 125 for _, target := range paths { 126 linkname := filepath.Join(drmPath, filepath.Base(target)) 127 err := os.Symlink(target, linkname) 128 if err != nil { 129 t.Fatal(err) 130 } 131 } 132 133 info, err := gpu.New(option.WithChroot(tmp)) 134 if err != nil { 135 t.Fatalf("Expected nil err, but got %v", err) 136 } 137 138 if len(info.GraphicsCards) != len(expectedAddrs) { 139 t.Fatalf("Expected %d graphics cards, got %d", len(expectedAddrs), len(info.GraphicsCards)) 140 } 141 foundAddrs := make([]string, 0) 142 for _, card := range info.GraphicsCards { 143 foundAddrs = append(foundAddrs, card.Address) 144 } 145 146 sort.Strings(expectedAddrs) 147 sort.Strings(foundAddrs) 148 149 if !slices.Equal(expectedAddrs, foundAddrs) { 150 t.Fatalf("Some cards not found") 151 } 152 }