Files
FabledSteward/tests/plugins/docker/test_routes.py
T
bvandeusen a9b3b11327
CI / lint (push) Successful in 2s
CI / unit (push) Successful in 48s
CI / integration (push) Successful in 2m27s
CI / publish (push) Successful in 1m10s
feat(docker): admin-gated per-host prune buttons on the image/disk page
M78 step 2. The /disk page surfaced reclaimable space + stopped counts but
said "prune deferred to a later milestone" — this wires the cleanup.

Adds POST /plugins/docker/disk/<host_id>/prune (admin only) that resolves
the host's linked AnsibleTarget → steward:target:<id> scope and fires the
bundled maintenance/docker_prune.yml through the ansible.run_playbook
capability (audited runner; the collection agent stays read-only). Mirrors
the host_agent update route. `target` ∈ {containers, images, system}:
- containers → docker container prune -f
- images     → docker image prune -af (prune_all_images)
- system     → docker system prune -f (conservative)

The disk() route now resolves each host group's linked target + capability
availability; disk.html renders three confirm-gated buttons per host, shown
only to admins when Ansible is available and the host has a linked target,
otherwise a clear "link a target" / "runner unavailable" hint (rules 24/26/27).

Tests: the prune_target→extra_vars mapping (the -af-only-for-images rule) is
extracted to a pure helper and unit-tested; disk_prune added to the module
smoke; disk.html covered by the existing template-parse test.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CAGR73DUowdVFVvYzLXC5C
2026-07-19 16:22:30 -04:00

67 lines
2.5 KiB
Python

"""Unit tests for the docker plugin's presentation layer (no DB).
CI never renders these templates through the running app (the unit-lane app is
created with testing=True, which skips plugin loading), so a Jinja syntax error
would otherwise ship green. These tests parse every docker template and smoke
the routes module so a broken tag or import is caught in the unit lane.
"""
import pathlib
import jinja2
from plugins.docker import routes as r
_TEMPLATES = pathlib.Path(r.__file__).parent / "templates" / "docker"
def test_all_docker_templates_parse():
env = jinja2.Environment()
files = sorted(_TEMPLATES.glob("*.html"))
assert files, "no docker templates found"
for f in files:
# Raises TemplateSyntaxError on an unbalanced/typo'd tag.
env.parse(f.read_text())
def test_routes_module_exposes_new_views():
# Import-smoke + confirms the #942 view functions are defined.
for name in ("container_detail", "container_history", "swarm", "disk", "index", "rows"):
assert callable(getattr(r, name)), name
assert r.docker_bp.name == "docker"
def test_disk_prune_view_defined():
# M78 admin-gated prune action.
assert callable(r.disk_prune)
def test_prune_extra_vars_mapping():
"""The prune buttons drive one playbook via prune_target; only 'images'
widens to ALL unused images (docker image prune -a), system stays -f."""
assert r._prune_extra_vars("containers") == {"prune_target": "containers"}
assert r._prune_extra_vars("images") == {
"prune_target": "images", "prune_all_images": True,
}
# System prune stays conservative — no prune_all_images key.
assert r._prune_extra_vars("system") == {"prune_target": "system"}
def test_human_bytes_formats_binary_units():
assert r._human_bytes(None) == "—"
assert r._human_bytes(0) == "0 B"
assert r._human_bytes(512) == "512 B"
assert r._human_bytes(1024) == "1.0 KiB"
assert r._human_bytes(1536) == "1.5 KiB"
assert r._human_bytes(1024 ** 2) == "1.0 MiB"
assert r._human_bytes(int(1.5 * 1024 ** 3)) == "1.5 GiB"
assert r._human_bytes(1024 ** 4) == "1.0 TiB"
def test_human_uptime_compact():
from datetime import datetime, timedelta, timezone
now = datetime.now(timezone.utc)
assert r._human_uptime(None) is None
assert r._human_uptime(now - timedelta(minutes=8)).endswith("m")
assert "h" in r._human_uptime(now - timedelta(hours=5, minutes=12))
assert "d" in r._human_uptime(now - timedelta(days=3, hours=4))