2018-09-01 00:08:36 +00:00
|
|
|
package nvidia
|
|
|
|
|
|
|
|
import (
|
2018-09-18 16:57:39 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-09-01 00:08:36 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/device/cmd/nvidia/nvml"
|
2018-09-18 16:57:39 +00:00
|
|
|
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/nomad/plugins/device"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-09-01 00:08:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MockNvmlClient struct {
|
|
|
|
FingerprintError error
|
|
|
|
FingerprintResponseReturned *nvml.FingerprintData
|
2018-09-07 17:13:50 +00:00
|
|
|
|
|
|
|
StatsError error
|
|
|
|
StatsResponseReturned []*nvml.StatsData
|
2018-09-01 00:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MockNvmlClient) GetFingerprintData() (*nvml.FingerprintData, error) {
|
|
|
|
return c.FingerprintResponseReturned, c.FingerprintError
|
|
|
|
}
|
2018-09-07 17:13:50 +00:00
|
|
|
|
|
|
|
func (c *MockNvmlClient) GetStatsData() ([]*nvml.StatsData, error) {
|
|
|
|
return c.StatsResponseReturned, c.StatsError
|
|
|
|
}
|
2018-09-18 16:57:39 +00:00
|
|
|
|
|
|
|
func TestReserve(t *testing.T) {
|
|
|
|
for _, testCase := range []struct {
|
|
|
|
Name string
|
|
|
|
ExpectedReservation *device.ContainerReservation
|
|
|
|
ExpectedError error
|
|
|
|
Device *NvidiaDevice
|
|
|
|
RequestedIDs []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "All RequestedIDs are not managed by Device",
|
|
|
|
ExpectedReservation: nil,
|
|
|
|
ExpectedError: &reservationError{[]string{
|
|
|
|
"UUID1",
|
|
|
|
"UUID2",
|
|
|
|
"UUID3",
|
|
|
|
}},
|
|
|
|
RequestedIDs: []string{
|
|
|
|
"UUID1",
|
|
|
|
"UUID2",
|
|
|
|
"UUID3",
|
|
|
|
},
|
|
|
|
Device: &NvidiaDevice{
|
|
|
|
logger: hclog.NewNullLogger(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Some RequestedIDs are not managed by Device",
|
|
|
|
ExpectedReservation: nil,
|
|
|
|
ExpectedError: &reservationError{[]string{
|
|
|
|
"UUID1",
|
|
|
|
"UUID2",
|
|
|
|
}},
|
|
|
|
RequestedIDs: []string{
|
|
|
|
"UUID1",
|
|
|
|
"UUID2",
|
|
|
|
"UUID3",
|
|
|
|
},
|
|
|
|
Device: &NvidiaDevice{
|
|
|
|
devices: map[string]struct{}{
|
|
|
|
"UUID3": {},
|
|
|
|
},
|
|
|
|
logger: hclog.NewNullLogger(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "All RequestedIDs are managed by Device",
|
|
|
|
ExpectedReservation: &device.ContainerReservation{
|
|
|
|
Envs: map[string]string{
|
|
|
|
nvidiaVisibleDevices: "UUID1,UUID2,UUID3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ExpectedError: nil,
|
|
|
|
RequestedIDs: []string{
|
|
|
|
"UUID1",
|
|
|
|
"UUID2",
|
|
|
|
"UUID3",
|
|
|
|
},
|
|
|
|
Device: &NvidiaDevice{
|
|
|
|
devices: map[string]struct{}{
|
|
|
|
"UUID1": {},
|
|
|
|
"UUID2": {},
|
|
|
|
"UUID3": {},
|
|
|
|
},
|
|
|
|
logger: hclog.NewNullLogger(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "No IDs requested",
|
|
|
|
ExpectedReservation: &device.ContainerReservation{},
|
|
|
|
ExpectedError: nil,
|
|
|
|
RequestedIDs: nil,
|
|
|
|
Device: &NvidiaDevice{
|
|
|
|
devices: map[string]struct{}{
|
|
|
|
"UUID1": {},
|
|
|
|
"UUID2": {},
|
|
|
|
"UUID3": {},
|
|
|
|
},
|
|
|
|
logger: hclog.NewNullLogger(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
actualReservation, actualError := testCase.Device.Reserve(testCase.RequestedIDs)
|
|
|
|
req := require.New(t)
|
|
|
|
req.Equal(testCase.ExpectedReservation, actualReservation)
|
|
|
|
req.Equal(testCase.ExpectedError, actualError)
|
|
|
|
}
|
|
|
|
}
|