mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-06-26 18:18:24 +00:00
Add proc.devices.New constructor
Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
@@ -29,29 +29,18 @@ import (
|
||||
func TestCreateControlDevices(t *testing.T) {
|
||||
logger, _ := testlog.NewNullLogger()
|
||||
|
||||
nvidiaDevices := &devices.DevicesMock{
|
||||
GetFunc: func(name devices.Name) (devices.Major, bool) {
|
||||
devs := map[devices.Name]devices.Major{
|
||||
"nvidia-frontend": 195,
|
||||
"nvidia-uvm": 243,
|
||||
}
|
||||
|
||||
// devs550_40 represents the device map from the nvidia gpu drivers >= 550.40.x
|
||||
devs550_40 := map[devices.Name]devices.Major{
|
||||
"nvidia": 195,
|
||||
"nvidia-uvm": 243,
|
||||
}
|
||||
|
||||
d, ok := devs[name]
|
||||
if ok {
|
||||
return d, ok
|
||||
}
|
||||
|
||||
// if device d is not found, fallback to the second mock device map
|
||||
d, ok = devs550_40[name]
|
||||
return d, ok
|
||||
},
|
||||
}
|
||||
nvidiaDevices := devices.New(
|
||||
devices.WithDeviceToMajor(map[string]int{
|
||||
"nvidia-frontend": 195,
|
||||
"nvidia-uvm": 243,
|
||||
}),
|
||||
)
|
||||
nvidia550Devices := devices.New(
|
||||
devices.WithDeviceToMajor(map[string]int{
|
||||
"nvidia": 195,
|
||||
"nvidia-uvm": 243,
|
||||
}),
|
||||
)
|
||||
|
||||
mknodeError := errors.New("mknode error")
|
||||
|
||||
@@ -60,7 +49,6 @@ func TestCreateControlDevices(t *testing.T) {
|
||||
root string
|
||||
devices devices.Devices
|
||||
mknodeError error
|
||||
hasError bool
|
||||
expectedError error
|
||||
expectedCalls []struct {
|
||||
S string
|
||||
@@ -69,11 +57,26 @@ func TestCreateControlDevices(t *testing.T) {
|
||||
}
|
||||
}{
|
||||
{
|
||||
description: "no root specified",
|
||||
description: "no root specified; pre 550 driver",
|
||||
root: "",
|
||||
devices: nvidiaDevices,
|
||||
mknodeError: nil,
|
||||
hasError: false,
|
||||
expectedCalls: []struct {
|
||||
S string
|
||||
N1 int
|
||||
N2 int
|
||||
}{
|
||||
{"/dev/nvidiactl", 195, 255},
|
||||
{"/dev/nvidia-modeset", 195, 254},
|
||||
{"/dev/nvidia-uvm", 243, 0},
|
||||
{"/dev/nvidia-uvm-tools", 243, 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "no root specified; 550 driver",
|
||||
root: "",
|
||||
devices: nvidia550Devices,
|
||||
mknodeError: nil,
|
||||
expectedCalls: []struct {
|
||||
S string
|
||||
N1 int
|
||||
@@ -89,7 +92,6 @@ func TestCreateControlDevices(t *testing.T) {
|
||||
description: "some root specified",
|
||||
root: "/some/root",
|
||||
devices: nvidiaDevices,
|
||||
hasError: false,
|
||||
mknodeError: nil,
|
||||
expectedCalls: []struct {
|
||||
S string
|
||||
@@ -105,7 +107,6 @@ 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
|
||||
@@ -124,24 +125,8 @@ func TestCreateControlDevices(t *testing.T) {
|
||||
return 0, false
|
||||
},
|
||||
},
|
||||
hasError: true,
|
||||
expectedError: errInvalidDeviceNode,
|
||||
},
|
||||
{
|
||||
description: "nvidia device renamed from nvidia-frontend to nvidia",
|
||||
devices: nvidiaDevices,
|
||||
hasError: false,
|
||||
expectedCalls: []struct {
|
||||
S string
|
||||
N1 int
|
||||
N2 int
|
||||
}{
|
||||
{"/dev/nvidiactl", 195, 255},
|
||||
{"/dev/nvidia-modeset", 195, 254},
|
||||
{"/dev/nvidia-uvm", 243, 0},
|
||||
{"/dev/nvidia-uvm-tools", 243, 1},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
@@ -160,11 +145,7 @@ func TestCreateControlDevices(t *testing.T) {
|
||||
d.mknoder = mknode
|
||||
|
||||
err := d.CreateNVIDIAControlDevices()
|
||||
if tc.hasError {
|
||||
require.ErrorContains(t, err, tc.expectedError.Error())
|
||||
} else {
|
||||
require.Nil(t, err)
|
||||
}
|
||||
require.ErrorIs(t, err, tc.expectedError)
|
||||
require.EqualValues(t, tc.expectedCalls, mknode.MknodeCalls())
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user