Skip to content

Commit f0de6c1

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

File tree

4 files changed

+200
-13
lines changed

4 files changed

+200
-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: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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 = `#!/busybox/sh
35+
set -euo pipefail
36+
37+
# Create a temporary directory and rootfs path
38+
TMPDIR="$(mktemp -d)"
39+
ROOTFS="${TMPDIR}/rootfs"
40+
mkdir -p "${ROOTFS}"
41+
42+
# Expose ROOTFS for the child namespace
43+
export ROOTFS
44+
45+
busybox wget -qO- http://cdimage.ubuntu.com/ubuntu-base/releases/22.04/release/ubuntu-base-22.04-base-amd64.tar.gz \
46+
| tar -C "$ROOTFS" -xz
47+
48+
# Enter a new mount + PID namespace so we can pivot_root without touching the
49+
# container's original filesystem.
50+
busybox unshare --mount --pid --fork --propagation private -- busybox sh -eux <<'IN_NS'
51+
: "${ROOTFS:?}"
52+
53+
# 1 Bind-mount the new root and make the mount private
54+
busybox mount --bind "$ROOTFS" "$ROOTFS"
55+
busybox mount --make-private "$ROOTFS"
56+
cd "$ROOTFS"
57+
58+
# 2 Minimal virtual filesystems
59+
busybox mount -t proc proc proc
60+
busybox mount -t sysfs sys sys
61+
busybox mount -t tmpfs tmp tmp
62+
busybox mount -t tmpfs run run
63+
64+
# 3 GPU setup via nvidia-container-cli
65+
nvidia-container-cli --load-kmods \
66+
configure \
67+
--no-cgroups --utility --device=0 "$(pwd)"
68+
69+
# 4 Switch root into the prepared filesystem
70+
mkdir -p mnt
71+
busybox pivot_root . mnt
72+
busybox umount -l /mnt
73+
74+
exec nvidia-smi -L
75+
IN_NS
76+
`
77+
78+
dockerRunCmdTemplate = `docker run --name %s -d --privileged --runtime=nvidia \
79+
-e NVIDIA_VISIBLE_DEVICES=all \
80+
-e NVIDIA_DRIVER_CAPABILITIES=all \
81+
-v %s:/libnvidia-container-cli.sh \
82+
--entrypoint /libnvidia-container-cli.sh %s`
83+
)
84+
85+
var _ = Describe("nvidia-container-cli", Ordered, ContinueOnFailure, func() {
86+
var (
87+
runner Runner
88+
dockerImage = fmt.Sprintf("%s:%s", imageName, imageTag)
89+
containerName = "nvidia-cli-e2e"
90+
hostOutput string
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+
// Capture the host GPU list.
114+
var err error
115+
hostOutput, _, err = runner.Run("nvidia-smi -L")
116+
Expect(err).ToNot(HaveOccurred())
117+
})
118+
119+
AfterAll(func(ctx context.Context) {
120+
// Cleanup: remove the container and the temporary script on the host.
121+
runner.Run(fmt.Sprintf("docker rm -f %s", containerName))
122+
})
123+
124+
It("should report the same GPUs inside the container as on the host", func(ctx context.Context) {
125+
testScriptPath := "/tmp/libnvidia-container-cli.sh"
126+
127+
// Write the script to the remote host and make it executable.
128+
createScriptCmd := fmt.Sprintf(
129+
"cat > %s <<'EOF'\n%s\nEOF\nchmod +x %s",
130+
testScriptPath, libnvidiaContainerCliTestTemplate, testScriptPath,
131+
)
132+
133+
_, _, err := runner.Run(createScriptCmd)
134+
Expect(err).ToNot(HaveOccurred())
135+
136+
// If a container with the same name exists from a previous test run, remove it first.
137+
_, _, err = runner.Run(fmt.Sprintf("docker rm -f %s", containerName))
138+
Expect(err).ToNot(HaveOccurred())
139+
140+
// Build the docker run command (detached mode) from the template so it
141+
// stays readable while still resulting in a single-line invocation.
142+
dockerRunCmd := fmt.Sprintf(dockerRunCmdTemplate, containerName, testScriptPath, dockerImage)
143+
144+
// Launch the container in detached mode.
145+
_, _, err = runner.Run(dockerRunCmd)
146+
Expect(err).ToNot(HaveOccurred())
147+
148+
//check if the container is running
149+
containerOutput, _, err := runner.Run(fmt.Sprintf("docker ps -a | grep %s", containerName))
150+
Expect(err).ToNot(HaveOccurred())
151+
fmt.Println("containerOutput: ", containerOutput)
152+
153+
hostOutputNormalized := strings.TrimSpace(strings.ReplaceAll(hostOutput, "\r", ""))
154+
155+
// Poll the logs of the already running container until we observe
156+
// the GPU list matching the host or until a 5-minute timeout elapses.
157+
Eventually(func() string {
158+
logs, logsErr, err := runner.Run(fmt.Sprintf("docker logs --tail 10 %s", containerName))
159+
if err != nil {
160+
return ""
161+
}
162+
163+
fmt.Println("logs: ", logs)
164+
fmt.Println("logsErr: ", logsErr)
165+
166+
lines := strings.Split(strings.TrimSpace(logs), "\n")
167+
if len(lines) == 0 {
168+
return ""
169+
}
170+
171+
last := strings.TrimSpace(strings.ReplaceAll(lines[len(lines)-1], "\r", ""))
172+
return last
173+
}, "5m", "10s").Should(Equal(hostOutputNormalized))
174+
175+
// Remove the script from the remote host.
176+
runner.Run(fmt.Sprintf("rm -f %s", testScriptPath))
177+
})
178+
})

0 commit comments

Comments
 (0)