2022-03-09 15:52:50 +00:00
|
|
|
/*
|
|
|
|
# Copyright (c) 2021, 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 (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
testlog "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
2022-03-10 13:00:24 +00:00
|
|
|
func TestMountsReturnsEmptyDevices(t *testing.T) {
|
|
|
|
d := mounts{}
|
|
|
|
devices, err := d.Devices()
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, devices)
|
|
|
|
}
|
|
|
|
|
2022-03-09 15:52:50 +00:00
|
|
|
func TestMounts(t *testing.T) {
|
2023-02-07 11:10:32 +00:00
|
|
|
|
|
|
|
mountOptions := []string{
|
|
|
|
"ro",
|
|
|
|
"nosuid",
|
|
|
|
"nodev",
|
|
|
|
"bind",
|
|
|
|
}
|
|
|
|
|
2022-03-09 15:52:50 +00:00
|
|
|
logger, logHook := testlog.NewNullLogger()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
description string
|
|
|
|
expectedError error
|
|
|
|
expectedMounts []Mount
|
2022-04-05 07:35:27 +00:00
|
|
|
input *mounts
|
2022-03-09 15:52:50 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "nill lookup returns error",
|
|
|
|
expectedError: fmt.Errorf("no lookup defined"),
|
2022-04-05 07:35:27 +00:00
|
|
|
input: &mounts{},
|
2022-03-09 15:52:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "empty required returns no mounts",
|
|
|
|
expectedError: nil,
|
2022-04-05 07:35:27 +00:00
|
|
|
input: &mounts{
|
2022-03-09 15:52:50 +00:00
|
|
|
lookup: &lookup.LocatorMock{
|
|
|
|
LocateFunc: func(string) ([]string, error) {
|
|
|
|
return []string{"located"}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "required returns located",
|
|
|
|
expectedError: nil,
|
2022-04-05 07:35:27 +00:00
|
|
|
input: &mounts{
|
2022-03-09 15:52:50 +00:00
|
|
|
lookup: &lookup.LocatorMock{
|
|
|
|
LocateFunc: func(string) ([]string, error) {
|
|
|
|
return []string{"located"}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: []string{"required"},
|
|
|
|
},
|
2023-02-07 11:10:32 +00:00
|
|
|
expectedMounts: []Mount{{Path: "located", HostPath: "located", Options: mountOptions}},
|
2022-03-09 15:52:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "mounts removes located duplicates",
|
|
|
|
expectedError: nil,
|
2022-04-05 07:35:27 +00:00
|
|
|
input: &mounts{
|
2022-03-09 15:52:50 +00:00
|
|
|
lookup: &lookup.LocatorMock{
|
|
|
|
LocateFunc: func(string) ([]string, error) {
|
|
|
|
return []string{"located"}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: []string{"required0", "required1"},
|
|
|
|
},
|
2023-02-07 11:10:32 +00:00
|
|
|
expectedMounts: []Mount{{Path: "located", HostPath: "located", Options: mountOptions}},
|
2022-03-09 15:52:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "mounts skips located errors",
|
2022-04-05 07:35:27 +00:00
|
|
|
input: &mounts{
|
2022-03-09 15:52:50 +00:00
|
|
|
lookup: &lookup.LocatorMock{
|
|
|
|
LocateFunc: func(s string) ([]string, error) {
|
|
|
|
if s == "error" {
|
|
|
|
return nil, fmt.Errorf(s)
|
|
|
|
}
|
|
|
|
return []string{s}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: []string{"required0", "error", "required1"},
|
|
|
|
},
|
2023-02-07 11:10:32 +00:00
|
|
|
expectedMounts: []Mount{{Path: "required0", HostPath: "required0", Options: mountOptions}, {Path: "required1", HostPath: "required1", Options: mountOptions}},
|
2022-03-09 15:52:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "mounts skips unlocated",
|
2022-04-05 07:35:27 +00:00
|
|
|
input: &mounts{
|
2022-03-09 15:52:50 +00:00
|
|
|
lookup: &lookup.LocatorMock{
|
|
|
|
LocateFunc: func(s string) ([]string, error) {
|
|
|
|
if s == "empty" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return []string{s}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: []string{"required0", "empty", "required1"},
|
|
|
|
},
|
2023-02-07 11:10:32 +00:00
|
|
|
expectedMounts: []Mount{{Path: "required0", HostPath: "required0", Options: mountOptions}, {Path: "required1", HostPath: "required1", Options: mountOptions}},
|
2022-03-09 15:52:50 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-29 14:50:17 +00:00
|
|
|
description: "mounts adds multiple",
|
2022-04-05 07:35:27 +00:00
|
|
|
input: &mounts{
|
2022-03-09 15:52:50 +00:00
|
|
|
lookup: &lookup.LocatorMock{
|
|
|
|
LocateFunc: func(s string) ([]string, error) {
|
|
|
|
if s == "multiple" {
|
|
|
|
return []string{"multiple0", "multiple1"}, nil
|
|
|
|
}
|
|
|
|
return []string{s}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: []string{"required0", "multiple", "required1"},
|
|
|
|
},
|
|
|
|
expectedMounts: []Mount{
|
2023-02-07 11:10:32 +00:00
|
|
|
{Path: "required0", HostPath: "required0", Options: mountOptions},
|
|
|
|
{Path: "multiple0", HostPath: "multiple0", Options: mountOptions},
|
|
|
|
{Path: "multiple1", HostPath: "multiple1", Options: mountOptions},
|
|
|
|
{Path: "required1", HostPath: "required1", Options: mountOptions},
|
2022-06-29 14:50:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "mounts uses relative path",
|
|
|
|
input: &mounts{
|
|
|
|
lookup: &lookup.LocatorMock{
|
|
|
|
LocateFunc: func(s string) ([]string, error) {
|
2022-07-15 14:42:06 +00:00
|
|
|
return []string{"/some/root/located"}, nil
|
2022-06-29 14:50:17 +00:00
|
|
|
},
|
|
|
|
},
|
2022-07-15 14:42:06 +00:00
|
|
|
root: "/some/root",
|
2022-06-29 14:50:17 +00:00
|
|
|
required: []string{"required0", "multiple", "required1"},
|
|
|
|
},
|
|
|
|
expectedMounts: []Mount{
|
2023-02-07 11:10:32 +00:00
|
|
|
{Path: "/located", HostPath: "/some/root/located", Options: mountOptions},
|
2022-03-09 15:52:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
logHook.Reset()
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
|
|
tc.input.logger = logger
|
|
|
|
mounts, err := tc.input.Mounts()
|
|
|
|
|
|
|
|
if tc.expectedError != nil {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
require.ElementsMatch(t, tc.expectedMounts, mounts)
|
|
|
|
|
|
|
|
// We check that the mock is called for each element of required
|
|
|
|
if tc.input.lookup != nil {
|
|
|
|
mock := tc.input.lookup.(*lookup.LocatorMock)
|
|
|
|
require.Len(t, mock.LocateCalls(), len(tc.input.required))
|
|
|
|
var args []string
|
|
|
|
for _, c := range mock.LocateCalls() {
|
|
|
|
args = append(args, c.S)
|
|
|
|
}
|
|
|
|
require.EqualValues(t, args, tc.input.required)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|