Skip to content

Commit f84c038

Browse files
[no-relnote] Add E2E for libnvidia-container
Signed-off-by: Carlos Eduardo Arango Gutierrez <[email protected]>
1 parent f55ef6a commit f84c038

File tree

4 files changed

+189
-11
lines changed

4 files changed

+189
-11
lines changed

tests/e2e/Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ LOG_ARTIFACTS_DIR ?= $(CURDIR)/e2e_logs
2020

2121
GINKGO_BIN := $(CURDIR)/bin/ginkgo
2222

23+
# If GINKGO_FOCUS is not set, run all tests
24+
# current available tests:
25+
# - nvidia-container-cli
26+
# - docker
27+
GINKGO_FOCUS ?= nvidia-container-cli
28+
2329
test: $(GINKGO_BIN)
24-
$(GINKGO_BIN) $(GINKGO_ARGS) -v --json-report ginkgo.json ./tests/e2e/...
30+
$(GINKGO_BIN) $(GINKGO_ARGS) -v --json-report ginkgo.json --focus="$(GINKGO_FOCUS)" ./tests/e2e/...
2531

2632
$(GINKGO_BIN):
2733
mkdir -p $(CURDIR)/bin

tests/e2e/e2e_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,8 @@ func getTestEnv() {
6262

6363
installCTK = getEnvVarOrDefault("E2E_INSTALL_CTK", false)
6464

65-
if installCTK {
66-
imageName = getRequiredEnvvar[string]("E2E_IMAGE_NAME")
67-
68-
imageTag = getRequiredEnvvar[string]("E2E_IMAGE_TAG")
69-
70-
}
65+
imageName = getRequiredEnvvar[string]("E2E_IMAGE_NAME")
66+
imageTag = getRequiredEnvvar[string]("E2E_IMAGE_TAG")
7167

7268
sshKey = getRequiredEnvvar[string]("E2E_SSH_KEY")
7369
sshUser = getRequiredEnvvar[string]("E2E_SSH_USER")

tests/e2e/installer.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,20 @@ var dockerInstallTemplate = `
2828
#! /usr/bin/env bash
2929
set -xe
3030
31-
: ${IMAGE:={{.Image}}}
31+
# if the TEMP_DIR is already set, use it
32+
if [ -f /tmp/ctk_e2e_temp_dir.txt ]; then
33+
TEMP_DIR=$(cat /tmp/ctk_e2e_temp_dir.txt)
34+
else
35+
TEMP_DIR="/tmp/ctk_e2e.$(date +%s)_$RANDOM"
36+
echo "$TEMP_DIR" > /tmp/ctk_e2e_temp_dir.txt
37+
fi
38+
39+
# if TEMP_DIR does not exist, create it
40+
if [ ! -d "$TEMP_DIR" ]; then
41+
mkdir -p "$TEMP_DIR"
42+
fi
3243
33-
# Create a temporary directory
34-
TEMP_DIR="/tmp/ctk_e2e.$(date +%s)_$RANDOM"
35-
mkdir -p "$TEMP_DIR"
44+
: ${IMAGE:={{.Image}}}
3645
3746
# Given that docker has an init function that checks for the existence of the
3847
# nvidia-container-toolkit, we need to create a symlink to the nvidia-container-runtime-hook
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
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 e2e
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"strings"
23+
24+
. "github.com/onsi/ginkgo/v2"
25+
. "github.com/onsi/gomega"
26+
)
27+
28+
const (
29+
// libnvidiaContainerCliTestTemplate is an on-the-fly script that prepares a
30+
// minimal rootfs with GPU support and finally prints the list of visible GPUs
31+
// using `nvidia-smi -L`. The exit code as well as the standard output of this
32+
// script are therefore a good indicator of whether the NVIDIA Container
33+
// Toolkit is functioning correctly inside the container.
34+
libnvidiaContainerCliTestTemplate = `#!/usr/bin/env bash
35+
set -euo pipefail
36+
37+
apt-get update -y && apt-get install -y curl gnupg2
38+
39+
WORKDIR="$(mktemp -d)"
40+
ROOTFS="${WORKDIR}/rootfs"
41+
mkdir -p "${ROOTFS}"
42+
43+
export WORKDIR ROOTFS # make them visible in the child shell
44+
45+
unshare --mount --pid --fork --propagation private -- bash -eux <<'IN_NS'
46+
: "${ROOTFS:?}" "${WORKDIR:?}" # abort if either is empty
47+
48+
# 1 Populate minimal Ubuntu base
49+
curl -L http://cdimage.ubuntu.com/ubuntu-base/releases/22.04/release/ubuntu-base-22.04-base-amd64.tar.gz \
50+
| tar -C "$ROOTFS" -xz
51+
52+
# 2 Add non-root user
53+
useradd -R "$ROOTFS" -U -u 1000 -s /bin/bash nvidia
54+
55+
# 3 Bind-mount new root and unshare mounts
56+
mount --bind "$ROOTFS" "$ROOTFS"
57+
mount --make-private "$ROOTFS"
58+
cd "$ROOTFS"
59+
60+
# 4 Minimal virtual filesystems
61+
mount -t proc proc proc
62+
mount -t sysfs sys sys
63+
mount -t tmpfs tmp tmp
64+
mount -t tmpfs run run
65+
66+
# 5 GPU setup
67+
nvidia-container-cli --load-kmods --debug=container-cli.log \
68+
configure --ldconfig=@/sbin/ldconfig.real \
69+
--no-cgroups --utility --device=0 "$(pwd)"
70+
71+
# 6 Switch root
72+
mkdir -p mnt
73+
pivot_root . mnt
74+
umount -l /mnt
75+
76+
exec nvidia-smi -L
77+
IN_NS
78+
`
79+
80+
dockerRunCmdTemplate = `docker run --name %s -d --privileged --runtime=nvidia \
81+
-e NVIDIA_VISIBLE_DEVICES=all \
82+
-e NVIDIA_DRIVER_CAPABILITIES=all \
83+
-v %s:/libnvidia-container-cli.sh \
84+
--entrypoint /libnvidia-container-cli.sh %s`
85+
)
86+
87+
var _ = Describe("nvidia-container-cli", Ordered, ContinueOnFailure, func() {
88+
var (
89+
runner Runner
90+
testScript = "/tmp/libnvidia-container-cli.sh"
91+
dockerImage = "ghcr.io/nvidia/container-toolkit:5e8c1411-ubuntu20.04"
92+
containerName = "nvidia-cli-e2e"
93+
dockerRunCmd string
94+
)
95+
96+
BeforeAll(func(ctx context.Context) {
97+
runner = NewRunner(
98+
WithHost(sshHost),
99+
WithPort(sshPort),
100+
WithSshKey(sshKey),
101+
WithSshUser(sshUser),
102+
)
103+
104+
if installCTK {
105+
installer, err := NewToolkitInstaller(
106+
WithRunner(runner),
107+
WithImage(imageName+":"+imageTag),
108+
WithTemplate(dockerInstallTemplate),
109+
)
110+
Expect(err).ToNot(HaveOccurred())
111+
112+
err = installer.Install()
113+
Expect(err).ToNot(HaveOccurred())
114+
}
115+
})
116+
117+
AfterAll(func(ctx context.Context) {
118+
// Cleanup: remove the container and the temporary script on the host.
119+
runner.Run(fmt.Sprintf("docker rm -f %s", containerName))
120+
runner.Run(fmt.Sprintf("rm -f %s", testScript))
121+
})
122+
123+
It("should report the same GPUs inside the container as on the host", func(ctx context.Context) {
124+
// Write the script to the remote host and make it executable.
125+
createScriptCmd := fmt.Sprintf(
126+
"cat > %s <<'EOF'\n%s\nEOF\nchmod +x %s",
127+
testScript, libnvidiaContainerCliTestTemplate, testScript,
128+
)
129+
130+
_, _, err := runner.Run(createScriptCmd)
131+
Expect(err).ToNot(HaveOccurred())
132+
133+
// If a container with the same name exists from a previous test run, remove it first.
134+
runner.Run(fmt.Sprintf("docker rm -f %s", containerName))
135+
136+
// Build the docker run command (detached mode) from the template so it
137+
// stays readable while still resulting in a single-line invocation.
138+
dockerRunCmd = fmt.Sprintf(dockerRunCmdTemplate, containerName, testScript, dockerImage)
139+
140+
// Launch the container in detached mode.
141+
_, _, err = runner.Run(dockerRunCmd)
142+
Expect(err).ToNot(HaveOccurred())
143+
144+
// Capture the host GPU list.
145+
hostOutput, _, err := runner.Run("nvidia-smi -L")
146+
Expect(err).ToNot(HaveOccurred())
147+
148+
hostOutput = strings.TrimSpace(strings.ReplaceAll(hostOutput, "\r", ""))
149+
150+
// Poll the logs of the already running container until we observe
151+
// the GPU list matching the host or until a 5-minute timeout elapses.
152+
Eventually(func() string {
153+
logs, _, err := runner.Run(fmt.Sprintf("docker logs %s", containerName))
154+
if err != nil {
155+
return ""
156+
}
157+
158+
lines := strings.Split(strings.TrimSpace(logs), "\n")
159+
if len(lines) == 0 {
160+
return ""
161+
}
162+
163+
last := strings.TrimSpace(strings.ReplaceAll(lines[len(lines)-1], "\r", ""))
164+
return last
165+
}, "5m", "5s").Should(Equal(hostOutput))
166+
})
167+
})

0 commit comments

Comments
 (0)