Compare commits
3
Commits
1fea9024f1
...
c034afd0f8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c034afd0f8 | ||
|
|
af82d4b26d | ||
|
|
b7b19ad7f1 |
No files matched your search
@@ -18,9 +18,12 @@ Asserts:
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import concurrent.futures
|
||||
import hashlib
|
||||
import json
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
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")
|
||||
|
||||
with patch("subprocess.run") as mock_subproc:
|
||||
with patch("appctl_router.run_bounded_subprocess") as mock_subproc:
|
||||
# Over HTTP without token
|
||||
resp_http = send_daemon_request(
|
||||
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")
|
||||
|
||||
with patch("subprocess.run") as mock_subproc:
|
||||
with patch("appctl_router.run_bounded_subprocess") as mock_subproc:
|
||||
resp = send_daemon_request(
|
||||
endpoint=daemon_env.socket_path,
|
||||
operation_request=deploy_req,
|
||||
@@ -435,6 +438,55 @@ def test_concurrent_mutation_conflict(daemon_env):
|
||||
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):
|
||||
"""
|
||||
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)
|
||||
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(
|
||||
endpoint=daemon_env.socket_path,
|
||||
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("APPCTL_SOCKET_PATH", str(daemon_env.socket_path))
|
||||
|
||||
with patch.object(daemon_env.daemon.router, "_op_workload_update") as mock_update:
|
||||
mock_update.return_value = ({"action": "updated", "message": "Updated via daemon"}, "sha12345")
|
||||
def mock_subproc(cmd, *args, **kwargs):
|
||||
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(
|
||||
target_dir=daemon_env.docs_dir,
|
||||
deployment_config={"branch": "main", "actions": ["git_pull", "compose_up"]},
|
||||
branch="main",
|
||||
)
|
||||
assert success is True
|
||||
assert mock_update.call_count == 1
|
||||
assert mock_sub.call_count >= 1
|
||||
@@ -1336,6 +1336,108 @@ def test_operational_router_workload_config_redacts_evaluated_secrets_for_operat
|
||||
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.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"
|
||||
|
||||
Reference in new issue
Block a user