- #5298 "Docker overview: verdict line; only the problem tiles that aren't zero" - #5299 "Docker overview: swarm services as a filterable, sortable table with a Stack column" - #5300 "Docker overview: failures listed once, each with a link to the failed task's logs" - #5301 "Docker overview: standalone containers in one table with a Host column" - #5302 "Service page: replicas, image, task history, resource history and logs across all its tasks" overview.py holds the rows, filters and sort as plain functions. The filter bar sits outside the polled fragment and the URL keeps its state. A service's page reads every task container Steward still holds anything for, the live ones and removed ones known by their events, so a failed task's logs are reachable after Docker has removed it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
208 lines
12 KiB
Python
208 lines
12 KiB
Python
"""The Docker page's tables (#5299, #5301) and a service's page (#5302): the
|
|
rows, their search, filters and sort, and the helpers that find a service's
|
|
tasks, logs and summed history."""
|
|
import re
|
|
from datetime import datetime, timedelta, timezone
|
|
from types import SimpleNamespace as NS
|
|
|
|
from quart import session
|
|
|
|
from plugins.docker import service as svc
|
|
from plugins.docker.overview import (
|
|
NO_STACK, container_row, filter_options, filter_rows, service_row, sort_rows, stack_of,
|
|
)
|
|
from plugins.docker.routes import _filters, _page_url, _sort_url, _task_rows
|
|
from tests._templates import render_plugin_template
|
|
|
|
NOW = datetime(2026, 10, 7, 12, 0, tzinfo=timezone.utc)
|
|
TASK = "x7q2m9k4p1r8s5t3v6w0y2z4a" # 25 characters, like Swarm's
|
|
|
|
|
|
def _replica(host, cpu=10.0, mem=100, started=2, status="running", **kw):
|
|
return dict(ghost=False, host=host, host_id=f"id-{host}", name=f"r.{host}", status=status,
|
|
cpu_pct=cpu, mem_bytes=mem, started_at=NOW - timedelta(hours=started), image="img:1",
|
|
health=kw.get("health"))
|
|
|
|
|
|
def _service(name, state="healthy", running=1, desired=1, replicas=None, **kw):
|
|
s = dict(name=name, mode="replicated", running=running, desired=desired, state=state,
|
|
crash_looping=False, failures_24h=0, last_error=None, trouble_since=None,
|
|
replicas=replicas if replicas is not None else [_replica("a")])
|
|
s.update(kw)
|
|
return s
|
|
|
|
|
|
def _container(name, **kw):
|
|
base = dict(name=name, host_id="h1", image="nginx:1", status="running", health=None, exit_code=None,
|
|
oom_killed=False, restart_count=0, compose_project=None, started_at=NOW - timedelta(hours=1),
|
|
scraped_at=NOW)
|
|
base.update(kw)
|
|
c = NS(**base)
|
|
usage = {"cores": 0.5, "mem_bytes": 2048}
|
|
return container_row(c, label=name, host="nas", usage=usage, ports=[], stale=False,
|
|
exit_failed=bool(kw.get("exit_code")), now=NOW)
|
|
|
|
|
|
def test_a_service_reads_its_stack_from_its_name():
|
|
assert stack_of("arr_radarr") == "arr" and stack_of("steward_app_db") == "steward"
|
|
assert stack_of("portainer") is None and stack_of("_x") is None and stack_of("x_") is None
|
|
|
|
|
|
def test_a_service_row_adds_up_its_running_replicas():
|
|
s = _service("arr_radarr", running=2, desired=3, state="degraded", replicas=[
|
|
_replica("b", cpu=50.0, mem=300, started=5), _replica("a", cpu=25.0, mem=100, started=1),
|
|
_replica("c", cpu=99.0, mem=999, status="exited"), {"ghost": True, "host": "edge", "count": 1}])
|
|
row = service_row(s, NOW)
|
|
assert row["stack"] == "arr" and row["trouble"] == 2
|
|
assert row["cpu_cores"] == 0.75 and row["mem_bytes"] == 400 # the exited one is not counted
|
|
assert row["started_at"] == NOW - timedelta(hours=1) and row["up_s"] == 3600
|
|
assert row["nodes"] == ["a", "b", "c"] and row["ghost_nodes"] == ["edge"]
|
|
assert row["image"] == "img:1"
|
|
empty = service_row(_service("solo", state="down", running=0, replicas=[]), NOW)
|
|
assert empty["cpu_cores"] is None and empty["mem_bytes"] is None and empty["up_s"] is None
|
|
|
|
|
|
def test_rows_filter_by_search_stack_host_and_status():
|
|
rows = [service_row(_service("arr_radarr"), NOW),
|
|
service_row(_service("arr_sonarr", state="down", running=0, replicas=[]), NOW),
|
|
service_row(_service("portainer", replicas=[_replica("b")]), NOW),
|
|
_container("traefik", compose_project="edge"),
|
|
_container("backup", status="exited", exit_code=0)]
|
|
|
|
def names(**kw):
|
|
return [r["name"] for r in filter_rows(rows, **kw)]
|
|
|
|
assert names(q="SONARR") == ["arr_sonarr"] and names(q="nginx") == ["traefik", "backup"]
|
|
assert names(stack="arr") == ["arr_radarr", "arr_sonarr"] and names(q="edge") == ["traefik"]
|
|
assert names(stack=NO_STACK) == ["portainer", "backup"]
|
|
assert names(host="b") == ["portainer"] and names(host="nas") == ["traefik", "backup"]
|
|
assert names(state="problem") == ["arr_sonarr"]
|
|
assert names(state="stopped") == ["arr_sonarr", "backup"]
|
|
assert names(state="healthy") == ["arr_radarr", "portainer", "traefik"]
|
|
assert filter_options(rows[:3], rows[3:]) == {"stacks": ["arr", "edge"], "hosts": ["a", "b", "nas"]}
|
|
|
|
|
|
def test_rows_sort_by_column_with_missing_values_last():
|
|
busy = service_row(_service("busy", replicas=[_replica("a", cpu=300.0)]), NOW)
|
|
idle = service_row(_service("idle", replicas=[_replica("a", cpu=5.0)]), NOW)
|
|
down = service_row(_service("down", state="down", running=0, replicas=[]), NOW)
|
|
rows = [idle, down, busy]
|
|
assert [r["name"] for r in sort_rows(rows, "cpu", True)] == ["busy", "idle", "down"]
|
|
assert [r["name"] for r in sort_rows(rows, "cpu", False)] == ["idle", "busy", "down"]
|
|
assert [r["name"] for r in sort_rows(rows, "status", False)] == ["down", "busy", "idle"]
|
|
assert [r["name"] for r in sort_rows(rows, "name", True)] == ["idle", "down", "busy"]
|
|
|
|
|
|
def test_the_filters_live_in_the_url_without_their_defaults():
|
|
f = _filters({"q": " web ", "stack": "arr", "state": "bogus", "sort": "nope", "dir": "x"})
|
|
assert f == {"q": "web", "stack": "arr", "host": "", "state": "", "sort": "status", "dir": "asc"}
|
|
assert _page_url(f) == "/plugins/docker/?q=web&stack=arr"
|
|
assert _page_url(f, stack="") == "/plugins/docker/?q=web"
|
|
plain = _filters({})
|
|
assert _sort_url(plain, "cpu") == "/plugins/docker/?sort=cpu" # busiest first by default
|
|
assert _sort_url(plain, "name") == "/plugins/docker/?sort=name"
|
|
cpu = _filters({"sort": "cpu"})
|
|
assert cpu["dir"] == "desc" and _sort_url(cpu, "cpu") == "/plugins/docker/?sort=cpu&dir=asc"
|
|
|
|
|
|
def test_a_task_container_name_gives_its_slot_and_task():
|
|
assert svc.task_of("web", f"web.1.{TASK}") == ("1", TASK)
|
|
assert svc.task_of("web", f"web.q8n2.{TASK}") == ("q8n2", TASK) # a global service's node slot
|
|
assert svc.task_of("web", f"web_db.1.{TASK}") is None
|
|
assert svc.task_of("arr", f"arr_radarr.1.{TASK}") is None
|
|
assert svc.task_of("web", "web.1.short") is None
|
|
|
|
|
|
def test_service_history_sums_replicas_after_averaging_each():
|
|
since = NOW - timedelta(hours=1)
|
|
t = since + timedelta(minutes=10)
|
|
samples = [("a", t, 100.0, 1000), ("a", t + timedelta(seconds=20), 300.0, 3000),
|
|
("b", t + timedelta(seconds=5), 50.0, 500),
|
|
("a", t + timedelta(minutes=5), 10.0, 100)]
|
|
out = svc.sum_series(samples, since, NOW, points=60)
|
|
assert out == [(t, 250.0, 2500), (t + timedelta(minutes=5), 10.0, 100)]
|
|
|
|
|
|
def test_a_failure_carries_the_task_whose_logs_tell_why():
|
|
sick = {"name": "codex_db", "tasks_known": True, "failures_24h": 2,
|
|
"last_error": {"state": "failed", "error": "boom", "task_id": TASK, "node": "nas", "at": None}}
|
|
(row,) = svc.failures_24h([], [sick], {})
|
|
assert row["kind"] == "service" and row["task_id"] == TASK
|
|
old = {"name": "legacy", "tasks_known": False, "failures_24h": None, "last_error": None}
|
|
died = NS(container_name=f"legacy.1.{TASK}", host_id="h1", at=NOW, event="die", detail="exit 1")
|
|
(row,) = svc.failures_24h([died], [old], {"h1": "nas"})
|
|
assert row["kind"] == "service" and row["task_id"] == TASK
|
|
|
|
|
|
def test_task_rows_say_where_each_ran_and_why_it_ended():
|
|
rows = _task_rows([
|
|
{"task_id": TASK, "slot": 2, "node_id": "n1", "state": "failed", "desired_state": "shutdown",
|
|
"error": "task: non-zero exit (1)", "exit_code": 1, "at": "2026-10-07T11:00:00Z"},
|
|
{"task_id": "y" * 25, "slot": 2, "node_id": "n9" * 10, "state": "running", "desired_state": "running"}],
|
|
{"n1": "docker02"})
|
|
assert rows[0]["node"] == "docker02" and rows[0]["failed"] and rows[0]["why"] == "task: non-zero exit (1)"
|
|
assert rows[1]["node"] == ("n9" * 10)[:12] and rows[1]["current"] and not rows[1]["failed"]
|
|
|
|
|
|
async def test_the_service_page_puts_state_first_and_offers_each_tasks_logs(app):
|
|
now = datetime.now(timezone.utc)
|
|
s = _service("arr_radarr", running=1, desired=2, state="degraded", failures_24h=3, replicas=[
|
|
dict(_replica("docker02"), name=f"arr_radarr.1.{TASK}"), {"ghost": True, "host": "edge01", "count": 1}],
|
|
last_error={"state": "failed", "error": "task: non-zero exit (1)", "exit_code": 1, "node": "docker02",
|
|
"age_s": 300, "task_id": TASK, "slot": 2})
|
|
replicas = [dict(s["replicas"][0], usage={"cores": 0.1, "cpus": 4, "cpu_share": 2.5, "mem_bytes": 100,
|
|
"mem_pct": None, "mem_of": None, "mem_limit": None},
|
|
uptime="2h"), s["replicas"][1]]
|
|
tasks = _task_rows([{"task_id": TASK, "slot": 2, "node_id": "n1", "state": "failed",
|
|
"desired_state": "shutdown", "error": "task: non-zero exit (1)", "exit_code": 1}],
|
|
{"n1": "docker02"})
|
|
svc_row = NS(image="lscr.io/linuxserver/radarr:5@sha256:abc", updated_at=now)
|
|
async with app.test_request_context(f"/plugins/docker/service/arr_radarr?task={TASK}"):
|
|
session["user_role"] = "viewer"
|
|
page = await render_plugin_template(
|
|
app, "docker", "docker/service.html", name="arr_radarr", service=s, row=service_row(s, now),
|
|
svc=svc_row, tasks=tasks, replicas=replicas, stack="arr", current_range="24h",
|
|
task_choices=[(TASK, f"2 · {TASK[:10]}"), ("y" * 25, "1 · yyyyyyyyyy")], focus_task=TASK)
|
|
page = re.sub(r">\s+<", "><", " ".join(page.split()))
|
|
assert '<nav class="breadcrumb"' in page and 'href="/plugins/docker/">Docker</a>' in page
|
|
assert page.index("Degraded") < page.index("Last failed task") < page.index('id="svc-replicas"')
|
|
assert "1 of 2 running" in page and '<a class="chip" href="/plugins/docker/?stack=arr">arr</a>' in page
|
|
assert f'href="?task={TASK}#logs">its logs →</a>' in page and "5m ago" in page
|
|
assert "radarr:5@sha256:abc" in page
|
|
assert "1 running on a node with no Steward agent" in page and '<a href="/hosts/id-docker02">docker02</a>' in page
|
|
assert page.index('id="svc-replicas"') < page.index('id="svc-history"') < page.index('id="tasks"') < page.index('id="logs"')
|
|
assert 'hx-get="/plugins/docker/service/arr_radarr/history"' in page and 'id="time-range"' in page
|
|
assert '<span class="badge badge-crit">Failed</span>' in page
|
|
assert f'<option value="{TASK}" selected>2 · {TASK[:10]}</option>' in page
|
|
assert 'hx-get="/plugins/docker/service/arr_radarr/logs/lines"' in page
|
|
|
|
|
|
async def test_a_service_nobody_reports_is_not_found(app):
|
|
async with app.test_request_context("/plugins/docker/service/gone"):
|
|
session["user_role"] = "viewer"
|
|
page = await render_plugin_template(app, "docker", "docker/service.html", name="gone", service=None)
|
|
assert "Service not found" in page and 'id="logs"' not in page
|
|
|
|
|
|
async def test_a_service_history_shows_now_and_peak(app):
|
|
async with app.test_request_context("/plugins/docker/service/web/history"):
|
|
page = await render_plugin_template(
|
|
app, "docker", "docker/_service_history.html", have_data=True, range_key="24h",
|
|
cpu_now=0.5, cpu_peak=2.0, mem_now=100 << 20, mem_peak=300 << 20,
|
|
spark_cpu="<svg></svg>", spark_mem="<svg></svg>")
|
|
empty = await render_plugin_template(
|
|
app, "docker", "docker/_service_history.html", have_data=False, range_key="1h")
|
|
assert "0.50 cores" in page and "peak 2.0 cores" in page and "peak 300.0 MiB" in page
|
|
assert "Not enough samples in the last 1h" in empty
|
|
|
|
|
|
async def test_a_service_log_line_names_its_task(app):
|
|
lines = [{"ts": NOW, "stream": "stderr", "line": "<b>boom</b>", "task": f"1 · {TASK[:10]}"}]
|
|
async with app.test_request_context("/plugins/docker/service/web/logs/lines"):
|
|
page = await render_plugin_template(app, "docker", "docker/_container_logs_lines.html",
|
|
lines=lines, name="web", filtered=False, service=True)
|
|
empty = await render_plugin_template(app, "docker", "docker/_container_logs_lines.html",
|
|
lines=[], name="web", filtered=False, service=True)
|
|
assert f"1 · {TASK[:10]}</span>" in page and "<b>boom</b>" in page
|
|
assert "one of its tasks" in empty
|