|
| 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 | + |
| 23 | + . "github.com/onsi/ginkgo/v2" |
| 24 | + . "github.com/onsi/gomega" |
| 25 | + "sigs.k8s.io/kind/pkg/build/nodeimage" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + nvidiaCdiRefreshInstallTemplate = ` |
| 30 | + if ! systemctl status nvidia-cdi-refresh.path | grep "Loaded: loaded"; then |
| 31 | + echo "nvidia-cdi-refresh.path is not loaded" |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | + if ! systemctl status nvidia-cdi-refresh.service | grep "Loaded: loaded"; then |
| 35 | + echo "nvidia-cdi-refresh.service is not loaded" |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | + ` |
| 39 | + |
| 40 | + nvidiaCdiRefreshFileExistsTemplate = ` |
| 41 | + # is /var/run/cdi/nvidia.yaml exists? and exit with 0 if it does not exist |
| 42 | + if [ ! -f /var/run/cdi/nvidia.yaml ]; then |
| 43 | + echo "nvidia.yaml file does not exist" |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | +
|
| 47 | + # generate the nvidia.yaml file |
| 48 | + nvidia-ctk cdi generate --output=/tmp/nvidia.yaml |
| 49 | +
|
| 50 | + # diff the generated file with the one in /var/run/cdi/nvidia.yaml and exit with 0 if they are the same |
| 51 | + if ! diff /var/run/cdi/nvidia.yaml /tmp/nvidia.yaml; then |
| 52 | + echo "nvidia.yaml file is different" |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | + ` |
| 56 | + |
| 57 | + nvidiaCdiRefreshUpgradeTemplate = ` |
| 58 | + # remove the generated files |
| 59 | + rm /var/run/cdi/nvidia.yaml /tmp/nvidia.yaml |
| 60 | +
|
| 61 | + # Touch the nvidia-ctk binary to change the mtime |
| 62 | + # This will trigger the nvidia-cdi-refresh.path unit to call the |
| 63 | + # nvidia-cdi-refresh.service unit, simulating a change(update/downgrade) in the nvidia-ctk binary. |
| 64 | + touch $(which nvidia-ctk) |
| 65 | +
|
| 66 | + # wait for 3 seconds |
| 67 | + sleep 3 |
| 68 | +
|
| 69 | + # Check if the file /var/run/cdi/nvidia.yaml is created |
| 70 | + if [ ! -f /var/run/cdi/nvidia.yaml ]; then |
| 71 | + echo "nvidia.yaml file is not created after updating the modules.dep file" |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | +
|
| 75 | + # generate the nvidia.yaml file |
| 76 | + nvidia-ctk cdi generate --output=/tmp/nvidia.yaml |
| 77 | +
|
| 78 | + # diff the generated file with the one in /var/run/cdi/nvidia.yaml and exit with 0 if they are the same |
| 79 | + if ! diff /var/run/cdi/nvidia.yaml /tmp/nvidia.yaml; then |
| 80 | + echo "nvidia.yaml file is different" |
| 81 | + exit 1 |
| 82 | + fi |
| 83 | + ` |
| 84 | +) |
| 85 | + |
| 86 | +var _ = Describe("nvidia-cdi-refresh", Ordered, ContinueOnFailure, Label("systemd-unit"), func() { |
| 87 | + var ( |
| 88 | + nestedContainerRunner Runner |
| 89 | + outerContainerImage = nodeimage.DefaultBaseImage |
| 90 | + ) |
| 91 | + |
| 92 | + BeforeAll(func(ctx context.Context) { |
| 93 | + var err error |
| 94 | + nestedContainerRunner, err = NewNestedContainerRunner(runner, outerContainerImage, installCTK, imageName+":"+imageTag, testContainerName) |
| 95 | + Expect(err).ToNot(HaveOccurred()) |
| 96 | + }) |
| 97 | + |
| 98 | + AfterAll(func(ctx context.Context) { |
| 99 | + // Cleanup: remove the container and the temporary script on the host. |
| 100 | + // Use || true to ensure cleanup doesn't fail the test |
| 101 | + runner.Run(fmt.Sprintf("docker rm -f %s 2>/dev/null || true", testContainerName)) //nolint:errcheck |
| 102 | + }) |
| 103 | + |
| 104 | + When("installing nvidia-container-toolkit", Ordered, func() { |
| 105 | + It("should load the nvidia-cdi-refresh.path and nvidia-cdi-refresh.service units", func(ctx context.Context) { |
| 106 | + _, _, err := nestedContainerRunner.Run(nvidiaCdiRefreshInstallTemplate) |
| 107 | + Expect(err).ToNot(HaveOccurred()) |
| 108 | + }) |
| 109 | + |
| 110 | + It("should generate the nvidia.yaml file", func(ctx context.Context) { |
| 111 | + _, _, err := nestedContainerRunner.Run(nvidiaCdiRefreshFileExistsTemplate) |
| 112 | + Expect(err).ToNot(HaveOccurred()) |
| 113 | + }) |
| 114 | + |
| 115 | + It("should refresh the nvidia.yaml file after upgrading the nvidia-container-toolkit", func(ctx context.Context) { |
| 116 | + _, _, err := nestedContainerRunner.Run(nvidiaCdiRefreshUpgradeTemplate) |
| 117 | + Expect(err).ToNot(HaveOccurred()) |
| 118 | + }) |
| 119 | + }) |
| 120 | +}) |
0 commit comments