|
| 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 | + "html/template" |
| 23 | + "strings" |
| 24 | + |
| 25 | + . "github.com/onsi/ginkgo/v2" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + nvidiaCdiRefreshDegradedSystemdTemplate = ` |
| 31 | + # Read the TMPDIR |
| 32 | + TMPDIR=$(cat /tmp/ctk_e2e_temp_dir.txt) |
| 33 | + export TMPDIR |
| 34 | +
|
| 35 | + # uninstall the nvidia-container-toolkit |
| 36 | + apt-get remove -y nvidia-container-toolkit nvidia-container-toolkit-base libnvidia-container-tools libnvidia-container1 |
| 37 | + apt-get autoremove -y |
| 38 | +
|
| 39 | + # Remove the cdi file if it exists |
| 40 | + if [ -f /var/run/cdi/nvidia.yaml ]; then |
| 41 | + rm -f /var/run/cdi/nvidia.yaml |
| 42 | + fi |
| 43 | +
|
| 44 | + # Stop the nvidia-cdi-refresh.path and nvidia-cdi-refresh.service units |
| 45 | + systemctl stop nvidia-cdi-refresh.path |
| 46 | + systemctl stop nvidia-cdi-refresh.service |
| 47 | + |
| 48 | + # Reload the systemd daemon |
| 49 | + systemctl daemon-reload |
| 50 | + |
| 51 | + # Start the dummy service to force systemd to enter a degraded state |
| 52 | + cat <<EOF > /etc/systemd/system/dummy.service |
| 53 | +[Unit] |
| 54 | +Description=Dummy systemd service |
| 55 | +
|
| 56 | +[Service] |
| 57 | +Type=oneshot |
| 58 | +ExecStart=/usr/bin/sh -c "exit 0" |
| 59 | +EOF |
| 60 | +
|
| 61 | + # We know the dummy service will fail, so we can ignore the error |
| 62 | + systemctl start dummy.service 2>/dev/null || true |
| 63 | +
|
| 64 | + # Install the nvidia-container-toolkit |
| 65 | + dpkg -i ${TMPDIR}/packages/ubuntu18.04/amd64/libnvidia-container1_*_amd64.deb ${TMPDIR}/packages/ubuntu18.04/amd64/nvidia-container-toolkit-base_*_amd64.deb ${TMPDIR}/packages/ubuntu18.04/amd64/libnvidia-container-tools_*_amd64.deb |
| 66 | + ` |
| 67 | + nvidiaCdiRefreshPathActiveTemplate = ` |
| 68 | + if ! systemctl status nvidia-cdi-refresh.path | grep "Active: active"; then |
| 69 | + echo "nvidia-cdi-refresh.path is not Active" |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | + ` |
| 73 | + nvidiaCdiRefreshServiceLoadedTemplate = ` |
| 74 | + if ! systemctl status nvidia-cdi-refresh.service | grep "Loaded: loaded"; then |
| 75 | + echo "nvidia-cdi-refresh.service is not loaded" |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | + ` |
| 79 | + |
| 80 | + nvidiaCdiRefreshFileExistsTemplate = ` |
| 81 | + # is /var/run/cdi/nvidia.yaml exists? and exit with 0 if it does not exist |
| 82 | + if [ ! -f /var/run/cdi/nvidia.yaml ]; then |
| 83 | + echo "nvidia.yaml file does not exist" |
| 84 | + exit 1 |
| 85 | + fi |
| 86 | +
|
| 87 | + # generate the nvidia.yaml file |
| 88 | + nvidia-ctk cdi generate --output=/tmp/nvidia.yaml |
| 89 | +
|
| 90 | + # diff the generated file with the one in /var/run/cdi/nvidia.yaml and exit with 0 if they are the same |
| 91 | + if ! diff /var/run/cdi/nvidia.yaml /tmp/nvidia.yaml; then |
| 92 | + echo "nvidia.yaml file is different" |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | + ` |
| 96 | + |
| 97 | + nvidiaCdiRefreshUpgradeTemplate = ` |
| 98 | + # remove the generated files |
| 99 | + rm /var/run/cdi/nvidia.yaml /tmp/nvidia.yaml |
| 100 | +
|
| 101 | + # Touch the nvidia-ctk binary to change the mtime |
| 102 | + # This will trigger the nvidia-cdi-refresh.path unit to call the |
| 103 | + # nvidia-cdi-refresh.service unit, simulating a change(update/downgrade) in the nvidia-ctk binary. |
| 104 | + touch $(which nvidia-ctk) |
| 105 | +
|
| 106 | + # wait for 3 seconds |
| 107 | + sleep 3 |
| 108 | +
|
| 109 | + # Check if the file /var/run/cdi/nvidia.yaml is created |
| 110 | + if [ ! -f /var/run/cdi/nvidia.yaml ]; then |
| 111 | + echo "nvidia.yaml file is not created after updating the modules.dep file" |
| 112 | + exit 1 |
| 113 | + fi |
| 114 | +
|
| 115 | + # generate the nvidia.yaml file |
| 116 | + nvidia-ctk cdi generate --output=/tmp/nvidia.yaml |
| 117 | +
|
| 118 | + # diff the generated file with the one in /var/run/cdi/nvidia.yaml and exit with 0 if they are the same |
| 119 | + if ! diff /var/run/cdi/nvidia.yaml /tmp/nvidia.yaml; then |
| 120 | + echo "nvidia.yaml file is different" |
| 121 | + exit 1 |
| 122 | + fi |
| 123 | + ` |
| 124 | +) |
| 125 | + |
| 126 | +var _ = Describe("nvidia-cdi-refresh", Ordered, ContinueOnFailure, Label("systemd-unit"), func() { |
| 127 | + var ( |
| 128 | + nestedContainerRunner Runner |
| 129 | + // TODO(@ArangoGutierrez): https://github.com/NVIDIA/nvidia-container-toolkit/pull/1235/files#r2302013660 |
| 130 | + outerContainerImage = "docker.io/kindest/base:v20250521-31a79fd4" |
| 131 | + ) |
| 132 | + |
| 133 | + BeforeAll(func(ctx context.Context) { |
| 134 | + var err error |
| 135 | + nestedContainerRunner, err = NewNestedContainerRunner(runner, outerContainerImage, installCTK, imageName+":"+imageTag, testContainerName) |
| 136 | + Expect(err).ToNot(HaveOccurred()) |
| 137 | + }) |
| 138 | + |
| 139 | + AfterAll(func(ctx context.Context) { |
| 140 | + // Cleanup: remove the container and the temporary script on the host. |
| 141 | + // Use || true to ensure cleanup doesn't fail the test |
| 142 | + runner.Run(fmt.Sprintf("docker rm -f %s 2>/dev/null || true", testContainerName)) //nolint:errcheck |
| 143 | + }) |
| 144 | + |
| 145 | + When("installing nvidia-container-toolkit", Ordered, func() { |
| 146 | + It("should load the nvidia-cdi-refresh.path unit", func(ctx context.Context) { |
| 147 | + _, _, err := nestedContainerRunner.Run(nvidiaCdiRefreshPathActiveTemplate) |
| 148 | + Expect(err).ToNot(HaveOccurred()) |
| 149 | + }) |
| 150 | + |
| 151 | + It("should load the nvidia-cdi-refresh.service unit", func(ctx context.Context) { |
| 152 | + _, _, err := nestedContainerRunner.Run(nvidiaCdiRefreshServiceLoadedTemplate) |
| 153 | + Expect(err).ToNot(HaveOccurred()) |
| 154 | + }) |
| 155 | + |
| 156 | + It("should generate the nvidia.yaml file", func(ctx context.Context) { |
| 157 | + _, _, err := nestedContainerRunner.Run(nvidiaCdiRefreshFileExistsTemplate) |
| 158 | + Expect(err).ToNot(HaveOccurred()) |
| 159 | + }) |
| 160 | + |
| 161 | + It("should refresh the nvidia.yaml file after upgrading the nvidia-container-toolkit", func(ctx context.Context) { |
| 162 | + _, _, err := nestedContainerRunner.Run(nvidiaCdiRefreshUpgradeTemplate) |
| 163 | + Expect(err).ToNot(HaveOccurred()) |
| 164 | + }) |
| 165 | + }) |
| 166 | + |
| 167 | + When("installing nvidia-container-toolkit on a system with a degraded systemd", Ordered, func() { |
| 168 | + BeforeAll(func(ctx context.Context) { |
| 169 | + tmpl, err := template.New("nvidiaCdiRefreshDegradedSystemd").Parse(nvidiaCdiRefreshDegradedSystemdTemplate) |
| 170 | + Expect(err).ToNot(HaveOccurred()) |
| 171 | + |
| 172 | + var nvidiaCdiRefreshDegradedSystemd strings.Builder |
| 173 | + err = tmpl.Execute(&nvidiaCdiRefreshDegradedSystemd, struct { |
| 174 | + ToolkitImage string |
| 175 | + }{ |
| 176 | + ToolkitImage: imageName + ":" + imageTag, |
| 177 | + }) |
| 178 | + Expect(err).ToNot(HaveOccurred()) |
| 179 | + |
| 180 | + _, _, err = nestedContainerRunner.Run(nvidiaCdiRefreshDegradedSystemd.String()) |
| 181 | + Expect(err).ToNot(HaveOccurred()) |
| 182 | + }) |
| 183 | + |
| 184 | + It("should load the nvidia-cdi-refresh.path unit", func(ctx context.Context) { |
| 185 | + _, _, err := nestedContainerRunner.Run(nvidiaCdiRefreshPathActiveTemplate) |
| 186 | + Expect(err).ToNot(HaveOccurred()) |
| 187 | + }) |
| 188 | + |
| 189 | + It("should load the nvidia-cdi-refresh.service unit", func(ctx context.Context) { |
| 190 | + _, _, err := nestedContainerRunner.Run(nvidiaCdiRefreshServiceLoadedTemplate) |
| 191 | + Expect(err).ToNot(HaveOccurred()) |
| 192 | + }) |
| 193 | + |
| 194 | + It("should generate the nvidia.yaml file", func(ctx context.Context) { |
| 195 | + _, _, err := nestedContainerRunner.Run(nvidiaCdiRefreshFileExistsTemplate) |
| 196 | + Expect(err).ToNot(HaveOccurred()) |
| 197 | + }) |
| 198 | + |
| 199 | + It("should generate the nvidia.yaml file", func(ctx context.Context) { |
| 200 | + _, _, err := nestedContainerRunner.Run(nvidiaCdiRefreshFileExistsTemplate) |
| 201 | + Expect(err).ToNot(HaveOccurred()) |
| 202 | + }) |
| 203 | + }) |
| 204 | +}) |
0 commit comments