mirror of
https://github.com/clearml/go-nvlib
synced 2025-03-03 18:52:30 +00:00
Extract MockNVDeviceLib into helper function in nvdev tests
Signed-off-by: Kevin Klues <kklues@nvidia.com>
This commit is contained in:
parent
bdc2553e78
commit
2b4f40a90b
@ -24,6 +24,58 @@ import (
|
||||
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvml"
|
||||
)
|
||||
|
||||
func newMockDeviceLib() Interface {
|
||||
mockDevice := &nvml.DeviceMock{
|
||||
GetNameFunc: func() (string, nvml.Return) {
|
||||
return "MockDevice", nvml.SUCCESS
|
||||
},
|
||||
GetMigModeFunc: func() (int, int, nvml.Return) {
|
||||
return nvml.DEVICE_MIG_ENABLE, nvml.DEVICE_MIG_ENABLE, nvml.SUCCESS
|
||||
},
|
||||
GetMemoryInfoFunc: func() (nvml.Memory, nvml.Return) {
|
||||
memory := nvml.Memory{
|
||||
Total: 40 * 1024 * 1024 * 1024,
|
||||
}
|
||||
return memory, nvml.SUCCESS
|
||||
},
|
||||
GetGpuInstanceProfileInfoFunc: func(Profile int) (nvml.GpuInstanceProfileInfo, nvml.Return) {
|
||||
info := nvml.GpuInstanceProfileInfo{}
|
||||
switch Profile {
|
||||
case nvml.GPU_INSTANCE_PROFILE_1_SLICE,
|
||||
nvml.GPU_INSTANCE_PROFILE_1_SLICE_REV1:
|
||||
info.MemorySizeMB = 5 * 1024
|
||||
case nvml.GPU_INSTANCE_PROFILE_1_SLICE_REV2:
|
||||
info.MemorySizeMB = 10 * 1024
|
||||
case nvml.GPU_INSTANCE_PROFILE_2_SLICE,
|
||||
nvml.GPU_INSTANCE_PROFILE_2_SLICE_REV1:
|
||||
info.MemorySizeMB = 10 * 1024
|
||||
case nvml.GPU_INSTANCE_PROFILE_3_SLICE:
|
||||
info.MemorySizeMB = 20 * 1024
|
||||
case nvml.GPU_INSTANCE_PROFILE_4_SLICE:
|
||||
info.MemorySizeMB = 20 * 1024
|
||||
case nvml.GPU_INSTANCE_PROFILE_7_SLICE:
|
||||
info.MemorySizeMB = 40 * 1024
|
||||
case nvml.GPU_INSTANCE_PROFILE_6_SLICE,
|
||||
nvml.GPU_INSTANCE_PROFILE_8_SLICE:
|
||||
fallthrough
|
||||
default:
|
||||
return info, nvml.ERROR_NOT_SUPPORTED
|
||||
}
|
||||
return info, nvml.SUCCESS
|
||||
},
|
||||
}
|
||||
mockNvml := &nvml.InterfaceMock{
|
||||
DeviceGetCountFunc: func() (int, nvml.Return) {
|
||||
return 1, nvml.SUCCESS
|
||||
},
|
||||
DeviceGetHandleByIndexFunc: func(Index int) (nvml.Device, nvml.Return) {
|
||||
return mockDevice, nvml.SUCCESS
|
||||
},
|
||||
}
|
||||
|
||||
return New(WithNvml(mockNvml), WithVerifySymbols(false))
|
||||
}
|
||||
|
||||
func TestParseMigProfile(t *testing.T) {
|
||||
testCases := []struct {
|
||||
description string
|
||||
@ -291,55 +343,7 @@ func TestParseMigProfile(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
mockDevice := &nvml.DeviceMock{
|
||||
GetNameFunc: func() (string, nvml.Return) {
|
||||
return "MockDevice", nvml.SUCCESS
|
||||
},
|
||||
GetMigModeFunc: func() (int, int, nvml.Return) {
|
||||
return nvml.DEVICE_MIG_ENABLE, nvml.DEVICE_MIG_ENABLE, nvml.SUCCESS
|
||||
},
|
||||
GetMemoryInfoFunc: func() (nvml.Memory, nvml.Return) {
|
||||
memory := nvml.Memory{
|
||||
Total: 40 * 1024 * 1024 * 1024,
|
||||
}
|
||||
return memory, nvml.SUCCESS
|
||||
},
|
||||
GetGpuInstanceProfileInfoFunc: func(Profile int) (nvml.GpuInstanceProfileInfo, nvml.Return) {
|
||||
info := nvml.GpuInstanceProfileInfo{}
|
||||
switch Profile {
|
||||
case nvml.GPU_INSTANCE_PROFILE_1_SLICE,
|
||||
nvml.GPU_INSTANCE_PROFILE_1_SLICE_REV1:
|
||||
info.MemorySizeMB = 5 * 1024
|
||||
case nvml.GPU_INSTANCE_PROFILE_1_SLICE_REV2:
|
||||
info.MemorySizeMB = 10 * 1024
|
||||
case nvml.GPU_INSTANCE_PROFILE_2_SLICE,
|
||||
nvml.GPU_INSTANCE_PROFILE_2_SLICE_REV1:
|
||||
info.MemorySizeMB = 10 * 1024
|
||||
case nvml.GPU_INSTANCE_PROFILE_3_SLICE:
|
||||
info.MemorySizeMB = 20 * 1024
|
||||
case nvml.GPU_INSTANCE_PROFILE_4_SLICE:
|
||||
info.MemorySizeMB = 20 * 1024
|
||||
case nvml.GPU_INSTANCE_PROFILE_7_SLICE:
|
||||
info.MemorySizeMB = 40 * 1024
|
||||
case nvml.GPU_INSTANCE_PROFILE_6_SLICE,
|
||||
nvml.GPU_INSTANCE_PROFILE_8_SLICE:
|
||||
fallthrough
|
||||
default:
|
||||
return info, nvml.ERROR_NOT_SUPPORTED
|
||||
}
|
||||
return info, nvml.SUCCESS
|
||||
},
|
||||
}
|
||||
mockNvml := &nvml.InterfaceMock{
|
||||
DeviceGetCountFunc: func() (int, nvml.Return) {
|
||||
return 1, nvml.SUCCESS
|
||||
},
|
||||
DeviceGetHandleByIndexFunc: func(Index int) (nvml.Device, nvml.Return) {
|
||||
return mockDevice, nvml.SUCCESS
|
||||
},
|
||||
}
|
||||
|
||||
d := New(WithNvml(mockNvml), WithVerifySymbols(false))
|
||||
d := newMockDeviceLib()
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.description, func(t *testing.T) {
|
||||
err := d.AssertValidMigProfileFormat(tc.device)
|
||||
|
Loading…
Reference in New Issue
Block a user