mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-06-12 17:41:02 +00:00
Following the refactoring of device request extraction, we can now make CDI device requests consistent with other methods. This change moves to using image.VisibleDevices instead of separate calls to CDIDevicesFromMounts and VisibleDevicesFromEnvVar. The handling of annotation-based requests will be addressed in a follow-up. Signed-off-by: Evan Lezar <elezar@nvidia.com>
163 lines
4.3 KiB
Go
163 lines
4.3 KiB
Go
/**
|
|
# 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 modifier
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
|
testlog "github.com/sirupsen/logrus/hooks/test"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
|
|
)
|
|
|
|
func TestGetAnnotationDevices(t *testing.T) {
|
|
testCases := []struct {
|
|
description string
|
|
prefixes []string
|
|
annotations map[string]string
|
|
expectedDevices []string
|
|
expectedError error
|
|
}{
|
|
{
|
|
description: "no annotations",
|
|
},
|
|
{
|
|
description: "no matching annotations",
|
|
prefixes: []string{"not-prefix/"},
|
|
annotations: map[string]string{
|
|
"prefix/foo": "example.com/device=bar",
|
|
},
|
|
},
|
|
{
|
|
description: "single matching annotation",
|
|
prefixes: []string{"prefix/"},
|
|
annotations: map[string]string{
|
|
"prefix/foo": "example.com/device=bar",
|
|
},
|
|
expectedDevices: []string{"example.com/device=bar"},
|
|
},
|
|
{
|
|
description: "multiple matching annotations",
|
|
prefixes: []string{"prefix/", "another-prefix/"},
|
|
annotations: map[string]string{
|
|
"prefix/foo": "example.com/device=bar",
|
|
"another-prefix/bar": "example.com/device=baz",
|
|
},
|
|
expectedDevices: []string{"example.com/device=bar", "example.com/device=baz"},
|
|
},
|
|
{
|
|
description: "multiple matching annotations with duplicate devices",
|
|
prefixes: []string{"prefix/", "another-prefix/"},
|
|
annotations: map[string]string{
|
|
"prefix/foo": "example.com/device=bar",
|
|
"another-prefix/bar": "example.com/device=bar",
|
|
},
|
|
expectedDevices: []string{"example.com/device=bar"},
|
|
},
|
|
{
|
|
description: "invalid devices",
|
|
prefixes: []string{"prefix/"},
|
|
annotations: map[string]string{
|
|
"prefix/foo": "example.com/device",
|
|
},
|
|
expectedError: fmt.Errorf("invalid device %q", "example.com/device"),
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
devices, err := getAnnotationDevices(tc.prefixes, tc.annotations)
|
|
if tc.expectedError != nil {
|
|
require.Error(t, err)
|
|
return
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
require.ElementsMatch(t, tc.expectedDevices, devices)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetDevicesFromSpec(t *testing.T) {
|
|
logger, _ := testlog.NewNullLogger()
|
|
|
|
testCases := []struct {
|
|
description string
|
|
input cdiModifier
|
|
spec *specs.Spec
|
|
expectedDevices []string
|
|
}{
|
|
{
|
|
description: "empty spec yields no devices",
|
|
},
|
|
{
|
|
description: "cdi devices from mounts",
|
|
input: cdiModifier{
|
|
defaultKind: "nvidia.com/gpu",
|
|
acceptEnvvarUnprivileged: true,
|
|
acceptDeviceListAsVolumeMounts: true,
|
|
},
|
|
spec: &specs.Spec{
|
|
Mounts: []specs.Mount{
|
|
{
|
|
Destination: "/var/run/nvidia-container-devices/cdi/nvidia.com/gpu/0",
|
|
Source: "/dev/null",
|
|
},
|
|
{
|
|
Destination: "/var/run/nvidia-container-devices/cdi/nvidia.com/gpu/1",
|
|
Source: "/dev/null",
|
|
},
|
|
},
|
|
},
|
|
expectedDevices: []string{"nvidia.com/gpu=0", "nvidia.com/gpu=1"},
|
|
},
|
|
{
|
|
description: "cdi devices from envvar",
|
|
input: cdiModifier{
|
|
defaultKind: "nvidia.com/gpu",
|
|
acceptEnvvarUnprivileged: true,
|
|
acceptDeviceListAsVolumeMounts: true,
|
|
},
|
|
spec: &specs.Spec{
|
|
Process: &specs.Process{
|
|
Env: []string{"NVIDIA_VISIBLE_DEVICES=0,example.com/class=device"},
|
|
},
|
|
},
|
|
expectedDevices: []string{"nvidia.com/gpu=0", "example.com/class=device"},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc.input.logger = logger
|
|
|
|
spec := &oci.SpecMock{
|
|
LoadFunc: func() (*specs.Spec, error) {
|
|
return tc.spec, nil
|
|
},
|
|
}
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
devices, err := tc.input.getDevicesFromSpec(spec)
|
|
require.NoError(t, err)
|
|
require.EqualValues(t, tc.expectedDevices, devices)
|
|
})
|
|
}
|
|
}
|