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"
|
|
|
|
|
2023-06-05 13:47:38 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/image"
|
2022-05-09 14:05:21 +00:00
|
|
|
testlog "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-06-05 13:47:38 +00:00
|
|
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvlib/info"
|
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-06-05 13:47:38 +00:00
|
|
|
info info.Interface
|
|
|
|
image image.CUDA
|
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
|
|
|
{
|
|
|
|
description: "nvml non-tegra resolves to legacy",
|
|
|
|
mode: "auto",
|
|
|
|
info: &infoInterfaceMock{
|
|
|
|
HasNvmlFunc: func() (bool, string) {
|
|
|
|
return true, "nvml"
|
|
|
|
},
|
|
|
|
IsTegraSystemFunc: func() (bool, string) {
|
|
|
|
return false, "tegra"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedMode: "legacy",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "non-nvml non-tegra resolves to legacy",
|
|
|
|
mode: "auto",
|
|
|
|
info: &infoInterfaceMock{
|
|
|
|
HasNvmlFunc: func() (bool, string) {
|
|
|
|
return false, "nvml"
|
|
|
|
},
|
|
|
|
IsTegraSystemFunc: func() (bool, string) {
|
|
|
|
return false, "tegra"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedMode: "legacy",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "nvml tegra resolves to legacy",
|
|
|
|
mode: "auto",
|
|
|
|
info: &infoInterfaceMock{
|
|
|
|
HasNvmlFunc: func() (bool, string) {
|
|
|
|
return true, "nvml"
|
|
|
|
},
|
|
|
|
IsTegraSystemFunc: func() (bool, string) {
|
|
|
|
return true, "tegra"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedMode: "legacy",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "non-nvml tegra resolves to csv",
|
|
|
|
mode: "auto",
|
|
|
|
info: &infoInterfaceMock{
|
|
|
|
HasNvmlFunc: func() (bool, string) {
|
|
|
|
return false, "nvml"
|
|
|
|
},
|
|
|
|
IsTegraSystemFunc: func() (bool, string) {
|
|
|
|
return true, "tegra"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedMode: "csv",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "cdi devices resolves to cdi",
|
|
|
|
mode: "auto",
|
|
|
|
expectedMode: "cdi",
|
|
|
|
image: image.CUDA{
|
|
|
|
"NVIDIA_VISIBLE_DEVICES": "nvidia.com/gpu=all",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "multiple cdi devices resolves to cdi",
|
|
|
|
mode: "auto",
|
|
|
|
expectedMode: "cdi",
|
|
|
|
image: image.CUDA{
|
|
|
|
"NVIDIA_VISIBLE_DEVICES": "nvidia.com/gpu=0,nvidia.com/gpu=1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "at least one non-cdi device resolves to legacy",
|
|
|
|
mode: "auto",
|
|
|
|
image: image.CUDA{
|
|
|
|
"NVIDIA_VISIBLE_DEVICES": "nvidia.com/gpu=0,0",
|
|
|
|
},
|
|
|
|
info: &infoInterfaceMock{
|
|
|
|
HasNvmlFunc: func() (bool, string) {
|
|
|
|
return true, "nvml"
|
|
|
|
},
|
|
|
|
IsTegraSystemFunc: func() (bool, string) {
|
|
|
|
return true, "tegra"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedMode: "legacy",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "at least one non-cdi device resolves to csv",
|
|
|
|
mode: "auto",
|
|
|
|
image: image.CUDA{
|
|
|
|
"NVIDIA_VISIBLE_DEVICES": "nvidia.com/gpu=0,0",
|
|
|
|
},
|
|
|
|
info: &infoInterfaceMock{
|
|
|
|
HasNvmlFunc: func() (bool, string) {
|
|
|
|
return false, "nvml"
|
|
|
|
},
|
|
|
|
IsTegraSystemFunc: func() (bool, string) {
|
|
|
|
return true, "tegra"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedMode: "csv",
|
|
|
|
},
|
2022-05-09 14:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
2023-06-05 13:47:38 +00:00
|
|
|
r := resolver{
|
|
|
|
logger: logger,
|
|
|
|
info: tc.info,
|
|
|
|
}
|
|
|
|
mode := r.resolveMode(tc.mode, tc.image)
|
2022-05-09 14:05:21 +00:00
|
|
|
require.EqualValues(t, tc.expectedMode, mode)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|