|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +// Copyright 2026 NVIDIA CORPORATION |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +package assertions |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "slices" |
| 12 | + "strconv" |
| 13 | + "strings" |
| 14 | + "time" |
| 15 | + |
| 16 | + ginkgo "github.com/onsi/ginkgo/v2" |
| 17 | + "github.com/onsi/gomega" |
| 18 | + |
| 19 | + "github.com/NVIDIA/k8s-test-infra/tests/e2e/go/framework/kube" |
| 20 | +) |
| 21 | + |
| 22 | +// dcgm-exporter field names asserted here. |
| 23 | +const ( |
| 24 | + dcgmExporterSelector = "app=nvidia-dcgm-exporter" |
| 25 | + dcgmExporterDaemonSet = "nvidia-dcgm-exporter" |
| 26 | + dcgmExporterPort = 9400 |
| 27 | + |
| 28 | + fiDevGPUTemp = "DCGM_FI_DEV_GPU_TEMP" |
| 29 | + fiDevPowerUsage = "DCGM_FI_DEV_POWER_USAGE" |
| 30 | + fiDevXidErrors = "DCGM_FI_DEV_XID_ERRORS" |
| 31 | + fiProfPCIeTx = "DCGM_FI_PROF_PCIE_TX_BYTES" |
| 32 | +) |
| 33 | + |
| 34 | +// DCGMDeviceMetrics asserts dcgm-exporter telemetry from the mock NVML: |
| 35 | +// - one DCGM_FI_DEV_GPU_TEMP sample per GPU, each in a plausible range; |
| 36 | +// - positive DCGM_FI_DEV_POWER_USAGE for every GPU; |
| 37 | +// - a modelName label matching the profile's display name; |
| 38 | +// - on GPM (Hopper+) profiles, one positive DCGM_FI_PROF_PCIE_TX_BYTES per GPU; |
| 39 | +// - power that varies over time. |
| 40 | +func DCGMDeviceMetrics(ctx context.Context, k *kube.Client, ns, gpuName string, gpuCount int, gpm bool, timeout, poll time.Duration) { |
| 41 | + ginkgo.GinkgoHelper() |
| 42 | + |
| 43 | + WaitDaemonSetReady(ctx, k, ns, dcgmExporterDaemonSet, timeout, poll) |
| 44 | + pod := dcgmExporterPod(ctx, k, ns) |
| 45 | + |
| 46 | + // Poll until the first DEV temperature samples appear. |
| 47 | + var metrics string |
| 48 | + ginkgo.By("scraping dcgm-exporter /metrics") |
| 49 | + gomega.Eventually(func() (int, error) { |
| 50 | + var err error |
| 51 | + metrics, err = scrapeDCGM(ctx, k, ns, pod) |
| 52 | + if err != nil { |
| 53 | + return 0, err |
| 54 | + } |
| 55 | + return len(promValues(metrics, fiDevGPUTemp)), nil |
| 56 | + }).WithContext(ctx).WithTimeout(timeout).WithPolling(poll). |
| 57 | + Should(gomega.BeNumerically(">", 0), "no %s samples from dcgm-exporter", fiDevGPUTemp) |
| 58 | + |
| 59 | + temps := promValues(metrics, fiDevGPUTemp) |
| 60 | + gomega.Expect(temps).To(gomega.HaveLen(gpuCount), "%s sample count", fiDevGPUTemp) |
| 61 | + for _, t := range temps { |
| 62 | + gomega.Expect(t).To(gomega.And(gomega.BeNumerically(">=", 20), gomega.BeNumerically("<=", 100)), |
| 63 | + "%s value %.1f outside plausible range [20,100]", fiDevGPUTemp, t) |
| 64 | + } |
| 65 | + |
| 66 | + powers := promValues(metrics, fiDevPowerUsage) |
| 67 | + gomega.Expect(powers).NotTo(gomega.BeEmpty(), "%s missing", fiDevPowerUsage) |
| 68 | + for _, p := range powers { |
| 69 | + gomega.Expect(p).To(gomega.BeNumerically(">", 0), "%s not positive", fiDevPowerUsage) |
| 70 | + } |
| 71 | + |
| 72 | + gomega.Expect(metrics).To(gomega.ContainSubstring(fmt.Sprintf("modelName=%q", gpuName)), |
| 73 | + "modelName label %q not found in dcgm-exporter metrics", gpuName) |
| 74 | + |
| 75 | + // GPM profiling metrics; present only on Hopper+ profiles. |
| 76 | + if gpm { |
| 77 | + ginkgo.By("checking GPM profiling metrics (DCGM_FI_PROF_*)") |
| 78 | + gomega.Eventually(func() (int, error) { |
| 79 | + var err error |
| 80 | + metrics, err = scrapeDCGM(ctx, k, ns, pod) |
| 81 | + if err != nil { |
| 82 | + return 0, err |
| 83 | + } |
| 84 | + return len(promValues(metrics, fiProfPCIeTx)), nil |
| 85 | + }).WithContext(ctx).WithTimeout(timeout).WithPolling(poll). |
| 86 | + Should(gomega.Equal(gpuCount), "%s sample count (GPM path)", fiProfPCIeTx) |
| 87 | + for _, v := range promValues(metrics, fiProfPCIeTx) { |
| 88 | + gomega.Expect(v).To(gomega.BeNumerically(">", 0), "%s not positive", fiProfPCIeTx) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Power must differ across two scrapes (dynamic metrics). |
| 93 | + ginkgo.By("checking DCGM_FI_DEV_POWER_USAGE varies over time") |
| 94 | + baseline := promRaw(metrics, fiDevPowerUsage) |
| 95 | + gomega.Expect(baseline).NotTo(gomega.BeEmpty(), "%s not present for time-varying check", fiDevPowerUsage) |
| 96 | + gomega.Eventually(func() (bool, error) { |
| 97 | + cur, err := scrapeDCGM(ctx, k, ns, pod) |
| 98 | + if err != nil { |
| 99 | + return false, err |
| 100 | + } |
| 101 | + return !slices.Equal(promRaw(cur, fiDevPowerUsage), baseline), nil |
| 102 | + }).WithContext(ctx).WithTimeout(timeout).WithPolling(poll). |
| 103 | + Should(gomega.BeTrue(), "%s did not vary over time", fiDevPowerUsage) |
| 104 | +} |
| 105 | + |
| 106 | +// DCGMXidReported polls until at least one GPU reports xid as |
| 107 | +// DCGM_FI_DEV_XID_ERRORS (healthy default is 0). |
| 108 | +func DCGMXidReported(ctx context.Context, k *kube.Client, ns string, xid int, timeout, poll time.Duration) { |
| 109 | + ginkgo.GinkgoHelper() |
| 110 | + |
| 111 | + WaitDaemonSetReady(ctx, k, ns, dcgmExporterDaemonSet, timeout, poll) |
| 112 | + pod := dcgmExporterPod(ctx, k, ns) |
| 113 | + |
| 114 | + ginkgo.By(fmt.Sprintf("waiting for %s == %d", fiDevXidErrors, xid)) |
| 115 | + gomega.Eventually(func() (bool, error) { |
| 116 | + metrics, err := scrapeDCGM(ctx, k, ns, pod) |
| 117 | + if err != nil { |
| 118 | + return false, err |
| 119 | + } |
| 120 | + for _, v := range promValues(metrics, fiDevXidErrors) { |
| 121 | + if int(v) == xid { |
| 122 | + return true, nil |
| 123 | + } |
| 124 | + } |
| 125 | + return false, nil |
| 126 | + }).WithContext(ctx).WithTimeout(timeout).WithPolling(poll). |
| 127 | + Should(gomega.BeTrue(), "%s did not report injected Xid %d", fiDevXidErrors, xid) |
| 128 | +} |
| 129 | + |
| 130 | +func dcgmExporterPod(ctx context.Context, k *kube.Client, ns string) string { |
| 131 | + ginkgo.GinkgoHelper() |
| 132 | + pod, err := k.FirstPodName(ctx, ns, dcgmExporterSelector) |
| 133 | + gomega.Expect(err).NotTo(gomega.HaveOccurred(), "find dcgm-exporter pod") |
| 134 | + gomega.Expect(pod).NotTo(gomega.BeEmpty(), "dcgm-exporter pod not found in %s", ns) |
| 135 | + return pod |
| 136 | +} |
| 137 | + |
| 138 | +// scrapeDCGM fetches the exporter's Prometheus text via the API-server pod proxy. |
| 139 | +func scrapeDCGM(ctx context.Context, k *kube.Client, ns, pod string) (string, error) { |
| 140 | + raw := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s:%d/proxy/metrics", ns, pod, dcgmExporterPort) |
| 141 | + return k.GetRawQuiet(ctx, raw) |
| 142 | +} |
| 143 | + |
| 144 | +// promRaw returns the value token (last field) of every `metric{...}` series. |
| 145 | +func promRaw(metrics, metric string) []string { |
| 146 | + prefix := metric + "{" |
| 147 | + var out []string |
| 148 | + for _, line := range strings.Split(metrics, "\n") { |
| 149 | + line = strings.TrimSpace(line) |
| 150 | + if !strings.HasPrefix(line, prefix) { |
| 151 | + continue |
| 152 | + } |
| 153 | + if fields := strings.Fields(line); len(fields) >= 2 { |
| 154 | + out = append(out, fields[len(fields)-1]) |
| 155 | + } |
| 156 | + } |
| 157 | + return out |
| 158 | +} |
| 159 | + |
| 160 | +// promValues is promRaw parsed to float64 (unparseable tokens dropped). |
| 161 | +func promValues(metrics, metric string) []float64 { |
| 162 | + raw := promRaw(metrics, metric) |
| 163 | + out := make([]float64, 0, len(raw)) |
| 164 | + for _, s := range raw { |
| 165 | + if v, err := strconv.ParseFloat(s, 64); err == nil { |
| 166 | + out = append(out, v) |
| 167 | + } |
| 168 | + } |
| 169 | + return out |
| 170 | +} |
0 commit comments