Files
josh 21014c1268
CI / Lint + build + test (push) Successful in 1m37s
Release / release (push) Successful in 11m43s
fix(inventory): read GPU model from device field, not vendor field
`lspci -D -mm -nn` prefixes every line with the PCI address as a bare
token before the three quoted class/vendor/device fields, so the
device name sits at fields[3] — not fields[2], which is the vendor.
The probe was indexing [2] and recording every GPU's model as its
vendor string ("Intel Corporation" instead of "Alder Lake-N [UHD
Graphics]"), which made every SpecValidate mismatch on real hosts
once the expected spec named the device.

Extract the per-line parse into parseLspciMMLine, handle both the
modern -D layout (addr + class/vendor/device) and the legacy
layout without an address prefix (class/vendor/device), and cover
both paths plus the non-GPU-class skip in inventory_test.go.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-19 22:53:42 -04:00

223 lines
5.6 KiB
Go

package probes
import (
"strings"
"testing"
"vetting/internal/spec"
)
func TestParseCPUInfoInto(t *testing.T) {
sample := `processor : 0
vendor_id : GenuineIntel
model name : Intel(R) N95
stepping : 3
cpu cores : 4
flags : fpu vme de pse tsc msr aes sse4_2 vmx
processor : 1
vendor_id : GenuineIntel
model name : Intel(R) N95
`
var c spec.CPUSpec
parseCPUInfoInto(strings.NewReader(sample), &c)
if c.Model != "Intel(R) N95" {
t.Fatalf("model: got %q", c.Model)
}
if c.Vendor != "GenuineIntel" {
t.Fatalf("vendor: got %q", c.Vendor)
}
if c.Stepping != "3" {
t.Fatalf("stepping: got %q", c.Stepping)
}
if c.PhysicalCores != 4 {
t.Fatalf("physical cores: got %d", c.PhysicalCores)
}
// Only vmx, aes, sse4_2 should survive the allow-list filter.
if len(c.Flags) != 3 {
t.Fatalf("flags count: got %v", c.Flags)
}
}
func TestParseOSRelease(t *testing.T) {
in := `PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
VERSION_ID="12"
NAME=Debian
`
dist, ver := parseOSRelease(in)
if dist != "Debian GNU/Linux 12 (bookworm)" {
t.Fatalf("distribution: got %q", dist)
}
if ver != "12" {
t.Fatalf("version: got %q", ver)
}
}
func TestParseDmidecodeSize(t *testing.T) {
cases := []struct {
in string
want int
}{
{"16 GB", 16},
{"32 GiB", 32},
{"8192 MB", 8},
{"No Module Installed", 0},
{"garbage", 0},
}
for _, c := range cases {
if got := parseDmidecodeSize(c.in); got != c.want {
t.Errorf("parseDmidecodeSize(%q) = %d, want %d", c.in, got, c.want)
}
}
}
func TestParseMTS(t *testing.T) {
if n := parseMTS("3200 MT/s"); n != 3200 {
t.Errorf("parseMTS MT/s: got %d", n)
}
if n := parseMTS("2400 MHz"); n != 2400 {
t.Errorf("parseMTS MHz: got %d", n)
}
if n := parseMTS("Unknown"); n != 0 {
t.Errorf("parseMTS unknown: got %d", n)
}
}
func TestCleanDmidecodeValue(t *testing.T) {
cases := map[string]string{
"Micron": "Micron",
"Not Specified": "",
" Unknown ": "",
"To Be Filled By O.E.M.": "",
"": "",
}
for in, want := range cases {
if got := cleanDmidecodeValue(in); got != want {
t.Errorf("cleanDmidecodeValue(%q) = %q, want %q", in, got, want)
}
}
}
func TestParseDIMMs(t *testing.T) {
// Two populated + two empty slots. Verifies the multi-section parser
// produces the expected count and the empty-slot detection fires on
// "No Module Installed".
sample := `Handle 0x0032, DMI type 17, 84 bytes
Memory Device
Size: 16 GB
Locator: DIMM_A1
Manufacturer: Micron
Part Number: 8ATF2G64AZ-3G2E1
Configured Memory Speed: 3200 MT/s
Handle 0x0033, DMI type 17, 84 bytes
Memory Device
Size: No Module Installed
Locator: DIMM_A2
Manufacturer: Not Specified
Handle 0x0034, DMI type 17, 84 bytes
Memory Device
Size: 16 GB
Locator: DIMM_B1
Manufacturer: Micron
Part Number: 8ATF2G64AZ-3G2E1
Speed: 3200 MT/s
Handle 0x0035, DMI type 17, 84 bytes
Memory Device
Size: No Module Installed
Locator: DIMM_B2
`
dimms := parseDIMMs(strings.NewReader(sample), Logger{})
if len(dimms) != 4 {
t.Fatalf("expected 4 DIMM entries, got %d: %+v", len(dimms), dimms)
}
populated := 0
for _, d := range dimms {
if d.Populated {
populated++
}
}
if populated != 2 {
t.Fatalf("expected 2 populated, got %d", populated)
}
if dimms[0].Slot != "DIMM_A1" || dimms[0].SizeGB != 16 || dimms[0].SpeedMTS != 3200 {
t.Fatalf("DIMM_A1 mismatch: %+v", dimms[0])
}
if dimms[0].PartNumber != "8ATF2G64AZ-3G2E1" {
t.Fatalf("DIMM_A1 part number mismatch: %q", dimms[0].PartNumber)
}
if dimms[1].Populated || dimms[1].Slot != "DIMM_A2" {
t.Fatalf("DIMM_A2 should be empty: %+v", dimms[1])
}
}
func TestParseLspciMMLine(t *testing.T) {
cases := []struct {
name string
line string
wantAddr string
wantModel string
wantSkipped bool
}{
{
name: "with -D, N95 integrated GPU",
line: `0000:00:02.0 "VGA compatible controller [0300]" "Intel Corporation [8086]" "Alder Lake-N [UHD Graphics] [46d0]" -r1c "Intel Corporation [8086]" "Device [7270]"`,
wantAddr: "0000:00:02.0",
wantModel: "Alder Lake-N [UHD Graphics]",
},
{
name: "without -D, legacy three-quoted layout",
line: `"VGA compatible controller" "NVIDIA Corporation" "GP104 [GeForce GTX 1080]"`,
wantAddr: "",
wantModel: "GP104 [GeForce GTX 1080]",
},
{
name: "3D controller class also picked up",
line: `0000:01:00.0 "3D controller [0302]" "NVIDIA Corporation [10de]" "TU104GL [Tesla T4] [1eb8]"`,
wantAddr: "0000:01:00.0",
wantModel: "TU104GL [Tesla T4]",
},
{
name: "non-GPU class is skipped",
line: `0000:01:00.0 "Ethernet controller [0200]" "Intel Corporation [8086]" "I350 [1521]"`,
wantSkipped: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
addr, model := parseLspciMMLine(tc.line)
if tc.wantSkipped {
if model != "" {
t.Fatalf("expected skip, got addr=%q model=%q", addr, model)
}
return
}
if addr != tc.wantAddr {
t.Errorf("addr: got %q want %q", addr, tc.wantAddr)
}
if model != tc.wantModel {
t.Errorf("model: got %q want %q", model, tc.wantModel)
}
})
}
}
func TestParseLspciVerbose(t *testing.T) {
sample := `01:00.0 VGA compatible controller: NVIDIA Corporation
Subsystem: ASUSTeK
Kernel driver in use: nvidia
Memory at f6000000 (32-bit, non-prefetchable) [size=16M]
Memory at c0000000 (64-bit, prefetchable) [size=8G]
Memory at d0000000 (64-bit, prefetchable) [size=32M]
`
vram, driver := parseLspciVerbose(sample)
if vram != 8 {
t.Errorf("VRAM: got %d, want 8", vram)
}
if driver != "nvidia" {
t.Errorf("driver: got %q", driver)
}
}