2022-03-15 15:27:34 +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 config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-03-29 10:01:12 +00:00
|
|
|
func TestGetConfigWithCustomConfig(t *testing.T) {
|
2023-08-04 15:56:15 +00:00
|
|
|
testDir := t.TempDir()
|
|
|
|
t.Setenv(configOverride, testDir)
|
2022-03-15 15:27:34 +00:00
|
|
|
|
|
|
|
filename := filepath.Join(testDir, configFilePath)
|
|
|
|
|
2023-08-04 15:56:15 +00:00
|
|
|
// By default debug is disabled
|
|
|
|
contents := []byte("[nvidia-container-runtime]\ndebug = \"/nvidia-container-toolkit.log\"")
|
2022-03-15 15:27:34 +00:00
|
|
|
|
|
|
|
require.NoError(t, os.MkdirAll(filepath.Dir(filename), 0766))
|
2023-08-25 15:43:31 +00:00
|
|
|
require.NoError(t, os.WriteFile(filename, contents, 0600))
|
2022-03-15 15:27:34 +00:00
|
|
|
|
2022-03-29 10:01:12 +00:00
|
|
|
cfg, err := GetConfig()
|
2022-03-15 15:27:34 +00:00
|
|
|
require.NoError(t, err)
|
2023-08-04 15:56:15 +00:00
|
|
|
require.Equal(t, "/nvidia-container-toolkit.log", cfg.NVIDIAContainerRuntimeConfig.DebugFilePath)
|
2022-03-15 15:27:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 10:01:12 +00:00
|
|
|
func TestGetConfig(t *testing.T) {
|
2022-03-15 15:27:34 +00:00
|
|
|
testCases := []struct {
|
2023-07-04 12:26:44 +00:00
|
|
|
description string
|
|
|
|
contents []string
|
|
|
|
expectedError error
|
|
|
|
inspectLdconfig bool
|
2024-01-09 09:51:12 +00:00
|
|
|
distIdsLike []string
|
2023-07-04 12:26:44 +00:00
|
|
|
expectedConfig *Config
|
2022-03-15 15:27:34 +00:00
|
|
|
}{
|
|
|
|
{
|
2023-07-04 12:26:44 +00:00
|
|
|
description: "empty config is default",
|
|
|
|
inspectLdconfig: true,
|
2022-03-29 10:01:12 +00:00
|
|
|
expectedConfig: &Config{
|
2023-08-10 12:15:24 +00:00
|
|
|
AcceptEnvvarUnprivileged: true,
|
|
|
|
SupportedDriverCapabilities: "compat32,compute,display,graphics,ngx,utility,video",
|
2022-04-06 13:21:57 +00:00
|
|
|
NVIDIAContainerCLIConfig: ContainerCLIConfig{
|
2023-07-04 12:26:44 +00:00
|
|
|
Root: "",
|
|
|
|
LoadKmods: true,
|
|
|
|
Ldconfig: "WAS_CHECKED",
|
2022-03-29 10:01:12 +00:00
|
|
|
},
|
|
|
|
NVIDIAContainerRuntimeConfig: RuntimeConfig{
|
|
|
|
DebugFilePath: "/dev/null",
|
2022-03-11 15:37:41 +00:00
|
|
|
LogLevel: "info",
|
2022-05-04 12:39:14 +00:00
|
|
|
Runtimes: []string{"docker-runc", "runc"},
|
2022-05-09 12:13:42 +00:00
|
|
|
Mode: "auto",
|
|
|
|
Modes: modesConfig{
|
|
|
|
CSV: csvModeConfig{
|
|
|
|
MountSpecPath: "/etc/nvidia-container-runtime/host-files-for-container.d",
|
|
|
|
},
|
2023-03-06 11:30:40 +00:00
|
|
|
CDI: cdiModeConfig{
|
2023-03-23 18:18:22 +00:00
|
|
|
DefaultKind: "nvidia.com/gpu",
|
|
|
|
AnnotationPrefixes: []string{"cdi.k8s.io/"},
|
2023-06-22 12:48:22 +00:00
|
|
|
SpecDirs: []string{"/etc/cdi", "/var/run/cdi"},
|
2023-03-06 11:30:40 +00:00
|
|
|
},
|
2022-05-09 12:13:42 +00:00
|
|
|
},
|
2022-03-29 10:01:12 +00:00
|
|
|
},
|
2023-05-24 08:21:50 +00:00
|
|
|
NVIDIAContainerRuntimeHookConfig: RuntimeHookConfig{
|
|
|
|
Path: "nvidia-container-runtime-hook",
|
|
|
|
},
|
2022-03-29 12:02:40 +00:00
|
|
|
NVIDIACTKConfig: CTKConfig{
|
|
|
|
Path: "nvidia-ctk",
|
|
|
|
},
|
2022-03-15 15:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "config options set inline",
|
|
|
|
contents: []string{
|
2023-03-09 09:14:38 +00:00
|
|
|
"accept-nvidia-visible-devices-envvar-when-unprivileged = false",
|
2023-08-10 12:15:24 +00:00
|
|
|
"supported-driver-capabilities = \"compute,utility\"",
|
2022-03-29 12:02:40 +00:00
|
|
|
"nvidia-container-cli.root = \"/bar/baz\"",
|
2023-07-04 12:26:44 +00:00
|
|
|
"nvidia-container-cli.load-kmods = false",
|
|
|
|
"nvidia-container-cli.ldconfig = \"/foo/bar/ldconfig\"",
|
2024-01-09 09:51:12 +00:00
|
|
|
"nvidia-container-cli.user = \"foo:bar\"",
|
2022-03-15 15:27:34 +00:00
|
|
|
"nvidia-container-runtime.debug = \"/foo/bar\"",
|
2022-03-15 12:29:14 +00:00
|
|
|
"nvidia-container-runtime.discover-mode = \"not-legacy\"",
|
2022-03-11 15:37:41 +00:00
|
|
|
"nvidia-container-runtime.log-level = \"debug\"",
|
2022-05-04 12:39:14 +00:00
|
|
|
"nvidia-container-runtime.runtimes = [\"/some/runtime\",]",
|
2022-05-09 12:13:42 +00:00
|
|
|
"nvidia-container-runtime.mode = \"not-auto\"",
|
2023-03-06 11:30:40 +00:00
|
|
|
"nvidia-container-runtime.modes.cdi.default-kind = \"example.vendor.com/device\"",
|
2023-03-23 18:18:22 +00:00
|
|
|
"nvidia-container-runtime.modes.cdi.annotation-prefixes = [\"cdi.k8s.io/\", \"example.vendor.com/\",]",
|
2023-06-22 12:48:22 +00:00
|
|
|
"nvidia-container-runtime.modes.cdi.spec-dirs = [\"/except/etc/cdi\", \"/not/var/run/cdi\",]",
|
2022-05-09 12:13:42 +00:00
|
|
|
"nvidia-container-runtime.modes.csv.mount-spec-path = \"/not/etc/nvidia-container-runtime/host-files-for-container.d\"",
|
2023-05-24 08:21:50 +00:00
|
|
|
"nvidia-container-runtime-hook.path = \"/foo/bar/nvidia-container-runtime-hook\"",
|
2022-03-29 12:02:40 +00:00
|
|
|
"nvidia-ctk.path = \"/foo/bar/nvidia-ctk\"",
|
2022-03-15 15:27:34 +00:00
|
|
|
},
|
2022-03-29 10:01:12 +00:00
|
|
|
expectedConfig: &Config{
|
2023-08-10 12:15:24 +00:00
|
|
|
AcceptEnvvarUnprivileged: false,
|
|
|
|
SupportedDriverCapabilities: "compute,utility",
|
2022-04-06 13:21:57 +00:00
|
|
|
NVIDIAContainerCLIConfig: ContainerCLIConfig{
|
2023-07-04 12:26:44 +00:00
|
|
|
Root: "/bar/baz",
|
|
|
|
LoadKmods: false,
|
|
|
|
Ldconfig: "/foo/bar/ldconfig",
|
2024-01-09 09:51:12 +00:00
|
|
|
User: "foo:bar",
|
2022-03-29 10:01:12 +00:00
|
|
|
},
|
|
|
|
NVIDIAContainerRuntimeConfig: RuntimeConfig{
|
|
|
|
DebugFilePath: "/foo/bar",
|
2022-03-11 15:37:41 +00:00
|
|
|
LogLevel: "debug",
|
2022-05-04 12:39:14 +00:00
|
|
|
Runtimes: []string{"/some/runtime"},
|
2022-05-09 12:13:42 +00:00
|
|
|
Mode: "not-auto",
|
|
|
|
Modes: modesConfig{
|
|
|
|
CSV: csvModeConfig{
|
|
|
|
MountSpecPath: "/not/etc/nvidia-container-runtime/host-files-for-container.d",
|
|
|
|
},
|
2023-03-06 11:30:40 +00:00
|
|
|
CDI: cdiModeConfig{
|
|
|
|
DefaultKind: "example.vendor.com/device",
|
2023-03-23 18:18:22 +00:00
|
|
|
AnnotationPrefixes: []string{
|
|
|
|
"cdi.k8s.io/",
|
|
|
|
"example.vendor.com/",
|
|
|
|
},
|
2023-06-22 12:48:22 +00:00
|
|
|
SpecDirs: []string{
|
|
|
|
"/except/etc/cdi",
|
|
|
|
"/not/var/run/cdi",
|
|
|
|
},
|
2023-03-06 11:30:40 +00:00
|
|
|
},
|
2022-05-09 12:13:42 +00:00
|
|
|
},
|
2022-03-29 10:01:12 +00:00
|
|
|
},
|
2023-05-24 08:21:50 +00:00
|
|
|
NVIDIAContainerRuntimeHookConfig: RuntimeHookConfig{
|
|
|
|
Path: "/foo/bar/nvidia-container-runtime-hook",
|
|
|
|
},
|
2022-03-29 12:02:40 +00:00
|
|
|
NVIDIACTKConfig: CTKConfig{
|
|
|
|
Path: "/foo/bar/nvidia-ctk",
|
|
|
|
},
|
2022-03-15 15:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "config options set in section",
|
|
|
|
contents: []string{
|
2023-03-09 09:14:38 +00:00
|
|
|
"accept-nvidia-visible-devices-envvar-when-unprivileged = false",
|
2023-08-10 12:15:24 +00:00
|
|
|
"supported-driver-capabilities = \"compute,utility\"",
|
2022-03-29 12:02:40 +00:00
|
|
|
"[nvidia-container-cli]",
|
|
|
|
"root = \"/bar/baz\"",
|
2023-07-04 12:26:44 +00:00
|
|
|
"load-kmods = false",
|
|
|
|
"ldconfig = \"/foo/bar/ldconfig\"",
|
2024-01-09 09:51:12 +00:00
|
|
|
"user = \"foo:bar\"",
|
2022-03-15 15:27:34 +00:00
|
|
|
"[nvidia-container-runtime]",
|
|
|
|
"debug = \"/foo/bar\"",
|
2022-03-15 12:29:14 +00:00
|
|
|
"discover-mode = \"not-legacy\"",
|
2022-03-11 15:37:41 +00:00
|
|
|
"log-level = \"debug\"",
|
2022-05-04 12:39:14 +00:00
|
|
|
"runtimes = [\"/some/runtime\",]",
|
2022-05-09 12:13:42 +00:00
|
|
|
"mode = \"not-auto\"",
|
2023-03-06 11:30:40 +00:00
|
|
|
"[nvidia-container-runtime.modes.cdi]",
|
|
|
|
"default-kind = \"example.vendor.com/device\"",
|
2023-03-23 18:18:22 +00:00
|
|
|
"annotation-prefixes = [\"cdi.k8s.io/\", \"example.vendor.com/\",]",
|
2023-06-22 12:48:22 +00:00
|
|
|
"spec-dirs = [\"/except/etc/cdi\", \"/not/var/run/cdi\",]",
|
2022-05-09 12:13:42 +00:00
|
|
|
"[nvidia-container-runtime.modes.csv]",
|
|
|
|
"mount-spec-path = \"/not/etc/nvidia-container-runtime/host-files-for-container.d\"",
|
2023-05-24 08:21:50 +00:00
|
|
|
"[nvidia-container-runtime-hook]",
|
|
|
|
"path = \"/foo/bar/nvidia-container-runtime-hook\"",
|
2022-03-29 12:02:40 +00:00
|
|
|
"[nvidia-ctk]",
|
|
|
|
"path = \"/foo/bar/nvidia-ctk\"",
|
2022-03-15 15:27:34 +00:00
|
|
|
},
|
2022-03-29 10:01:12 +00:00
|
|
|
expectedConfig: &Config{
|
2023-08-10 12:15:24 +00:00
|
|
|
AcceptEnvvarUnprivileged: false,
|
|
|
|
SupportedDriverCapabilities: "compute,utility",
|
2022-04-06 13:21:57 +00:00
|
|
|
NVIDIAContainerCLIConfig: ContainerCLIConfig{
|
2023-07-04 12:26:44 +00:00
|
|
|
Root: "/bar/baz",
|
|
|
|
LoadKmods: false,
|
|
|
|
Ldconfig: "/foo/bar/ldconfig",
|
2024-01-09 09:51:12 +00:00
|
|
|
User: "foo:bar",
|
2022-03-29 10:01:12 +00:00
|
|
|
},
|
|
|
|
NVIDIAContainerRuntimeConfig: RuntimeConfig{
|
|
|
|
DebugFilePath: "/foo/bar",
|
2022-03-11 15:37:41 +00:00
|
|
|
LogLevel: "debug",
|
2022-05-04 12:39:14 +00:00
|
|
|
Runtimes: []string{"/some/runtime"},
|
2022-05-09 12:13:42 +00:00
|
|
|
Mode: "not-auto",
|
|
|
|
Modes: modesConfig{
|
|
|
|
CSV: csvModeConfig{
|
|
|
|
MountSpecPath: "/not/etc/nvidia-container-runtime/host-files-for-container.d",
|
|
|
|
},
|
2023-03-06 11:30:40 +00:00
|
|
|
CDI: cdiModeConfig{
|
|
|
|
DefaultKind: "example.vendor.com/device",
|
2023-03-23 18:18:22 +00:00
|
|
|
AnnotationPrefixes: []string{
|
|
|
|
"cdi.k8s.io/",
|
|
|
|
"example.vendor.com/",
|
|
|
|
},
|
2023-06-22 12:48:22 +00:00
|
|
|
SpecDirs: []string{
|
|
|
|
"/except/etc/cdi",
|
|
|
|
"/not/var/run/cdi",
|
|
|
|
},
|
2023-03-06 11:30:40 +00:00
|
|
|
},
|
2022-05-09 12:13:42 +00:00
|
|
|
},
|
2022-03-29 10:01:12 +00:00
|
|
|
},
|
2023-05-24 08:21:50 +00:00
|
|
|
NVIDIAContainerRuntimeHookConfig: RuntimeHookConfig{
|
|
|
|
Path: "/foo/bar/nvidia-container-runtime-hook",
|
|
|
|
},
|
2022-03-29 12:02:40 +00:00
|
|
|
NVIDIACTKConfig: CTKConfig{
|
|
|
|
Path: "/foo/bar/nvidia-ctk",
|
|
|
|
},
|
2022-03-15 15:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
2024-01-09 09:51:12 +00:00
|
|
|
{
|
|
|
|
description: "suse config",
|
|
|
|
distIdsLike: []string{"suse", "opensuse"},
|
|
|
|
inspectLdconfig: true,
|
|
|
|
expectedConfig: &Config{
|
|
|
|
AcceptEnvvarUnprivileged: true,
|
|
|
|
SupportedDriverCapabilities: "compat32,compute,display,graphics,ngx,utility,video",
|
|
|
|
NVIDIAContainerCLIConfig: ContainerCLIConfig{
|
|
|
|
Root: "",
|
|
|
|
LoadKmods: true,
|
|
|
|
Ldconfig: "WAS_CHECKED",
|
|
|
|
User: "root:video",
|
|
|
|
},
|
|
|
|
NVIDIAContainerRuntimeConfig: RuntimeConfig{
|
|
|
|
DebugFilePath: "/dev/null",
|
|
|
|
LogLevel: "info",
|
|
|
|
Runtimes: []string{"docker-runc", "runc"},
|
|
|
|
Mode: "auto",
|
|
|
|
Modes: modesConfig{
|
|
|
|
CSV: csvModeConfig{
|
|
|
|
MountSpecPath: "/etc/nvidia-container-runtime/host-files-for-container.d",
|
|
|
|
},
|
|
|
|
CDI: cdiModeConfig{
|
|
|
|
DefaultKind: "nvidia.com/gpu",
|
|
|
|
AnnotationPrefixes: []string{"cdi.k8s.io/"},
|
|
|
|
SpecDirs: []string{"/etc/cdi", "/var/run/cdi"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
NVIDIAContainerRuntimeHookConfig: RuntimeHookConfig{
|
|
|
|
Path: "nvidia-container-runtime-hook",
|
|
|
|
},
|
|
|
|
NVIDIACTKConfig: CTKConfig{
|
|
|
|
Path: "nvidia-ctk",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "suse config overrides user",
|
|
|
|
distIdsLike: []string{"suse", "opensuse"},
|
|
|
|
inspectLdconfig: true,
|
|
|
|
contents: []string{
|
|
|
|
"nvidia-container-cli.user = \"foo:bar\"",
|
|
|
|
},
|
|
|
|
expectedConfig: &Config{
|
|
|
|
AcceptEnvvarUnprivileged: true,
|
|
|
|
SupportedDriverCapabilities: "compat32,compute,display,graphics,ngx,utility,video",
|
|
|
|
NVIDIAContainerCLIConfig: ContainerCLIConfig{
|
|
|
|
Root: "",
|
|
|
|
LoadKmods: true,
|
|
|
|
Ldconfig: "WAS_CHECKED",
|
|
|
|
User: "foo:bar",
|
|
|
|
},
|
|
|
|
NVIDIAContainerRuntimeConfig: RuntimeConfig{
|
|
|
|
DebugFilePath: "/dev/null",
|
|
|
|
LogLevel: "info",
|
|
|
|
Runtimes: []string{"docker-runc", "runc"},
|
|
|
|
Mode: "auto",
|
|
|
|
Modes: modesConfig{
|
|
|
|
CSV: csvModeConfig{
|
|
|
|
MountSpecPath: "/etc/nvidia-container-runtime/host-files-for-container.d",
|
|
|
|
},
|
|
|
|
CDI: cdiModeConfig{
|
|
|
|
DefaultKind: "nvidia.com/gpu",
|
|
|
|
AnnotationPrefixes: []string{"cdi.k8s.io/"},
|
|
|
|
SpecDirs: []string{"/etc/cdi", "/var/run/cdi"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
NVIDIAContainerRuntimeHookConfig: RuntimeHookConfig{
|
|
|
|
Path: "nvidia-container-runtime-hook",
|
|
|
|
},
|
|
|
|
NVIDIACTKConfig: CTKConfig{
|
|
|
|
Path: "nvidia-ctk",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-03-15 15:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
2024-01-09 09:51:12 +00:00
|
|
|
defer setGetDistIDLikeForTest(tc.distIdsLike)()
|
2022-03-15 15:27:34 +00:00
|
|
|
reader := strings.NewReader(strings.Join(tc.contents, "\n"))
|
|
|
|
|
2023-08-04 15:56:15 +00:00
|
|
|
tomlCfg, err := loadConfigTomlFrom(reader)
|
2022-03-15 15:27:34 +00:00
|
|
|
if tc.expectedError != nil {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2023-08-04 15:56:15 +00:00
|
|
|
cfg, err := tomlCfg.Config()
|
|
|
|
require.NoError(t, err)
|
2022-03-15 15:27:34 +00:00
|
|
|
|
2023-07-04 12:26:44 +00:00
|
|
|
// We first handle the ldconfig path since this is currently system-dependent.
|
|
|
|
if tc.inspectLdconfig {
|
|
|
|
ldconfig := cfg.NVIDIAContainerCLIConfig.Ldconfig
|
|
|
|
require.True(t, strings.HasPrefix(ldconfig, "@/sbin/ldconfig"))
|
|
|
|
remaining := strings.TrimPrefix(ldconfig, "@/sbin/ldconfig")
|
|
|
|
require.True(t, remaining == ".real" || remaining == "")
|
|
|
|
|
|
|
|
cfg.NVIDIAContainerCLIConfig.Ldconfig = "WAS_CHECKED"
|
|
|
|
}
|
|
|
|
|
2022-03-15 15:27:34 +00:00
|
|
|
require.EqualValues(t, tc.expectedConfig, cfg)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-01-09 09:51:12 +00:00
|
|
|
|
|
|
|
// setGetDistIDsLikeForTest overrides the distribution IDs that would normally be read from the /etc/os-release file.
|
|
|
|
func setGetDistIDLikeForTest(ids []string) func() {
|
|
|
|
if ids == nil {
|
|
|
|
return func() {}
|
|
|
|
}
|
|
|
|
original := getDistIDLike
|
|
|
|
|
|
|
|
getDistIDLike = func() []string {
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
getDistIDLike = original
|
|
|
|
}
|
|
|
|
}
|