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 (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-03-29 10:01:12 +00:00
|
|
|
func TestGetConfigWithCustomConfig(t *testing.T) {
|
2022-03-15 15:27:34 +00:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// By default debug is disabled
|
|
|
|
contents := []byte("[nvidia-container-runtime]\ndebug = \"/nvidia-container-toolkit.log\"")
|
|
|
|
testDir := filepath.Join(wd, "test")
|
|
|
|
filename := filepath.Join(testDir, configFilePath)
|
|
|
|
|
|
|
|
os.Setenv(configOverride, testDir)
|
|
|
|
|
|
|
|
require.NoError(t, os.MkdirAll(filepath.Dir(filename), 0766))
|
|
|
|
require.NoError(t, ioutil.WriteFile(filename, contents, 0766))
|
|
|
|
|
|
|
|
defer func() { require.NoError(t, os.RemoveAll(testDir)) }()
|
|
|
|
|
2022-03-29 10:01:12 +00:00
|
|
|
cfg, err := GetConfig()
|
2022-03-15 15:27:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-29 10:01:12 +00:00
|
|
|
require.Equal(t, cfg.NVIDIAContainerRuntimeConfig.DebugFilePath, "/nvidia-container-toolkit.log")
|
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 {
|
|
|
|
description string
|
|
|
|
contents []string
|
|
|
|
expectedError error
|
2022-03-29 10:01:12 +00:00
|
|
|
expectedConfig *Config
|
2022-03-15 15:27:34 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "empty config is default",
|
2022-03-29 10:01:12 +00:00
|
|
|
expectedConfig: &Config{
|
2022-04-06 13:21:57 +00:00
|
|
|
NVIDIAContainerCLIConfig: ContainerCLIConfig{
|
2022-03-29 10:01:12 +00:00
|
|
|
Root: "",
|
|
|
|
},
|
|
|
|
NVIDIAContainerRuntimeConfig: RuntimeConfig{
|
|
|
|
DebugFilePath: "/dev/null",
|
|
|
|
Experimental: false,
|
2022-04-06 12:30:49 +00:00
|
|
|
DiscoverMode: "auto",
|
2022-03-11 15:37:41 +00:00
|
|
|
LogLevel: "info",
|
2022-05-04 12:39:14 +00:00
|
|
|
Runtimes: []string{"docker-runc", "runc"},
|
2022-03-29 10:01:12 +00:00
|
|
|
},
|
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{
|
2022-03-29 12:02:40 +00:00
|
|
|
"nvidia-container-cli.root = \"/bar/baz\"",
|
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.experimental = true",
|
|
|
|
"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-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{
|
2022-04-06 13:21:57 +00:00
|
|
|
NVIDIAContainerCLIConfig: ContainerCLIConfig{
|
2022-03-29 12:02:40 +00:00
|
|
|
Root: "/bar/baz",
|
2022-03-29 10:01:12 +00:00
|
|
|
},
|
|
|
|
NVIDIAContainerRuntimeConfig: RuntimeConfig{
|
|
|
|
DebugFilePath: "/foo/bar",
|
|
|
|
Experimental: true,
|
|
|
|
DiscoverMode: "not-legacy",
|
2022-03-11 15:37:41 +00:00
|
|
|
LogLevel: "debug",
|
2022-05-04 12:39:14 +00:00
|
|
|
Runtimes: []string{"/some/runtime"},
|
2022-03-29 10:01:12 +00:00
|
|
|
},
|
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{
|
2022-03-29 12:02:40 +00:00
|
|
|
"[nvidia-container-cli]",
|
|
|
|
"root = \"/bar/baz\"",
|
2022-03-15 15:27:34 +00:00
|
|
|
"[nvidia-container-runtime]",
|
|
|
|
"debug = \"/foo/bar\"",
|
2022-03-15 12:29:14 +00:00
|
|
|
"experimental = true",
|
|
|
|
"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-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{
|
2022-04-06 13:21:57 +00:00
|
|
|
NVIDIAContainerCLIConfig: ContainerCLIConfig{
|
2022-03-29 12:02:40 +00:00
|
|
|
Root: "/bar/baz",
|
2022-03-29 10:01:12 +00:00
|
|
|
},
|
|
|
|
NVIDIAContainerRuntimeConfig: RuntimeConfig{
|
|
|
|
DebugFilePath: "/foo/bar",
|
|
|
|
Experimental: true,
|
|
|
|
DiscoverMode: "not-legacy",
|
2022-03-11 15:37:41 +00:00
|
|
|
LogLevel: "debug",
|
2022-05-04 12:39:14 +00:00
|
|
|
Runtimes: []string{"/some/runtime"},
|
2022-03-29 10:01:12 +00:00
|
|
|
},
|
2022-03-29 12:02:40 +00:00
|
|
|
NVIDIACTKConfig: CTKConfig{
|
|
|
|
Path: "/foo/bar/nvidia-ctk",
|
|
|
|
},
|
2022-03-15 15:27:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
|
|
reader := strings.NewReader(strings.Join(tc.contents, "\n"))
|
|
|
|
|
2022-03-29 10:01:12 +00:00
|
|
|
cfg, err := loadConfigFrom(reader)
|
2022-03-15 15:27:34 +00:00
|
|
|
if tc.expectedError != nil {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.EqualValues(t, tc.expectedConfig, cfg)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|