-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for CDI spec generation for passthrough devices
Signed-off-by: Christopher Desiniotis <cdesiniotis@nvidia.com>
- Loading branch information
1 parent
808d390
commit c115fd1
Showing
4 changed files
with
625 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/** | ||
# Copyright (c) NVIDIA CORPORATION. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
**/ | ||
|
||
package cdi | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/container-orchestrated-devices/container-device-interface/specs-go" | ||
"github.com/stretchr/testify/require" | ||
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvpci" | ||
) | ||
|
||
func TestGetAllDeviceSpecs(t *testing.T) { | ||
testCases := []struct { | ||
description string | ||
lib *vfiolib | ||
expectedDeviceSpecs []specs.Device | ||
}{ | ||
{ | ||
description: "no NVIDIA devices", | ||
lib: &vfiolib{ | ||
nvpcilib: &nvpciInterfaceMock{ | ||
GetGPUsFunc: func() ([]*nvpci.NvidiaPCIDevice, error) { | ||
devices := []*nvpci.NvidiaPCIDevice{} | ||
return devices, nil | ||
}, | ||
}, | ||
}, | ||
expectedDeviceSpecs: nil, | ||
}, | ||
{ | ||
description: "one NVIDIA device, not bound to vfio-pci", | ||
lib: &vfiolib{ | ||
nvpcilib: &nvpciInterfaceMock{ | ||
GetGPUsFunc: func() ([]*nvpci.NvidiaPCIDevice, error) { | ||
devices := []*nvpci.NvidiaPCIDevice{ | ||
{ | ||
Address: "000:3B:00.0", | ||
Device: 0x2331, | ||
IommuGroup: -1, | ||
Driver: "nvidia", | ||
}, | ||
} | ||
return devices, nil | ||
}, | ||
}, | ||
}, | ||
expectedDeviceSpecs: nil, | ||
}, | ||
{ | ||
description: "one NVIDIA device, bound to vfio-pci", | ||
lib: &vfiolib{ | ||
nvpcilib: &nvpciInterfaceMock{ | ||
GetGPUsFunc: func() ([]*nvpci.NvidiaPCIDevice, error) { | ||
devices := []*nvpci.NvidiaPCIDevice{ | ||
{ | ||
Address: "000:3B:00.0", | ||
Device: 0x2331, | ||
IommuGroup: 60, | ||
Driver: "vfio-pci", | ||
}, | ||
} | ||
return devices, nil | ||
}, | ||
}, | ||
}, | ||
expectedDeviceSpecs: []specs.Device{ | ||
{ | ||
Name: "0", | ||
ContainerEdits: specs.ContainerEdits{ | ||
DeviceNodes: []*specs.DeviceNode{ | ||
{ | ||
Path: "/dev/vfio/60", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
description: "multiple NVIDIA devices, one bound to vfio-pci", | ||
lib: &vfiolib{ | ||
nvpcilib: &nvpciInterfaceMock{ | ||
GetGPUsFunc: func() ([]*nvpci.NvidiaPCIDevice, error) { | ||
devices := []*nvpci.NvidiaPCIDevice{ | ||
{ | ||
Address: "000:3B:00.0", | ||
Device: 0x2331, | ||
IommuGroup: 60, | ||
Driver: "vfio-pci", | ||
}, | ||
{ | ||
Address: "000:86:00.0", | ||
Device: 0x2331, | ||
IommuGroup: -1, | ||
Driver: "", | ||
}, | ||
} | ||
return devices, nil | ||
}, | ||
}, | ||
}, | ||
expectedDeviceSpecs: []specs.Device{ | ||
{ | ||
Name: "0", | ||
ContainerEdits: specs.ContainerEdits{ | ||
DeviceNodes: []*specs.DeviceNode{ | ||
{ | ||
Path: "/dev/vfio/60", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
description: "multiple NVIDIA devices, all bound to vfio-pci", | ||
lib: &vfiolib{ | ||
nvpcilib: &nvpciInterfaceMock{ | ||
GetGPUsFunc: func() ([]*nvpci.NvidiaPCIDevice, error) { | ||
devices := []*nvpci.NvidiaPCIDevice{ | ||
{ | ||
Address: "000:3B:00.0", | ||
Device: 0x2331, | ||
IommuGroup: 60, | ||
Driver: "vfio-pci", | ||
}, | ||
{ | ||
Address: "000:86:00.0", | ||
Device: 0x2331, | ||
IommuGroup: 90, | ||
Driver: "vfio-pci", | ||
}, | ||
} | ||
return devices, nil | ||
}, | ||
}, | ||
}, | ||
expectedDeviceSpecs: []specs.Device{ | ||
{ | ||
Name: "0", | ||
ContainerEdits: specs.ContainerEdits{ | ||
DeviceNodes: []*specs.DeviceNode{ | ||
{ | ||
Path: "/dev/vfio/60", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "1", | ||
ContainerEdits: specs.ContainerEdits{ | ||
DeviceNodes: []*specs.DeviceNode{ | ||
{ | ||
Path: "/dev/vfio/90", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.description, func(t *testing.T) { | ||
deviceSpecs, err := tc.lib.GetAllDeviceSpecs() | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expectedDeviceSpecs, deviceSpecs) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.