2021-10-11 14:31:02 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 2021, 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.
|
|
|
|
*/
|
|
|
|
|
2024-09-27 08:36:39 +00:00
|
|
|
package docker
|
2021-10-11 14:31:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2023-12-01 01:10:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-04-24 11:58:03 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/docker"
|
2023-06-09 15:17:54 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/tools/container"
|
2021-10-11 14:31:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUpdateConfigDefaultRuntime(t *testing.T) {
|
|
|
|
const runtimeDir = "/test/runtime/dir"
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
setAsDefault bool
|
|
|
|
runtimeName string
|
|
|
|
expectedDefaultRuntimeName interface{}
|
|
|
|
}{
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
setAsDefault: false,
|
|
|
|
expectedDefaultRuntimeName: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
setAsDefault: true,
|
|
|
|
runtimeName: "NAME",
|
|
|
|
expectedDefaultRuntimeName: "NAME",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
setAsDefault: true,
|
|
|
|
runtimeName: "nvidia",
|
|
|
|
expectedDefaultRuntimeName: "nvidia",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
2024-09-27 08:36:39 +00:00
|
|
|
o := &container.Options{
|
|
|
|
RuntimeName: tc.runtimeName,
|
|
|
|
RuntimeDir: runtimeDir,
|
|
|
|
SetAsDefault: tc.setAsDefault,
|
2021-10-11 14:31:02 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
config := docker.Config(map[string]interface{}{})
|
2021-10-11 14:31:02 +00:00
|
|
|
|
2023-06-09 15:17:54 +00:00
|
|
|
err := o.UpdateConfig(&config)
|
2021-10-11 14:31:02 +00:00
|
|
|
require.NoError(t, err, "%d: %v", i, tc)
|
|
|
|
|
|
|
|
defaultRuntimeName := config["default-runtime"]
|
|
|
|
require.EqualValues(t, tc.expectedDefaultRuntimeName, defaultRuntimeName, "%d: %v", i, tc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateConfig(t *testing.T) {
|
|
|
|
const runtimeDir = "/test/runtime/dir"
|
|
|
|
|
|
|
|
testCases := []struct {
|
2023-02-23 13:43:15 +00:00
|
|
|
config docker.Config
|
2021-10-11 14:31:02 +00:00
|
|
|
setAsDefault bool
|
|
|
|
runtimeName string
|
|
|
|
expectedConfig map[string]interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{},
|
|
|
|
setAsDefault: false,
|
|
|
|
expectedConfig: map[string]interface{}{
|
|
|
|
"runtimes": map[string]interface{}{
|
|
|
|
"nvidia": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
2023-02-24 09:06:59 +00:00
|
|
|
"nvidia-cdi": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime.cdi",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
"nvidia-legacy": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime.legacy",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
2021-10-11 14:31:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{},
|
|
|
|
setAsDefault: false,
|
|
|
|
runtimeName: "NAME",
|
|
|
|
expectedConfig: map[string]interface{}{
|
|
|
|
"runtimes": map[string]interface{}{
|
|
|
|
"NAME": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
2023-02-24 09:06:59 +00:00
|
|
|
"nvidia-cdi": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime.cdi",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
"nvidia-legacy": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime.legacy",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
2021-10-11 14:31:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"runtimes": map[string]interface{}{
|
|
|
|
"nvidia": map[string]interface{}{
|
|
|
|
"path": "nvidia-container-runtime",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setAsDefault: false,
|
|
|
|
expectedConfig: map[string]interface{}{
|
|
|
|
"runtimes": map[string]interface{}{
|
|
|
|
"nvidia": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
2023-02-24 09:06:59 +00:00
|
|
|
"nvidia-cdi": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime.cdi",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
"nvidia-legacy": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime.legacy",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
2021-10-11 14:31:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"runtimes": map[string]interface{}{
|
|
|
|
"not-nvidia": map[string]interface{}{
|
|
|
|
"path": "some-other-path",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedConfig: map[string]interface{}{
|
|
|
|
"runtimes": map[string]interface{}{
|
|
|
|
"not-nvidia": map[string]interface{}{
|
|
|
|
"path": "some-other-path",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
"nvidia": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
2023-02-24 09:06:59 +00:00
|
|
|
"nvidia-cdi": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime.cdi",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
"nvidia-legacy": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime.legacy",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
2021-10-11 14:31:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"default-runtime": "runc",
|
|
|
|
},
|
|
|
|
setAsDefault: true,
|
|
|
|
runtimeName: "nvidia",
|
|
|
|
expectedConfig: map[string]interface{}{
|
|
|
|
"default-runtime": "nvidia",
|
|
|
|
"runtimes": map[string]interface{}{
|
|
|
|
"nvidia": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
2023-02-24 09:06:59 +00:00
|
|
|
"nvidia-cdi": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime.cdi",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
"nvidia-legacy": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime.legacy",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
2021-10-11 14:31:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"exec-opts": []string{"native.cgroupdriver=systemd"},
|
|
|
|
"log-driver": "json-file",
|
|
|
|
"log-opts": map[string]string{
|
|
|
|
"max-size": "100m",
|
|
|
|
},
|
|
|
|
"storage-driver": "overlay2",
|
|
|
|
},
|
|
|
|
expectedConfig: map[string]interface{}{
|
|
|
|
"exec-opts": []string{"native.cgroupdriver=systemd"},
|
|
|
|
"log-driver": "json-file",
|
|
|
|
"log-opts": map[string]string{
|
|
|
|
"max-size": "100m",
|
|
|
|
},
|
|
|
|
"storage-driver": "overlay2",
|
|
|
|
"runtimes": map[string]interface{}{
|
|
|
|
"nvidia": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
2023-02-24 09:06:59 +00:00
|
|
|
"nvidia-cdi": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime.cdi",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
"nvidia-legacy": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime.legacy",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
2021-10-11 14:31:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
2023-08-25 15:50:43 +00:00
|
|
|
tc := tc
|
|
|
|
|
2024-09-27 08:36:39 +00:00
|
|
|
o := &container.Options{
|
|
|
|
RuntimeName: tc.runtimeName,
|
|
|
|
RuntimeDir: runtimeDir,
|
|
|
|
SetAsDefault: tc.setAsDefault,
|
2021-10-11 14:31:02 +00:00
|
|
|
}
|
2023-02-23 13:43:15 +00:00
|
|
|
|
2023-06-09 15:17:54 +00:00
|
|
|
err := o.UpdateConfig(&tc.config)
|
2021-10-11 14:31:02 +00:00
|
|
|
require.NoError(t, err, "%d: %v", i, tc)
|
|
|
|
|
|
|
|
configContent, err := json.MarshalIndent(tc.config, "", " ")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedContent, err := json.MarshalIndent(tc.expectedConfig, "", " ")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.EqualValues(t, string(expectedContent), string(configContent), "%d: %v", i, tc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRevertConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2023-02-23 13:43:15 +00:00
|
|
|
config docker.Config
|
2021-10-11 14:31:02 +00:00
|
|
|
expectedConfig map[string]interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{},
|
|
|
|
expectedConfig: map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"runtimes": map[string]interface{}{
|
|
|
|
"nvidia": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedConfig: map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"runtimes": map[string]interface{}{
|
|
|
|
"nvidia": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedConfig: map[string]interface{}{},
|
|
|
|
},
|
2023-02-24 09:06:59 +00:00
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"runtimes": map[string]interface{}{
|
|
|
|
"nvidia": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
"nvidia-cdi": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime.cdi",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
"nvidia-legacy": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime.legacy",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedConfig: map[string]interface{}{},
|
|
|
|
},
|
2021-10-11 14:31:02 +00:00
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"default-runtime": "nvidia",
|
|
|
|
"runtimes": map[string]interface{}{
|
|
|
|
"nvidia": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedConfig: map[string]interface{}{
|
|
|
|
"default-runtime": "runc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"default-runtime": "not-nvidia",
|
|
|
|
"runtimes": map[string]interface{}{
|
|
|
|
"nvidia": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedConfig: map[string]interface{}{
|
|
|
|
"default-runtime": "not-nvidia",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"exec-opts": []string{"native.cgroupdriver=systemd"},
|
|
|
|
"log-driver": "json-file",
|
|
|
|
"log-opts": map[string]string{
|
|
|
|
"max-size": "100m",
|
|
|
|
},
|
|
|
|
"storage-driver": "overlay2",
|
|
|
|
"runtimes": map[string]interface{}{
|
|
|
|
"nvidia": map[string]interface{}{
|
|
|
|
"path": "/test/runtime/dir/nvidia-container-runtime",
|
|
|
|
"args": []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedConfig: map[string]interface{}{
|
|
|
|
"exec-opts": []string{"native.cgroupdriver=systemd"},
|
|
|
|
"log-driver": "json-file",
|
|
|
|
"log-opts": map[string]string{
|
|
|
|
"max-size": "100m",
|
|
|
|
},
|
|
|
|
"storage-driver": "overlay2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
2023-08-25 15:50:43 +00:00
|
|
|
tc := tc
|
2024-09-27 08:36:39 +00:00
|
|
|
o := &container.Options{}
|
2023-06-09 15:17:54 +00:00
|
|
|
err := o.RevertConfig(&tc.config)
|
2021-10-11 14:31:02 +00:00
|
|
|
|
|
|
|
require.NoError(t, err, "%d: %v", i, tc)
|
|
|
|
|
|
|
|
configContent, err := json.MarshalIndent(tc.config, "", " ")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedContent, err := json.MarshalIndent(tc.expectedConfig, "", " ")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.EqualValues(t, string(expectedContent), string(configContent), "%d: %v", i, tc)
|
|
|
|
}
|
|
|
|
}
|