"""The Docker page leads with what needs attention, ranked the way the MCP tools rank it, and marks a container whose report is old as stale (#5123).""" import re from datetime import datetime, timedelta, timezone from types import SimpleNamespace as NS from plugins.docker import service as svc from plugins.docker.overview import DOCKER_STATES, NO_STACK, container_row, service_row from plugins.docker.routes import _is_stale from tests._templates import render_plugin_template from tests.test_style_tokens import hex_colours NOW = datetime.now(timezone.utc) def _c(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, cpu_pct=5.0, mem_used_pct=10.0, compose_project=None, service_name=None, ports_json=None, started_at=NOW - timedelta(hours=2), scraped_at=NOW - timedelta(seconds=20)) base.update(kw) return NS(**base) def test_needs_attention_ranks_the_worst_problem_first(): ranked = [{"container": _c(n), "problems": p, "since": None, "replaced": False} for n, p in [ ("fine", []), ("oom", ["oom_killed"]), ("sick", ["unhealthy"]), ("loop", ["crash_looping"]), ("exit", ["exited_nonzero"]), ("bounce", ["restarting", "crash_looping"])]] assert [r["container"].name for r in svc.needs_attention(ranked)] == ["bounce", "loop", "sick", "oom", "exit"] def test_stale_is_older_than_the_agents_window(): assert not _is_stale(_c("a"), NOW, 180) assert _is_stale(_c("a", scraped_at=NOW - timedelta(minutes=10)), NOW, 180) assert _is_stale(_c("a", scraped_at=None), NOW, 180) # A fresh row on a host whose Docker stopped answering is stale too (#5152). assert _is_stale(_c("a"), NOW, 180, {"h1": object()}) assert not _is_stale(_c("a"), NOW, 180, {"h2": object()}) assert svc.stale_after_seconds({"PLUGINS": {"host_agent": {"stale_after_seconds": 60}}}) == 60 assert svc.stale_after_seconds({}) == 180 _F = {"q": "", "stack": "", "host": "", "state": "", "sort": "status", "dir": "asc"} async def _rows(app, **over): """docker/rows.html with what routes.rows() hands it (#5298–#5301), nothing set unless given.""" ctx = dict(services=[], containers=[], services_total=0, containers_total=0, services_healthy=0, failing_services=[], failures=[], docker_hosts=0, docker_down=[], attention=[], says=svc.PROBLEM_SAYS, severity=svc.PROBLEM_SEVERITY, unhealthy=0, restarting=0, stale=0, running=0, stopped=0, f=dict(_F), filtered=False, sort_url=lambda key: f"/plugins/docker/?sort={key}", page_url=lambda **kw: "/plugins/docker/?" + "&".join(f"{k}={v}" for k, v in kw.items())) ctx.update(over) async with app.test_request_context("/plugins/docker/rows"): page = await render_plugin_template(app, "docker", "docker/rows.html", **ctx) return re.sub(r">\s+<", "><", " ".join(page.split())) def _crow(c, *, host="nas", stale=False, exit_failed=False, ports=()): return container_row(c, label=svc.display_name(c), host=host, usage=svc.usage(c, 1, None), ports=list(ports), stale=stale, exit_failed=exit_failed, now=NOW) async def test_a_crash_loop_comes_first_and_a_stale_row_says_so(app): loop = _c("api", status="restarting", exit_code=1, restart_count=7) old = _c("cache", scraped_at=NOW - timedelta(minutes=30), cpu_pct=95.0) attention = [{"container": loop, "label": "api", "problems": ["restarting", "crash_looping"], "replaced": False, "since": datetime.now(timezone.utc) - timedelta(minutes=8), "stale": False}] page = await _rows(app, attention=attention, restarting=1, stale=1, running=1, stopped=1, docker_hosts=1, containers=[_crow(loop, exit_failed=True), _crow(old, stale=True)], containers_total=2) # #5298: the verdict first; each problem section carries its own count (#5214). assert page.index('class="page-verdict"') < page.index("Needs attention") assert "1 needs attention" in page and "1 container running, 1 stopped" in page assert " unhealthy" not in page and "Failed in the last 24h" not in page and "stat-strip" not in page card = page[page.index("Needs attention"):page.index("")] assert 'Restarting' in card assert 'href="/plugins/docker/container/h1/api" title="api">api' in card assert "is restarting and keeps dying · exit 1 · since 8m ago" in card assert 'href="/plugins/docker/container/h1/api/logs"' in card assert "cache" not in card assert page.index("Needs attention") < page.index("1' ' · 1 restarting') in card assert '">1 stale' in page[:page.index("

")] # stale sits in the verdict assert 'class="badge badge-warn" title="Last report' in page # the stale row # CPU in cores, toned by its share of the host's CPUs through Settings → Thresholds. assert ('0.95 cores') in page assert "" not in page and "No containers reported yet" in page assert "Nothing needs attention" in page and "stat-strip" not in page async def test_a_host_whose_docker_stopped_answering_says_so(app): now = datetime.now(timezone.utc) # not the module's NOW: |ago reads the clock down = [{"host_id": "h1", "host_name": "nas", "status": "unavailable", "detail": "no Docker socket at /var/run/docker.sock", "since": now - timedelta(minutes=5)}, {"host_id": "h2", "host_name": "web-1", "status": "error", "detail": "permission denied on /var/run/docker.sock", "since": now - timedelta(minutes=1)}] page = await _rows(app, docker_down=down) card = page[page.index("Docker not answering"):page.index("")] assert 'No Dockernas' in card assert "no Docker socket at /var/run/docker.sock · since 5m " in card assert 'Errorweb-1' in card async def test_no_card_while_every_docker_answers(app): assert "Docker not answering" not in await _rows(app) def _sick(now): return {"name": "codex_db", "mode": "replicated", "running": 0, "desired": 1, "state": "down", "crash_looping": True, "failures_1h": 7, "failures_24h": 9, "tasks_known": True, "trouble_since": now - timedelta(minutes=20), "replicas": [], "previous_tasks": 7, "last_error": {"state": "failed", "error": "task: non-zero exit (1)", "exit_code": 1, "node": "nas", "at": None, "age_s": 120, "task_id": "t" * 25, "slot": 1}} async def test_a_failing_service_leads_and_the_failures_are_listed_once_with_their_logs(app): """#5300: each failure links to the logs of what died; a service's to its failed task's lines on the service page.""" now = datetime.now(timezone.utc) sick = _sick(now) failures = [{"kind": "service", "name": "codex_db", "host_id": None, "host": "nas", "count": 9, "at": now - timedelta(minutes=2), "detail": "task: non-zero exit (1)", "task_id": "t" * 25}, {"kind": "container", "name": "backup", "host_id": "h1", "host": "nas", "count": 1, "at": now - timedelta(hours=3), "detail": "exit 2", "task_id": None}] page = await _rows(app, services=[service_row(sick, now)], services_total=1, failing_services=[sick], failures=failures, docker_hosts=1) card = page[page.index("Needs attention"):page.index("")] assert ('Crash-looping' 'codex_db') in card assert "0 of 1 running · 7 tasks failed in the last hour · last task failed: task: non-zero exit (1)" in card assert f'href="/plugins/docker/service/codex_db?task={"t" * 25}#logs"' in card assert "since 0/1 services healthy' in page listed = page[page.index('id="failures"'):] listed = listed[:listed.index("")] assert listed.index("codex_db") < listed.index("backup") # newest first assert "9 failures · last: task: non-zero exit (1) · nas" in listed assert f'/plugins/docker/service/codex_db?task={"t" * 25}#logs"' in listed and "Logs of that task" in listed assert 'href="/plugins/docker/container/h1/backup/logs"' in listed # The count sits in the heading of the list it counts: one widget, not a tile beside it. assert 'Failed in the last 24h 2' in page assert "stat-tile" not in page async def test_services_and_containers_are_tables_that_sort_and_filter(app): """#5299, #5301: one row per service with its stack; every standalone container in one table with its host; headers sort; a stack chip filters.""" now = datetime.now(timezone.utc) web = {"name": "arr_radarr", "mode": "replicated", "running": 2, "desired": 2, "state": "healthy", "crash_looping": False, "failures_24h": 0, "last_error": None, "trouble_since": None, "image": "lscr.io/linuxserver/radarr:5", "replicas": [ {"ghost": False, "host": "docker02", "host_id": "h2", "name": "arr_radarr.1.x", "status": "running", "cpu_pct": 25.0, "mem_bytes": 300 << 20, "started_at": now - timedelta(hours=3), "image": "lscr.io/linuxserver/radarr:5", "health": None}, {"ghost": True, "host": "edge01", "count": 1}]} proxy = _c("traefik", host_id="h1", compose_project="edge", image="traefik:v3.1", ports_json=None, cpu_pct=3.0) page = await _rows(app, services=[service_row(web, now)], services_total=3, services_healthy=3, containers=[_crow(proxy, ports=[{"host_port": 443, "container_port": 443, "protocol": "tcp"}])], containers_total=1, running=1, docker_hosts=2, f=dict(_F, sort="cpu", dir="desc")) services = page[page.index('id="docker-services"'):] services = services[:services.index("")] assert "1 of 3" in services and 'href="/plugins/docker/swarm"' in services assert 'href="/plugins/docker/service/arr_radarr" title="arr_radarr · lscr.io/linuxserver/radarr:5">arr_radarr' in services assert '0.25 cores<" in services and ">300.0 MiB<" in services and ">3h" in services assert 'aria-sort="descending"' in services and 'href="/plugins/docker/?sort=mem"' in services containers = page[page.index('id="docker-containers"'):] assert "Other containers" in containers and ">nas<" in containers assert 'traefik:v3.1' in containers assert '
443:443/tcp
' in containers assert not hex_colours(page), hex_colours(page) async def test_a_filter_that_matches_nothing_says_so(app): page = await _rows(app, services_total=4, containers_total=2, filtered=True, f=dict(_F, q="zzz")) assert "Nothing matches these filters" in page and "No containers reported yet" not in page async def test_the_docker_page_has_a_filter_bar_the_rows_read(app): from quart import session async with app.test_request_context("/plugins/docker/?stack=arr"): session["user_role"] = "viewer" page = await render_plugin_template( app, "docker", "docker/index.html", poll_interval=10, has_swarm=True, has_disk=False, f=dict(_F, stack="arr", sort="mem", dir="desc"), choices={"stacks": ["arr", "edge"], "hosts": ["a", "b"]}, states=DOCKER_STATES, no_stack=NO_STACK) bar = re.search(r'
', page, re.S).group(0) assert 'type="search" id="docker-q" name="q"' in bar assert '' in bar and f'docker02' in page and "42.0 GiB" in page assert 'class="tone-warn"' in page and ">+6.0 GiB<" in page and "Change (7d)" in page assert "growth shows once there are two readings" in page assert not hex_colours(page), hex_colours(page) def _widget_svc(name, state="healthy", running=1, desired=1, hosts=("VDNT-Docker02",), **kw): base = dict(name=name, state=state, running=running, desired=desired, crash_looping=False, failures_1h=0, last_error=None, trouble_since=None, replicas=[{"host": h, "ghost": False} for h in hosts]) base.update(kw) return base async def _services_widget(app, **ctx): base = dict(bad_services=[], bad_containers=[], healthy=[], healthy_more=0, services_total=0, services_healthy=0, running_count=0, failures=[], host_name={"h1": "VDNT-Docker02"}, total_count=1, widget_id="1") base.update(ctx) async with app.test_request_context("/plugins/docker/widget"): return await render_plugin_template(app, "docker", "docker/widget.html", **base) async def test_the_services_widget_lists_what_is_wrong_then_packs_the_healthy(app): """#5252: a verdict, the troubled service with why and since when, the failures by name, then healthy services one line each in a packed grid.""" bad = _widget_svc("codex_db", state="down", running=0, crash_looping=True, failures_1h=7, trouble_since=datetime.now(timezone.utc) - timedelta(minutes=9)) page = await _services_widget( app, bad_services=[bad], services_total=3, services_healthy=2, running_count=4, failures=[{"name": "codex_db", "host": "VDNT-Docker03", "count": 7, "at": NOW - timedelta(minutes=1), "detail": "exit 1"}], healthy=[{"kind": "service", "s": _widget_svc("curator_app")}, {"kind": "service", "s": _widget_svc("web", running=2, desired=2, hosts=("a", "b", "a"))}, {"kind": "container", "c": _c("portainer_agent")}], healthy_more=12) assert "2/3 services healthy" in page and "4 other containers running" in page and "1 failed in 24h" in page assert page.index("Needs a look") < page.index("Failed in the last 24h") < page.index("Healthy") assert "crash-looping: 7 failed in the last hour" in page and "9m ago" in page assert "7 times, last" in page and "exit 1" in page assert 'class="w-grid"' in page and " · a, b" in page and ">2/2<" in page assert "+12 more healthy" in page assert not hex_colours(page), hex_colours(page) async def test_a_troubled_container_says_since_when_on_the_widget(app): """#5810: a container in trouble carries its since, as the Docker page does.""" sick = _c("backup_cron", host_id="h1", status="running", health="unhealthy") page = await _services_widget( app, bad_containers=[sick], bad_since={("h1", "backup_cron"): datetime.now(timezone.utc) - timedelta(minutes=40)}) page = " ".join(page.split()) assert "unhealthy since CI: Android · build on VDNT-Docker03" in page and "1.6 of 8 cores" in page assert ">1.6 cores<" in page and ">0.25 cores<" in page and ">820.0 MiB<" in page and ">—<" in page # Android peaks at 3.2 cores, so 1.6 is half height; tdarr's 0.25 sits under the 1-core floor. assert 'class="gap"' in page and "height:50.0%" in page and "height:25.0%" in page assert "Peak 3.2 cores in the last 6h; bars reach the top at 3.2 cores" in page assert "Peak 0.25 cores in the last 6h; bars reach the top at 1.0 cores" in page assert "No samples in the last 6h" in page assert len(re.findall(r"]", page)) == 4 assert ">CI: Android · build<" in page # the long CI name only as the hover title assert not hex_colours(page), hex_colours(page) def test_bars_reach_the_peak_but_never_below_the_floor(): """#5214: a 0.05 → 0.10 wobble stays near the floor; a 1.5 → 3 spike fills.""" idle = svc.scaled_bars([0.05, 0.10, None], 1.0) assert idle == {"bars": [5.0, 10.0, None], "peak": 0.10, "roof": 1.0} busy = svc.scaled_bars([1.5, 3.0], 1.0) assert busy["bars"] == [50.0, 100.0] and busy["roof"] == 3.0 assert svc.scaled_bars([None, None], 1.0) == {"bars": [None, None], "peak": None, "roof": 1.0} def test_bar_slots_end_at_the_bucket_now_falls_in(): now = datetime(2026, 10, 7, 12, 7, 30, tzinfo=timezone.utc) slots, width = svc.bar_slots(now, 6) assert width == 900 and len(slots) == 24 assert slots[-1] == datetime(2026, 10, 7, 12, 0, tzinfo=timezone.utc) assert slots[0] == datetime(2026, 10, 7, 6, 15, tzinfo=timezone.utc) def test_builders_and_ci_jobs_read_by_what_they_are_on_a_hosts_page(): assert svc.display_name(_c("buildx_buildkit_steward-builder0")) == "BuildKit builder · steward-builder" ci = "FORGEJO-ACTIONS-TASK-8597-WORKFLOW-CI-JOB-unit" assert svc.task_label(_c(ci)) == "CI: CI · unit" # not a swarm task: display_name assert svc.task_label(_c("codex_db.1.37ma", service_name="codex_db")) == "codex_db · 1" def _panel_row(name, cpu, status="running", problems=()): c = _c(name, cpu_pct=cpu, status=status) return {"c": c, "label": name, "problems": list(problems), "since": None, "usage": svc.usage(c, 4, 8 << 30), "uptime": "2h" if status == "running" else None} def _panel_rows(): return ([_panel_row(f"busy{n:02d}", float(n)) for n in range(1, 11)] + [_panel_row("old", None, status="exited"), _panel_row("dead", None, status="exited", problems=["exited_nonzero"])]) def test_a_hosts_docker_panel_shows_problems_then_the_busiest_and_folds_the_rest(): """#5276: 33 containers made the card run off the page.""" from plugins.docker.routes import HOST_PANEL_BUSIEST, host_panel_rows shown, folded = host_panel_rows(_panel_rows()) assert [r["label"] for r in shown] == ["dead"] + [f"busy{n:02d}" for n in range(10, 2, -1)] assert len(shown) == 1 + HOST_PANEL_BUSIEST assert [r["label"] for r in folded] == ["busy02", "busy01", "old"] async def test_the_folded_containers_sit_behind_one_line(app): from plugins.docker.routes import host_panel_rows shown, folded = host_panel_rows(_panel_rows()) async with app.test_request_context("/plugins/docker/host/h1"): page = await render_plugin_template( app, "docker", "docker/host_panel.html", rows=shown, folded=folded, says=svc.PROBLEM_SAYS, severity=svc.PROBLEM_SEVERITY, running=10, stopped=2, problems=1, replaced=0, busiest=8) fold = page.index("busy10<") < fold < page.index(">busy01<") assert page.index(">dead<") < page.index(">busy10<") assert len(re.findall(r"]", page)) == 6 # one header row per table assert not hex_colours(page), hex_colours(page)