"""The deferred restart must re-apply the patch before it restarts middlewared. Patching at PREINIT and restarting minutes later is only sound while the patched files are still on the live path when middlewared re-imports them. They may not be: the patch lives in an overlay mounted inside /usr, and anything that remounts that hierarchy detaches it. On 2026-08-19 a systemd-sysext refresh over /usr ran four seconds after apply.sh mounted its overlay; the deferred restart then loaded stock modules and every B2 cloud_backup job failed for nineteen hours while apply.log reported "OK". These tests pin the ordering that makes that non-recoverable failure impossible: re-apply, verify, restart, verify again. """ import os import re import subprocess import pytest HERE = os.path.dirname(__file__) WAIT_RESTART = os.path.join(HERE, "..", "patch", "wait_restart.sh") APPLY_SH = os.path.join(HERE, "..", "patch", "apply.sh") def wait_restart_source(): with open(WAIT_RESTART, encoding="utf-8") as fh: return fh.read() def apply_source(): with open(APPLY_SH, encoding="utf-8") as fh: return fh.read() def test_wait_restart_is_executable(): # apply.sh schedules it as `/bin/bash