package discovery import ( "os" "path/filepath" "testing" ) func TestNVIDIADriverFrom(t *testing.T) { for _, tc := range []struct{ text, want string }{ {"NVRM version: NVIDIA UNIX x86_64 Kernel Module 570.86.15 Thu Jan 23 21:30:38 UTC 2025\nGCC version: gcc version 12.2.0\n", "570.86.15"}, {"NVRM version: NVIDIA UNIX Open Kernel Module for x86_64 580.82.07 Release Build (dvs-builder@U16-I3-B03-4-3) Wed Aug 27 00:51:12 UTC 2025\n", "580.82.07"}, {"NVRM version: NVIDIA UNIX Open Kernel Module for aarch64 580.95.05 Release Build\n", "580.95.05"}, {"NVRM version: NVIDIA UNIX x86_64 Kernel Module 535.54 Mon\n", "535.54"}, {"something else\n", ""}, } { p := filepath.Join(t.TempDir(), "version") if err := os.WriteFile(p, []byte(tc.text), 0o600); err != nil { t.Fatal(err) } if got := nvidiaDriverFrom(p); got != tc.want { t.Errorf("%q: got %q, want %q", tc.text, got, tc.want) } } } func TestACardOnTheBusIsSeenWithoutADriver(t *testing.T) { root := t.TempDir() dev := func(name, vendor, class string) { d := filepath.Join(root, name) if err := os.MkdirAll(d, 0o750); err != nil { t.Fatal(err) } _ = os.WriteFile(filepath.Join(d, "vendor"), []byte(vendor+"\n"), 0o600) _ = os.WriteFile(filepath.Join(d, "class"), []byte(class+"\n"), 0o600) } dev("0000:00:1f.0", "0x8086", "0x060100") // the chipset if hasNVIDIADeviceIn(root) { t.Fatal("a machine with no NVIDIA device said it had one") } dev("0000:01:00.1", "0x10de", "0x040300") // the card's HDMI audio alone is not a GPU if hasNVIDIADeviceIn(root) { t.Fatal("NVIDIA's audio device was taken for a GPU") } dev("0000:01:00.0", "0x10de", "0x030000") if !hasNVIDIADeviceIn(root) { t.Fatal("a VGA controller of NVIDIA's was not seen") } h := Host{NVIDIADevice: true} if !h.CardWithoutDriver() { t.Error("a card with no driver version is a card without a driver") } h.NVIDIADriver = "580.82.07" if h.CardWithoutDriver() { t.Error("a card with a driver was reported as without one") } }