Skip to content

Commit

Permalink
[no-relnote] Add toolkit install unit test
Browse files Browse the repository at this point in the history
This change adds basic toolkit installation unit tests. This required
that the source for files be specified when installing to allow for
a testdata folder to be used.

This replaces the currently unused shell-based tests in /test/container.

Signed-off-by: Evan Lezar <[email protected]>
  • Loading branch information
elezar committed Nov 5, 2024
1 parent 5bc0315 commit 8d869ac
Show file tree
Hide file tree
Showing 35 changed files with 177 additions and 110 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dist
artifacts
/dist
/artifacts
*.swp
*.swo
/coverage.out*
Expand All @@ -10,4 +10,4 @@ artifacts
/nvidia-container-toolkit
/nvidia-ctk
/shared-*
/release-*
/release-*
6 changes: 0 additions & 6 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,6 @@ image-packaging:
optional: true

# Define publish test helpers
.test:toolkit:
extends:
- .integration
variables:
TEST_CASES: "toolkit"

.test:docker:
extends:
- .integration
Expand Down
2 changes: 1 addition & 1 deletion deployments/container/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ build-packaging: PACKAGE_DIST = all
# Test targets
test-%: DIST = $(*)

TEST_CASES ?= toolkit docker crio containerd
TEST_CASES ?= docker crio containerd
$(TEST_TARGETS): test-%:
TEST_CASES="$(TEST_CASES)" bash -x $(CURDIR)/test/container/main.sh run \
$(CURDIR)/shared-$(*) \
Expand Down
3 changes: 1 addition & 2 deletions test/container/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ shopt -s lastpipe
readonly basedir="$(dirname "$(realpath "$0")")"
source "${basedir}/common.sh"

source "${basedir}/toolkit_test.sh"
source "${basedir}/docker_test.sh"
source "${basedir}/crio_test.sh"
source "${basedir}/containerd_test.sh"
Expand Down Expand Up @@ -66,7 +65,7 @@ done

trap '"$CLEANUP" && testing::cleanup' ERR

readonly test_cases="${TEST_CASES:-toolkit docker crio containerd}"
readonly test_cases="${TEST_CASES:-docker crio containerd}"

testing::cleanup
for tc in ${test_cases}; do
Expand Down
76 changes: 0 additions & 76 deletions test/container/toolkit_test.sh

This file was deleted.

1 change: 1 addition & 0 deletions testdata/installer/artifacts/deb/usr/bin/nvidia-cdi-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvidia-cdi-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvidia-container-cli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvidia-container-runtime
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvidia-container-runtime-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvidia-container-runtime.cdi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvidia-container-runtime.legacy
1 change: 1 addition & 0 deletions testdata/installer/artifacts/deb/usr/bin/nvidia-ctk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvidia-ctk
Empty file.
1 change: 1 addition & 0 deletions testdata/installer/artifacts/rpm/usr/bin/nvidia-cdi-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvidia-cdi-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvidia-container-cli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvidia-container-runtime
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvidia-container-runtime-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvidia-container-runtime.cdi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvidia-container-runtime.legacy
1 change: 1 addition & 0 deletions testdata/installer/artifacts/rpm/usr/bin/nvidia-ctk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nvidia-ctk
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion tools/container/nvidia-toolkit/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func Run(c *cli.Context, o *options) error {

o.toolkitOptions.ContainerRuntimeRuntimes = *cli.NewStringSlice(lowlevelRuntimePaths...)
}
err = toolkit.Install(c, &o.toolkitOptions, o.toolkitRoot())
err = toolkit.Install(c, &o.toolkitOptions, "", o.toolkitRoot())
if err != nil {
return fmt.Errorf("unable to install toolkit: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions tools/container/toolkit/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ const (

// installContainerRuntimes sets up the NVIDIA container runtimes, copying the executables
// and implementing the required wrapper
func installContainerRuntimes(toolkitDir string, driverRoot string) error {
func installContainerRuntimes(sourceRoot string, toolkitDir string) error {
runtimes := operator.GetRuntimes()
for _, runtime := range runtimes {
r := newNvidiaContainerRuntimeInstaller(runtime.Path)
r := newNvidiaContainerRuntimeInstaller(filepath.Join(sourceRoot, runtime.Path))

_, err := r.install(toolkitDir)
if err != nil {
Expand Down
39 changes: 20 additions & 19 deletions tools/container/toolkit/toolkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,9 @@ func TryDelete(cli *cli.Context, toolkitRoot string) error {
}

// Install installs the components of the NVIDIA container toolkit.
// The specified sourceRoot is searched for the components to install.
// Any existing installation is removed.
func Install(cli *cli.Context, opts *Options, toolkitRoot string) error {
func Install(cli *cli.Context, opts *Options, sourceRoot string, toolkitRoot string) error {
log.Infof("Installing NVIDIA container toolkit to '%v'", toolkitRoot)

log.Infof("Removing existing NVIDIA container toolkit installation")
Expand All @@ -307,42 +308,42 @@ func Install(cli *cli.Context, opts *Options, toolkitRoot string) error {
log.Errorf("Ignoring error: %v", fmt.Errorf("could not create required directories: %v", err))
}

err = installContainerLibraries(toolkitRoot)
err = installContainerLibraries(sourceRoot, toolkitRoot)
if err != nil && !opts.ignoreErrors {
return fmt.Errorf("error installing NVIDIA container library: %v", err)
} else if err != nil {
log.Errorf("Ignoring error: %v", fmt.Errorf("error installing NVIDIA container library: %v", err))
}

err = installContainerRuntimes(toolkitRoot, opts.DriverRoot)
err = installContainerRuntimes(sourceRoot, toolkitRoot)
if err != nil && !opts.ignoreErrors {
return fmt.Errorf("error installing NVIDIA container runtime: %v", err)
} else if err != nil {
log.Errorf("Ignoring error: %v", fmt.Errorf("error installing NVIDIA container runtime: %v", err))
}

nvidiaContainerCliExecutable, err := installContainerCLI(toolkitRoot)
nvidiaContainerCliExecutable, err := installContainerCLI(sourceRoot, toolkitRoot)
if err != nil && !opts.ignoreErrors {
return fmt.Errorf("error installing NVIDIA container CLI: %v", err)
} else if err != nil {
log.Errorf("Ignoring error: %v", fmt.Errorf("error installing NVIDIA container CLI: %v", err))
}

nvidiaContainerRuntimeHookPath, err := installRuntimeHook(toolkitRoot, toolkitConfigPath)
nvidiaContainerRuntimeHookPath, err := installRuntimeHook(sourceRoot, toolkitRoot, toolkitConfigPath)
if err != nil && !opts.ignoreErrors {
return fmt.Errorf("error installing NVIDIA container runtime hook: %v", err)
} else if err != nil {
log.Errorf("Ignoring error: %v", fmt.Errorf("error installing NVIDIA container runtime hook: %v", err))
}

nvidiaCTKPath, err := installContainerToolkitCLI(toolkitRoot)
nvidiaCTKPath, err := installContainerToolkitCLI(sourceRoot, toolkitRoot)
if err != nil && !opts.ignoreErrors {
return fmt.Errorf("error installing NVIDIA Container Toolkit CLI: %v", err)
} else if err != nil {
log.Errorf("Ignoring error: %v", fmt.Errorf("error installing NVIDIA Container Toolkit CLI: %v", err))
}

nvidiaCDIHookPath, err := installContainerCDIHookCLI(toolkitRoot)
nvidiaCDIHookPath, err := installContainerCDIHookCLI(sourceRoot, toolkitRoot)
if err != nil && !opts.ignoreErrors {
return fmt.Errorf("error installing NVIDIA Container CDI Hook CLI: %v", err)
} else if err != nil {
Expand Down Expand Up @@ -378,7 +379,7 @@ func Install(cli *cli.Context, opts *Options, toolkitRoot string) error {
// A predefined set of library candidates are considered, with the first one
// resulting in success being installed to the toolkit folder. The install process
// resolves the symlink for the library and copies the versioned library itself.
func installContainerLibraries(toolkitRoot string) error {
func installContainerLibraries(sourceRoot string, toolkitRoot string) error {
log.Infof("Installing NVIDIA container library to '%v'", toolkitRoot)

libs := []string{
Expand All @@ -387,7 +388,7 @@ func installContainerLibraries(toolkitRoot string) error {
}

for _, l := range libs {
err := installLibrary(l, toolkitRoot)
err := installLibrary(l, sourceRoot, toolkitRoot)
if err != nil {
return fmt.Errorf("failed to install %s: %v", l, err)
}
Expand All @@ -397,8 +398,8 @@ func installContainerLibraries(toolkitRoot string) error {
}

// installLibrary installs the specified library to the toolkit directory.
func installLibrary(libName string, toolkitRoot string) error {
libraryPath, err := findLibrary("", libName)
func installLibrary(libName string, sourceRoot string, toolkitRoot string) error {
libraryPath, err := findLibrary(sourceRoot, libName)
if err != nil {
return fmt.Errorf("error locating NVIDIA container library: %v", err)
}
Expand Down Expand Up @@ -524,9 +525,9 @@ func installToolkitConfig(c *cli.Context, toolkitConfigPath string, nvidiaContai
}

// installContainerToolkitCLI installs the nvidia-ctk CLI executable and wrapper.
func installContainerToolkitCLI(toolkitDir string) (string, error) {
func installContainerToolkitCLI(sourceRoot string, toolkitDir string) (string, error) {
e := executable{
source: "/usr/bin/nvidia-ctk",
source: filepath.Join(sourceRoot, "/usr/bin/nvidia-ctk"),
target: executableTarget{
dotfileName: "nvidia-ctk.real",
wrapperName: "nvidia-ctk",
Expand All @@ -537,9 +538,9 @@ func installContainerToolkitCLI(toolkitDir string) (string, error) {
}

// installContainerCDIHookCLI installs the nvidia-cdi-hook CLI executable and wrapper.
func installContainerCDIHookCLI(toolkitDir string) (string, error) {
func installContainerCDIHookCLI(sourceRoot string, toolkitDir string) (string, error) {
e := executable{
source: "/usr/bin/nvidia-cdi-hook",
source: filepath.Join(sourceRoot, "/usr/bin/nvidia-cdi-hook"),
target: executableTarget{
dotfileName: "nvidia-cdi-hook.real",
wrapperName: "nvidia-cdi-hook",
Expand All @@ -551,15 +552,15 @@ func installContainerCDIHookCLI(toolkitDir string) (string, error) {

// installContainerCLI sets up the NVIDIA container CLI executable, copying the executable
// and implementing the required wrapper
func installContainerCLI(toolkitRoot string) (string, error) {
func installContainerCLI(sourceRoot string, toolkitRoot string) (string, error) {
log.Infof("Installing NVIDIA container CLI from '%v'", nvidiaContainerCliSource)

env := map[string]string{
"LD_LIBRARY_PATH": toolkitRoot,
}

e := executable{
source: nvidiaContainerCliSource,
source: filepath.Join(sourceRoot, nvidiaContainerCliSource),
target: executableTarget{
dotfileName: "nvidia-container-cli.real",
wrapperName: "nvidia-container-cli",
Expand All @@ -576,15 +577,15 @@ func installContainerCLI(toolkitRoot string) (string, error) {

// installRuntimeHook sets up the NVIDIA runtime hook, copying the executable
// and implementing the required wrapper
func installRuntimeHook(toolkitRoot string, configFilePath string) (string, error) {
func installRuntimeHook(sourceRoot string, toolkitRoot string, configFilePath string) (string, error) {
log.Infof("Installing NVIDIA container runtime hook from '%v'", nvidiaContainerRuntimeHookSource)

argLines := []string{
fmt.Sprintf("-config \"%s\"", configFilePath),
}

e := executable{
source: nvidiaContainerRuntimeHookSource,
source: filepath.Join(sourceRoot, nvidiaContainerRuntimeHookSource),
target: executableTarget{
dotfileName: "nvidia-container-runtime-hook.real",
wrapperName: "nvidia-container-runtime-hook",
Expand Down
Loading

0 comments on commit 8d869ac

Please sign in to comment.