- scripts/appctl_auth.py: default DEFAULT_DEPLOYMENTS_FILE to WORKLOADS_DIR/SITES_DIR or ~/Workloads/deployments.yaml and honor WORKLOADS_DIR in load_admitted_workloads() - scripts/appctl_daemon.py: default DEPLOYMENTS_FILE to WORKLOADS_DIR/deployments.yaml (~/Workloads/deployments.yaml) instead of CORE_DIR/config/deployments.yaml - scripts/gitops_dispatcher.py: default DEPLOYMENTS_FILE to WORKLOADS_DIR/deployments.yaml and remove CONFIG_DIR/deployments.yaml fallback in get_trusted_repository_mapping() so missing registry fails closed - tests: add regression tests for default ~/Workloads/deployments.yaml resolution and fail-closed behavior; isolate test_operational_router_workload_update_propagates_non_zero_exit_and_stderr from host registry
1685 lines
56 KiB
Python
1685 lines
56 KiB
Python
"""
|
|
tests/unit/test_appctl_router.py — Unit Tests for Operational Router & Execution Engine (P4 Slice 4)
|
|
|
|
Behavior-driven test naming convention:
|
|
test_<subject>_<accepts|rejects|enforces>_<condition>
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
import yaml
|
|
from appctl_audit import AuditLogger, TargetLockManager
|
|
from appctl_auth import Principal, Role
|
|
from appctl_contracts import ErrorCode, OperationRequest
|
|
from appctl_router import (
|
|
MAX_OUTPUT_BYTES,
|
|
OperationalRouter,
|
|
RouterExecutionError,
|
|
RouterValidationError,
|
|
assemble_workload_env,
|
|
parse_dotenv,
|
|
resolve_canonical_domain,
|
|
run_bounded_subprocess,
|
|
validate_workload_preflight,
|
|
)
|
|
from core_manifest import parse_manifest
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def set_test_domain(monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.setenv("HOMELAB_DOMAIN", "example.test")
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_sites_dir(tmp_path: Path) -> Path:
|
|
sites = tmp_path / "Sites"
|
|
sites.mkdir(parents=True, exist_ok=True)
|
|
return sites
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_core_dir(tmp_path: Path) -> Path:
|
|
core = tmp_path / "Core"
|
|
core.mkdir(parents=True, exist_ok=True)
|
|
return core
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_workload_dir(mock_sites_dir: Path) -> Path:
|
|
app_dir = mock_sites_dir / "snappymail"
|
|
app_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
manifest_content = """
|
|
schema_version: "1.0"
|
|
name: "snappymail"
|
|
source:
|
|
type: "git"
|
|
url: "https://github.com/example/snappymail.git"
|
|
ref: "main"
|
|
deployment:
|
|
strategy: "compose"
|
|
actions:
|
|
- "git_pull"
|
|
- "compose_pull"
|
|
- "compose_up"
|
|
env:
|
|
WORKLOAD_CUSTOM_VAR: "custom_value"
|
|
"""
|
|
(app_dir / "app.yaml").write_text(manifest_content.strip(), encoding="utf-8")
|
|
|
|
compose_content = """
|
|
services:
|
|
webmail:
|
|
image: "snappymail:latest"
|
|
container_name: "snappymail"
|
|
restart: "unless-stopped"
|
|
"""
|
|
(app_dir / "docker-compose.yml").write_text(
|
|
compose_content.strip(), encoding="utf-8"
|
|
)
|
|
return app_dir
|
|
|
|
|
|
@pytest.fixture
|
|
def admin_principal() -> Principal:
|
|
return Principal(identity="operator", role=Role.ADMIN, auth_method="bearer_token")
|
|
|
|
|
|
@pytest.fixture
|
|
def operator_principal() -> Principal:
|
|
return Principal(
|
|
identity="gitops_bot", role=Role.OPERATOR, auth_method="bearer_token"
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def viewer_principal() -> Principal:
|
|
return Principal(
|
|
identity="unix_uid_1000", role=Role.VIEWER, auth_method="unix_peercred"
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Pure-Python Inert Dotenv Parser
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_parse_dotenv_parses_valid_key_values(tmp_path: Path):
|
|
env_file = tmp_path / ".env"
|
|
env_file.write_text(
|
|
"""
|
|
# Comment line
|
|
APP_PORT=8080
|
|
APP_HOST="127.0.0.1"
|
|
SINGLE_QUOTE='single_val'
|
|
EMPTY_VAL=
|
|
WITH_EQUALS=key=value=more
|
|
""",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
parsed = parse_dotenv(env_file)
|
|
assert parsed["APP_PORT"] == "8080"
|
|
assert parsed["APP_HOST"] == "127.0.0.1"
|
|
assert parsed["SINGLE_QUOTE"] == "single_val"
|
|
assert parsed["EMPTY_VAL"] == ""
|
|
assert parsed["WITH_EQUALS"] == "key=value=more"
|
|
|
|
|
|
def test_parse_dotenv_preserves_shell_injection_as_inert_literals(tmp_path: Path):
|
|
env_file = tmp_path / ".env"
|
|
env_file.write_text(
|
|
"""
|
|
INJECTION_ONE=$(touch /tmp/pwned)
|
|
INJECTION_TWO=`cat /etc/passwd`
|
|
VARIABLE_REF=$HOMELAB_DOMAIN
|
|
""",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
parsed = parse_dotenv(env_file)
|
|
assert parsed["INJECTION_ONE"] == "$(touch /tmp/pwned)"
|
|
assert parsed["INJECTION_TWO"] == "`cat /etc/passwd`"
|
|
assert parsed["VARIABLE_REF"] == "$HOMELAB_DOMAIN"
|
|
assert not Path("/tmp/pwned").exists()
|
|
|
|
|
|
def test_parse_dotenv_rejects_invalid_key_syntax(tmp_path: Path):
|
|
bad_keys = ["APP-PORT=80", "123NUM=val", "KEY WITH SPACES=val", "@INVALID=1"]
|
|
for bad in bad_keys:
|
|
env_file = tmp_path / ".env"
|
|
env_file.write_text(bad, encoding="utf-8")
|
|
with pytest.raises(RouterValidationError) as exc_info:
|
|
parse_dotenv(env_file)
|
|
assert "Prohibited environment key syntax" in str(exc_info.value)
|
|
|
|
|
|
def test_parse_dotenv_rejects_reserved_environment_keys(tmp_path: Path):
|
|
for reserved in ["PATH=foo", "LD_PRELOAD=bar", "SHELL=/bin/bash", "USER=root"]:
|
|
env_file = tmp_path / ".env"
|
|
env_file.write_text(reserved, encoding="utf-8")
|
|
with pytest.raises(RouterValidationError) as exc_info:
|
|
parse_dotenv(env_file)
|
|
assert "Prohibited reserved environment key" in str(exc_info.value)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Canonical Domain Resolution & Environment Assembly
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_resolve_canonical_domain_precedence(monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.setenv("HOMELAB_DOMAIN", "domain-a.test")
|
|
monkeypatch.setenv("DOMAIN_SUFFIX", "domain-b.test")
|
|
monkeypatch.setenv("DOMAIN", "domain-c.test")
|
|
assert resolve_canonical_domain() == "domain-a.test"
|
|
|
|
monkeypatch.delenv("HOMELAB_DOMAIN")
|
|
assert resolve_canonical_domain() == "domain-b.test"
|
|
|
|
monkeypatch.delenv("DOMAIN_SUFFIX")
|
|
assert resolve_canonical_domain() == "domain-c.test"
|
|
|
|
# Fails closed when domain is unset
|
|
monkeypatch.delenv("DOMAIN")
|
|
monkeypatch.setattr("appctl_router.ENGINE_HOMELAB_DOMAIN", "")
|
|
with pytest.raises(RouterValidationError, match="Canonical domain is unset"):
|
|
resolve_canonical_domain()
|
|
|
|
# Fails closed when domain is whitespace only
|
|
monkeypatch.setenv("HOMELAB_DOMAIN", " ")
|
|
with pytest.raises(RouterValidationError, match="Canonical domain is unset"):
|
|
resolve_canonical_domain()
|
|
|
|
# Strips leading and trailing whitespace
|
|
monkeypatch.setenv("HOMELAB_DOMAIN", " stripped.example.test ")
|
|
assert resolve_canonical_domain() == "stripped.example.test"
|
|
|
|
|
|
def test_assemble_workload_env_constructs_6_layer_environment(
|
|
mock_workload_dir: Path, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.setenv("HOMELAB_DOMAIN", "example.test")
|
|
manifest = parse_manifest(mock_workload_dir / "app.yaml")
|
|
|
|
# Add local .env
|
|
(mock_workload_dir / ".env").write_text(
|
|
"LOCAL_ENV_VAR=local_val\n", encoding="utf-8"
|
|
)
|
|
|
|
env = assemble_workload_env(mock_workload_dir, manifest, "snappymail")
|
|
|
|
# Layer 1
|
|
assert "PATH" in env
|
|
# Layer 3 & 4
|
|
assert env["HOMELAB_DOMAIN"] == "example.test"
|
|
assert env["SERVICE_NAME"] == "snappymail"
|
|
assert env["SERVICE_DOMAIN"] == "snappymail.example.test"
|
|
assert env["PROXY_NETWORK"] == "proxy-net"
|
|
# Layer 5 (manifest env)
|
|
assert env["WORKLOAD_CUSTOM_VAR"] == "custom_value"
|
|
# Layer 6 (.env)
|
|
assert env["LOCAL_ENV_VAR"] == "local_val"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Bounded Subprocess Execution Engine
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_run_bounded_subprocess_executes_valid_command():
|
|
code, out, err = run_bounded_subprocess(["echo", "hello world"])
|
|
assert code == 0
|
|
assert out.strip() == "hello world"
|
|
assert err == ""
|
|
|
|
|
|
def test_run_bounded_subprocess_enforces_256_kib_cap():
|
|
# Produce > 256 KiB of output
|
|
python_cmd = [
|
|
"python3",
|
|
"-c",
|
|
f"import sys; sys.stdout.write('A' * ({MAX_OUTPUT_BYTES} + 1024))",
|
|
]
|
|
code, out, _ = run_bounded_subprocess(python_cmd)
|
|
assert code == 0
|
|
assert len(out.encode("utf-8")) <= MAX_OUTPUT_BYTES + 200
|
|
assert "[TRUNCATED: Output exceeded 256 KiB limit]" in out
|
|
|
|
|
|
def test_run_bounded_subprocess_times_out():
|
|
cmd = ["sleep", "2"]
|
|
with pytest.raises(RouterExecutionError) as exc_info:
|
|
run_bounded_subprocess(cmd, timeout=0.1)
|
|
assert "timed out after 0.1 seconds" in str(exc_info.value)
|
|
assert exc_info.value.exit_code == 124
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Workload Pre-Flight Gate
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_validate_workload_preflight_accepts_valid_workload(
|
|
mock_workload_dir: Path, mock_sites_dir: Path
|
|
):
|
|
manifest, compose_file = validate_workload_preflight(
|
|
mock_workload_dir, mock_sites_dir, "snappymail"
|
|
)
|
|
assert manifest.name == "snappymail"
|
|
assert compose_file.exists()
|
|
|
|
|
|
def test_validate_workload_preflight_rejects_path_traversal(
|
|
mock_sites_dir: Path, tmp_path: Path
|
|
):
|
|
outside_dir = tmp_path / "outside_dir"
|
|
outside_dir.mkdir(parents=True, exist_ok=True)
|
|
with pytest.raises(RouterValidationError) as exc_info:
|
|
validate_workload_preflight(outside_dir, mock_sites_dir)
|
|
assert "Path traversal rejected" in str(exc_info.value)
|
|
|
|
|
|
def test_validate_workload_preflight_rejects_prohibited_compose_security(
|
|
mock_workload_dir: Path, mock_sites_dir: Path
|
|
):
|
|
# Inject privileged: true into docker-compose.yml
|
|
bad_compose = """
|
|
services:
|
|
webmail:
|
|
image: "snappymail:latest"
|
|
privileged: true
|
|
"""
|
|
(mock_workload_dir / "docker-compose.yml").write_text(bad_compose, encoding="utf-8")
|
|
|
|
with pytest.raises(RouterValidationError) as exc_info:
|
|
validate_workload_preflight(mock_workload_dir, mock_sites_dir, "snappymail")
|
|
assert "Compose AST security validation failed" in str(exc_info.value)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Operational Router Dispatch Pipeline
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_operational_router_dispatches_platform_health(
|
|
tmp_path: Path, admin_principal: Principal
|
|
):
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
)
|
|
req = OperationRequest.model_validate({"operation": "platform.health"})
|
|
resp = router.dispatch(req, admin_principal)
|
|
assert resp.success is True
|
|
assert resp.result is not None
|
|
assert resp.result["status"] == "healthy"
|
|
|
|
|
|
def test_operational_router_dispatches_platform_sync_homepage(
|
|
tmp_path: Path, admin_principal: Principal
|
|
):
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
)
|
|
req = OperationRequest.model_validate({"operation": "platform.sync_homepage"})
|
|
with patch("appctl_router.cmd_sync_homepage") as mock_sync:
|
|
resp = router.dispatch(req, admin_principal)
|
|
assert resp.success is True
|
|
mock_sync.assert_called_once()
|
|
|
|
|
|
def test_operational_router_enforces_rbac_denial(
|
|
tmp_path: Path, viewer_principal: Principal
|
|
):
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
)
|
|
req = OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.deploy",
|
|
"target": "snappymail",
|
|
}
|
|
)
|
|
resp = router.dispatch(req, viewer_principal)
|
|
assert resp.success is False
|
|
assert resp.error is not None
|
|
assert resp.error.code == ErrorCode.ERR_PERMISSION_DENIED
|
|
|
|
|
|
def test_operational_router_rejects_non_existent_target(
|
|
tmp_path: Path, admin_principal: Principal
|
|
):
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
)
|
|
req = OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.get",
|
|
"target": "nonexistent_workload_999",
|
|
}
|
|
)
|
|
resp = router.dispatch(req, admin_principal)
|
|
assert resp.success is False
|
|
assert resp.error is not None
|
|
assert resp.error.code == ErrorCode.ERR_TARGET_NOT_FOUND
|
|
|
|
|
|
def test_operational_router_rejects_concurrent_mutation(
|
|
tmp_path: Path, admin_principal: Principal, mock_workload_dir: Path
|
|
):
|
|
lock_mgr = TargetLockManager(tmp_path / "locks")
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=lock_mgr,
|
|
)
|
|
req = OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.deploy",
|
|
"target": "snappymail",
|
|
}
|
|
)
|
|
|
|
mock_app = MagicMock()
|
|
mock_app.name = "snappymail"
|
|
mock_app.dir_path = str(mock_workload_dir)
|
|
|
|
# Hold the lock on snappymail
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
lock_mgr.lock_target("snappymail"),
|
|
):
|
|
resp = router.dispatch(req, admin_principal)
|
|
assert resp.success is False
|
|
assert resp.error is not None
|
|
assert resp.error.code == ErrorCode.ERR_CONCURRENT_MUTATION
|
|
|
|
|
|
def test_operational_router_dispatches_workload_deploy(
|
|
tmp_path: Path, admin_principal: Principal, mock_workload_dir: Path
|
|
):
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_workload_dir.parent,
|
|
)
|
|
req = OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.deploy",
|
|
"target": "snappymail",
|
|
"parameters": {"recreate": True, "remove_orphans": True, "build": False},
|
|
}
|
|
)
|
|
|
|
mock_app = MagicMock()
|
|
mock_app.name = "snappymail"
|
|
mock_app.dir_path = str(mock_workload_dir)
|
|
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch(
|
|
"appctl_router.run_bounded_subprocess",
|
|
return_value=(0, "Container started", ""),
|
|
) as mock_run,
|
|
patch("appctl_router.cmd_sync_homepage"),
|
|
):
|
|
resp = router.dispatch(req, admin_principal)
|
|
assert resp.success is True
|
|
assert resp.result is not None
|
|
assert resp.result["action"] == "deployed"
|
|
mock_run.assert_called_once()
|
|
assert "up" in mock_run.call_args[0][0]
|
|
|
|
|
|
def test_operational_router_workload_deploy_build_flags(
|
|
tmp_path: Path, admin_principal: Principal, mock_workload_dir: Path
|
|
):
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_workload_dir.parent,
|
|
)
|
|
mock_app = MagicMock()
|
|
mock_app.name = "snappymail"
|
|
mock_app.dir_path = str(mock_workload_dir)
|
|
|
|
# 1. Deploy with build=True passes --pull
|
|
req_build = OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.deploy",
|
|
"target": "snappymail",
|
|
"parameters": {"build": True},
|
|
}
|
|
)
|
|
executed_commands: list[list[str]] = []
|
|
|
|
def mock_subproc(cmd: list[str], **kwargs):
|
|
executed_commands.append(cmd)
|
|
return 0, "ok", ""
|
|
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
|
patch("appctl_router.cmd_sync_homepage"),
|
|
):
|
|
resp = router.dispatch(req_build, admin_principal)
|
|
assert resp.success is True
|
|
build_calls = [c for c in executed_commands if "build" in c]
|
|
assert len(build_calls) == 1
|
|
assert build_calls[0] == [
|
|
"docker",
|
|
"compose",
|
|
"-f",
|
|
str(mock_workload_dir / "docker-compose.yml"),
|
|
"build",
|
|
"--pull",
|
|
]
|
|
|
|
# 2. Deploy with no_cache=True passes --pull and --no-cache
|
|
req_no_cache = OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.deploy",
|
|
"target": "snappymail",
|
|
"parameters": {"no_cache": True},
|
|
}
|
|
)
|
|
executed_commands.clear()
|
|
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
|
patch("appctl_router.cmd_sync_homepage"),
|
|
):
|
|
resp = router.dispatch(req_no_cache, admin_principal)
|
|
assert resp.success is True
|
|
build_calls = [c for c in executed_commands if "build" in c]
|
|
assert len(build_calls) == 1
|
|
assert build_calls[0] == [
|
|
"docker",
|
|
"compose",
|
|
"-f",
|
|
str(mock_workload_dir / "docker-compose.yml"),
|
|
"build",
|
|
"--pull",
|
|
"--no-cache",
|
|
]
|
|
|
|
|
|
def test_operational_router_workload_update_rejects_dirty_git_tree(
|
|
tmp_path: Path, admin_principal: Principal, mock_workload_dir: Path
|
|
):
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_workload_dir.parent,
|
|
)
|
|
req = OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.update",
|
|
"target": "snappymail",
|
|
}
|
|
)
|
|
|
|
mock_app = MagicMock()
|
|
mock_app.name = "snappymail"
|
|
mock_app.dir_path = str(mock_workload_dir)
|
|
|
|
# Simulate dirty working tree from git status --porcelain
|
|
def mock_subproc(cmd: list[str], **kwargs):
|
|
if "status" in cmd:
|
|
return 0, " M app.yaml\n", ""
|
|
return 0, "", ""
|
|
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
|
):
|
|
resp = router.dispatch(req, admin_principal)
|
|
assert resp.success is False
|
|
assert resp.error is not None
|
|
assert resp.error.code == ErrorCode.ERR_VALIDATION_FAILED
|
|
assert "uncommitted changes" in resp.error.message
|
|
|
|
|
|
def test_operational_router_workload_update_rejects_branch_mismatch(
|
|
tmp_path: Path, admin_principal: Principal, mock_workload_dir: Path
|
|
):
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_workload_dir.parent,
|
|
)
|
|
req = OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.update",
|
|
"target": "snappymail",
|
|
"parameters": {"expected_branch": "feature/branch"},
|
|
}
|
|
)
|
|
|
|
mock_app = MagicMock()
|
|
mock_app.name = "snappymail"
|
|
mock_app.dir_path = str(mock_workload_dir)
|
|
|
|
def mock_subproc(cmd: list[str], **kwargs):
|
|
if "status" in cmd:
|
|
return 0, "", ""
|
|
if "pull" in cmd:
|
|
return 0, "Fast-forward", ""
|
|
if "rev-parse" in cmd:
|
|
return 0, "1234567", ""
|
|
return 0, "", ""
|
|
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
|
):
|
|
resp = router.dispatch(req, admin_principal)
|
|
assert resp.success is False
|
|
assert resp.error is not None
|
|
assert resp.error.code == ErrorCode.ERR_VALIDATION_FAILED
|
|
assert "Post-pull branch mismatch" in resp.error.message
|
|
|
|
|
|
def test_operational_router_workload_update_succeeds_on_clean_tree(
|
|
tmp_path: Path, admin_principal: Principal, mock_workload_dir: Path
|
|
):
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_workload_dir.parent,
|
|
)
|
|
req = OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.update",
|
|
"target": "snappymail",
|
|
"parameters": {"expected_branch": "main"},
|
|
}
|
|
)
|
|
|
|
mock_app = MagicMock()
|
|
mock_app.name = "snappymail"
|
|
mock_app.dir_path = str(mock_workload_dir)
|
|
|
|
executed_commands: list[list[str]] = []
|
|
|
|
def mock_subproc(cmd: list[str], **kwargs):
|
|
executed_commands.append(cmd)
|
|
if "status" in cmd:
|
|
return 0, "", ""
|
|
if "pull" in cmd:
|
|
return 0, "Updating 1111..2222\nFast-forward", ""
|
|
if "rev-parse" in cmd:
|
|
return 0, "a1b2c3d", ""
|
|
return 0, "success", ""
|
|
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
|
patch("appctl_router.cmd_sync_homepage"),
|
|
):
|
|
resp = router.dispatch(req, admin_principal)
|
|
assert resp.success is True
|
|
assert resp.result is not None
|
|
assert resp.result["commit_sha"] == "a1b2c3d"
|
|
# Verify git pull was called with expected branch
|
|
pull_calls = [c for c in executed_commands if "pull" in c]
|
|
assert len(pull_calls) == 2 # git pull and docker compose pull
|
|
|
|
|
|
def test_operational_router_workload_update_build_and_no_cache_flags(
|
|
tmp_path: Path, admin_principal: Principal, mock_workload_dir: Path
|
|
):
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_workload_dir.parent,
|
|
)
|
|
mock_app = MagicMock()
|
|
mock_app.name = "snappymail"
|
|
mock_app.dir_path = str(mock_workload_dir)
|
|
|
|
executed_commands: list[list[str]] = []
|
|
|
|
def mock_subproc(cmd: list[str], **kwargs):
|
|
executed_commands.append(cmd)
|
|
if "status" in cmd:
|
|
return 0, "", ""
|
|
if "pull" in cmd and "git" in cmd:
|
|
return 0, "Updating 1111..2222\nFast-forward", ""
|
|
if "rev-parse" in cmd:
|
|
return 0, "a1b2c3d", ""
|
|
return 0, "success", ""
|
|
|
|
# 1. Update with build=True
|
|
req_build = OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.update",
|
|
"target": "snappymail",
|
|
"parameters": {"build": True},
|
|
}
|
|
)
|
|
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
|
patch("appctl_router.cmd_sync_homepage"),
|
|
):
|
|
resp = router.dispatch(req_build, admin_principal)
|
|
assert resp.success is True
|
|
build_calls = [c for c in executed_commands if "build" in c]
|
|
assert len(build_calls) == 1
|
|
assert build_calls[0] == [
|
|
"docker",
|
|
"compose",
|
|
"-f",
|
|
str(mock_workload_dir / "docker-compose.yml"),
|
|
"build",
|
|
"--pull",
|
|
]
|
|
# Also ensure compose_up was executed
|
|
up_calls = [c for c in executed_commands if "up" in c]
|
|
assert len(up_calls) == 1
|
|
|
|
# 2. Update with no_cache=True implies build=True and appends --no-cache
|
|
req_no_cache = OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.update",
|
|
"target": "snappymail",
|
|
"parameters": {"no_cache": True},
|
|
}
|
|
)
|
|
executed_commands.clear()
|
|
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
|
patch("appctl_router.cmd_sync_homepage"),
|
|
):
|
|
resp = router.dispatch(req_no_cache, admin_principal)
|
|
assert resp.success is True
|
|
build_calls = [c for c in executed_commands if "build" in c]
|
|
assert len(build_calls) == 1
|
|
assert build_calls[0] == [
|
|
"docker",
|
|
"compose",
|
|
"-f",
|
|
str(mock_workload_dir / "docker-compose.yml"),
|
|
"build",
|
|
"--pull",
|
|
"--no-cache",
|
|
]
|
|
up_calls = [c for c in executed_commands if "up" in c]
|
|
assert len(up_calls) == 1
|
|
|
|
|
|
def test_operational_router_workload_update_declared_compose_build(
|
|
tmp_path: Path, admin_principal: Principal, mock_workload_dir: Path
|
|
):
|
|
# Rewrite app.yaml with declared compose_build action
|
|
manifest_content = """
|
|
schema_version: "1.0"
|
|
name: "snappymail"
|
|
source:
|
|
type: "git"
|
|
url: "https://github.com/example/snappymail.git"
|
|
ref: "main"
|
|
deployment:
|
|
strategy: "compose"
|
|
actions:
|
|
- "git_pull"
|
|
- "compose_build"
|
|
- "compose_up"
|
|
"""
|
|
(mock_workload_dir / "app.yaml").write_text(
|
|
manifest_content.strip(), encoding="utf-8"
|
|
)
|
|
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_workload_dir.parent,
|
|
)
|
|
mock_app = MagicMock()
|
|
mock_app.name = "snappymail"
|
|
mock_app.dir_path = str(mock_workload_dir)
|
|
|
|
executed_commands: list[list[str]] = []
|
|
|
|
def mock_subproc(cmd: list[str], **kwargs):
|
|
executed_commands.append(cmd)
|
|
if "status" in cmd:
|
|
return 0, "", ""
|
|
if "pull" in cmd and "git" in cmd:
|
|
return 0, "Updating 1111..2222\nFast-forward", ""
|
|
if "rev-parse" in cmd:
|
|
return 0, "a1b2c3d", ""
|
|
return 0, "success", ""
|
|
|
|
# Update with declared compose_build invokes build --pull
|
|
req = OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.update",
|
|
"target": "snappymail",
|
|
}
|
|
)
|
|
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
|
patch("appctl_router.cmd_sync_homepage"),
|
|
):
|
|
resp = router.dispatch(req, admin_principal)
|
|
assert resp.success is True
|
|
build_calls = [c for c in executed_commands if "build" in c]
|
|
assert len(build_calls) == 1
|
|
assert build_calls[0] == [
|
|
"docker",
|
|
"compose",
|
|
"-f",
|
|
str(mock_workload_dir / "docker-compose.yml"),
|
|
"build",
|
|
"--pull",
|
|
]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Core Platform Stack & Core Service Dispatch (Issue #2)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_operational_router_dispatches_core_stack_operations(
|
|
tmp_path: Path,
|
|
admin_principal: Principal,
|
|
mock_sites_dir: Path,
|
|
mock_core_dir: Path,
|
|
):
|
|
compose_content = """
|
|
services:
|
|
traefik:
|
|
image: "traefik:v3.6"
|
|
container_name: "traefik"
|
|
authelia:
|
|
image: "authelia/authelia:latest"
|
|
container_name: "authelia"
|
|
"""
|
|
(mock_core_dir / "docker-compose.yml").write_text(
|
|
compose_content.strip(), encoding="utf-8"
|
|
)
|
|
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_sites_dir,
|
|
core_dir=mock_core_dir,
|
|
)
|
|
|
|
executed_cmds: list[list[str]] = []
|
|
|
|
def mock_subproc(cmd: list[str], **kwargs):
|
|
executed_cmds.append(cmd)
|
|
return 0, "ok", ""
|
|
|
|
with (
|
|
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
|
patch("appctl_router.cmd_sync_homepage") as mock_sync,
|
|
):
|
|
# 1. Deploy core stack
|
|
resp_deploy = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{"operation": "workload.deploy", "target": "core"}
|
|
),
|
|
admin_principal,
|
|
)
|
|
assert resp_deploy.success is True
|
|
assert executed_cmds[-1] == [
|
|
"docker",
|
|
"compose",
|
|
"-f",
|
|
str(mock_core_dir / "docker-compose.yml"),
|
|
"up",
|
|
"-d",
|
|
"--remove-orphans",
|
|
]
|
|
|
|
# 2. Stop core stack (uses `down` for full stack)
|
|
resp_stop = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{"operation": "workload.stop", "target": "core"}
|
|
),
|
|
admin_principal,
|
|
)
|
|
assert resp_stop.success is True
|
|
assert executed_cmds[-1] == [
|
|
"docker",
|
|
"compose",
|
|
"-f",
|
|
str(mock_core_dir / "docker-compose.yml"),
|
|
"down",
|
|
]
|
|
|
|
# 3. Restart core stack
|
|
resp_restart = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{"operation": "workload.restart", "target": "core"}
|
|
),
|
|
admin_principal,
|
|
)
|
|
assert resp_restart.success is True
|
|
assert executed_cmds[-1] == [
|
|
"docker",
|
|
"compose",
|
|
"-f",
|
|
str(mock_core_dir / "docker-compose.yml"),
|
|
"restart",
|
|
]
|
|
|
|
# 4. Config core stack
|
|
resp_config = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{"operation": "workload.config", "target": "core"}
|
|
),
|
|
admin_principal,
|
|
)
|
|
assert resp_config.success is True
|
|
assert executed_cmds[-1] == [
|
|
"docker",
|
|
"compose",
|
|
"-f",
|
|
str(mock_core_dir / "docker-compose.yml"),
|
|
"config",
|
|
]
|
|
|
|
# Core operations do not trigger Homepage Sites sync
|
|
mock_sync.assert_not_called()
|
|
|
|
|
|
def test_operational_router_dispatches_core_service_container_scoped_operations(
|
|
tmp_path: Path,
|
|
admin_principal: Principal,
|
|
mock_sites_dir: Path,
|
|
mock_core_dir: Path,
|
|
):
|
|
(mock_core_dir / "docker-compose.yml").write_text(
|
|
"services:\n traefik:\n image: traefik:v3.6\n", encoding="utf-8"
|
|
)
|
|
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_sites_dir,
|
|
core_dir=mock_core_dir,
|
|
)
|
|
|
|
executed_cmds: list[list[str]] = []
|
|
|
|
def mock_subproc(cmd: list[str], **kwargs):
|
|
executed_cmds.append(cmd)
|
|
return 0, "ok", ""
|
|
|
|
with patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc):
|
|
# 1. Deploy single core service (traefik)
|
|
resp_deploy = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{"operation": "workload.deploy", "target": "traefik"}
|
|
),
|
|
admin_principal,
|
|
)
|
|
assert resp_deploy.success is True
|
|
assert executed_cmds[-1][-1] == "traefik"
|
|
assert "up" in executed_cmds[-1]
|
|
|
|
# 2. Stop single core service (uses `stop traefik`, NOT `down`)
|
|
resp_stop = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{"operation": "workload.stop", "target": "traefik"}
|
|
),
|
|
admin_principal,
|
|
)
|
|
assert resp_stop.success is True
|
|
assert executed_cmds[-1] == [
|
|
"docker",
|
|
"compose",
|
|
"-f",
|
|
str(mock_core_dir / "docker-compose.yml"),
|
|
"stop",
|
|
"traefik",
|
|
]
|
|
|
|
# 3. Restart single core service
|
|
resp_restart = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{"operation": "workload.restart", "target": "traefik"}
|
|
),
|
|
admin_principal,
|
|
)
|
|
assert resp_restart.success is True
|
|
assert executed_cmds[-1] == [
|
|
"docker",
|
|
"compose",
|
|
"-f",
|
|
str(mock_core_dir / "docker-compose.yml"),
|
|
"restart",
|
|
"traefik",
|
|
]
|
|
|
|
# 4. Pull single core service
|
|
resp_pull = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{"operation": "workload.pull", "target": "traefik"}
|
|
),
|
|
admin_principal,
|
|
)
|
|
assert resp_pull.success is True
|
|
assert executed_cmds[-2] == [
|
|
"docker",
|
|
"compose",
|
|
"-f",
|
|
str(mock_core_dir / "docker-compose.yml"),
|
|
"pull",
|
|
"traefik",
|
|
]
|
|
assert executed_cmds[-1] == [
|
|
"docker",
|
|
"compose",
|
|
"-f",
|
|
str(mock_core_dir / "docker-compose.yml"),
|
|
"up",
|
|
"-d",
|
|
"--remove-orphans",
|
|
"traefik",
|
|
]
|
|
|
|
# 5. Logs single core service
|
|
resp_logs = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{"operation": "workload.logs", "target": "traefik"}
|
|
),
|
|
admin_principal,
|
|
)
|
|
assert resp_logs.success is True
|
|
assert executed_cmds[-1][-1] == "traefik"
|
|
|
|
|
|
def test_operational_router_dispatches_core_update_without_manifest(
|
|
tmp_path: Path,
|
|
admin_principal: Principal,
|
|
mock_sites_dir: Path,
|
|
mock_core_dir: Path,
|
|
):
|
|
(mock_core_dir / "docker-compose.yml").write_text(
|
|
"services:\n traefik:\n image: traefik:v3.6\n", encoding="utf-8"
|
|
)
|
|
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_sites_dir,
|
|
core_dir=mock_core_dir,
|
|
)
|
|
|
|
executed_cmds: list[list[str]] = []
|
|
|
|
def mock_subproc(cmd: list[str], **kwargs):
|
|
executed_cmds.append(cmd)
|
|
if "status" in cmd:
|
|
return 0, "", ""
|
|
if "rev-parse" in cmd:
|
|
return 0, "c0ffee1", ""
|
|
return 0, "ok", ""
|
|
|
|
with patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc):
|
|
resp = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{"operation": "workload.update", "target": "core"}
|
|
),
|
|
admin_principal,
|
|
)
|
|
assert resp.success is True
|
|
assert resp.result is not None
|
|
assert resp.result["commit_sha"] == "c0ffee1"
|
|
assert resp.result["actions"] == ["compose_pull", "compose_up"]
|
|
|
|
|
|
def test_operational_router_rejects_operator_role_on_core_targets(
|
|
tmp_path: Path,
|
|
operator_principal: Principal,
|
|
mock_sites_dir: Path,
|
|
mock_core_dir: Path,
|
|
):
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_sites_dir,
|
|
core_dir=mock_core_dir,
|
|
)
|
|
for target in ("core", "traefik", "authelia"):
|
|
resp = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{"operation": "workload.restart", "target": target}
|
|
),
|
|
operator_principal,
|
|
)
|
|
assert resp.success is False
|
|
assert resp.error is not None
|
|
assert resp.error.code == ErrorCode.ERR_PERMISSION_DENIED
|
|
|
|
|
|
def test_operational_router_workload_list_honors_filter_params(
|
|
tmp_path: Path,
|
|
viewer_principal: Principal,
|
|
mock_workload_dir: Path,
|
|
mock_core_dir: Path,
|
|
):
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_workload_dir.parent,
|
|
core_dir=mock_core_dir,
|
|
)
|
|
|
|
# Without --core: only Sites workloads (1)
|
|
resp_sites = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.list",
|
|
"parameters": {"core": False, "fetch": False, "ssl": False},
|
|
}
|
|
),
|
|
viewer_principal,
|
|
)
|
|
assert resp_sites.success is True
|
|
assert resp_sites.result is not None
|
|
assert resp_sites.result["total"] == 1
|
|
assert resp_sites.result["workloads"][0]["name"] == "snappymail"
|
|
|
|
# With --core and --ssl: includes Core services and SSL inspection
|
|
with patch(
|
|
"appctl_router.check_ssl_cert",
|
|
return_value={"status": "🟢 80d remaining", "domain": "example.test"},
|
|
) as mock_ssl:
|
|
resp_core = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.list",
|
|
"parameters": {"core": True, "fetch": False, "ssl": True},
|
|
}
|
|
),
|
|
viewer_principal,
|
|
)
|
|
assert resp_core.success is True
|
|
assert resp_core.result is not None
|
|
names = {w["name"] for w in resp_core.result["workloads"]}
|
|
assert "snappymail" in names
|
|
assert "traefik" in names
|
|
assert "authelia" in names
|
|
assert mock_ssl.call_count >= 1
|
|
|
|
|
|
def test_operational_router_workload_list_includes_live_docker_and_two_axis_status(
|
|
tmp_path: Path,
|
|
viewer_principal: Principal,
|
|
mock_workload_dir: Path,
|
|
mock_core_dir: Path,
|
|
):
|
|
"""Ensure workload.list always inspects docker status and computes two-axis status for apps and core."""
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_workload_dir.parent,
|
|
core_dir=mock_core_dir,
|
|
)
|
|
|
|
with patch(
|
|
"appctl_router.get_docker_status", return_value="🟢 Running (1)"
|
|
) as mock_docker:
|
|
# 1. Sites workload offline (fetch=False)
|
|
resp = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.list",
|
|
"parameters": {"core": False, "fetch": False, "ssl": False},
|
|
}
|
|
),
|
|
viewer_principal,
|
|
)
|
|
assert resp.success is True
|
|
assert resp.result is not None
|
|
workload = resp.result["workloads"][0]
|
|
assert workload["name"] == "snappymail"
|
|
assert workload["status"] == "🟢 Running (1)"
|
|
assert "deployment_status" in workload
|
|
assert "provenance_status" in workload
|
|
assert workload["provenance_status"] == "— Not Checked"
|
|
mock_docker.assert_called_with(dir_path=mock_workload_dir)
|
|
|
|
# 2. Core services included (core=True)
|
|
resp_core = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.list",
|
|
"parameters": {"core": True, "fetch": False, "ssl": False},
|
|
}
|
|
),
|
|
viewer_principal,
|
|
)
|
|
assert resp_core.success is True
|
|
assert resp_core.result is not None
|
|
core_workloads = {w["name"]: w for w in resp_core.result["workloads"]}
|
|
assert "traefik" in core_workloads
|
|
traefik = core_workloads["traefik"]
|
|
assert traefik["status"] == "🟢 Running (1)"
|
|
assert traefik["provenance_status"] == "— Core Platform"
|
|
assert "deployment_status" in traefik
|
|
|
|
|
|
def test_operational_router_workload_list_fetch_passes_fetch_upstream(
|
|
tmp_path: Path,
|
|
viewer_principal: Principal,
|
|
mock_workload_dir: Path,
|
|
mock_core_dir: Path,
|
|
):
|
|
"""Ensure workload.list with fetch=True calls inspect_workload_two_axis with fetch_upstream=True."""
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_workload_dir.parent,
|
|
core_dir=mock_core_dir,
|
|
)
|
|
|
|
with (
|
|
patch("appctl_router.get_docker_status", return_value="🟢 Running (1)"),
|
|
patch("appctl_router.inspect_workload_two_axis") as mock_inspect,
|
|
):
|
|
mock_two_axis = MagicMock()
|
|
mock_two_axis.checkout.status_label = "✓ Synced (main)"
|
|
mock_two_axis.provenance.status.value = "✓ Synced"
|
|
mock_inspect.return_value = mock_two_axis
|
|
|
|
resp_fetch = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.list",
|
|
"parameters": {"core": False, "fetch": True, "ssl": False},
|
|
}
|
|
),
|
|
viewer_principal,
|
|
)
|
|
assert resp_fetch.success is True
|
|
assert resp_fetch.result is not None
|
|
mock_inspect.assert_called_once()
|
|
_, kwargs = mock_inspect.call_args
|
|
assert kwargs.get("fetch_upstream") is True
|
|
w = resp_fetch.result["workloads"][0]
|
|
assert w["deployment_status"] == "✓ Synced (main)"
|
|
assert w["provenance_status"] == "✓ Synced"
|
|
|
|
|
|
def test_operational_router_workload_list_handles_manifest_error(
|
|
tmp_path: Path,
|
|
viewer_principal: Principal,
|
|
mock_sites_dir: Path,
|
|
mock_core_dir: Path,
|
|
):
|
|
"""Ensure workload.list sets deployment_status and provenance_status correctly for apps with manifest errors."""
|
|
broken_app_dir = mock_sites_dir / "broken-app"
|
|
broken_app_dir.mkdir(parents=True, exist_ok=True)
|
|
(broken_app_dir / "app.yaml").write_text(
|
|
"invalid_yaml: [unclosed list", encoding="utf-8"
|
|
)
|
|
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_sites_dir,
|
|
core_dir=mock_core_dir,
|
|
)
|
|
|
|
with patch("appctl_router.get_docker_status", return_value="🔴 Stopped"):
|
|
resp = router.dispatch(
|
|
OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.list",
|
|
"parameters": {"core": False, "fetch": False, "ssl": False},
|
|
}
|
|
),
|
|
viewer_principal,
|
|
)
|
|
assert resp.success is True
|
|
assert resp.result is not None
|
|
apps = {w["name"]: w for w in resp.result["workloads"]}
|
|
assert "broken-app" in apps
|
|
broken = apps["broken-app"]
|
|
assert broken["manifest_error"] is not None
|
|
assert broken["deployment_status"] == "⚠️ Manifest Error"
|
|
assert broken["provenance_status"] == "⚠️ Invalid Manifest"
|
|
|
|
|
|
def test_operational_router_workload_config_redacts_evaluated_secrets_for_operator(
|
|
tmp_path: Path,
|
|
mock_sites_dir: Path,
|
|
mock_core_dir: Path,
|
|
):
|
|
"""Ensure workload.config redacts evaluated secrets in YAML output for non-admin operators (CR-DI-03)."""
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_sites_dir,
|
|
core_dir=mock_core_dir,
|
|
)
|
|
workload_dir = mock_sites_dir / "docs"
|
|
workload_dir.mkdir(parents=True, exist_ok=True)
|
|
(workload_dir / "docker-compose.yml").write_text(
|
|
"services:\n docs:\n image: docs:latest\n"
|
|
)
|
|
(workload_dir / "app.yaml").write_text("name: docs\n")
|
|
|
|
operator_principal = Principal(
|
|
identity="alice", role=Role.OPERATOR, auth_method="bearer_token"
|
|
)
|
|
admin_principal = Principal(
|
|
identity="admin", role=Role.ADMIN, auth_method="bearer_token"
|
|
)
|
|
|
|
raw_yaml_stdout = """services:
|
|
docs:
|
|
environment:
|
|
DB_PASSWORD: supersecretpassword123
|
|
API_TOKEN: tok_secretabc
|
|
PUBLIC_SETTING: allowed_value
|
|
image: docs:latest
|
|
"""
|
|
|
|
mock_app = MagicMock(name="docs")
|
|
mock_app.name = "docs"
|
|
mock_app.dir_path = workload_dir
|
|
mock_app.is_core = False
|
|
|
|
req = OperationRequest.model_validate(
|
|
{"operation": "workload.config", "target": "docs"}
|
|
)
|
|
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch("appctl_router.load_admitted_workloads", return_value={"docs"}),
|
|
patch(
|
|
"appctl_router.run_bounded_subprocess",
|
|
return_value=(0, raw_yaml_stdout, ""),
|
|
),
|
|
):
|
|
# 1. Non-admin operator receives redacted output
|
|
resp_op = router.dispatch(req, operator_principal)
|
|
assert resp_op.success is True
|
|
assert resp_op.result is not None
|
|
output_yaml = resp_op.result["output"]
|
|
assert "supersecretpassword123" not in output_yaml
|
|
assert "tok_secretabc" not in output_yaml
|
|
assert "allowed_value" in output_yaml
|
|
parsed_op = yaml.safe_load(output_yaml)
|
|
env = parsed_op["services"]["docs"]["environment"]
|
|
assert env["DB_PASSWORD"] == "[REDACTED]"
|
|
assert env["API_TOKEN"] == "[REDACTED]"
|
|
assert env["PUBLIC_SETTING"] == "allowed_value"
|
|
|
|
# 2. Admin receives plaintext unredacted output
|
|
resp_admin = router.dispatch(req, admin_principal)
|
|
assert resp_admin.success is True
|
|
assert resp_admin.result is not None
|
|
assert "supersecretpassword123" in resp_admin.result["output"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("exit_code", "stderr_msg"),
|
|
[
|
|
(1, "Error: container failed to start: port in use"),
|
|
(2, "Error response from daemon: network homelab_edge not found"),
|
|
],
|
|
)
|
|
def test_operational_router_workload_deploy_propagates_non_zero_exit_and_stderr(
|
|
tmp_path: Path,
|
|
admin_principal: Principal,
|
|
mock_workload_dir: Path,
|
|
exit_code: int,
|
|
stderr_msg: str,
|
|
):
|
|
audit_file = tmp_path / "audit.jsonl"
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(audit_file),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_workload_dir.parent,
|
|
)
|
|
req = OperationRequest.model_validate({
|
|
"operation": "workload.deploy",
|
|
"target": "snappymail",
|
|
})
|
|
|
|
mock_app = MagicMock()
|
|
mock_app.name = "snappymail"
|
|
mock_app.dir_path = str(mock_workload_dir)
|
|
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch("appctl_router.run_bounded_subprocess", return_value=(exit_code, "", stderr_msg)),
|
|
patch("appctl_router.cmd_sync_homepage"),
|
|
):
|
|
resp = router.dispatch(req, admin_principal)
|
|
|
|
assert resp.success is False
|
|
assert resp.error is not None
|
|
assert resp.error.code == ErrorCode.ERR_EXECUTION_FAILED
|
|
assert stderr_msg in resp.error.message
|
|
|
|
audit_lines = [json.loads(line) for line in audit_file.read_text(encoding="utf-8").splitlines() if line.strip()]
|
|
failed_events = [ev for ev in audit_lines if ev.get("phase") == "FAILED"]
|
|
assert len(failed_events) >= 1
|
|
failed_event = failed_events[-1]
|
|
assert failed_event["target"] == "snappymail"
|
|
assert failed_event["result"]["exit_code"] == exit_code
|
|
assert failed_event["result"]["error_code"] == ErrorCode.ERR_EXECUTION_FAILED.value
|
|
assert stderr_msg in failed_event["result"]["message"]
|
|
|
|
|
|
def test_operational_router_workload_update_propagates_non_zero_exit_and_stderr(
|
|
tmp_path: Path,
|
|
operator_principal: Principal,
|
|
mock_workload_dir: Path,
|
|
):
|
|
audit_file = tmp_path / "audit.jsonl"
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(audit_file),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_workload_dir.parent,
|
|
)
|
|
req = OperationRequest.model_validate({
|
|
"operation": "workload.update",
|
|
"target": "snappymail",
|
|
"parameters": {"expected_branch": "main"},
|
|
})
|
|
|
|
mock_app = MagicMock()
|
|
mock_app.name = "snappymail"
|
|
mock_app.dir_path = str(mock_workload_dir)
|
|
|
|
def mock_subproc(cmd: list[str], **kwargs):
|
|
if "status" in cmd:
|
|
return (0, "", "")
|
|
if "pull" in cmd:
|
|
return (2, "", "fatal: unable to access repository: connection refused")
|
|
return (0, "", "")
|
|
|
|
with (
|
|
patch("appctl_router.load_admitted_workloads", return_value={"snappymail"}),
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
|
patch("appctl_router.cmd_sync_homepage"),
|
|
):
|
|
resp = router.dispatch(req, operator_principal)
|
|
|
|
assert resp.success is False
|
|
assert resp.error is not None
|
|
assert resp.error.code == ErrorCode.ERR_EXECUTION_FAILED
|
|
assert "fatal: unable to access repository: connection refused" in resp.error.message
|
|
|
|
audit_lines = [json.loads(line) for line in audit_file.read_text(encoding="utf-8").splitlines() if line.strip()]
|
|
failed_events = [ev for ev in audit_lines if ev.get("phase") == "FAILED"]
|
|
assert len(failed_events) >= 1
|
|
failed_event = failed_events[-1]
|
|
assert failed_event["result"]["exit_code"] == 2
|
|
assert "fatal: unable to access repository: connection refused" in failed_event["result"]["message"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_dotenv_rejects_reserved_platform_keys(tmp_path: Path):
|
|
"""Ensure parse_dotenv rejects platform routing and docker config variables in .env (CR-DI-07)."""
|
|
env_file = tmp_path / ".env"
|
|
for reserved_key in [
|
|
"HOMELAB_DOMAIN",
|
|
"DOMAIN_SUFFIX",
|
|
"DOMAIN",
|
|
"PROXY_NETWORK",
|
|
"CERT_RESOLVER",
|
|
"DOCKER_CONFIG",
|
|
]:
|
|
env_file.write_text(f"{reserved_key}=override_val\n", encoding="utf-8")
|
|
with pytest.raises(
|
|
RouterValidationError, match="Prohibited reserved environment key"
|
|
):
|
|
parse_dotenv(env_file, validate_keys=True)
|
|
|
|
|
|
def test_operational_router_workload_update_succeeds_when_source_ref_is_tag_and_deployment_branch_matches(
|
|
tmp_path: Path, admin_principal: Principal, mock_sites_dir: Path
|
|
):
|
|
"""Ensure workload.update validates deployment.branch (Tier 2) and does not conflate with source.ref (Tier 1) (CR-SS-01)."""
|
|
app_dir = mock_sites_dir / "bitetrack"
|
|
app_dir.mkdir(parents=True, exist_ok=True)
|
|
manifest_content = """
|
|
name: "bitetrack"
|
|
source:
|
|
type: "git"
|
|
url: "https://github.com/example/bitetrack.git"
|
|
ref: "v2.5.0"
|
|
deployment:
|
|
strategy: "compose"
|
|
branch: "main"
|
|
actions:
|
|
- "compose_up"
|
|
"""
|
|
(app_dir / "app.yaml").write_text(manifest_content.strip(), encoding="utf-8")
|
|
(app_dir / "docker-compose.yml").write_text("services:\n app:\n image: app:latest\n", encoding="utf-8")
|
|
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_sites_dir,
|
|
)
|
|
req = OperationRequest.model_validate({
|
|
"operation": "workload.update",
|
|
"target": "bitetrack",
|
|
"parameters": {"expected_branch": "main"},
|
|
})
|
|
|
|
mock_app = MagicMock()
|
|
mock_app.name = "bitetrack"
|
|
mock_app.dir_path = str(app_dir)
|
|
|
|
executed_cmds: list[list[str]] = []
|
|
|
|
def mock_subproc(cmd: list[str], **kwargs):
|
|
executed_cmds.append(cmd)
|
|
if "status" in cmd:
|
|
return 0, "", ""
|
|
if "pull" in cmd and "git" in cmd:
|
|
return 0, "Fast-forward", ""
|
|
if "rev-parse" in cmd:
|
|
return 0, "1234567", ""
|
|
return 0, "ok", ""
|
|
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
|
patch("appctl_router.cmd_sync_homepage"),
|
|
):
|
|
resp = router.dispatch(req, admin_principal)
|
|
assert resp.success is True
|
|
assert resp.result is not None
|
|
pull_calls = [c for c in executed_cmds if "pull" in c and "git" in c]
|
|
assert len(pull_calls) == 1
|
|
assert pull_calls[0] == ["git", "-C", str(app_dir), "pull", "--ff-only", "origin", "--", "main"]
|
|
|
|
|
|
def test_operational_router_workload_update_rejects_deployment_branch_mismatch_image_only(
|
|
tmp_path: Path, admin_principal: Principal, mock_sites_dir: Path
|
|
):
|
|
"""Ensure workload.update validates deployment.branch for image-only workloads with source=None (CR-SS-01)."""
|
|
app_dir = mock_sites_dir / "imageonly"
|
|
app_dir.mkdir(parents=True, exist_ok=True)
|
|
manifest_content = """
|
|
name: "imageonly"
|
|
deployment:
|
|
strategy: "compose"
|
|
branch: "production"
|
|
actions:
|
|
- "compose_up"
|
|
"""
|
|
(app_dir / "app.yaml").write_text(manifest_content.strip(), encoding="utf-8")
|
|
(app_dir / "docker-compose.yml").write_text("services:\n app:\n image: app:latest\n", encoding="utf-8")
|
|
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_sites_dir,
|
|
)
|
|
req = OperationRequest.model_validate({
|
|
"operation": "workload.update",
|
|
"target": "imageonly",
|
|
"parameters": {"expected_branch": "main"},
|
|
})
|
|
|
|
mock_app = MagicMock()
|
|
mock_app.name = "imageonly"
|
|
mock_app.dir_path = str(app_dir)
|
|
|
|
def mock_subproc(cmd: list[str], **kwargs):
|
|
if "status" in cmd:
|
|
return 0, "", ""
|
|
if "pull" in cmd and "git" in cmd:
|
|
return 0, "Fast-forward", ""
|
|
if "rev-parse" in cmd:
|
|
return 0, "1234567", ""
|
|
return 0, "ok", ""
|
|
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
|
):
|
|
resp = router.dispatch(req, admin_principal)
|
|
assert resp.success is False
|
|
assert resp.error is not None
|
|
assert resp.error.code == ErrorCode.ERR_VALIDATION_FAILED
|
|
assert "Post-pull branch mismatch" in resp.error.message
|
|
assert "expected 'main'" in resp.error.message
|
|
assert "declared 'production'" in resp.error.message
|
|
|
|
|
|
def test_operational_router_catches_unhandled_runtime_exceptions_and_records_failed_audit(
|
|
tmp_path: Path, admin_principal: Principal, mock_sites_dir: Path
|
|
):
|
|
"""Ensure unexpected exceptions in router return 500 ERR_EXECUTION_FAILED and log FAILED audit (CR-SO-04)."""
|
|
audit_file = tmp_path / "audit.jsonl"
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(audit_file),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=mock_sites_dir,
|
|
)
|
|
req = OperationRequest.model_validate({
|
|
"operation": "platform.sync_homepage",
|
|
})
|
|
|
|
with patch("appctl_router.cmd_sync_homepage", side_effect=RuntimeError("Corrupt filesystem")):
|
|
resp = router.dispatch(req, admin_principal)
|
|
assert resp.success is False
|
|
assert resp.error is not None
|
|
assert resp.error.code == ErrorCode.ERR_EXECUTION_FAILED
|
|
assert "Unexpected execution failure" in resp.error.message
|
|
|
|
events = [json.loads(line) for line in audit_file.read_text().splitlines() if line.strip()]
|
|
assert len(events) == 2
|
|
assert events[0]["phase"] == "DISPATCHED"
|
|
assert events[1]["phase"] == "FAILED"
|
|
assert events[1]["result"]["error_code"] == ErrorCode.ERR_EXECUTION_FAILED.value
|
|
|
|
|
|
def test_trigger_homepage_sync_acquires_homepage_sync_lock(tmp_path: Path, mock_sites_dir: Path):
|
|
"""Ensure _trigger_homepage_sync executes under homepage_sync_lock (CR-DI-05)."""
|
|
lock_mgr = TargetLockManager(tmp_path / "locks")
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=lock_mgr,
|
|
sites_dir=mock_sites_dir,
|
|
)
|
|
lock_acquired = False
|
|
|
|
def mock_sync():
|
|
nonlocal lock_acquired
|
|
lock_file = lock_mgr.lock_dir / "_homepage_sync.lock"
|
|
assert lock_file.exists()
|
|
lock_acquired = True
|
|
|
|
with patch("appctl_router.cmd_sync_homepage", side_effect=mock_sync):
|
|
router._trigger_homepage_sync()
|
|
assert lock_acquired is True
|
|
|
|
|
|
def test_operational_router_workload_update_pulls_monorepo_when_parent_git_exists(
|
|
tmp_path: Path, admin_principal: Principal, mock_workload_dir: Path
|
|
):
|
|
"""Ensure workload.update targets parent monorepo .git when <app>/.git is absent (#33)."""
|
|
monorepo_root = mock_workload_dir.parent
|
|
(monorepo_root / ".git").mkdir()
|
|
|
|
router = OperationalRouter(
|
|
audit_logger=AuditLogger(tmp_path / "audit.jsonl"),
|
|
lock_manager=TargetLockManager(tmp_path / "locks"),
|
|
sites_dir=monorepo_root,
|
|
)
|
|
req = OperationRequest.model_validate(
|
|
{
|
|
"operation": "workload.update",
|
|
"target": "snappymail",
|
|
"parameters": {"expected_branch": "main"},
|
|
}
|
|
)
|
|
|
|
mock_app = MagicMock()
|
|
mock_app.name = "snappymail"
|
|
mock_app.dir_path = str(mock_workload_dir)
|
|
|
|
executed: list[tuple[list[str], Path | None]] = []
|
|
|
|
def mock_subproc(cmd: list[str], **kwargs):
|
|
executed.append((cmd, kwargs.get("cwd")))
|
|
if "status" in cmd:
|
|
return 0, "", ""
|
|
if "pull" in cmd:
|
|
return 0, "Updating 1111..2222\nFast-forward", ""
|
|
if "rev-parse" in cmd:
|
|
return 0, "beef123", ""
|
|
return 0, "success", ""
|
|
|
|
with (
|
|
patch("appctl_router.resolve_app", return_value=mock_app),
|
|
patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc),
|
|
patch("appctl_router.cmd_sync_homepage"),
|
|
):
|
|
resp = router.dispatch(req, admin_principal)
|
|
assert resp.success is True
|
|
assert resp.result is not None
|
|
assert resp.result["commit_sha"] == "beef123"
|
|
|
|
status_calls = [c for c, _ in executed if c[0] == "git" and "status" in c]
|
|
assert status_calls == [
|
|
["git", "-C", str(monorepo_root), "status", "--porcelain", "--", "snappymail/"]
|
|
]
|
|
git_pull_calls = [(c, cwd) for c, cwd in executed if c[0] == "git" and "pull" in c]
|
|
assert git_pull_calls == [
|
|
(["git", "-C", str(monorepo_root), "pull", "--ff-only", "origin", "--", "main"], monorepo_root)
|
|
]
|
|
rev_parse_calls = [c for c, _ in executed if c[0] == "git" and "rev-parse" in c]
|
|
assert rev_parse_calls == [
|
|
["git", "-C", str(monorepo_root), "rev-parse", "--short", "HEAD"]
|
|
]
|
|
# Compose commands still run in app_dir
|
|
compose_cwds = [cwd for c, cwd in executed if c[0] == "docker"]
|
|
assert compose_cwds and all(cwd == mock_workload_dir for cwd in compose_cwds)
|