diff --git a/internal/lookup/root/root_test.go b/internal/lookup/root/root_test.go new file mode 100644 index 00000000..c256deb6 --- /dev/null +++ b/internal/lookup/root/root_test.go @@ -0,0 +1,81 @@ +/** +# Copyright 2023 NVIDIA CORPORATION +# +# 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 root + +import ( + "path/filepath" + "testing" + + testlog "github.com/sirupsen/logrus/hooks/test" + "github.com/stretchr/testify/require" + + "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" + "github.com/NVIDIA/nvidia-container-toolkit/internal/test" +) + +func TestDriverLibrariesLocate(t *testing.T) { + logger, _ := testlog.NewNullLogger() + + moduleRoot, err := test.GetModuleRoot() + require.NoError(t, err) + + testCases := []struct { + rootFs string + inputs []string + expected string + expectedError error + }{ + { + rootFs: "rootfs-empty", + inputs: []string{"libcuda.so.1", "libcuda.so.*", "libcuda.so.*.*", "libcuda.so.999.88.77"}, + expectedError: lookup.ErrNotFound, + }, + { + rootFs: "rootfs-no-cache-lib64", + inputs: []string{"libcuda.so.1", "libcuda.so.*", "libcuda.so.*.*", "libcuda.so.999.88.77"}, + expected: "/usr/lib64/libcuda.so.999.88.77", + }, + { + rootFs: "rootfs-1", + inputs: []string{"libcuda.so.1", "libcuda.so.*", "libcuda.so.*.*", "libcuda.so.999.88.77"}, + expected: "/lib/x86_64-linux-gnu/libcuda.so.999.88.77", + }, + { + rootFs: "rootfs-2", + inputs: []string{"libcuda.so.1", "libcuda.so.*", "libcuda.so.*.*", "libcuda.so.999.88.77"}, + expected: "/var/lib/nvidia/lib64/libcuda.so.999.88.77", + }, + } + + for _, tc := range testCases { + for _, input := range tc.inputs { + t.Run(tc.rootFs+input, func(t *testing.T) { + rootfs := filepath.Join(moduleRoot, "testdata", "lookup", tc.rootFs) + driver := New( + WithLogger(logger), + WithDriverRoot(rootfs), + ) + + candidates, err := driver.Libraries().Locate(input) + require.ErrorIs(t, err, tc.expectedError) + if tc.expectedError == nil { + require.Equal(t, []string{filepath.Join(rootfs, tc.expected)}, candidates) + } + }) + } + } +} diff --git a/testdata/go.mod b/testdata/go.mod new file mode 100644 index 00000000..e69de29b diff --git a/testdata/lookup/rootfs-1/README.md b/testdata/lookup/rootfs-1/README.md new file mode 100644 index 00000000..ba0c4a17 --- /dev/null +++ b/testdata/lookup/rootfs-1/README.md @@ -0,0 +1,2 @@ +This rootfs represents a host with the CUDA driver libraries installed in +/lib/x86_64-linux-gnu. The included /etc/ld.so.cache was copied from such as system. \ No newline at end of file diff --git a/testdata/lookup/rootfs-1/etc/ld.so.cache b/testdata/lookup/rootfs-1/etc/ld.so.cache new file mode 100644 index 00000000..8ef99f3f Binary files /dev/null and b/testdata/lookup/rootfs-1/etc/ld.so.cache differ diff --git a/testdata/lookup/rootfs-1/lib/x86_64-linux-gnu/libcuda.so.1 b/testdata/lookup/rootfs-1/lib/x86_64-linux-gnu/libcuda.so.1 new file mode 120000 index 00000000..b25100e4 --- /dev/null +++ b/testdata/lookup/rootfs-1/lib/x86_64-linux-gnu/libcuda.so.1 @@ -0,0 +1 @@ +libcuda.so.999.88.77 \ No newline at end of file diff --git a/testdata/lookup/rootfs-1/lib/x86_64-linux-gnu/libcuda.so.999.88.77 b/testdata/lookup/rootfs-1/lib/x86_64-linux-gnu/libcuda.so.999.88.77 new file mode 100644 index 00000000..e69de29b diff --git a/testdata/lookup/rootfs-2/README.md b/testdata/lookup/rootfs-2/README.md new file mode 100644 index 00000000..cf26d880 --- /dev/null +++ b/testdata/lookup/rootfs-2/README.md @@ -0,0 +1,3 @@ +This rootfs represents a host with the CUDA driver libraries installed in +/var/lib/nvidia/lib64. The included /etc/ld.so.cache was generated in a container +simulating such as system. \ No newline at end of file diff --git a/testdata/lookup/rootfs-2/etc/ld.so.cache b/testdata/lookup/rootfs-2/etc/ld.so.cache new file mode 100644 index 00000000..5fdcf29f Binary files /dev/null and b/testdata/lookup/rootfs-2/etc/ld.so.cache differ diff --git a/testdata/lookup/rootfs-2/var/lib/nvidia/lib64/libcuda.so.1 b/testdata/lookup/rootfs-2/var/lib/nvidia/lib64/libcuda.so.1 new file mode 120000 index 00000000..b25100e4 --- /dev/null +++ b/testdata/lookup/rootfs-2/var/lib/nvidia/lib64/libcuda.so.1 @@ -0,0 +1 @@ +libcuda.so.999.88.77 \ No newline at end of file diff --git a/testdata/lookup/rootfs-2/var/lib/nvidia/lib64/libcuda.so.999.88.77 b/testdata/lookup/rootfs-2/var/lib/nvidia/lib64/libcuda.so.999.88.77 new file mode 100644 index 00000000..e69de29b diff --git a/testdata/lookup/rootfs-empty/README.md b/testdata/lookup/rootfs-empty/README.md new file mode 100644 index 00000000..384dec06 --- /dev/null +++ b/testdata/lookup/rootfs-empty/README.md @@ -0,0 +1 @@ +The folders represents an empty rootfs. diff --git a/testdata/lookup/rootfs-no-cache-lib64/usr/lib64/libcuda.so.1 b/testdata/lookup/rootfs-no-cache-lib64/usr/lib64/libcuda.so.1 new file mode 120000 index 00000000..b25100e4 --- /dev/null +++ b/testdata/lookup/rootfs-no-cache-lib64/usr/lib64/libcuda.so.1 @@ -0,0 +1 @@ +libcuda.so.999.88.77 \ No newline at end of file diff --git a/testdata/lookup/rootfs-no-cache-lib64/usr/lib64/libcuda.so.999.88.77 b/testdata/lookup/rootfs-no-cache-lib64/usr/lib64/libcuda.so.999.88.77 new file mode 100644 index 00000000..e69de29b