import signal import subprocess import sys import time from pathlib import Path import pytest from k1link.launchd_logs import bounded_launchd_output, service_command, service_log_path from k1link.rotating_process import Journal, supervise def retained(path, backups): return b"".join( p.read_bytes() for p in [Path(f"{path}.{i}") for i in range(backups, 0, -1)] + [path] if p.exists() ) def test_binary_rotation_is_exact_and_bounded_across_restart(tmp_path): p = tmp_path / "test.log" j = Journal(p, max_bytes=7, backups=2) j.write(bytes(range(21))) j.close() assert retained(p, 2) == bytes(range(21)) j = Journal(p, max_bytes=7, backups=2) j.write(bytes(range(21, 35))) j.close() assert retained(p, 2) == bytes(range(14, 35)) assert all(x.stat().st_size <= 7 for x in tmp_path.glob("test.log*") if x.suffix != ".lock") def test_journal_owns_writer_and_refuses_linked_targets(tmp_path): p = tmp_path / "test.log" j = Journal(p, max_bytes=4, backups=1) with pytest.raises(BlockingIOError): Journal(p, max_bytes=4, backups=1) outside = tmp_path / "restore" outside.write_bytes(b"preserve") Path(f"{p}.1").symlink_to(outside) j.write(b"1234") with pytest.raises(ValueError): j.write(b"5") j.close() assert outside.read_bytes() == b"preserve" def test_child_stdout_stderr_and_exit_code(tmp_path): p = tmp_path / "child.log" result = supervise( [ sys.executable, "-c", 'import os;os.write(1,b"stdout\\x00");os.write(2,b"stderr");raise SystemExit(7)', ], p, max_bytes=100, backups=2, ) assert result == 7 and p.read_bytes() == b"stdout\x00stderr" def test_signal_reaches_child_and_supervisor_exits(tmp_path): path = tmp_path / "child.log" code = ( "import signal,time,os;signal.signal(signal.SIGTERM,lambda *a:exit(0));" 'os.write(1,b"ready");time.sleep(30)' ) proc = subprocess.Popen( [ sys.executable, "-m", "k1link.rotating_process", "--log", str(path), "--", sys.executable, "-c", code, ] ) try: for _ in range(100): if path.exists() and path.read_bytes() == b"ready": break time.sleep(0.02) else: pytest.fail("child failed to start") proc.send_signal(signal.SIGTERM) assert proc.wait(timeout=5) == 0 finally: if proc.poll() is None: proc.kill() proc.wait() def test_wrapping_preserves_command_environment_and_network_policy(tmp_path): d = { "Label": "example", "ProgramArguments": [ "/usr/bin/ssh", "-N", "-R", "127.0.0.1:18080:127.0.0.1:8000", "mission-gpu", ], "EnvironmentVariables": {"PRIVATE_VALUE": "not-printed"}, "StandardErrorPath": str(tmp_path / "old.log"), "KeepAlive": True, } result = bounded_launchd_output(d, tmp_path) assert service_command(result) == d["ProgramArguments"] assert result["EnvironmentVariables"] == d["EnvironmentVariables"] assert result["KeepAlive"] is True assert service_log_path(result) == tmp_path / "old.managed.log" assert result["StandardOutPath"] == result["StandardErrorPath"] == "/dev/null" assert bounded_launchd_output(result, tmp_path) == result assert d["StandardErrorPath"] == str(tmp_path / "old.log") def test_existing_wrapper_cannot_restore_unbounded_launchd_output(tmp_path): value = bounded_launchd_output( { "ProgramArguments": ["/usr/bin/ssh", "-N", "existing"], "StandardErrorPath": str(tmp_path / "old.log"), }, tmp_path, ) value["StandardOutPath"] = value["StandardErrorPath"] = str(tmp_path / "old.log") updated = bounded_launchd_output(value, tmp_path) assert updated["StandardErrorPath"] == updated["StandardOutPath"] == "/dev/null" assert updated["ProgramArguments"].count("k1link.rotating_process") == 1