From cade3c7f4b9e61fefb9c02d2ee9c4a1a79243afc Mon Sep 17 00:00:00 2001 From: "kubestellar-hive[bot]" <223556219+Copilot@users.noreply.github.com> Date: Sat, 26 Sep 2026 00:32:27 -0400 Subject: [PATCH] [quality] test: cover scrapeDCGMPerCluster branches in pkg/api/gpuworker scrapeDCGMPerCluster had 0% coverage even under the aggregated pkg/api go-test run because no existing test drove Worker.collectUtilization far enough to invoke it with a non-empty reservation list AND the DCGM flag enabled. Add three direct unit tests covering the observable behaviours the per- reservation collector relies on: 1. DCGM disabled (default) returns nil so callers fall back to the legacy zero-memory path. 2. DCGM enabled with no reservations returns a non-nil empty map. 3. DCGM enabled with reservations for unreachable clusters silently drops them (no partial data, no panic). Duplicate cluster entries exercise the unique-cluster set path. Package coverage rises from 41.5% to 49.7% (function 0% -> 66.7%). No production code changes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: kubestellar-hive[bot] <223556219+Copilot@users.noreply.github.com> --- pkg/api/gpuworker/worker_test.go | 70 ++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/pkg/api/gpuworker/worker_test.go b/pkg/api/gpuworker/worker_test.go index be4d319463..5825d5c229 100644 --- a/pkg/api/gpuworker/worker_test.go +++ b/pkg/api/gpuworker/worker_test.go @@ -482,3 +482,73 @@ func TestWorker_DCGMEnabled_NamespaceMiss_Zero(t *testing.T) { worker.collectForReservation(context.Background(), reservation, dcgmByNs) mockStore.AssertExpectations(t) } + +// Issue 9135 — scrapeDCGMPerCluster direct coverage. +// The wrapper around gpu.ScrapeByNamespace has three observable behaviours +// the per-reservation collector relies on: +// 1. DCGM disabled → returns nil, so callers fall back to the legacy zero. +// 2. Unique clusters are deduplicated so a shared exporter is not hit N times. +// 3. Clusters whose rest config is unavailable are silently dropped from the +// returned map instead of failing the whole scrape. +// None of these were exercised previously, leaving scrapeDCGMPerCluster at +// 0% coverage even under the aggregated pkg/api go-test run. + +func TestScrapeDCGMPerCluster_Disabled_ReturnsNil(t *testing.T) { + t.Setenv("GPU_METRICS_DCGM_ENABLED", "") + + mockStore := new(test.MockStore) + k8sClient, _ := k8s.NewMultiClusterClient("") + worker := New(mockStore, k8sClient, nil) + + reservations := []models.GPUReservation{ + {ID: uuid.New(), Cluster: "c1", Namespace: "ns-a", GPUCount: 1}, + {ID: uuid.New(), Cluster: "c2", Namespace: "ns-b", GPUCount: 1}, + } + + got := worker.scrapeDCGMPerCluster(reservations, 100*time.Millisecond) + if got != nil { + t.Fatalf("dcgmEnabled=false: want nil map, got %v", got) + } +} + +func TestScrapeDCGMPerCluster_Enabled_NoReservations_EmptyMap(t *testing.T) { + t.Setenv("GPU_METRICS_DCGM_ENABLED", "true") + + mockStore := new(test.MockStore) + k8sClient, _ := k8s.NewMultiClusterClient("") + worker := New(mockStore, k8sClient, nil) + + got := worker.scrapeDCGMPerCluster(nil, 100*time.Millisecond) + if got == nil { + t.Fatal("dcgmEnabled=true with no reservations: want non-nil empty map, got nil") + } + if len(got) != 0 { + t.Fatalf("want empty map, got %d entries", len(got)) + } +} + +func TestScrapeDCGMPerCluster_Enabled_UnreachableClusters_SilentSkip(t *testing.T) { + t.Setenv("GPU_METRICS_DCGM_ENABLED", "true") + + mockStore := new(test.MockStore) + k8sClient, _ := k8s.NewMultiClusterClient("") + worker := New(mockStore, k8sClient, nil) + + // Multiple reservations across two clusters, neither has a rest config + // registered. GetRestConfig must fail for both, and the function must + // silently drop them from the output map (no panic, no partial data). + // The duplicate "c1" entries also exercise the unique-cluster set path. + reservations := []models.GPUReservation{ + {ID: uuid.New(), Cluster: "c1", Namespace: "ns-a", GPUCount: 1}, + {ID: uuid.New(), Cluster: "c1", Namespace: "ns-b", GPUCount: 1}, + {ID: uuid.New(), Cluster: "c2", Namespace: "ns-c", GPUCount: 1}, + } + + got := worker.scrapeDCGMPerCluster(reservations, 100*time.Millisecond) + if got == nil { + t.Fatal("want non-nil map (per-cluster errors are silent), got nil") + } + if len(got) != 0 { + t.Fatalf("all clusters unreachable: want empty map, got %d entries: %v", len(got), got) + } +}