from __future__ import annotations import os import sqlite3 import subprocess import sys import time from pathlib import Path import pytest from app.gitea import GiteaClient from app.jobs import ScanJobRunner from app.processes import git_environment, run_bounded from app.store import ScanStore def test_git_environment_forces_foreground_housekeeping_and_keeps_extra_config() -> None: env = git_environment({}, extra_config=[("http.extraHeader", "Authorization: token t")]) pairs = { env[f"GIT_CONFIG_KEY_{index}"]: env[f"GIT_CONFIG_VALUE_{index}"] for index in range(int(env["GIT_CONFIG_COUNT"])) } assert pairs == { "gc.autoDetach": "false", "maintenance.autoDetach": "false", "http.extraHeader": "Authorization: token t", } def test_run_bounded_returns_output_and_raises_on_failure() -> None: ok = run_bounded([sys.executable, "-c", "print('hi')"], timeout=10) assert ok.stdout.strip() == b"hi" with pytest.raises(subprocess.CalledProcessError): run_bounded([sys.executable, "-c", "raise SystemExit(3)"], timeout=10) @pytest.mark.skipif(os.name != "posix", reason="process groups are POSIX-only") def test_run_bounded_timeout_kills_grandchildren(tmp_path: Path) -> None: marker = tmp_path / "grandchild.pid" script = ( "import subprocess, sys, time\n" "child = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(60)'])\n" f"open({str(marker)!r}, 'w').write(str(child.pid))\n" "time.sleep(60)\n" ) with pytest.raises(subprocess.TimeoutExpired): run_bounded([sys.executable, "-c", script], timeout=2) grandchild = int(marker.read_text()) deadline = time.monotonic() + 5 while time.monotonic() < deadline: if _process_gone_or_zombie(grandchild): return time.sleep(0.05) pytest.fail("grandchild survived the timeout kill") def _process_gone_or_zombie(pid: int) -> bool: """A killed orphan stays a zombie until PID 1 reaps it (tini does in production). Inside a CI job container PID 1 may not reap, so a zombie counts as killed. """ try: stat = Path(f"/proc/{pid}/stat").read_text() except (FileNotFoundError, ProcessLookupError): return True # Field 3, after the parenthesised command name, is the process state. return stat.rsplit(")", 1)[1].split()[0] in {"Z", "X"} def test_idle_claim_never_takes_the_write_lock(tmp_path: Path) -> None: store = ScanStore(str(tmp_path / "repopulse.db")) writer = sqlite3.connect(tmp_path / "repopulse.db", timeout=0) try: writer.execute("BEGIN IMMEDIATE") started = time.monotonic() assert store.claim_next_job() is None # Without the read-only probe this would block on busy_timeout (5 s). assert time.monotonic() - started < 2 finally: writer.rollback() writer.close() def test_idle_worker_backs_off_and_enqueue_wakes_it(tmp_path: Path) -> None: runner = ScanJobRunner(ScanStore(str(tmp_path / "repopulse.db")), gitea=GiteaClient()) runner.poll_seconds = 0.5 runner.idle_poll_seconds = 5.0 delays = [] for iterations in range(1, 7): runner._idle_iterations = iterations delays.append(runner._idle_delay()) assert delays == [0.5, 1.0, 2.0, 4.0, 5.0, 5.0] runner.wake() started = time.monotonic() assert runner._wake.wait(1) assert time.monotonic() - started < 0.5