Files
dashboards/tests/test_live_fleet.py
T
ducoterra 9757e3c947 phase: 03_system_per_host_panels
**Phase 03 final verification pass — all 4 tasks already in `complete/`; verified criteria, fixed 3 pre-existing defects.**

- **Verified live** (headless Chromium + curl against `:8090`, all 6 sources `ok`): 3 panels × six heroes in order `cpu,mem,temp,disk,gpus,net`; charts per panel `util,net[,gpu]` (gateway: no GPU chart/hole); legends name metrics only — no legend names a foreign host; every `<svg>` `role="img"` + host-scoped `aria-label`; 2 sections, 0 `<select>`; `grep -rn "renderCores|core_usage" src/dashboards/web/` → nothing; console error-free; new shot `data/logs/final-system-panels.png`.
- **Defect fixed (credential hygiene, invariant 6)**: `collectors.py` put the *dialed* URL into every `UpstreamError`, and that string lands in the health record (`/api/health`) and the `event` table (`/api/events` → page); `/api/config` also echoed `GUFO_BASE_URL`/`VLLM_BASE_URL` verbatim. Added `config.strip_userinfo()` (now what `Target.display_url` uses) and redacted all message/config URLs. Tests: `test_a_failing_host_never_echoes_its_token_in_the_message` (×3 paths), `test_a_failing_engine_never_echoes_credentials_in_its_url`, `test_a_dead_host_never_writes_its_token_into_the_feed`, `test_engine_urls_are_redacted_the_same_way_hosts_are`, `test_strip_userinfo_is_the_one_redaction_every_url_leaves_by`. (`parse_targets` already moves `HOST_TARGETS` userinfo into a header, so this is a backstop for hosts and a real fix for engine URLs.)
- **Defect fixed (invariant 2)**: `/system` footnote printed `Δ{HOST_INTERVAL}` as the measurement; it now prints each host's measured `window_seconds` (range when they disagree) — `rates over Δ10.0s · poll 10s`. Pinned by `test_system_footnote_prints_the_window_it_actually_measured`. Also fixed the page's stale "window in each header" note and the empty-state's `--token` (argv) advice → `--token-file`.
- `bash .agents/validate.sh` → **EXIT=0**: **472 passed, 7 skipped**, coverage **96.87%** (floor 94), `ruff check src tests agent` clean, `pyright` 0 errors, `node --check` clean · `uv run pytest -m live --live` → **7 passed** · `uv pip list --outdated` empty.
- Criteria: no multi-host legend ✅ · no per-core visual ✅ · six heroes/panel incl. no-GPU `n/a` uncoloured ✅ · per-core history still queryable ✅ (354 pts) · ≥3 shape screenshots ✅ (`system-driveripper/deskwork/gateway.png` + dark/light/stale) · pytest+coverage+lint ✅ · phases 01/02 unchanged (their pin tests + `/api/summary` shape tests pass; live UPS gap in AGENTS.md re-confirmed read-only against real upsd) ✅.
- **Docs**: AGENTS.md — invariant 6 rewritten to cover message/event redaction, `collectors.py` line, new "Δ is a measurement" rule, "hosts answering total" → "nine-figure fleet-totals panel" (the subtitle still counts hosts), test count 452→472, coverage 96.8→96.9, rewrapped an over-long line. README.md — same fleet-totals wording, `RATE_WINDOW_SECONDS` now covers every measured rate (not just tokens), agent route list completed (`/`, `/schema`, `?top=N`), `LIST CMD` in the diagram, new paragraph on URL redaction.
- **Deviation**: the criterion's literal `curl …/api/system/history?host=…&metric=cpu.usage_pct@core7` uses singular params that endpoint has never accepted (it takes `hosts=`/`metrics=` and ignores unknown ones). Verified 354 points via `…system/history?hosts=driveripper&metrics=cpu.usage_pct@core7&range=1h` and `/api/history?source=host&host=…&metric=…`; I did **not** add singular param aliases (unauthorized API surface). No live infra changes made — reads only.
- **Next pending phase**: `04_llm_chart_readability`.
2026-10-17 04:42:09 -04:00

190 lines
9.4 KiB
Python

"""The only tests that touch a network: `uv run pytest -m live --live`.
Everything else in this suite is hermetic on purpose (see conftest.py). These are the
bridge to reality — they re-run the *production* code paths (`collectors.fetch_*`,
`NutMonitor.collect`) against real targets, which is what catches an upstream that
renamed a series or a driver that started reporting a temperature in kelvin.
They read exactly the env vars the app reads, so the dev loop is:
./scripts/dev-tunnels.sh start # agents + upsd forwarded to localhost
set -a; . .env.dev; set +a
uv run pytest -m live --live
Each test skips when its source is not configured, so running the whole set with half a
fleet reachable is a normal outcome, not a failure.
**Nothing in here may write.** The UPS test asserts the command gating is honest; it never
sends an instant command — a battery test on hardware someone else is plugged into is not
a unit test.
"""
from __future__ import annotations
import asyncio
import os
import httpx
import pytest
from card_contract import assert_card_contract
from dashboards import collectors
from dashboards.config import Settings, load
from dashboards.rates import RateEstimator
from dashboards.sources import nut
from dashboards.sources.host import HostCollector
pytestmark = pytest.mark.live
@pytest.fixture(scope="module")
def settings() -> Settings:
"""Live env, not the import-time SETTINGS singleton."""
return load()
@pytest.fixture
def host_collector() -> HostCollector:
return HostCollector(rates=RateEstimator(span=10.0))
async def test_every_host_agent_answers_and_parses(settings: Settings,
host_collector: HostCollector) -> None:
"""Each HOST_TARGETS entry must answer /snapshot and yield the metrics the page renders."""
if not settings.host_targets:
pytest.skip("HOST_TARGETS is unset — start ./scripts/dev-tunnels.sh")
required = {"cpu.usage_pct", "mem.used_pct", "disk.used_pct"}
async with httpx.AsyncClient(timeout=settings.host_timeout) as client:
for target in settings.host_targets:
# Poll twice: usage_pct is a Δ/jiffies rate, and a rate has no second point yet
# on the first scrape. That is the design (the agent never remembers who polled
# it), so a live test that scraped once would be asserting a false failure.
await collectors.fetch_host_snapshot(client, target, host_collector)
await asyncio.sleep(1.2)
result = await collectors.fetch_host_snapshot(client, target, host_collector)
metrics = {sample.metric for sample in result.samples}
assert result.payload["info"]["hostname"], f"{target.name}: no hostname"
missing = required - metrics
assert not missing, f"{target.name}: snapshot parsed but no {sorted(missing)}"
# a section the agent could not read is a `degraded` note, never a silent zero
assert result.payload.get("degraded") in (None, []), (
f"{target.name}: agent reported {result.payload.get('degraded')}"
)
async def test_every_host_serves_the_hero_card_contract(settings: Settings,
host_collector: HostCollector) -> None:
"""The phase-03 `/system` card paths exist on the **real** agents, not just the captures.
Values may honestly be `n/a` on real hardware (gateway reports no CPU sensor and no GPU
at all); the *shape* is what phase 03 renders, so this is the test that catches an agent
upgrade renaming a field. Hermetic twin: `tests/test_system_contract.py`.
"""
if not settings.host_targets:
pytest.skip("HOST_TARGETS is unset — start ./scripts/dev-tunnels.sh")
async with httpx.AsyncClient(timeout=settings.host_timeout) as client:
for target in settings.host_targets:
await collectors.fetch_host_snapshot(client, target, host_collector)
await asyncio.sleep(1.2)
result = await collectors.fetch_host_snapshot(client, target, host_collector)
assert_card_contract(result.payload)
assert result.payload["cpu"]["temp_c"] == result.payload["temperatures"]["cpu_c"], (
f"{target.name}: the card and /api/summary would report different CPU temps"
)
@pytest.mark.parametrize("engine", ["gufo", "vllm"])
async def test_engine_still_publishes_the_series_we_parse(engine: str,
settings: Settings) -> None:
"""Metric names are verified against the deployed engine, never guessed.
The assertion is that at least one cumulative counter per family *resolves* — that is
what the Δ rates, the throughput cells, and the history rows all hang off. If an
upgrade renames it, this fails here instead of printing 0 tok/s on the dashboard.
"""
base_url = settings.gufo_url if engine == "gufo" else settings.vllm_url
api_key = settings.gufo_api_key if engine == "gufo" else settings.vllm_api_key
if not base_url:
pytest.skip(f"{engine.upper()}_BASE_URL is unset — that engine is not deployed here")
counters = {
"gufo": ("tokens_generated_total", "tokens_prompt_total"),
"vllm": ("generation_tokens_total", "prompt_tokens_total"),
}[engine]
rates = RateEstimator(span=settings.rate_window)
async with httpx.AsyncClient(timeout=settings.llm_timeout) as client:
result = await collectors.fetch_engine(client, engine, base_url, api_key, rates,
with_slots=engine == "gufo")
metrics = result.payload["metrics"]
resolved = [key for key in counters if metrics[key]["source"] is not None]
assert resolved, (
f"{engine}: none of {counters} resolved at {base_url} — upstream renamed its "
f"counters, update {engine.upper()}_SERIES in sources/llm.py"
)
sessions = result.payload["sessions"]
assert sessions["active"] is None or sessions["active"] >= 0
assert result.samples, f"{engine}: nothing to store, so the charts would stay empty"
def require_upsd(settings: Settings) -> None:
"""Skip, not fail, when there is no real upsd to talk to."""
if not settings.nut_enabled:
pytest.skip("NUT_HOST is unset — no upsd to talk to")
if os.environ.get("NUT_SIMULATE", "").lower() in ("1", "true", "yes", "on"):
pytest.skip("NUT_SIMULATE=1 — point NUT_HOST at the real upsd for this run")
async def test_upsd_reports_both_upses_with_sane_units(settings: Settings) -> None:
"""Real batteries: plausible volts/percent, and the health checks are never silent."""
require_upsd(settings)
result = await collectors.NutMonitor(settings).collect()
upses = result.payload["ups"]
assert upses, "upsd answered but exported no UPS (check ups.conf driver instances)"
for card in upses:
name = card["name"]
assert card["status"], f"{name}: ups.status is empty"
assert card["health"]["checks"], f"{name}: assess() produced no health checks"
numbers = card["numbers"]
charge = numbers.get("charge_pct")
assert charge is None or 0.0 <= charge <= 100.0, f"{name}: charge_pct={charge}"
# kelvin-as-celsius is the classic unit bug here; 273 would sail past a bare >0
temp = numbers.get("battery_temperature")
assert temp is None or -20.0 <= temp <= 80.0, f"{name}: battery temp {temp} is not °C"
async def test_ups_command_gating_is_honest_without_sending_anything(settings: Settings) -> None:
"""Every advertised command is either enabled or carries the reason it is not.
Deliberately does not POST anything: `POST /api/ups/command` behaviour is covered by
tests/test_api.py against a fake client, because running a deep battery test on live
hardware to prove a button works is exactly what this app is careful about.
"""
require_upsd(settings)
result = await collectors.NutMonitor(settings).collect()
seen = False
for card in result.payload["ups"]:
for entry in card["commands"]:
seen = True
assert isinstance(entry["enabled"], bool), f"{card['name']}: {entry} has no gate state"
if not entry["enabled"]:
assert entry.get("reason"), (
f"{card['name']}: {entry['command']} disabled with no reason — the UI "
f"would show a dead button with no explanation"
)
if entry.get("risk") == nut.RISK_DANGEROUS:
assert entry.get("confirm_text"), (
f"{card['name']}: {entry['command']} can cut power without a typed confirmation"
)
if not seen:
pytest.skip("the UPS advertises no commands we model (see ACTION_CATALOG)")
def test_target_config_is_clean_and_leak_free(settings: Settings) -> None:
"""Every configured target parses, and none of them leak a userinfo token."""
names = [target.name for target in settings.host_targets]
assert len(names) == len(set(names)), f"duplicate target names: {names}"
for target in settings.host_targets:
assert target.url.startswith(("http://", "https://")), f"{target.name}: {target.url}"
assert "@" not in target.display_url, f"{target.name}: display_url leaks credentials"
for problem in settings.host_target_problems:
assert isinstance(problem, str) and problem, f"empty problem: {problem!r}"