Compare commits
4
Commits
main
...
af910c7d2e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af910c7d2e | ||
|
|
5164f4270c | ||
|
|
2be1514866 | ||
|
|
f3f769f302 |
No files matched your search
@@ -6,6 +6,7 @@ backup.zip
|
|||||||
.envrc
|
.envrc
|
||||||
.direnv
|
.direnv
|
||||||
.venv
|
.venv
|
||||||
|
.worktrees/
|
||||||
nixos/instance.nix
|
nixos/instance.nix
|
||||||
|
|
||||||
# SSL / ACME Certificates & Keys
|
# SSL / ACME Certificates & Keys
|
||||||
|
|||||||
@@ -18,9 +18,12 @@ Asserts:
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import concurrent.futures
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import socket
|
import socket
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
@@ -287,7 +290,7 @@ def test_auth_missing_token_fails_closed(daemon_env):
|
|||||||
"""
|
"""
|
||||||
deploy_req = OperationRequest(operation=OperationType.WORKLOAD_DEPLOY, target="docs")
|
deploy_req = OperationRequest(operation=OperationType.WORKLOAD_DEPLOY, target="docs")
|
||||||
|
|
||||||
with patch("subprocess.run") as mock_subproc:
|
with patch("appctl_router.run_bounded_subprocess") as mock_subproc:
|
||||||
# Over HTTP without token
|
# Over HTTP without token
|
||||||
resp_http = send_daemon_request(
|
resp_http = send_daemon_request(
|
||||||
endpoint=daemon_env.http_url,
|
endpoint=daemon_env.http_url,
|
||||||
@@ -401,7 +404,7 @@ def test_daemon_preflight_and_dotenv_safety(daemon_env):
|
|||||||
|
|
||||||
deploy_req = OperationRequest(operation=OperationType.WORKLOAD_DEPLOY, target="docs")
|
deploy_req = OperationRequest(operation=OperationType.WORKLOAD_DEPLOY, target="docs")
|
||||||
|
|
||||||
with patch("subprocess.run") as mock_subproc:
|
with patch("appctl_router.run_bounded_subprocess") as mock_subproc:
|
||||||
resp = send_daemon_request(
|
resp = send_daemon_request(
|
||||||
endpoint=daemon_env.socket_path,
|
endpoint=daemon_env.socket_path,
|
||||||
operation_request=deploy_req,
|
operation_request=deploy_req,
|
||||||
@@ -435,6 +438,55 @@ def test_concurrent_mutation_conflict(daemon_env):
|
|||||||
assert resp.error.code.http_status == 409
|
assert resp.error.code.http_status == 409
|
||||||
|
|
||||||
|
|
||||||
|
def test_daemon_multi_threaded_lock_contention_rejects_concurrent_mutation(daemon_env):
|
||||||
|
"""
|
||||||
|
Simultaneous mutating requests for the same target result in one 200 SUCCESS
|
||||||
|
and one 409 ERR_CONCURRENT_MUTATION via multi-threaded lock contention.
|
||||||
|
"""
|
||||||
|
deploy_req = OperationRequest(operation=OperationType.WORKLOAD_DEPLOY, target="docs")
|
||||||
|
first_started = threading.Event()
|
||||||
|
|
||||||
|
def slow_subproc(cmd, *args, **kwargs):
|
||||||
|
first_started.set()
|
||||||
|
time.sleep(0.2)
|
||||||
|
return (0, "Started", "")
|
||||||
|
|
||||||
|
def send_first():
|
||||||
|
return send_daemon_request(
|
||||||
|
endpoint=daemon_env.socket_path,
|
||||||
|
operation_request=deploy_req,
|
||||||
|
token=ADMIN_SECRET,
|
||||||
|
)
|
||||||
|
|
||||||
|
def send_second():
|
||||||
|
first_started.wait(timeout=2.0)
|
||||||
|
return send_daemon_request(
|
||||||
|
endpoint=daemon_env.socket_path,
|
||||||
|
operation_request=deploy_req,
|
||||||
|
token=ADMIN_SECRET,
|
||||||
|
)
|
||||||
|
|
||||||
|
with (
|
||||||
|
patch("appctl_router.run_bounded_subprocess", side_effect=slow_subproc),
|
||||||
|
patch("appctl_router.OperationalRouter._trigger_homepage_sync"),
|
||||||
|
concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor,
|
||||||
|
):
|
||||||
|
fut1 = executor.submit(send_first)
|
||||||
|
fut2 = executor.submit(send_second)
|
||||||
|
resp1 = fut1.result(timeout=5.0)
|
||||||
|
resp2 = fut2.result(timeout=5.0)
|
||||||
|
|
||||||
|
responses = [resp1, resp2]
|
||||||
|
successes = [r for r in responses if r.success]
|
||||||
|
conflicts = [r for r in responses if not r.success and r.error and r.error.code == ErrorCode.ERR_CONCURRENT_MUTATION]
|
||||||
|
|
||||||
|
assert len(successes) == 1
|
||||||
|
assert len(conflicts) == 1
|
||||||
|
assert successes[0].result is not None
|
||||||
|
assert successes[0].result["action"] == "deployed"
|
||||||
|
assert conflicts[0].error.code.http_status == 409
|
||||||
|
|
||||||
|
|
||||||
def test_execution_failure_audit(daemon_env):
|
def test_execution_failure_audit(daemon_env):
|
||||||
"""
|
"""
|
||||||
Failing Docker subprocess returns 500 ERR_EXECUTION_FAILED and records phase: FAILED.
|
Failing Docker subprocess returns 500 ERR_EXECUTION_FAILED and records phase: FAILED.
|
||||||
@@ -470,7 +522,7 @@ def test_audit_failure_blocks_mutation(daemon_env):
|
|||||||
daemon_env.audit_file.chmod(0o400)
|
daemon_env.audit_file.chmod(0o400)
|
||||||
deploy_req = OperationRequest(operation=OperationType.WORKLOAD_DEPLOY, target="docs")
|
deploy_req = OperationRequest(operation=OperationType.WORKLOAD_DEPLOY, target="docs")
|
||||||
|
|
||||||
with patch("subprocess.run") as mock_subproc:
|
with patch("appctl_router.run_bounded_subprocess") as mock_subproc:
|
||||||
resp = send_daemon_request(
|
resp = send_daemon_request(
|
||||||
endpoint=daemon_env.socket_path,
|
endpoint=daemon_env.socket_path,
|
||||||
operation_request=deploy_req,
|
operation_request=deploy_req,
|
||||||
@@ -515,13 +567,20 @@ def test_gitops_dispatcher_delegates_to_daemon(daemon_env, monkeypatch):
|
|||||||
monkeypatch.setenv("GITOPS_APPCTL_TOKEN", OPERATOR_SECRET)
|
monkeypatch.setenv("GITOPS_APPCTL_TOKEN", OPERATOR_SECRET)
|
||||||
monkeypatch.setenv("APPCTL_SOCKET_PATH", str(daemon_env.socket_path))
|
monkeypatch.setenv("APPCTL_SOCKET_PATH", str(daemon_env.socket_path))
|
||||||
|
|
||||||
with patch.object(daemon_env.daemon.router, "_op_workload_update") as mock_update:
|
def mock_subproc(cmd, *args, **kwargs):
|
||||||
mock_update.return_value = ({"action": "updated", "message": "Updated via daemon"}, "sha12345")
|
if "status" in cmd:
|
||||||
|
return (0, "", "")
|
||||||
|
if "rev-parse" in cmd:
|
||||||
|
return (0, "sha12345", "")
|
||||||
|
return (0, "ok", "")
|
||||||
|
|
||||||
|
with patch("appctl_router.run_bounded_subprocess", side_effect=mock_subproc) as mock_sub, patch(
|
||||||
|
"appctl_router.OperationalRouter._trigger_homepage_sync"
|
||||||
|
):
|
||||||
success = gitops_dispatcher.execute_deployment(
|
success = gitops_dispatcher.execute_deployment(
|
||||||
target_dir=daemon_env.docs_dir,
|
target_dir=daemon_env.docs_dir,
|
||||||
deployment_config={"branch": "main", "actions": ["git_pull", "compose_up"]},
|
deployment_config={"branch": "main", "actions": ["git_pull", "compose_up"]},
|
||||||
branch="main",
|
branch="main",
|
||||||
)
|
)
|
||||||
assert success is True
|
assert success is True
|
||||||
assert mock_update.call_count == 1
|
assert mock_sub.call_count >= 1
|
||||||
@@ -7,6 +7,7 @@ test_<subject>_<accepts|rejects|enforces>_<condition>
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
@@ -1147,5 +1148,105 @@ def test_operational_router_workload_list_handles_manifest_error(
|
|||||||
assert broken["provenance_status"] == "⚠️ Invalid Manifest"
|
assert broken["provenance_status"] == "⚠️ Invalid Manifest"
|
||||||
|
|
||||||
|
|
||||||
|
@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.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"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in new issue
Block a user