2022-03-15 12:29:14 +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 modifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
|
2023-06-22 13:36:43 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/image"
|
2022-03-15 12:29:14 +00:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
testlog "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-05-09 13:42:59 +00:00
|
|
|
func TestNewCSVModifier(t *testing.T) {
|
2022-03-15 12:29:14 +00:00
|
|
|
logger, _ := testlog.NewNullLogger()
|
|
|
|
|
|
|
|
testCases := []struct {
|
2023-06-22 13:36:43 +00:00
|
|
|
description string
|
|
|
|
cfg *config.Config
|
|
|
|
image image.CUDA
|
|
|
|
expectedError error
|
|
|
|
expectedNil bool
|
2022-03-15 12:29:14 +00:00
|
|
|
}{
|
2022-02-25 08:54:04 +00:00
|
|
|
{
|
2023-06-22 13:36:43 +00:00
|
|
|
description: "visible devices not set returns nil",
|
|
|
|
image: image.CUDA{},
|
|
|
|
expectedNil: true,
|
2022-02-25 08:54:04 +00:00
|
|
|
},
|
|
|
|
{
|
2023-06-22 13:36:43 +00:00
|
|
|
description: "visible devices empty returns nil",
|
|
|
|
image: image.CUDA{"NVIDIA_VISIBLE_DEVICES": ""},
|
|
|
|
expectedNil: true,
|
2022-02-25 08:54:04 +00:00
|
|
|
},
|
|
|
|
{
|
2023-06-22 13:36:43 +00:00
|
|
|
description: "visible devices 'void' returns nil",
|
|
|
|
image: image.CUDA{"NVIDIA_VISIBLE_DEVICES": "void"},
|
|
|
|
expectedNil: true,
|
2022-02-25 08:54:04 +00:00
|
|
|
},
|
2022-03-15 12:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
2023-06-22 13:36:43 +00:00
|
|
|
m, err := NewCSVModifier(logger, tc.cfg, tc.image)
|
2022-03-15 12:29:14 +00:00
|
|
|
if tc.expectedError != nil {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2022-02-25 08:54:04 +00:00
|
|
|
|
|
|
|
if tc.expectedNil || tc.expectedError != nil {
|
|
|
|
require.Nil(t, m)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, m)
|
|
|
|
}
|
2022-03-15 12:29:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 12:12:17 +00:00
|
|
|
func TestCSVModifierRemovesHook(t *testing.T) {
|
2022-03-15 12:29:14 +00:00
|
|
|
logger, _ := testlog.NewNullLogger()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
description string
|
|
|
|
spec *specs.Spec
|
|
|
|
expectedError error
|
|
|
|
expectedSpec *specs.Spec
|
|
|
|
}{
|
|
|
|
{
|
2022-04-06 14:18:56 +00:00
|
|
|
description: "modification removes existing nvidia-container-runtime-hook",
|
2022-03-15 12:29:14 +00:00
|
|
|
spec: &specs.Spec{
|
|
|
|
Hooks: &specs.Hooks{
|
|
|
|
Prestart: []specs.Hook{
|
|
|
|
{
|
|
|
|
Path: "/path/to/nvidia-container-runtime-hook",
|
|
|
|
Args: []string{"/path/to/nvidia-container-runtime-hook", "prestart"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSpec: &specs.Spec{
|
|
|
|
Hooks: &specs.Hooks{
|
2022-06-15 12:12:17 +00:00
|
|
|
Prestart: []specs.Hook{},
|
2022-03-15 12:29:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-04-06 14:18:56 +00:00
|
|
|
description: "modification removes existing nvidia-container-toolkit",
|
2022-03-15 12:29:14 +00:00
|
|
|
spec: &specs.Spec{
|
|
|
|
Hooks: &specs.Hooks{
|
|
|
|
Prestart: []specs.Hook{
|
|
|
|
{
|
|
|
|
Path: "/path/to/nvidia-container-toolkit",
|
|
|
|
Args: []string{"/path/to/nvidia-container-toolkit", "prestart"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSpec: &specs.Spec{
|
|
|
|
Hooks: &specs.Hooks{
|
2022-06-15 12:12:17 +00:00
|
|
|
Prestart: []specs.Hook{},
|
2022-03-15 12:29:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
2022-06-15 12:12:17 +00:00
|
|
|
m := nvidiaContainerRuntimeHookRemover{logger: logger}
|
2022-03-15 12:29:14 +00:00
|
|
|
|
2022-06-15 12:12:17 +00:00
|
|
|
err := m.Modify(tc.spec)
|
2022-03-15 12:29:14 +00:00
|
|
|
if tc.expectedError != nil {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-06-15 12:12:17 +00:00
|
|
|
require.Empty(t, tc.spec.Hooks.Prestart)
|
2022-03-15 12:29:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|