Skip to content

Commit a29f0da

Browse files
[no-relnote] Move runner declaration to BeforeSuite
Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
1 parent bb0b341 commit a29f0da

File tree

4 files changed

+66
-38
lines changed

4 files changed

+66
-38
lines changed

tests/e2e/e2e_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030

3131
// Test context
3232
var (
33+
runner Runner
34+
3335
ctx context.Context
3436

3537
installCTK bool
@@ -49,13 +51,35 @@ func TestMain(t *testing.T) {
4951
RegisterFailHandler(Fail)
5052

5153
ctx = context.Background()
52-
getTestEnv()
5354

5455
RunSpecs(t,
5556
suiteName,
5657
)
5758
}
5859

60+
var _ = BeforeSuite(func() {
61+
getTestEnv()
62+
63+
runner = NewRunner(
64+
WithHost(sshHost),
65+
WithPort(sshPort),
66+
WithSshKey(sshKey),
67+
WithSshUser(sshUser),
68+
)
69+
70+
if installCTK {
71+
installer, err := NewToolkitInstaller(
72+
WithRunner(runner),
73+
WithImage(imageName+":"+imageTag),
74+
WithTemplate(dockerInstallTemplate),
75+
)
76+
Expect(err).ToNot(HaveOccurred())
77+
78+
err = installer.Install()
79+
Expect(err).ToNot(HaveOccurred())
80+
}
81+
})
82+
5983
// getTestEnv gets the test environment variables
6084
func getTestEnv() {
6185
defer GinkgoRecover()
@@ -73,7 +97,6 @@ func getTestEnv() {
7397
sshUser = getRequiredEnvvar[string]("E2E_SSH_USER")
7498
sshPort = getEnvVarOrDefault("E2E_SSH_PORT", "22")
7599
}
76-
77100
}
78101

79102
// getRequiredEnvvar returns the specified envvar if set or raises an error.

tests/e2e/nvidia-container-cli_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,12 @@ IN_NS
7171

7272
var _ = Describe("nvidia-container-cli", Ordered, ContinueOnFailure, Label("libnvidia-container"), func() {
7373
var (
74-
runner Runner
7574
nestedContainerRunner Runner
7675
containerName = "node-container-e2e"
7776
hostOutput string
7877
)
7978

8079
BeforeAll(func(ctx context.Context) {
81-
runner = NewRunner(
82-
WithHost(sshHost),
83-
WithPort(sshPort),
84-
WithSshKey(sshKey),
85-
WithSshUser(sshUser),
86-
)
87-
8880
var err error
8981
nestedContainerRunner, err = NewNestedContainerRunner(runner, installCTK, imageName+":"+imageTag, containerName)
9082
Expect(err).ToNot(HaveOccurred())

tests/e2e/nvidia-container-toolkit_test.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,11 @@ import (
2828

2929
// Integration tests for Docker runtime
3030
var _ = Describe("docker", Ordered, ContinueOnFailure, func() {
31-
var runner Runner
3231
var hostDriverVersion string
3332
var hostDriverMajor string
3433

3534
// Install the NVIDIA Container Toolkit
3635
BeforeAll(func(ctx context.Context) {
37-
runner = NewRunner(
38-
WithHost(sshHost),
39-
WithPort(sshPort),
40-
WithSshKey(sshKey),
41-
WithSshUser(sshUser),
42-
)
43-
44-
if installCTK {
45-
installer, err := NewToolkitInstaller(
46-
WithRunner(runner),
47-
WithImage(imageName+":"+imageTag),
48-
WithTemplate(dockerInstallTemplate),
49-
)
50-
Expect(err).ToNot(HaveOccurred())
51-
52-
err = installer.Install()
53-
Expect(err).ToNot(HaveOccurred())
54-
}
55-
5636
driverOutput, _, err := runner.Run("nvidia-smi -q | grep \"Driver Version\"")
5737
Expect(err).ToNot(HaveOccurred())
5838
parts := strings.SplitN(driverOutput, ":", 2)

tests/e2e/runner.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
const (
32-
startTestContainerTemplate = `docker run -d --name {{.ContainerName}} --privileged --runtime=nvidia \
32+
outerContainerTemplate = `docker run -d --name {{.ContainerName}} --privileged --runtime=nvidia \
3333
-e NVIDIA_VISIBLE_DEVICES=runtime.nvidia.com/gpu=all \
3434
-e NVIDIA_DRIVER_CAPABILITIES=all \
3535
{{ range $i, $a := .AdditionalArguments -}}
@@ -156,6 +156,8 @@ func NewNestedContainerRunner(runner Runner, installCTK bool, image string, cont
156156
return nil, fmt.Errorf("failed to remove container: %w", err)
157157
}
158158

159+
// If installCTK is true, install the toolkit on the host. before creating
160+
// the nested container.
159161
if installCTK {
160162
installer, err := NewToolkitInstaller(
161163
WithRunner(runner),
@@ -172,7 +174,6 @@ func NewNestedContainerRunner(runner Runner, installCTK bool, image string, cont
172174
}
173175
} else {
174176
// If installCTK is false, we use the preinstalled toolkit.
175-
// TODO: This should be updated for other distributions and other components of the toolkit.
176177
output, _, err := runner.Run("ls /lib/**/libnvidia-container*.so.*.*")
177178
if err != nil {
178179
return nil, fmt.Errorf("failed to list toolkit libraries: %w", err)
@@ -186,16 +187,48 @@ func NewNestedContainerRunner(runner Runner, installCTK bool, image string, cont
186187
for _, lib := range strings.Split(output, "\n") {
187188
additionalContainerArguments = append(additionalContainerArguments, "-v "+lib+":"+lib)
188189
}
189-
additionalContainerArguments = append(additionalContainerArguments, "-v /usr/bin/nvidia-container-cli:/usr/bin/nvidia-container-cli")
190+
191+
// Look for NVIDIA binaries in standard locations and mount them as volumes
192+
nvidiaBinaries := []string{
193+
"nvidia-container-cli",
194+
"nvidia-container-runtime",
195+
"nvidia-container-runtime-hook",
196+
"nvidia-ctk",
197+
"nvidia-cdi-hook",
198+
"nvidia-container-runtime.cdi",
199+
"nvidia-container-runtime.legacy",
200+
}
201+
202+
searchPaths := []string{
203+
"/usr/bin",
204+
"/usr/sbin",
205+
"/usr/local/bin",
206+
"/usr/local/sbin",
207+
}
208+
209+
for _, binary := range nvidiaBinaries {
210+
for _, searchPath := range searchPaths {
211+
binaryPath := searchPath + "/" + binary
212+
// Check if the binary exists at this path
213+
checkCmd := fmt.Sprintf("test -f %s && echo 'exists'", binaryPath)
214+
output, _, err := runner.Run(checkCmd)
215+
if err == nil && strings.TrimSpace(output) == "exists" {
216+
// Binary found, add it as a volume mount
217+
additionalContainerArguments = append(additionalContainerArguments,
218+
fmt.Sprintf("-v %s:%s", binaryPath, binaryPath))
219+
break // Move to the next binary once found
220+
}
221+
}
222+
}
190223
}
191224

192225
// Launch the container in detached mode.
193-
var startContainerScriptBuilder strings.Builder
194-
startContainerTemplate, err := template.New("startContainer").Parse(startTestContainerTemplate)
226+
var outerContainerScriptBuilder strings.Builder
227+
outerContainerTemplate, err := template.New("outerContainer").Parse(outerContainerTemplate)
195228
if err != nil {
196229
return nil, fmt.Errorf("failed to parse start container template: %w", err)
197230
}
198-
err = startContainerTemplate.Execute(&startContainerScriptBuilder, struct {
231+
err = outerContainerTemplate.Execute(&outerContainerScriptBuilder, struct {
199232
ContainerName string
200233
AdditionalArguments []string
201234
}{
@@ -206,8 +239,8 @@ func NewNestedContainerRunner(runner Runner, installCTK bool, image string, cont
206239
return nil, fmt.Errorf("failed to execute start container template: %w", err)
207240
}
208241

209-
startContainerScript := startContainerScriptBuilder.String()
210-
_, _, err = runner.Run(startContainerScript)
242+
outerContainerScript := outerContainerScriptBuilder.String()
243+
_, _, err = runner.Run(outerContainerScript)
211244
if err != nil {
212245
return nil, fmt.Errorf("failed to run start container script: %w", err)
213246
}

0 commit comments

Comments
 (0)