feat(appctl): implement docker status inspection and image revision extraction
- Implement get_docker_status supporting compose directories and single core containers - Implement get_docker_image_revision for OCI image provenance label extraction - Unmark test_docker_status_parser_running from xfail in test suite - Add unit tests for stopped, degraded, non-stack, single container, and error states
This commit is contained in:
1 parent
964e633da2
commit
82edd74aac
2 files changed
+179
-12
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from dataclasses import asdict, dataclass, field
|
||||
from pathlib import Path
|
||||
@@ -361,12 +362,97 @@ def get_core_services(core_dir: str | None = None) -> list[CoreService]:
|
||||
|
||||
def get_docker_status(
|
||||
dir_path: str | Path,
|
||||
container_name: str | None = None
|
||||
container_name: str | None = None,
|
||||
) -> str:
|
||||
path: Path = Path(dir_path)
|
||||
def _do_something(path: Path = path):
|
||||
return ""
|
||||
return _do_something()
|
||||
"""Get container running status for a docker-compose directory or single Core container."""
|
||||
try:
|
||||
if container_name:
|
||||
inspect_res = subprocess.run(
|
||||
["docker", "inspect", "-f", "{{.State.Running}}", container_name],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
state = inspect_res.stdout.strip()
|
||||
if state.lower() == "true":
|
||||
return "🟢 Running (1)"
|
||||
return "🔴 Stopped"
|
||||
|
||||
path = Path(dir_path)
|
||||
if path.exists() and not (path / "docker-compose.yml").is_file() and not (path / "docker-compose.yaml").is_file():
|
||||
return "⚪ Not Stack"
|
||||
|
||||
res = subprocess.run(
|
||||
["docker", "compose", "ps", "-q"],
|
||||
cwd=str(path),
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
container_ids = [c for c in res.stdout.strip().splitlines() if c]
|
||||
if not container_ids:
|
||||
return "🔴 Stopped"
|
||||
|
||||
inspect_res = subprocess.run(
|
||||
["docker", "inspect", "-f", "{{.State.Running}}"] + container_ids,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
running_states = inspect_res.stdout.strip().splitlines()
|
||||
running_count = sum(1 for s in running_states if s.lower() == "true")
|
||||
total_count = len(container_ids)
|
||||
|
||||
if running_count == total_count and total_count > 0:
|
||||
return f"🟢 Running ({running_count})"
|
||||
elif running_count > 0:
|
||||
return f"🟡 Degraded ({running_count}/{total_count})"
|
||||
else:
|
||||
return "🔴 Stopped"
|
||||
except (subprocess.SubprocessError, OSError):
|
||||
return "❓ Unknown"
|
||||
|
||||
|
||||
def get_docker_image_revision(
|
||||
dir_path: str | Path | None = None,
|
||||
container_name: str | None = None,
|
||||
) -> str | None:
|
||||
"""Extract `org.opencontainers.image.revision` label from running container."""
|
||||
try:
|
||||
target: str | None = container_name
|
||||
if not target and dir_path:
|
||||
res = subprocess.run(
|
||||
["docker", "compose", "ps", "-q"],
|
||||
cwd=str(dir_path),
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
ids = [c for c in res.stdout.strip().splitlines() if c]
|
||||
if ids:
|
||||
target = ids[0]
|
||||
|
||||
if not target:
|
||||
return None
|
||||
|
||||
inspect_res = subprocess.run(
|
||||
[
|
||||
"docker",
|
||||
"inspect",
|
||||
"-f",
|
||||
'{{ index .Config.Labels "org.opencontainers.image.revision" }}',
|
||||
target,
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
revision = inspect_res.stdout.strip()
|
||||
if revision and revision != "<no value>":
|
||||
return revision
|
||||
return None
|
||||
except (subprocess.SubprocessError, OSError):
|
||||
return None
|
||||
|
||||
def cmd_sync_homepage(args) -> None:
|
||||
pass
|
||||
@@ -189,15 +189,9 @@ def test_sites_app_source_provenance_normalization(app_factory: AppFactory):
|
||||
|
||||
## RED TESTS: TDD Goals for next implementation
|
||||
|
||||
@pytest.mark.xfail(reason="TDD in progress: get_docker_status not yet implemented")
|
||||
def test_docker_status_parser_running(monkeypatch):
|
||||
"""
|
||||
TDD Goal: Port `get_docker_status` to V2
|
||||
It must shell out to `docker-compse ps` and `docker inspect`
|
||||
then return the correct UI badge
|
||||
"""
|
||||
"""Ensure get_docker_status reports running compose stack when all containers are running."""
|
||||
def mock_run(args, **kwargs):
|
||||
"""Mocking a running"""
|
||||
mock = MagicMock()
|
||||
if "ps" in args:
|
||||
mock.stdout = "container123\n"
|
||||
@@ -210,6 +204,93 @@ def test_docker_status_parser_running(monkeypatch):
|
||||
status = engine.get_docker_status("/fake/dir")
|
||||
assert status == "🟢 Running (1)"
|
||||
|
||||
|
||||
def test_docker_status_parser_stopped(monkeypatch):
|
||||
"""Ensure get_docker_status reports stopped when no containers exist."""
|
||||
def mock_run(args, **kwargs):
|
||||
mock = MagicMock()
|
||||
if "ps" in args:
|
||||
mock.stdout = ""
|
||||
return mock
|
||||
|
||||
monkeypatch.setattr("subprocess.run", mock_run)
|
||||
|
||||
status = engine.get_docker_status("/fake/dir")
|
||||
assert status == "🔴 Stopped"
|
||||
|
||||
|
||||
def test_docker_status_parser_degraded(monkeypatch):
|
||||
"""Ensure get_docker_status reports degraded when partial containers are running."""
|
||||
def mock_run(args, **kwargs):
|
||||
mock = MagicMock()
|
||||
if "ps" in args:
|
||||
mock.stdout = "c1\nc2\n"
|
||||
if "inspect" in args:
|
||||
mock.stdout = "true\nfalse\n"
|
||||
return mock
|
||||
|
||||
monkeypatch.setattr("subprocess.run", mock_run)
|
||||
|
||||
status = engine.get_docker_status("/fake/dir")
|
||||
assert status == "🟡 Degraded (1/2)"
|
||||
|
||||
|
||||
def test_docker_status_parser_not_stack(tmp_path: Path):
|
||||
"""Ensure get_docker_status reports Not Stack for real directory without compose file."""
|
||||
empty_dir = tmp_path / "empty_service"
|
||||
empty_dir.mkdir()
|
||||
assert engine.get_docker_status(empty_dir) == "⚪ Not Stack"
|
||||
|
||||
|
||||
def test_docker_status_single_container(monkeypatch):
|
||||
"""Ensure get_docker_status inspects single Core container by name."""
|
||||
def mock_run(args, **kwargs):
|
||||
mock = MagicMock()
|
||||
if "authelia" in args:
|
||||
mock.stdout = "true\n"
|
||||
else:
|
||||
mock.stdout = "false\n"
|
||||
return mock
|
||||
|
||||
monkeypatch.setattr("subprocess.run", mock_run)
|
||||
|
||||
assert engine.get_docker_status("", container_name="authelia") == "🟢 Running (1)"
|
||||
assert engine.get_docker_status("", container_name="traefik") == "🔴 Stopped"
|
||||
|
||||
|
||||
def test_docker_status_error_handling(monkeypatch):
|
||||
"""Ensure get_docker_status handles subprocess failures gracefully."""
|
||||
def mock_err(*args, **kwargs):
|
||||
raise OSError("Docker socket unreachable")
|
||||
|
||||
monkeypatch.setattr("subprocess.run", mock_err)
|
||||
assert engine.get_docker_status("/fake/dir") == "❓ Unknown"
|
||||
|
||||
|
||||
def test_docker_image_revision_extraction(monkeypatch):
|
||||
"""Ensure get_docker_image_revision extracts org.opencontainers.image.revision."""
|
||||
def mock_run(args, **kwargs):
|
||||
mock = MagicMock()
|
||||
if "ps" in args:
|
||||
mock.stdout = "container123\n"
|
||||
if "inspect" in args:
|
||||
mock.stdout = "9d82577f489f0d478e465a36077538d0ded16ba5\n"
|
||||
return mock
|
||||
|
||||
monkeypatch.setattr("subprocess.run", mock_run)
|
||||
assert engine.get_docker_image_revision("/fake/dir") == "9d82577f489f0d478e465a36077538d0ded16ba5"
|
||||
|
||||
def mock_run_empty(args, **kwargs):
|
||||
mock = MagicMock()
|
||||
if "ps" in args:
|
||||
mock.stdout = "container123\n"
|
||||
if "inspect" in args:
|
||||
mock.stdout = "<no value>\n"
|
||||
return mock
|
||||
|
||||
monkeypatch.setattr("subprocess.run", mock_run_empty)
|
||||
assert engine.get_docker_image_revision("/fake/dir") is None
|
||||
|
||||
@pytest.mark.xfail(reason="TDD in progress: cmd_sync_homepage not yet implemented")
|
||||
def test_homepage_sync_groups_and_sorts_correctly(
|
||||
mock_core_dir: Path,
|
||||
|
||||
Reference in new issue
Block a user