2022-07-15 10:17:01 +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 discover
|
|
|
|
|
|
|
|
import (
|
2022-09-29 10:10:58 +00:00
|
|
|
"fmt"
|
2022-07-15 10:17:01 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-09-29 10:10:58 +00:00
|
|
|
testlog "github.com/sirupsen/logrus/hooks/test"
|
2022-07-15 10:17:01 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-12-12 14:26:21 +00:00
|
|
|
const (
|
|
|
|
testNvidiaCTKPath = "/foo/bar/nvidia-ctk"
|
|
|
|
)
|
|
|
|
|
2022-09-29 10:10:58 +00:00
|
|
|
func TestLDCacheUpdateHook(t *testing.T) {
|
|
|
|
logger, _ := testlog.NewNullLogger()
|
|
|
|
|
|
|
|
cfg := Config{
|
2023-01-18 10:41:29 +00:00
|
|
|
Root: "/",
|
|
|
|
NvidiaCTKPath: testNvidiaCTKPath,
|
2022-09-29 10:10:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
description string
|
|
|
|
mounts []Mount
|
|
|
|
mountError error
|
|
|
|
expectedError error
|
|
|
|
expectedArgs []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "empty mounts",
|
2022-09-29 10:14:12 +00:00
|
|
|
expectedArgs: []string{"nvidia-ctk", "hook", "update-ldcache"},
|
2022-09-29 10:10:58 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "mount error",
|
|
|
|
mountError: fmt.Errorf("mountError"),
|
|
|
|
expectedError: fmt.Errorf("mountError"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "library folders are added to args",
|
|
|
|
mounts: []Mount{
|
|
|
|
{
|
|
|
|
Path: "/usr/local/lib/libfoo.so",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "/usr/bin/notlib",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "/usr/local/libother/libfoo.so",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "/usr/local/lib/libbar.so",
|
|
|
|
},
|
|
|
|
},
|
2022-09-29 10:14:12 +00:00
|
|
|
expectedArgs: []string{"nvidia-ctk", "hook", "update-ldcache", "--folder", "/usr/local/lib", "--folder", "/usr/local/libother"},
|
2022-09-29 10:10:58 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "host paths are ignored",
|
|
|
|
mounts: []Mount{
|
|
|
|
{
|
|
|
|
HostPath: "/usr/local/other/libfoo.so",
|
|
|
|
Path: "/usr/local/lib/libfoo.so",
|
|
|
|
},
|
|
|
|
},
|
2022-09-29 10:14:12 +00:00
|
|
|
expectedArgs: []string{"nvidia-ctk", "hook", "update-ldcache", "--folder", "/usr/local/lib"},
|
2022-09-29 10:10:58 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
|
|
mountMock := &DiscoverMock{
|
|
|
|
MountsFunc: func() ([]Mount, error) {
|
|
|
|
return tc.mounts, tc.mountError
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expectedHook := Hook{
|
2022-12-12 14:26:21 +00:00
|
|
|
Path: testNvidiaCTKPath,
|
2022-09-29 10:10:58 +00:00
|
|
|
Args: tc.expectedArgs,
|
|
|
|
Lifecycle: "createContainer",
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := NewLDCacheUpdateHook(logger, mountMock, &cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
hooks, err := d.Hooks()
|
|
|
|
require.Len(t, mountMock.MountsCalls(), 1)
|
|
|
|
require.Len(t, mountMock.DevicesCalls(), 0)
|
|
|
|
require.Len(t, mountMock.HooksCalls(), 0)
|
|
|
|
if tc.expectedError != nil {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, hooks, 1)
|
|
|
|
|
|
|
|
require.EqualValues(t, hooks[0], expectedHook)
|
|
|
|
|
|
|
|
devices, err := d.Devices()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, devices)
|
|
|
|
|
|
|
|
mounts, err := d.Mounts()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, mounts)
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-15 10:17:01 +00:00
|
|
|
func TestIsLibName(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
isLib bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "",
|
|
|
|
isLib: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "lib/not/.so",
|
|
|
|
isLib: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "lib.so",
|
|
|
|
isLib: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "notlibcuda.so",
|
|
|
|
isLib: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "libcuda.so",
|
|
|
|
isLib: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "libcuda.so.1",
|
|
|
|
isLib: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "libcuda.soNOT",
|
|
|
|
isLib: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
require.Equal(t, tc.isLib, isLibName(tc.name))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|