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 (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
|
2022-02-25 08:54:04 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
|
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 {
|
2022-02-25 08:54:04 +00:00
|
|
|
description string
|
|
|
|
cfg *config.Config
|
|
|
|
spec oci.Spec
|
|
|
|
visibleDevices string
|
|
|
|
expectedError error
|
|
|
|
expectedNil bool
|
2022-03-15 12:29:14 +00:00
|
|
|
}{
|
2022-02-25 08:54:04 +00:00
|
|
|
{
|
|
|
|
description: "spec load error returns error",
|
|
|
|
spec: &oci.SpecMock{
|
2022-05-02 08:28:25 +00:00
|
|
|
LoadFunc: func() (*specs.Spec, error) {
|
|
|
|
return nil, fmt.Errorf("load failed")
|
2022-02-25 08:54:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: fmt.Errorf("load failed"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "visible devices not set returns nil",
|
|
|
|
visibleDevices: "NOT_SET",
|
|
|
|
expectedNil: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "visible devices empty returns nil",
|
|
|
|
visibleDevices: "",
|
|
|
|
expectedNil: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "visible devices 'void' returns nil",
|
|
|
|
visibleDevices: "void",
|
|
|
|
expectedNil: true,
|
|
|
|
},
|
2022-03-15 12:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
2022-02-25 08:54:04 +00:00
|
|
|
spec := tc.spec
|
|
|
|
if spec == nil {
|
|
|
|
spec = &oci.SpecMock{
|
|
|
|
LookupEnvFunc: func(s string) (string, bool) {
|
|
|
|
if tc.visibleDevices != "NOT_SET" && s == visibleDevicesEnvvar {
|
|
|
|
return tc.visibleDevices, true
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 13:42:59 +00:00
|
|
|
m, err := NewCSVModifier(logger, tc.cfg, spec)
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|