Files

34 lines
1.7 KiB
Python

#!/usr/bin/env python3
"""Execute the actual shutdown helper branch with disposable paths/fake PID1."""
import json
import os
from pathlib import Path
import subprocess
import tempfile
source = Path(__file__).resolve().parents[2] / "scripts/tor-helper.sh"
with tempfile.TemporaryDirectory(prefix="archy-shutdown-test-") as directory:
root = Path(directory)
binary = root / "bin"
binary.mkdir()
control = binary / "systemctl"
control.write_text('#!/bin/sh\nprintf "%s\\n" "$@" > "$FIXTURE/arguments"\nexit "$FIXTURE_EXIT"\n')
control.chmod(0o700)
script = source.read_text()
script = script.replace('ACTION_FILE="/var/lib/archipelago/tor-config/tor-action"', f'ACTION_FILE="{root}/action"')
script = script.replace('RESULT_FILE="/var/lib/archipelago/tor-config/tor-result"', f'RESULT_FILE="{root}/result"')
runner = root / "helper.sh"
runner.write_text(script)
for exit_code in (0, 1):
(root / "action").write_text('{"action":"poweroff"}')
env = dict(os.environ, PATH=str(binary) + os.pathsep + os.environ["PATH"],
FIXTURE=str(root), FIXTURE_EXIT=str(exit_code))
result = subprocess.run(["bash", str(runner)], env=env, capture_output=True, text=True, timeout=5)
assert result.returncode == exit_code, result.stderr
assert (root / "arguments").read_text().splitlines() == ["--no-ask-password", "--when=+5s", "poweroff"]
receipt = json.loads((root / "result").read_text())
assert receipt["ok"] is (exit_code == 0)
assert receipt.get("shutdown_requested", False) is (exit_code == 0)
assert not (root / "action").exists()
print("PASS: graceful shutdown command and failure reporting; no host shutdown invoked")