Skip to content

Commit e0f2e8c

Browse files
Add subsystem missing-file warning and partial/malformed nvpci tests
Signed-off-by: Karthik Vetrivel <kvetrivel@nvidia.com>
1 parent 6d17ff8 commit e0f2e8c

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

pkg/nvpci/nvpci.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ func (p *nvpci) getNvidiaDeviceByPciBusID(address string, cache map[string]*Nvid
309309
if err != nil {
310310
return nil, fmt.Errorf("unable to convert subsystem vendor string to uint16: %v", subsystemVendorStr)
311311
}
312-
case !os.IsNotExist(err):
312+
case os.IsNotExist(err):
313+
p.logger.Warningf("subsystem_vendor file not found for %s", address)
314+
default:
313315
return nil, fmt.Errorf("unable to read PCI subsystem vendor id for %s: %v", address, err)
314316
}
315317

@@ -322,7 +324,9 @@ func (p *nvpci) getNvidiaDeviceByPciBusID(address string, cache map[string]*Nvid
322324
if err != nil {
323325
return nil, fmt.Errorf("unable to convert subsystem device string to uint16: %v", subsystemDeviceStr)
324326
}
325-
case !os.IsNotExist(err):
327+
case os.IsNotExist(err):
328+
p.logger.Warningf("subsystem_device file not found for %s", address)
329+
default:
326330
return nil, fmt.Errorf("unable to read PCI subsystem device id for %s: %v", address, err)
327331
}
328332

pkg/nvpci/nvpci_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,63 @@ func TestNvpciSubsystemMissing(t *testing.T) {
111111
require.Equal(t, uint16(0), devices[0].SubsystemDevice, "SubsystemDevice should default to 0 when sysfs file is absent")
112112
}
113113

114+
func TestNvpciSubsystemPartial(t *testing.T) {
115+
testCases := []struct {
116+
Description string
117+
RemoveFile string
118+
SubsystemVendor uint16
119+
SubsystemDevice uint16
120+
}{
121+
{
122+
Description: "subsystem_vendor missing",
123+
RemoveFile: "subsystem_vendor",
124+
SubsystemVendor: 0,
125+
SubsystemDevice: 0x16c0,
126+
},
127+
{
128+
Description: "subsystem_device missing",
129+
RemoveFile: "subsystem_device",
130+
SubsystemVendor: 0x10de,
131+
SubsystemDevice: 0,
132+
},
133+
}
134+
135+
for _, tc := range testCases {
136+
t.Run(tc.Description, func(t *testing.T) {
137+
nvpci, err := NewMockNvpci()
138+
require.Nil(t, err, "Error creating NewMockNvpci")
139+
defer nvpci.Cleanup()
140+
141+
err = nvpci.AddMockA100("0000:80:05.1", 0, nil)
142+
require.Nil(t, err, "Error adding Mock A100 device to MockNvpci")
143+
144+
deviceDir := filepath.Join(nvpci.pciDevicesRoot, "0000:80:05.1")
145+
require.NoError(t, os.Remove(filepath.Join(deviceDir, tc.RemoveFile)))
146+
147+
devices, err := nvpci.GetGPUs()
148+
require.Nil(t, err, "Error getting GPUs")
149+
require.Equal(t, 1, len(devices), "Wrong number of GPU devices")
150+
require.Equal(t, tc.SubsystemVendor, devices[0].SubsystemVendor, "Wrong SubsystemVendor for device")
151+
require.Equal(t, tc.SubsystemDevice, devices[0].SubsystemDevice, "Wrong SubsystemDevice for device")
152+
})
153+
}
154+
}
155+
156+
func TestNvpciSubsystemMalformed(t *testing.T) {
157+
nvpci, err := NewMockNvpci()
158+
require.Nil(t, err, "Error creating NewMockNvpci")
159+
defer nvpci.Cleanup()
160+
161+
err = nvpci.AddMockA100("0000:80:05.1", 0, nil)
162+
require.Nil(t, err, "Error adding Mock A100 device to MockNvpci")
163+
164+
deviceDir := filepath.Join(nvpci.pciDevicesRoot, "0000:80:05.1")
165+
require.NoError(t, os.WriteFile(filepath.Join(deviceDir, "subsystem_vendor"), []byte("notanid"), 0644))
166+
167+
_, err = nvpci.GetGPUs()
168+
require.Error(t, err, "Expected error when subsystem_vendor contents are malformed")
169+
}
170+
114171
func TestNvpciIOMMUFD(t *testing.T) {
115172
testCases := []struct {
116173
Description string

0 commit comments

Comments
 (0)