Skip to content

Commit 0181dde

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

File tree

4 files changed

+190
-13
lines changed

4 files changed

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

0 commit comments

Comments
 (0)