Skip to content

Commit

Permalink
add unit test to replicate error nvidia device rename
Browse files Browse the repository at this point in the history
Signed-off-by: Tariq Ibrahim <[email protected]>
  • Loading branch information
tariq1890 committed Feb 2, 2024
1 parent 15f609a commit f3e733f
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions internal/system/nvdevices/devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,26 @@ func TestCreateControlDevices(t *testing.T) {
},
}

// nvidiaDevices550 represents the device map from the nvidia gpu drivers >= 550.40.x
nvidiaDevices550 := &devices.DevicesMock{
GetFunc: func(name devices.Name) (devices.Major, bool) {
devices := map[devices.Name]devices.Major{
"nvidia": 195,
"nvidia-uvm": 243,
}
d, ok := devices[name]
return d, ok
},
}

mknodeError := errors.New("mknode error")

testCases := []struct {
description string
root string
devices devices.Devices
mknodeError error
hasError bool
expectedError error
expectedCalls []struct {
S string
Expand All @@ -58,6 +71,7 @@ func TestCreateControlDevices(t *testing.T) {
root: "",
devices: nvidiaDevices,
mknodeError: nil,
hasError: false,
expectedCalls: []struct {
S string
N1 int
Expand All @@ -73,6 +87,7 @@ func TestCreateControlDevices(t *testing.T) {
description: "some root specified",
root: "/some/root",
devices: nvidiaDevices,
hasError: false,
mknodeError: nil,
expectedCalls: []struct {
S string
Expand All @@ -88,6 +103,7 @@ func TestCreateControlDevices(t *testing.T) {
{
description: "mknod error returns error",
devices: nvidiaDevices,
hasError: true,
mknodeError: mknodeError,
expectedError: mknodeError,
// We expect the first call to this to fail, and the rest to be skipped
Expand All @@ -106,8 +122,16 @@ func TestCreateControlDevices(t *testing.T) {
return 0, false
},
},
hasError: true,
expectedError: errInvalidDeviceNode,
},
{
description: "nvidia device renamed from nvidia-frontend to nvidia",
devices: nvidiaDevices550,
hasError: true,
expectedError: errors.New("failed to create device node nvidiactl"),
expectedCalls: nil,
},
}

for _, tc := range testCases {
Expand All @@ -126,9 +150,12 @@ func TestCreateControlDevices(t *testing.T) {
d.mknoder = mknode

err := d.CreateNVIDIAControlDevices()
require.ErrorIs(t, err, tc.expectedError)
if tc.hasError {
require.ErrorContains(t, err, tc.expectedError.Error())
} else {
require.Nil(t, err)
}
require.EqualValues(t, tc.expectedCalls, mknode.MknodeCalls())
})
}

}

0 comments on commit f3e733f

Please sign in to comment.