|
| 1 | +/** |
| 2 | +# Copyright 2024 NVIDIA CORPORATION |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +**/ |
| 16 | + |
| 17 | +package discover |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "testing" |
| 22 | + |
| 23 | + testlog "github.com/sirupsen/logrus/hooks/test" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + |
| 26 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" |
| 27 | +) |
| 28 | + |
| 29 | +func TestMountsToContainerPath(t *testing.T) { |
| 30 | + logger, _ := testlog.NewNullLogger() |
| 31 | + mountOptions := []string{ |
| 32 | + "ro", |
| 33 | + "nosuid", |
| 34 | + "nodev", |
| 35 | + "bind", |
| 36 | + } |
| 37 | + |
| 38 | + testCases := []struct { |
| 39 | + description string |
| 40 | + required []string |
| 41 | + locator lookup.Locator |
| 42 | + containerRoot string |
| 43 | + expectedMounts []Mount |
| 44 | + expectedError error |
| 45 | + }{ |
| 46 | + { |
| 47 | + description: "containerRoot is prepended", |
| 48 | + required: []string{"a/path/exists.txt", "another/path/exists.txt"}, |
| 49 | + locator: &lookup.LocatorMock{ |
| 50 | + LocateFunc: func(s string) ([]string, error) { |
| 51 | + return []string{"/located/root/" + s}, nil |
| 52 | + }, |
| 53 | + }, |
| 54 | + containerRoot: "/container", |
| 55 | + expectedMounts: []Mount{ |
| 56 | + { |
| 57 | + HostPath: "/located/root/a/path/exists.txt", |
| 58 | + Path: "/container/a/path/exists.txt", |
| 59 | + Options: mountOptions, |
| 60 | + }, |
| 61 | + { |
| 62 | + HostPath: "/located/root/another/path/exists.txt", |
| 63 | + Path: "/container/another/path/exists.txt", |
| 64 | + Options: mountOptions, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + description: "duplicate mounts are skipped", |
| 70 | + required: []string{"a/path/exists.txt", "another/path/exists.txt"}, |
| 71 | + locator: &lookup.LocatorMock{ |
| 72 | + LocateFunc: func(s string) ([]string, error) { |
| 73 | + return []string{"/located/root/single.txt"}, nil |
| 74 | + }, |
| 75 | + }, |
| 76 | + containerRoot: "/container", |
| 77 | + expectedMounts: []Mount{ |
| 78 | + { |
| 79 | + HostPath: "/located/root/single.txt", |
| 80 | + Path: "/container/a/path/exists.txt", |
| 81 | + Options: mountOptions, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + description: "locator errors are ignored", |
| 87 | + required: []string{"a/path/exists.txt"}, |
| 88 | + locator: &lookup.LocatorMock{ |
| 89 | + LocateFunc: func(s string) ([]string, error) { |
| 90 | + return nil, errors.New("not found") |
| 91 | + }, |
| 92 | + }, |
| 93 | + containerRoot: "/container", |
| 94 | + expectedMounts: []Mount{}, |
| 95 | + }, |
| 96 | + { |
| 97 | + description: "not located are ignored", |
| 98 | + required: []string{"a/path/exists.txt"}, |
| 99 | + locator: &lookup.LocatorMock{ |
| 100 | + LocateFunc: func(s string) ([]string, error) { |
| 101 | + return nil, nil |
| 102 | + }, |
| 103 | + }, |
| 104 | + containerRoot: "/container", |
| 105 | + expectedMounts: []Mount{}, |
| 106 | + }, |
| 107 | + { |
| 108 | + description: "second candidate is ignored", |
| 109 | + required: []string{"a/path/exists.txt"}, |
| 110 | + locator: &lookup.LocatorMock{ |
| 111 | + LocateFunc: func(s string) ([]string, error) { |
| 112 | + return []string{"/located/root/" + s, "/located2/root/" + s}, nil |
| 113 | + }, |
| 114 | + }, |
| 115 | + containerRoot: "/container", |
| 116 | + expectedMounts: []Mount{ |
| 117 | + { |
| 118 | + HostPath: "/located/root/a/path/exists.txt", |
| 119 | + Path: "/container/a/path/exists.txt", |
| 120 | + Options: mountOptions, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + for _, tc := range testCases { |
| 127 | + t.Run(tc.description, func(t *testing.T) { |
| 128 | + d := mountsToContainerPath{ |
| 129 | + logger: logger, |
| 130 | + locator: tc.locator, |
| 131 | + required: tc.required, |
| 132 | + containerRoot: tc.containerRoot, |
| 133 | + } |
| 134 | + |
| 135 | + devices, err := d.Devices() |
| 136 | + require.NoError(t, err) |
| 137 | + require.Empty(t, devices) |
| 138 | + |
| 139 | + hooks, err := d.Hooks() |
| 140 | + require.NoError(t, err) |
| 141 | + require.Empty(t, hooks) |
| 142 | + |
| 143 | + mounts, err := d.Mounts() |
| 144 | + require.ErrorIs(t, err, tc.expectedError) |
| 145 | + require.ElementsMatch(t, tc.expectedMounts, mounts) |
| 146 | + }) |
| 147 | + } |
| 148 | +} |
0 commit comments