2022-05-09 14:05:21 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 2022, 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 info
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-03-26 10:37:09 +00:00
|
|
|
"github.com/NVIDIA/go-nvlib/pkg/nvlib/info"
|
2023-10-10 11:48:38 +00:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2022-05-09 14:05:21 +00:00
|
|
|
testlog "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-12-01 01:10:10 +00:00
|
|
|
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/image"
|
2022-05-09 14:05:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestResolveAutoMode(t *testing.T) {
|
|
|
|
logger, _ := testlog.NewNullLogger()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
description string
|
|
|
|
mode string
|
|
|
|
expectedMode string
|
2023-08-25 07:58:06 +00:00
|
|
|
info map[string]bool
|
2023-10-10 11:48:38 +00:00
|
|
|
envmap map[string]string
|
|
|
|
mounts []string
|
2022-05-09 14:05:21 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "non-auto resolves to input",
|
|
|
|
mode: "not-auto",
|
|
|
|
expectedMode: "not-auto",
|
|
|
|
},
|
2023-06-05 13:47:38 +00:00
|
|
|
{
|
2023-08-25 07:58:06 +00:00
|
|
|
description: "no info defaults to legacy",
|
|
|
|
mode: "auto",
|
|
|
|
info: map[string]bool{},
|
|
|
|
expectedMode: "legacy",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "non-nvml, non-tegra, nvgpu resolves to csv",
|
2023-06-05 13:47:38 +00:00
|
|
|
mode: "auto",
|
2023-08-25 07:58:06 +00:00
|
|
|
info: map[string]bool{
|
|
|
|
"nvml": false,
|
|
|
|
"tegra": false,
|
|
|
|
"nvgpu": true,
|
2023-06-05 13:47:38 +00:00
|
|
|
},
|
2023-08-25 07:58:06 +00:00
|
|
|
expectedMode: "csv",
|
2023-06-05 13:47:38 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-25 07:58:06 +00:00
|
|
|
description: "non-nvml, tegra, non-nvgpu resolves to csv",
|
2023-06-05 13:47:38 +00:00
|
|
|
mode: "auto",
|
2023-08-25 07:58:06 +00:00
|
|
|
info: map[string]bool{
|
|
|
|
"nvml": false,
|
|
|
|
"tegra": true,
|
|
|
|
"nvgpu": false,
|
|
|
|
},
|
|
|
|
expectedMode: "csv",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "non-nvml, tegra, nvgpu resolves to csv",
|
|
|
|
mode: "auto",
|
|
|
|
info: map[string]bool{
|
|
|
|
"nvml": false,
|
|
|
|
"tegra": true,
|
|
|
|
"nvgpu": true,
|
|
|
|
},
|
|
|
|
expectedMode: "csv",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "nvml, non-tegra, non-nvgpu resolves to legacy",
|
|
|
|
mode: "auto",
|
|
|
|
info: map[string]bool{
|
|
|
|
"nvml": true,
|
|
|
|
"tegra": false,
|
|
|
|
"nvgpu": false,
|
2023-06-05 13:47:38 +00:00
|
|
|
},
|
|
|
|
expectedMode: "legacy",
|
|
|
|
},
|
|
|
|
{
|
2023-08-25 07:58:06 +00:00
|
|
|
description: "nvml, non-tegra, nvgpu resolves to csv",
|
2023-06-05 13:47:38 +00:00
|
|
|
mode: "auto",
|
2023-08-25 07:58:06 +00:00
|
|
|
info: map[string]bool{
|
|
|
|
"nvml": true,
|
|
|
|
"tegra": false,
|
|
|
|
"nvgpu": true,
|
|
|
|
},
|
|
|
|
expectedMode: "csv",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "nvml, tegra, non-nvgpu resolves to legacy",
|
|
|
|
mode: "auto",
|
|
|
|
info: map[string]bool{
|
|
|
|
"nvml": true,
|
|
|
|
"tegra": true,
|
|
|
|
"nvgpu": false,
|
2023-06-05 13:47:38 +00:00
|
|
|
},
|
|
|
|
expectedMode: "legacy",
|
|
|
|
},
|
|
|
|
{
|
2023-08-25 07:58:06 +00:00
|
|
|
description: "nvml, tegra, nvgpu resolves to csv",
|
2023-06-05 13:47:38 +00:00
|
|
|
mode: "auto",
|
2023-08-25 07:58:06 +00:00
|
|
|
info: map[string]bool{
|
|
|
|
"nvml": true,
|
|
|
|
"tegra": true,
|
|
|
|
"nvgpu": true,
|
2023-06-05 13:47:38 +00:00
|
|
|
},
|
|
|
|
expectedMode: "csv",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "cdi devices resolves to cdi",
|
|
|
|
mode: "auto",
|
|
|
|
expectedMode: "cdi",
|
2023-10-10 11:48:38 +00:00
|
|
|
envmap: map[string]string{
|
2023-06-05 13:47:38 +00:00
|
|
|
"NVIDIA_VISIBLE_DEVICES": "nvidia.com/gpu=all",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "multiple cdi devices resolves to cdi",
|
|
|
|
mode: "auto",
|
|
|
|
expectedMode: "cdi",
|
2023-10-10 11:48:38 +00:00
|
|
|
envmap: map[string]string{
|
2023-06-05 13:47:38 +00:00
|
|
|
"NVIDIA_VISIBLE_DEVICES": "nvidia.com/gpu=0,nvidia.com/gpu=1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "at least one non-cdi device resolves to legacy",
|
|
|
|
mode: "auto",
|
2023-10-10 11:48:38 +00:00
|
|
|
envmap: map[string]string{
|
2023-06-05 13:47:38 +00:00
|
|
|
"NVIDIA_VISIBLE_DEVICES": "nvidia.com/gpu=0,0",
|
|
|
|
},
|
2023-08-25 07:58:06 +00:00
|
|
|
info: map[string]bool{
|
|
|
|
"nvml": true,
|
|
|
|
"tegra": false,
|
|
|
|
"nvgpu": false,
|
2023-06-05 13:47:38 +00:00
|
|
|
},
|
|
|
|
expectedMode: "legacy",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "at least one non-cdi device resolves to csv",
|
|
|
|
mode: "auto",
|
2023-10-10 11:48:38 +00:00
|
|
|
envmap: map[string]string{
|
2023-06-05 13:47:38 +00:00
|
|
|
"NVIDIA_VISIBLE_DEVICES": "nvidia.com/gpu=0,0",
|
|
|
|
},
|
2023-08-25 07:58:06 +00:00
|
|
|
info: map[string]bool{
|
|
|
|
"nvml": false,
|
|
|
|
"tegra": true,
|
|
|
|
"nvgpu": false,
|
2023-06-05 13:47:38 +00:00
|
|
|
},
|
|
|
|
expectedMode: "csv",
|
|
|
|
},
|
2023-10-10 11:48:38 +00:00
|
|
|
{
|
|
|
|
description: "cdi mount devices resolves to CDI",
|
|
|
|
mode: "auto",
|
|
|
|
mounts: []string{
|
|
|
|
"/var/run/nvidia-container-devices/cdi/nvidia.com/gpu/0",
|
|
|
|
},
|
|
|
|
expectedMode: "cdi",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "cdi mount and non-CDI devices resolves to legacy",
|
|
|
|
mode: "auto",
|
|
|
|
mounts: []string{
|
|
|
|
"/var/run/nvidia-container-devices/cdi/nvidia.com/gpu/0",
|
|
|
|
"/var/run/nvidia-container-devices/all",
|
|
|
|
},
|
|
|
|
info: map[string]bool{
|
|
|
|
"nvml": true,
|
|
|
|
"tegra": false,
|
|
|
|
"nvgpu": false,
|
|
|
|
},
|
|
|
|
expectedMode: "legacy",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "cdi mount and non-CDI envvar resolves to legacy",
|
|
|
|
mode: "auto",
|
|
|
|
envmap: map[string]string{
|
|
|
|
"NVIDIA_VISIBLE_DEVICES": "0",
|
|
|
|
},
|
|
|
|
mounts: []string{
|
|
|
|
"/var/run/nvidia-container-devices/cdi/nvidia.com/gpu/0",
|
|
|
|
},
|
|
|
|
info: map[string]bool{
|
|
|
|
"nvml": true,
|
|
|
|
"tegra": false,
|
|
|
|
"nvgpu": false,
|
|
|
|
},
|
|
|
|
expectedMode: "legacy",
|
|
|
|
},
|
2022-05-09 14:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
2024-03-26 10:37:09 +00:00
|
|
|
properties := &info.PropertyExtractorMock{
|
2023-08-25 07:58:06 +00:00
|
|
|
HasNvmlFunc: func() (bool, string) {
|
|
|
|
return tc.info["nvml"], "nvml"
|
|
|
|
},
|
2024-03-26 10:37:09 +00:00
|
|
|
HasDXCoreFunc: func() (bool, string) {
|
|
|
|
return tc.info["dxcore"], "dxcore"
|
|
|
|
},
|
2023-08-25 07:58:06 +00:00
|
|
|
IsTegraSystemFunc: func() (bool, string) {
|
|
|
|
return tc.info["tegra"], "tegra"
|
|
|
|
},
|
2024-03-26 10:37:09 +00:00
|
|
|
HasTegraFilesFunc: func() (bool, string) {
|
|
|
|
return tc.info["tegra"], "tegra"
|
|
|
|
},
|
|
|
|
UsesOnlyNVGPUModuleFunc: func() (bool, string) {
|
2023-08-25 07:58:06 +00:00
|
|
|
return tc.info["nvgpu"], "nvgpu"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-10-10 11:48:38 +00:00
|
|
|
var mounts []specs.Mount
|
|
|
|
for _, d := range tc.mounts {
|
|
|
|
mount := specs.Mount{
|
|
|
|
Source: "/dev/null",
|
|
|
|
Destination: d,
|
|
|
|
}
|
|
|
|
mounts = append(mounts, mount)
|
|
|
|
}
|
|
|
|
image, _ := image.New(
|
|
|
|
image.WithEnvMap(tc.envmap),
|
|
|
|
image.WithMounts(mounts),
|
|
|
|
)
|
2024-03-26 10:37:09 +00:00
|
|
|
mode := resolveMode(logger, tc.mode, image, properties)
|
2022-05-09 14:05:21 +00:00
|
|
|
require.EqualValues(t, tc.expectedMode, mode)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|