"""The maintainer scripts must not stop the agent on an upgrade. dpkg calls the OLD package's prerm on an upgrade as well as on a removal, and rpm calls it with an install count. `systemctl disable --now` there stopped the agent on every upgrade and left it disabled — silently, because the box stays reachable and nothing else notices that check-ins have ceased (env-dev-01 and env-dev-08, 2026-09-09). """ from __future__ import annotations import shutil import subprocess from pathlib import Path import pytest PREREMOVE = Path(__file__).resolve().parents[1] / "packaging" / "scripts" / "preremove.sh" def _run(arg: str, tmp_path: Path) -> list[str]: """Run preremove with a fake `systemctl` on PATH and report the calls it made.""" calls = tmp_path / "systemctl.log" fake = tmp_path / "bin" fake.mkdir(exist_ok=True) (fake / "systemctl").write_text(f'#!/bin/sh\necho "$@" >> {calls}\nexit 0\n') (fake / "systemctl").chmod(0o755) env = {"PATH": f"{fake}:/usr/bin:/bin"} r = subprocess.run(["sh", str(PREREMOVE), arg], env=env, capture_output=True, text=True) assert r.returncode == 0, r.stderr return calls.read_text().splitlines() if calls.exists() else [] @pytest.mark.skipif(not Path("/run/systemd/system").is_dir(), reason="needs a systemd host to reach the disable branch") @pytest.mark.parametrize("arg", ["remove", "0"]) def test_a_real_removal_disables_the_units(arg, tmp_path): assert any("disable" in c for c in _run(arg, tmp_path)) @pytest.mark.parametrize("arg", ["upgrade", "failed-upgrade", "deconfigure", "1"]) def test_an_upgrade_leaves_the_units_alone(arg, tmp_path): assert _run(arg, tmp_path) == [] def test_the_script_is_shell_clean(): assert shutil.which("sh") subprocess.run(["sh", "-n", str(PREREMOVE)], check=True)