From 76df30747d0967b3ade09e24aa5aa8362cc719da Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Sat, 31 Oct 2026 22:29:52 -0500 Subject: [PATCH] test(harness): ensure portable socket path length and root-invariant audit failure handling --- tests/unit/test_appctl_audit.py | 43 +++++++++++++------------------- tests/unit/test_appctl_cli.py | 5 +++- tests/unit/test_appctl_daemon.py | 20 +++++++++------ 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/tests/unit/test_appctl_audit.py b/tests/unit/test_appctl_audit.py index 3d7d330..2ef385a 100644 --- a/tests/unit/test_appctl_audit.py +++ b/tests/unit/test_appctl_audit.py @@ -8,7 +8,6 @@ test___ from __future__ import annotations import json -import os import stat from pathlib import Path @@ -285,32 +284,24 @@ def test_audit_logger_automatically_redacts_sensitive_parameters( def test_audit_logger_fails_closed_when_audit_log_unwritable( tmp_path: Path, sample_principal: PrincipalAudit ): - read_only_dir = tmp_path / "readonly_audit" - read_only_dir.mkdir(parents=True, exist_ok=True) - unwritable_file = read_only_dir / "audit.jsonl" - unwritable_file.touch(mode=0o400) - os.chmod(unwritable_file, 0o400) + # Using a directory as the audit path causes os.open with O_WRONLY to fail with + # EISDIR / OSError across all platforms and environments (including root containers). + unwritable_target = tmp_path / "unwritable_audit_dir" + unwritable_target.mkdir(parents=True, exist_ok=True) - # In addition, make directory read-only - os.chmod(read_only_dir, 0o500) - - try: - logger = AuditLogger(unwritable_file) - with pytest.raises(AuditPersistenceError) as exc_info: - logger.log_dispatched( - operation_id="op_105", - principal=sample_principal, - action="workload.deploy", - target="jellyfin", - transport="unix_socket", - origin="peer_uid_1000", - client="appctl-cli/2.0", - parameters={"recreate": True}, - ) - assert "Failed to persist audit event" in str(exc_info.value) - finally: - os.chmod(read_only_dir, 0o700) - os.chmod(unwritable_file, 0o600) + logger = AuditLogger(unwritable_target) + with pytest.raises(AuditPersistenceError) as exc_info: + logger.log_dispatched( + operation_id="op_105", + principal=sample_principal, + action="workload.deploy", + target="jellyfin", + transport="unix_socket", + origin="peer_uid_1000", + client="appctl-cli/2.0", + parameters={"recreate": True}, + ) + assert "Failed to persist audit event" in str(exc_info.value) # --------------------------------------------------------------------------- diff --git a/tests/unit/test_appctl_cli.py b/tests/unit/test_appctl_cli.py index 43558af..e940457 100644 --- a/tests/unit/test_appctl_cli.py +++ b/tests/unit/test_appctl_cli.py @@ -473,9 +473,11 @@ def test_appctl_wrapper_fast_paths_completion_when_socket_exists(tmp_path: Path, import socket as std_socket import subprocess import sys + import tempfile bash_bin = shutil.which("bash") or "bash" - sock_path = tmp_path / "appctl.sock" + sock_dir = tempfile.TemporaryDirectory(prefix="appctl_sock_", dir="/tmp") + sock_path = Path(sock_dir.name) / "appctl.sock" srv = std_socket.socket(std_socket.AF_UNIX, std_socket.SOCK_STREAM) srv.bind(str(sock_path)) try: @@ -512,6 +514,7 @@ def test_appctl_wrapper_fast_paths_completion_when_socket_exists(tmp_path: Path, assert "core" in res_words.stdout finally: srv.close() + sock_dir.cleanup() def test_appctl_wrapper_fails_closed_when_socket_is_missing(tmp_path: Path): diff --git a/tests/unit/test_appctl_daemon.py b/tests/unit/test_appctl_daemon.py index c1b60f5..9f8b0d0 100644 --- a/tests/unit/test_appctl_daemon.py +++ b/tests/unit/test_appctl_daemon.py @@ -22,6 +22,7 @@ import concurrent.futures import hashlib import json import socket +import tempfile import threading import time from pathlib import Path @@ -29,7 +30,7 @@ from unittest.mock import patch import gitops_dispatcher import pytest -from appctl_audit import AuditLogger, TargetLockManager +from appctl_audit import AuditLogger, AuditPersistenceError, TargetLockManager from appctl_auth import Role, TokenDescriptor, TokenStore from appctl_contracts import ErrorCode, OperationRequest, OperationType from appctl_daemon import ( @@ -64,7 +65,8 @@ def daemon_env(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): - Valid 0600 audit log file """ monkeypatch.setenv("HOMELAB_DOMAIN", "example.test") - socket_path = tmp_path / "appctl.sock" + sock_dir = tempfile.TemporaryDirectory(prefix="appctl_sock_", dir="/tmp") + socket_path = Path(sock_dir.name) / "appctl.sock" http_port = get_free_port() http_host = "127.0.0.1" @@ -191,6 +193,7 @@ def daemon_env(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): yield ctx finally: daemon.stop() + sock_dir.cleanup() def test_health_endpoint(daemon_env): @@ -519,10 +522,16 @@ def test_audit_failure_blocks_mutation(daemon_env): Unwritable audit.jsonl aborts mutating command with 500 ERR_EXECUTION_FAILED and zero Docker invocations. """ - daemon_env.audit_file.chmod(0o400) deploy_req = OperationRequest(operation=OperationType.WORKLOAD_DEPLOY, target="docs") - with patch("appctl_router.run_bounded_subprocess") as mock_subproc: + with ( + patch.object( + daemon_env.daemon.audit_logger, + "emit_event", + side_effect=AuditPersistenceError("Simulated disk full or permission error"), + ), + patch("appctl_router.run_bounded_subprocess") as mock_subproc, + ): resp = send_daemon_request( endpoint=daemon_env.socket_path, operation_request=deploy_req, @@ -534,9 +543,6 @@ def test_audit_failure_blocks_mutation(daemon_env): assert resp.error.code.http_status == 500 assert mock_subproc.call_count == 0 - # Restore write permission for teardown - daemon_env.audit_file.chmod(0o600) - def test_daemon_restart_preserves_state(daemon_env): """