@@ -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+
114171func TestNvpciIOMMUFD (t * testing.T ) {
115172 testCases := []struct {
116173 Description string
0 commit comments