#!/usr/bin/env python3 """Exercise real replacement paths; external services/builds use local fixtures. These are ordinary-user installer controls, not native systemd acceptance. """ from pathlib import Path from concurrent.futures import ThreadPoolExecutor import os import itertools import subprocess import tempfile import tarfile import threading import unittest from unittest.mock import patch ROOT = Path(__file__).resolve().parents[2] INSTALLER = Path(os.environ.get("PULSE_INSTALLER_UNDER_TEST", ROOT / "install.sh")) SYSTEMCTL = r'''#!/usr/bin/env bash set -eu printf '%s\n' "$*" >> "$FIXTURE/calls" case "$1" in show) if [[ -f "$FIXTURE/stopped" ]]; then [[ "$CASE" != post_query_error ]] || exit 1 case "$CASE" in still_active) echo active ;; post_transition) echo deactivating ;; post_failed) echo failed ;; *) echo inactive ;; esac else case "$CASE" in query_error) exit 1 ;; query_timeout) sleep 10 ;; empty) : ;; malformed) printf 'active\ninactive\n' ;; inactive|failed|activating|deactivating|reloading|unknown) echo "$CASE" ;; *) echo active ;; esac fi ;; is-active) [[ -f "$FIXTURE/restarted" ]] ;; start) [[ "$FAULT" != recovery_start ]] || exit 1 [[ "$FAULT" == recovery_inactive ]] || touch "$FIXTURE/restarted" ;; stop) case "$CASE" in stop_error) exit 1 ;; stop_timeout) sleep 10 ;; esac touch "$FIXTURE/stopped" ;; *) exit 97 ;; esac ''' REPLACEMENT = r''' source "$INSTALLER" INSTALL_DIR="$FIXTURE/install" CONFIG_DIR="$FIXTURE/config" BUILD_FROM_SOURCE_MARKER="$INSTALL_DIR/BUILD_FROM_SOURCE" BINARY_LINK_PATH="$FIXTURE/link" CURRENT_VERSION=v6.5.0 PULSE_WAS_ACTIVE=false print_info() { echo "$*"; } print_success() { :; } print_error() { echo "$*" >&2; } detect_service_name() { echo pulse-custom; } # No network, package installation, signature bypass in production or root writes: # these fixture-only producers supply the inputs to the actual replacement path. ensure_update_disk_headroom() { return 0; } detect_pulse_architecture() { echo amd64; } run_upgrade_readiness_preflight() { return 0; } resolve_target_release() { LATEST_RELEASE=v6.6.0; } create_temp_archive_path() { printf '%s\n' "$FIXTURE/archive.tgz"; } download_release_archive() { command cp "$FIXTURE/input.tgz" "$3" [[ "$FAULT" == missing_signature ]] || touch "${3}.sshsig" } verify_release_signature() { echo SIGNATURE >> "$FIXTURE/calls" [[ "$FAULT" != signature ]] } validate_pulse_binary_architecture() { echo ARCHITECTURE >> "$FIXTURE/calls" [[ "$FAULT" != architecture ]] } install_additional_agent_binaries() { echo AGENTS >> "$FIXTURE/calls"; } deploy_agent_scripts() { echo SCRIPTS >> "$FIXTURE/calls"; } restore_selinux_contexts() { :; } cp() { if [[ "$*" == *'.pulse-stage-'* && "$FAULT" == copy ]]; then printf partial > "${@: -1}" return 1 fi command cp "$@" } chmod() { [[ "$FAULT" != permissions ]] || return 1 command chmod "$@" } mktemp() { [[ "$FAULT" != extract_directory || "$*" != *pulse-extract-* ]] || return 1 [[ "$FAULT" != stage_directory || "$*" != *'.pulse-stage-'* ]] || return 1 local created created=$(command mktemp "$@") || return 1 printf '%s\n' "$created" >> "$FIXTURE/temporary-paths" printf '%s\n' "$created" } mv() { echo RENAME >> "$FIXTURE/calls" case "$FAULT" in rename|recovery_start|recovery_inactive) return 1 ;; esac command mv "$@" } sleep() { : "$((SECONDS += $1))"; } apt-get() { return 0; } go() { echo 'go version go1.99.0 linux/amd64'; } npm() { return 0; } make() { return 0; } git() { if [[ "$1" == clone ]]; then local target="${@: -1}" mkdir -p "$target/frontend-modern" [[ "$FAULT" == missing_binary ]] || cp "$FIXTURE/new-binary" "$target/pulse" else [[ "$FAULT" != source_revision ]] || return 1 [[ "$FAULT" != source_revision_empty ]] || return 0 echo abc1234 fi } chown() { [[ "$FAULT" != ownership ]]; } install_binary_symlink() { echo REPLACED >> "$FIXTURE/calls"; } if [[ "$FLOW" == archive || "$FLOW" == local_archive ]]; then if [[ "$FLOW" == local_archive ]]; then ARCHIVE_OVERRIDE="$FIXTURE/input.tgz" FORCE_VERSION=v6.6.0 fi download_pulse else build_from_source fixture fi printf 'WAS_ACTIVE=%s\n' "$PULSE_WAS_ACTIVE" ''' STAGING_FAILURE = r''' source "$INSTALLER" INSTALL_DIR="$FIXTURE/install" CONFIG_DIR="$FIXTURE/config" CURRENT_VERSION=v6.5.0 LATEST_RELEASE=v6.6.0 FORCE_VERSION="" [[ "$FLOW" != version ]] || FORCE_VERSION=v6.6.0 for fn in print_header print_info check_root detect_os check_docker_environment backup_existing create_user offer_existing_auto_updates; do eval "$fn() { :; }" done check_proxmox_host() { return 1; } check_existing_installation() { return 0; } detect_service_name() { echo pulse-custom; } resolve_latest_release_tag_for_channel() { echo v6.6.0; } read_configured_update_channel() { echo stable; } run_upgrade_readiness_preflight() { return 0; } safe_read() { local selection=1 [[ "$FLOW" != reinstall ]] || selection=2 printf -v "$2" '%s' "$selection" } curl() { return 0; } download_pulse() { echo 'staging failed' >&2; exit 42; } main ''' MANUAL_EDITION_UPDATE = r''' source "$INSTALLER" INSTALL_DIR="$FIXTURE/install" CONFIG_DIR="$FIXTURE/config" BUILD_FROM_SOURCE_MARKER="$INSTALL_DIR/BUILD_FROM_SOURCE" BINARY_LINK_PATH="$FIXTURE/pulse" PINNED_RELEASE_SSH_PUBLIC_KEY=$(cat "$FIXTURE/key.pub") FORCE_VERSION="$TARGET_VERSION" [[ "$FLOW" != source ]] || BUILD_FROM_SOURCE=true [[ "$FLOW" != local ]] || ARCHIVE_OVERRIDE="$ARCHIVE" for fn in print_header check_root detect_os check_docker_environment \ offer_existing_auto_updates preflight_systemd_service_installation create_user \ install_additional_agent_binaries deploy_agent_scripts chown_install_dir_for_pulse \ restore_selinux_contexts setup_directories setup_update_command \ ensure_systemd_service_installed install_systemd_service create_marker_file print_completion; do eval "$fn() { :; }" done check_proxmox_host() { return 1; } detect_service_name() { echo pulse-custom; } detect_pulse_architecture() { echo amd64; } validate_pulse_binary_architecture() { return 0; } install_dependencies() { echo PACKAGES >> "$FIXTURE/calls"; } ensure_update_disk_headroom() { return 0; } run_upgrade_readiness_preflight() { return 0; } backup_existing() { echo BACKUP >> "$FIXTURE/calls"; } start_pulse() { systemctl start pulse-custom; } chown() { :; } update_timer_exists() { return 1; } safe_read() { return 1; } build_from_source() { echo SOURCE_BUILD >> "$FIXTURE/calls"; return 1; } curl() { echo UNEXPECTED_NETWORK >> "$FIXTURE/calls"; return 1; } resolve_latest_release_tag_for_channel() { echo PUBLIC_LOOKUP >> "$FIXTURE/calls" echo v6.6.0 } download_release_archive() { echo PUBLIC_ARCHIVE >> "$FIXTURE/calls" cp "$ARCHIVE" "$3" cp "$ARCHIVE.sshsig" "$3.sshsig" } main ''' class ServerInstallerStopTest(unittest.TestCase): def exercise(self, script, flow, case, fault=""): with tempfile.TemporaryDirectory() as temporary: fixture = Path(temporary) (fixture / "bin").mkdir() installed = fixture / "install" (installed / "bin").mkdir(parents=True) (fixture / "config").mkdir() originals = { installed / "bin/pulse": b"old executable\n", installed / "bin/pulse.old": b"pre-existing recovery binary\n", installed / "VERSION": b"v6.5.0\n", installed / "BUILD_FROM_SOURCE": b"old branch\n", fixture / "config/private-state": b"fixture persistent state\n", } for path, content in originals.items(): path.write_bytes(content) path.chmod(0o600) new_binary = b"#!/bin/sh\necho VERSION_PROBE >> \"$FIXTURE/calls\"\n" if fault == "version_timeout": new_binary += b"sleep 10\n" elif fault == "version_exit": new_binary += b"echo v6.6.0; exit 1\n" elif fault == "version_empty": new_binary += b"printf ' \\t\\n'\n" else: version = "v6.5.0" if fault == "version_mismatch" else "unknown" if fault == "version_unknown" else "v6.6.0" new_binary += ("echo " + version + "\n").encode() (fixture / "new-binary").write_bytes(new_binary) (fixture / "new-binary").chmod(0o700) with tarfile.open(fixture / "input.tgz", "w:gz") as archive: if fault != "missing_binary": archive.add(fixture / "new-binary", arcname="bin/pulse") version_file = fixture / "candidate-version" version_file.write_text("v6.6.0\n") archive.add(version_file, arcname="VERSION") if fault != "missing_signature": (fixture / "input.tgz.sshsig").touch() if fault == "extraction": (fixture / "input.tgz").write_bytes(b"not a tarball") (fixture / "bin/systemctl").write_text(SYSTEMCTL) (fixture / "bin/systemctl").chmod(0o700) env = {**os.environ, "FIXTURE": str(fixture), "CASE": case, "FLOW": flow, "INSTALLER": str(INSTALLER), "FAULT": fault, "PATH": str(fixture / "bin") + ":" + os.environ["PATH"]} result = subprocess.run(["bash", "-c", script], env=env, capture_output=True, text=True, timeout=30) return { "result": result, "new_binary": new_binary, "stage_paths": list(installed.glob("bin/.pulse-stage-*")), "temporary_paths_remaining": [p for p in (fixture / "temporary-paths").read_text().splitlines() if Path(p).exists()] if (fixture / "temporary-paths").exists() else [], "local_archive_retained": (fixture / "input.tgz").exists(), "files": {str(p.relative_to(fixture)): p.read_bytes() if p.exists() else None for p in originals}, "modes": {str(p.relative_to(fixture)): p.stat().st_mode & 0o777 if p.exists() else None for p in originals}, "originals": {str(p.relative_to(fixture)): b for p, b in originals.items()}, "calls": (fixture / "calls").read_text() if (fixture / "calls").exists() else "", "archive_retained": (fixture / "archive.tgz").exists(), "temporary_paths": (fixture / "temporary-paths").read_text().splitlines() if (fixture / "temporary-paths").exists() else [], "install_dir": installed, } def test_refused_replacements_preserve_installed_bytes_and_modes(self): # Each exercise owns its directory, archive, environment and subprocess. # Overlap only the two fixed flows; keep the real installer deadlines # and collect every result/exception on the test thread. The executor # drains even after a failed subtest; batches never share fixture state. with ThreadPoolExecutor(max_workers=2) as pool: for case in ("stop_error", "stop_timeout", "query_error", "query_timeout", "still_active", "post_query_error", "post_transition", "post_failed", "activating", "deactivating", "reloading", "unknown", "empty", "malformed"): pending = [(flow, pool.submit(self.exercise, REPLACEMENT, flow, case)) for flow in ("archive", "source")] for flow, future in pending: with self.subTest(flow=flow, case=case): observation = future.result() result = observation["result"] self.assertNotEqual(result.returncode, 0) self.assertIn("refusing binary replacement", result.stderr) self.assertEqual(observation["files"], observation["originals"]) self.assertTrue(all(m == 0o600 for m in observation["modes"].values())) self.assertNotIn("REPLACED", observation["calls"]) self.assertNotIn("RENAME", observation["calls"]) self.assertEqual(observation["stage_paths"], []) self.assertEqual(observation["temporary_paths_remaining"], []) self.assertNotRegex(observation["calls"], r"(?m)^(start|restart)\b") self.assertFalse(observation["archive_retained"]) def test_confirmed_stop_and_inactive_installs_keep_liveness_attribution(self): for flow in ("archive", "source"): for case in ("active", "inactive", "failed"): with self.subTest(flow=flow, case=case): observation = self.exercise(REPLACEMENT, flow, case) result = observation["result"] self.assertEqual(result.returncode, 0, result.stderr) self.assertEqual(observation["files"]["install/bin/pulse"], observation["new_binary"]) self.assertEqual(observation["files"]["config/private-state"], b"fixture persistent state\n") self.assertIn("WAS_ACTIVE=" + str(case == "active").lower(), result.stdout) self.assertEqual(observation["calls"].count("stop pulse-custom"), int(case != "inactive")) self.assertFalse(observation["archive_retained"]) def test_archive_admission_failures_never_stop_or_mutate_pulse(self): with ThreadPoolExecutor(max_workers=2) as pool: for fault in ("missing_signature", "signature", "extract_directory", "extraction", "missing_binary", "architecture", "stage_directory", "copy", "permissions", "ownership", "version_exit", "version_timeout", "version_unknown", "version_mismatch"): pending = [(flow, pool.submit(self.exercise, REPLACEMENT, flow, "active", fault)) for flow in ("archive", "local_archive")] for flow, future in pending: with self.subTest(flow=flow, fault=fault): observation = future.result() self.assertNotEqual(observation["result"].returncode, 0) self.assertEqual(observation["files"], observation["originals"]) self.assertTrue(all(m == 0o600 for m in observation["modes"].values())) self.assertNotRegex(observation["calls"], r"(?m)^(show|stop|start|restart|RENAME|AGENTS|SCRIPTS)\b") self.assertEqual(observation["stage_paths"], []) self.assertEqual(observation["temporary_paths_remaining"], []) self.assertFalse(observation["archive_retained"]) self.assertTrue(observation["local_archive_retained"]) def test_atomic_rename_failure_restores_only_a_previously_active_service(self): for flow, state, fault in itertools.product( ("archive", "source"), ("active", "inactive", "failed"), ("rename", "recovery_start", "recovery_inactive") ): with self.subTest(flow=flow, state=state, fault=fault): observation = self.exercise(REPLACEMENT, flow, state, fault) result = observation["result"] self.assertNotEqual(result.returncode, 0) self.assertEqual(observation["files"], observation["originals"]) self.assertTrue(all(m == 0o600 for m in observation["modes"].values())) self.assertEqual(observation["calls"].count("start pulse-custom"), int(state == "active")) self.assertNotIn("AGENTS", observation["calls"]) self.assertNotIn("SCRIPTS", observation["calls"]) if state == "active": if fault == "rename": self.assertIn("Pulse service is running again; the update failed", result.stdout) else: self.assertIn("could not be confirmed running", result.stderr) self.assertEqual(observation["stage_paths"], []) self.assertEqual(observation["temporary_paths_remaining"], []) self.assertFalse(observation["archive_retained"]) def test_source_admission_failures_never_stop_or_mutate_pulse(self): for fault in ("missing_binary", "stage_directory", "copy", "permissions", "ownership", "version_exit", "version_timeout", "version_empty", "source_revision", "source_revision_empty"): with self.subTest(fault=fault): observation = self.exercise(REPLACEMENT, "source", "active", fault) self.assertNotEqual(observation["result"].returncode, 0) self.assertEqual(observation["files"], observation["originals"]) self.assertTrue(all(m == 0o600 for m in observation["modes"].values())) self.assertNotRegex(observation["calls"], r"(?m)^(show|stop|start|restart|RENAME|AGENTS|SCRIPTS)\b") self.assertEqual(observation["stage_paths"], []) self.assertEqual(observation["temporary_paths_remaining"], []) def test_source_success_admits_bytes_before_stop_and_commits_once(self): observation = self.exercise(REPLACEMENT, "source", "active") self.assertEqual(observation["result"].returncode, 0, observation["result"].stderr) calls = observation["calls"] self.assertLess(calls.index("VERSION_PROBE"), calls.index("stop pulse-custom")) self.assertLess(calls.index("stop pulse-custom"), calls.index("RENAME")) self.assertEqual(calls.count("RENAME"), 1) self.assertEqual(calls.count("VERSION_PROBE"), 1) self.assertEqual(observation["files"]["install/bin/pulse"], observation["new_binary"]) self.assertEqual(observation["modes"]["install/bin/pulse"], 0o755) self.assertEqual(observation["files"]["install/bin/pulse.old"], observation["originals"]["install/bin/pulse.old"]) self.assertEqual(observation["files"]["install/VERSION"], b"fixture-abc1234\n") self.assertEqual(observation["files"]["install/BUILD_FROM_SOURCE"], b"fixture\n") self.assertEqual(observation["files"]["config/private-state"], b"fixture persistent state\n") self.assertEqual(observation["stage_paths"], []) self.assertEqual(observation["temporary_paths_remaining"], []) self.assertNotIn("start pulse-custom", calls) def test_archive_success_admits_bytes_before_stop_and_commits_once(self): observation = self.exercise(REPLACEMENT, "archive", "active") self.assertEqual(observation["result"].returncode, 0, observation["result"].stderr) calls = observation["calls"] self.assertLess(calls.index("SIGNATURE"), calls.index("ARCHITECTURE")) self.assertLess(calls.index("ARCHITECTURE"), calls.index("VERSION_PROBE")) self.assertLess(calls.index("VERSION_PROBE"), calls.index("stop pulse-custom")) self.assertLess(calls.index("stop pulse-custom"), calls.index("RENAME")) self.assertLess(calls.index("RENAME"), calls.index("AGENTS")) self.assertEqual(calls.count("RENAME"), 1) self.assertEqual(calls.count("VERSION_PROBE"), 1) self.assertEqual(observation["files"]["install/bin/pulse"], observation["new_binary"]) self.assertEqual(observation["modes"]["install/bin/pulse"], 0o755) self.assertEqual(observation["stage_paths"], []) self.assertEqual(observation["temporary_paths_remaining"], []) self.assertNotIn("start pulse-custom", calls) def test_archive_extraction_stages_on_the_install_filesystem(self): # /tmp is tmpfs on Debian 13 and many LXC templates: an extracted # release there is charged to the container's memory limit and # OOM-killed a 1 GiB LXC mid-update (#2785). for flow in ("archive", "local_archive"): with self.subTest(flow=flow): observation = self.exercise(REPLACEMENT, flow, "active") self.assertEqual(observation["result"].returncode, 0, observation["result"].stderr) extracts = [Path(p) for p in observation["temporary_paths"] if "pulse-extract-" in p] self.assertEqual(len(extracts), 1, observation["temporary_paths"]) self.assertEqual(extracts[0].parent, observation["install_dir"]) self.assertEqual(observation["temporary_paths_remaining"], []) def test_all_manual_update_flows_leave_service_running_when_staging_fails(self): for flow in ("version", "update", "reinstall"): with self.subTest(flow=flow): observation = self.exercise(STAGING_FAILURE, flow, "active") self.assertEqual(observation["result"].returncode, 42, observation["result"].stderr) self.assertEqual(observation["files"], observation["originals"]) self.assertNotRegex(observation["calls"], r"(?m)^stop\b") def test_manual_update_preserves_the_installed_pro_edition(self): # Follow the actual installer from installed identity through archive # admission. The version responders stand in for the two compiled # editions, but tar, SSH signature verification and replacement are real. cases = ( # name, installed reader, staged reply, path, version, expected refusal ("pro_default", "pro", "Pulse v6.6.0", "public", "", "Use the in-app updater"), ("pro_public_pin", "pro", "Pulse v6.6.0", "public", "v6.6.0", "Use the in-app updater"), ("pro_public_source", "pro", "Pulse v6.6.0", "source", "", "Use the in-app updater"), ("pro_wrong_edition", "pro", "Pulse v6.6.0", "local", "v6.6.0", "does not contain Pulse Pro"), ("pro_private_auto_version", "pro", "Pulse Pro v6.6.0", "local", "", ""), ("pro_private_bad_signature", "pro", "Pulse Pro v6.6.0", "local", "", "Cryptographic signature"), ("pro_private_wrong_version", "pro", "Pulse Pro v6.6.0", "local", "v6.6.1", "does not match"), ("community_public_update", "community", "Pulse v6.6.0", "public", "", ""), ("community_private_archive", "community", "Pulse Pro v6.6.0", "local", "v6.6.0", ""), ("legacy_community_update", "legacy", "Pulse v6.6.0", "public", "", ""), ("fresh_community_archive", "absent", "Pulse v6.6.0", "local", "v6.6.0", ""), ("unreadable_public_source", "failed", "Pulse v6.6.0", "source", "", "Cannot verify the installed Pulse edition"), ("broken_primary_with_legacy", "broken_link", "Pulse v6.6.0", "public", "v6.6.0", "Cannot verify the installed Pulse edition"), ("nonexecutable_primary_with_legacy", "nonexecutable", "Pulse v6.6.0", "public", "v6.6.0", "Cannot verify the installed Pulse edition"), ("unreadable_public_archive", "failed", "Pulse v6.6.0", "local", "v6.6.0", "does not contain Pulse Pro"), ("unreadable_malformed_pro_line", "failed", "Pulse Pro ambiguous v6.6.0", "local", "v6.6.0", "Cannot verify the staged Pulse Pro version"), ("unreadable_wrong_pro_version", "failed", "Pulse Pro v6.5.9", "local", "v6.6.0", "does not match"), ("unreadable_signed_pro_recovery", "failed", "Pulse Pro v6.6.0", "local", "", ""), ("unreadable_invalid_pro_signature", "failed", "Pulse Pro v6.6.0", "local", "", "Cryptographic signature"), ) for name, installed, staged, flow, version, refusal in cases: with self.subTest(name=name), tempfile.TemporaryDirectory() as directory: root = Path(directory) service_stub = root / "systemctl" service_stub.write_text(SYSTEMCTL) service_stub.chmod(0o700) binary = root / "install/bin/pulse" binary.parent.mkdir(parents=True) legacy = binary.parent.parent / "pulse" community = b"#!/bin/sh\nprintf '%s\\n' 'Pulse v6.5.0'\n" pro = b"#!/bin/sh\nprintf '%s\\n' 'Pulse Pro v6.5.0'\n" old_bytes = community if installed == "pro": old_bytes = pro elif installed == "failed": old_bytes = pro + b"exit 23\n" new_bytes = f"#!/bin/sh\nprintf '%s\\n' '{staged}'\n".encode() if installed in ("legacy", "broken_link", "nonexecutable"): legacy.write_bytes(community) legacy.chmod(0o755) if installed == "broken_link": binary.symlink_to(root / "missing-primary") elif installed not in ("legacy", "absent"): binary.write_bytes(old_bytes) binary.chmod(0o644 if installed == "nonexecutable" else 0o755) version_file = binary.parent.parent / "VERSION" if installed != "absent": version_file.write_text("v6.5.0\n") state = root / "config/system.json" state.parent.mkdir() state.write_text('{"updateChannel":"stable","fixture":"preserve"}\n') candidate = root / "candidate" candidate.write_bytes(new_bytes) candidate.chmod(0o755) candidate_version = root / "candidate-version" candidate_version.write_text("v6.6.0\n") # A Pro-shaped name is not evidence of the binary's edition. archive = root / "pulse-pro-v6.6.0-linux-amd64.tar.gz" with tarfile.open(archive, "w:gz") as payload: payload.add(candidate, arcname="bin/pulse") payload.add(candidate_version, arcname="VERSION") key = root / "key" subprocess.run(["ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-f", str(key)], check=True, capture_output=True, timeout=5) subprocess.run(["ssh-keygen", "-q", "-Y", "sign", "-f", str(key), "-n", "pulse-install", str(archive)], check=True, capture_output=True, timeout=5) (root / (archive.name + ".sig")).rename(root / (archive.name + ".sshsig")) if refusal == "Cryptographic signature": (root / (archive.name + ".sshsig")).write_text("invalid\n") env = {"PATH": str(root) + ":" + os.environ["PATH"], "HOME": directory, "FIXTURE": directory, "INSTALLER": str(INSTALLER), "FLOW": flow, "TARGET_VERSION": version, "ARCHIVE": str(archive), "CASE": "inactive" if installed == "absent" else "active", "FAULT": ""} result = subprocess.run(["bash", "-c", MANUAL_EDITION_UPDATE], env=env, capture_output=True, text=True, timeout=15) calls = (root / "calls").read_text() observed = result.stdout + result.stderr + calls self.assertIn("show pulse-custom", calls, observed) self.assertNotIn("UNEXPECTED_NETWORK", calls, observed) self.assertEqual(installed == "absent", "PACKAGES" in calls, observed) if refusal: self.assertNotEqual(0, result.returncode, observed) self.assertIn(refusal, observed) self.assertNotRegex(calls, r"(?m)^(stop|start|PUBLIC_LOOKUP|PUBLIC_ARCHIVE|SOURCE_BUILD)\b") if flow != "local": self.assertNotIn("BACKUP", calls) if installed == "broken_link": self.assertEqual(root / "missing-primary", binary.readlink()) else: self.assertEqual(old_bytes, binary.read_bytes()) self.assertEqual(0o644 if installed == "nonexecutable" else 0o755, binary.stat().st_mode & 0o777) self.assertEqual("v6.5.0\n", version_file.read_text()) else: self.assertEqual(0, result.returncode, observed) self.assertEqual(int(installed != "absent"), calls.count("stop pulse-custom"), observed) self.assertEqual(1, calls.count("start pulse-custom"), observed) self.assertEqual(new_bytes, binary.read_bytes()) self.assertEqual("v6.6.0\n", version_file.read_text()) self.assertEqual(flow == "public", "PUBLIC_ARCHIVE" in calls, observed) if flow == "local": self.assertNotIn("PUBLIC_LOOKUP", calls, observed) self.assertEqual('{"updateChannel":"stable","fixture":"preserve"}\n', state.read_text()) if installed in ("legacy", "broken_link", "nonexecutable"): self.assertEqual(community, legacy.read_bytes()) self.assertTrue(archive.is_file()) self.assertEqual([], list(binary.parent.glob(".pulse-stage-*"))) self.assertEqual([], list(binary.parent.parent.glob(".pulse-extract-*"))) class InstallerStopSchedulingTest(unittest.TestCase): """The faster schedule must retain every original adverse verdict.""" controls = ( ("test_refused_replacements_preserve_installed_bytes_and_modes", ("archive", "source"), ("stop_error", "stop_timeout", "query_error", "query_timeout", "still_active", "post_query_error", "post_transition", "post_failed", "activating", "deactivating", "reloading", "unknown", "empty", "malformed"), "stop_timeout"), ("test_archive_admission_failures_never_stop_or_mutate_pulse", ("archive", "local_archive"), ("missing_signature", "signature", "extract_directory", "extraction", "missing_binary", "architecture", "stage_directory", "copy", "permissions", "ownership", "version_exit", "version_timeout", "version_unknown", "version_mismatch"), "version_timeout"), ) def run_control(self, method, flows, cases, adverse, *, failed=None, exceptional=None): control = ServerInstallerStopTest(method) barrier = threading.Barrier(2) observations = [] archive = method == "test_archive_admission_failures_never_stop_or_mutate_pulse" def exercise(script, flow, case, fault=""): self.assertEqual(script, REPLACEMENT) self.assertEqual(case if archive else fault, "active" if archive else "") selected = fault if archive else case observations.append((flow, selected)) # Serial execution cannot rendezvous. These are synthetic results, # not shorter installer deadlines or interactions with a service. barrier.wait(timeout=2) if selected == adverse and flow == exceptional: raise RuntimeError("synthetic installer observation failure") status = 0 if selected == adverse and flow == failed else 1 originals = {"install/bin/pulse": b"old executable", "config/private-state": b"private state"} return { "result": subprocess.CompletedProcess([], status, "", "refusing binary replacement"), "files": originals.copy(), "originals": originals, "modes": {name: 0o600 for name in originals}, "calls": "", "stage_paths": [], "temporary_paths_remaining": [], "archive_retained": False, "local_archive_retained": True, } result = unittest.TestResult() with patch.object(control, "exercise", side_effect=exercise), \ patch(__name__ + ".ThreadPoolExecutor", wraps=ThreadPoolExecutor) as executor: control.run(result) executor.assert_called_once_with(max_workers=2) self.assertCountEqual(observations, itertools.product(flows, cases)) self.assertEqual(result.testsRun, 1) self.assertEqual(result.skipped, []) return result def test_each_flow_bad_result_still_fails_the_complete_matrix(self): for method, flows, cases, adverse in self.controls: for failed in (None, *flows): with self.subTest(method=method, failed=failed): result = self.run_control(method, flows, cases, adverse, failed=failed) self.assertEqual(result.errors, []) self.assertEqual(len(result.failures), int(failed is not None)) if failed is not None: self.assertIn(f"flow={failed!r}", result.failures[0][0].id()) def test_exception_does_not_drop_sibling_or_later_case_verdicts(self): for method, flows, cases, adverse in self.controls: for exceptional, failed in (flows, flows[::-1]): with self.subTest(method=method, exceptional=exceptional): result = self.run_control(method, flows, cases, adverse, failed=failed, exceptional=exceptional) self.assertEqual(len(result.errors), 1) self.assertIn(f"flow={exceptional!r}", result.errors[0][0].id()) self.assertIn("synthetic installer observation failure", result.errors[0][1]) self.assertEqual(len(result.failures), 1) self.assertIn(f"flow={failed!r}", result.failures[0][0].id()) if __name__ == "__main__": unittest.main()