Skip to content

Commit

Permalink
Add unit tests for CDI spec generation for passthrough devices
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Desiniotis <cdesiniotis@nvidia.com>
  • Loading branch information
cdesiniotis committed Jul 21, 2023
1 parent 808d390 commit c115fd1
Show file tree
Hide file tree
Showing 4 changed files with 625 additions and 2 deletions.
185 changes: 185 additions & 0 deletions internal/cdi/lib-vfio_test.go
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)
})
}
}
4 changes: 2 additions & 2 deletions internal/cdi/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type nvcdilib struct {
vendor string
class string

nvpcilib nvpci.Interface
nvpcilib nvpciInterface
}

// New creates a new instance of this library
Expand All @@ -51,7 +51,7 @@ func New(opts ...Option) (nvcdi.Interface, error) {
type Option func(*nvcdilib)

// WithNvpciLib sets the nvpci library for the library
func WithNvpciLib(nvpcilib nvpci.Interface) Option {
func WithNvpciLib(nvpcilib nvpciInterface) Option {
return func(l *nvcdilib) {
l.nvpcilib = nvpcilib
}
Expand Down
Loading

0 comments on commit c115fd1

Please sign in to comment.