Files
FabledSteward/tests/core/test_problems_first.py
T
bvandeusenandClaude Opus 5.5 8e890c6914
CI / lint (push) Successful in 2s
CI / integration (push) Successful in 50s
CI / unit (push) Successful in 1m20s
CI / publish (push) Successful in 1m13s
feat(ui): host page leads with its alerts; old readings say how old (#5210)
#5210 "Problems first, with since-time, across every list" (milestone 477),
second batch.

- Host page: an "Alerts on this host" strip first (rules on its agent
  series, its containers or its monitors: alert_svc.watches_host); its
  monitors problems first with "Down 12m · reason"; Docker above Ansible.
- Uptime page and widget: worst 24h uptime first, then 7d and 30d; hosts
  with no uptime yet last.
- Agent panel and fleet widget: last seen as "3m ago", not a clock time.
- SNMP: a device is "Reachable" only while it answered within the last 3
  polls; older readings say "No answer" with their age, and every device
  says when it was last read (was: reachable whenever any reading existed).
- Traefik: a router missing from 3 scrapes in a row is listed apart as no
  longer reported, by when it was last seen, instead of keeping its last
  state (a removed router stayed Failing).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 00:02:20 -04:00

56 lines
2.8 KiB
Python

"""Lists put what needs looking at first (#5210): hosts by their worst
signal, schedules failing first, maintenance windows active then upcoming."""
from types import SimpleNamespace as NS
from steward.ansible.routes import _schedule_order
from steward.hosts.service import host_rank
def test_hosts_sort_down_then_quiet_agent_then_pending_then_healthy():
down, up, pending = {"state": "down"}, {"state": "up"}, {"state": "pending"}
fresh, quiet = {"fresh": True}, {"fresh": False}
assert host_rank(down, fresh) == 0
assert host_rank(up, quiet) == 1 # monitors fine, but the agent went silent
assert host_rank(None, quiet) == 1
assert host_rank(pending, fresh) == 2
assert host_rank(up, fresh) == 3 and host_rank(None, fresh) == 3
assert host_rank(None, None) == 4 # nothing watches it
def _sched(name, *, enabled=True, last_error=None, status=None):
run = NS(status=NS(value=status)) if status else None
return {"s": NS(name=name, enabled=enabled, last_error=last_error), "last_run": run}
def test_schedules_sort_failing_first_and_paused_last():
rows = [_sched("nightly", status="success"), _sched("paused", enabled=False, status="failed"),
_sched("backup", status="failed"), _sched("deploy", status="running"),
_sched("broken", last_error="source not found"), _sched("cut", status="interrupted"),
_sched("busy", last_error="the last run is still going", status="success")]
assert [r["s"].name for r in sorted(rows, key=_schedule_order)] == [
"backup", "broken", "cut", "deploy", "busy", "nightly", "paused"]
def test_a_host_page_shows_only_the_alerts_on_that_host():
from steward.alerts.service import watches_host
def rule(source, resource):
return NS(source_module=source, resource_name=resource)
mine = {("http", "nas web")}
assert watches_host(rule("host_agent", "nas"), "nas", mine)
assert watches_host(rule("host_agent", "nas:/"), "nas", mine)
assert not watches_host(rule("host_agent", "nas2"), "nas", mine) # a name that merely starts alike
assert watches_host(rule("docker", "nas/plex"), "nas", mine)
assert not watches_host(rule("docker", "nas2/plex"), "nas", mine)
assert watches_host(rule("http", "nas web"), "nas", mine)
assert not watches_host(rule("http", "other web"), "nas", mine)
def test_uptime_lists_the_worst_first_and_the_unmeasured_last():
from steward.hosts.service import worst_uptime_first
hosts = [NS(id=i, name=n) for i, n in [("a", "alpha"), ("b", "bravo"), ("c", "charlie"), ("d", "delta")]]
uptime = {"a": {"24h": 100.0, "7d": 99.0, "30d": 99.5},
"b": {"24h": 92.0, "7d": 98.0, "30d": 99.0},
"c": {"24h": 100.0, "7d": 95.0, "30d": 97.0}}
assert [h.name for h in worst_uptime_first(hosts, uptime)] == ["bravo", "charlie", "alpha", "delta"]