"""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.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_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 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}] groups = [{"host": None, "host_id": "h1", "host_name": "nas", "running": 1, "stopped": 1, "grouped": False, "subgroups": [{"label": None, "containers": [ {"container": loop, "ports": [], "uptime": None, "stale": False, "usage": svc.usage(loop, 1, None), "exit_failed": True}, {"container": old, "ports": [], "uptime": "2h", "stale": True, "usage": svc.usage(old, 1, None), "exit_failed": False}]}]}] async with app.test_request_context("/plugins/docker/rows"): page = await render_plugin_template( app, "docker", "docker/rows.html", attention=attention, says=svc.PROBLEM_SAYS, severity=svc.PROBLEM_SEVERITY, unhealthy=0, restarting=1, stale=1, host_groups=groups, swarm_services=[], running=1, stopped=1, docker_hosts=1, failures=[], failing_services=[]) page = re.sub(r">\s+<", "><", " ".join(page.split())) 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 "cache" not in card assert page.index("Needs attention") < page.index("Restarting1' in page assert 'Stale1' in page 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 "history" not in page.lower() and "" not in page and "No containers reported yet" 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)}] async with app.test_request_context("/plugins/docker/rows"): page = await render_plugin_template( app, "docker", "docker/rows.html", docker_down=down, attention=[], says=svc.PROBLEM_SAYS, severity=svc.PROBLEM_SEVERITY, unhealthy=0, restarting=0, stale=0, host_groups=[], swarm_services=[], running=0, stopped=0, docker_hosts=0, failures=[], failing_services=[]) page = re.sub(r">\s+<", "><", " ".join(page.split())) 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): async with app.test_request_context("/plugins/docker/rows"): page = await render_plugin_template( app, "docker", "docker/rows.html", docker_down=[], attention=[], says=svc.PROBLEM_SAYS, severity=svc.PROBLEM_SEVERITY, unhealthy=0, restarting=0, stale=0, host_groups=[], swarm_services=[], running=0, stopped=0, docker_hosts=0, failures=[], failing_services=[]) assert "Docker not answering" not in page async def test_a_failing_service_leads_and_the_failures_are_listed(app): now = datetime.now(timezone.utc) sick = {"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}} failures = [{"kind": "service", "name": "codex_db", "host_id": None, "host": "nas", "count": 9, "at": now - timedelta(minutes=2), "detail": "task: non-zero exit (1)"}, {"kind": "container", "name": "backup", "host_id": "h1", "host": "nas", "count": 1, "at": now - timedelta(hours=3), "detail": "exit 2"}] async with app.test_request_context("/plugins/docker/rows"): page = await render_plugin_template( app, "docker", "docker/rows.html", docker_down=[], attention=[], says=svc.PROBLEM_SAYS, severity=svc.PROBLEM_SEVERITY, unhealthy=0, restarting=0, stale=0, host_groups=[], swarm_services=[sick], services_healthy=0, failing_services=[sick], failures=failures, running=0, stopped=0, docker_hosts=1) page = re.sub(r">\s+<", "><", " ".join(page.split())) card = page[page.index("Needs attention"):page.index("")] assert 'Crash-loopingcodex_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 "since Swarm services0/1' 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 'href="/plugins/docker/container/h1/backup">backup' in listed