package server import ( "bytes" "encoding/json" "net/http" "net/http/httptest" "testing" ) func TestGPUAgentHealthIsAuthenticatedAndVisibleInInventory(t *testing.T) { f := newFixture(t) f.server.Config.GPUAgentToken = "agent-token" fakeGPUPVE(t, f, "normal") status, out := f.request("POST", "/gpu-agent/health", "", map[string]any{ "node": "node-a", "samples": []map[string]any{{ "address": "0000:01:00.0", "temperatureC": 61.5, "utilizationPercent": 42.0, "memoryUsedBytes": 1024, "memoryTotalBytes": 4096, "driver": "nvidia", "driverBinding": "bound", }}, }, "") // f.request cannot attach the collector header, so the route must reject it. expect(t, 401, status, out) body, _ := json.Marshal(map[string]any{ "node": "node-a", "samples": []map[string]any{{"address": "0000:01:00.0", "temperatureC": 61.5, "driver": "nvidia", "driverBinding": "bound"}}, }) r := httptest.NewRequest(http.MethodPost, "/api/v1/gpu-agent/health", bytes.NewReader(body)) r.Header.Set("Content-Type", "application/json") r.Header.Set("X-GPU-Agent-Token", "agent-token") w := httptest.NewRecorder() f.server.Router.ServeHTTP(w, r) expect(t, 202, w.Code, w.Body.String()) status, out = f.request("GET", "/resources/gpus?node=node-a", f.login("admin"), nil, "") expect(t, 200, status, out) items := out["items"].([]any) var found bool for _, raw := range items { item := raw.(map[string]any) if item["address"] == "0000:01:00.0" { health := item["health"].(map[string]any) found = health["status"] == "collected" && health["driverBinding"] == "bound" && health["temperatureC"] == 61.5 && health["driver"] == "nvidia" } } if !found { t.Fatal("ingested GPU health was not visible in inventory", out) } }