| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- import json
- import subprocess
- import sys
- import unittest
- from pathlib import Path
- from unittest.mock import patch
- ROOT = Path(__file__).resolve().parents[1]
- sys.path.insert(0, str(ROOT / "scripts"))
- import deploy
- import reset_engine
- class FakeSystemd:
- def __init__(self, units, observer_result=0, final_differences=None):
- self.units = {
- name: {
- "present": True,
- "active": active,
- "enabled": enabled,
- }
- for name, active, enabled in units
- }
- self.commands = []
- self.observer_result = observer_result
- self.final_differences = final_differences or []
- def __call__(self, command, check=False, capture_output=False, text=False):
- self.commands.append(command)
- if command[:3] == ["systemctl", "--user", "show"]:
- unit = command[-1]
- state = self.units.get(unit, {"present": False})
- output = "loaded\n" if state["present"] else "not-found\n"
- return subprocess.CompletedProcess(command, 0, output, "")
- if command[:3] == ["systemctl", "--user", "is-active"]:
- active = self.units.get(command[-1], {}).get("active", False)
- return subprocess.CompletedProcess(command, 0 if active else 3, "", "")
- if command[:3] == ["systemctl", "--user", "is-enabled"]:
- enabled = self.units.get(command[-1], {}).get("enabled", False)
- return subprocess.CompletedProcess(command, 0 if enabled else 1, "", "")
- if command[:3] == ["systemctl", "--user", "stop"]:
- self.units.setdefault(command[-1], {})["active"] = False
- return subprocess.CompletedProcess(command, 0, "", "")
- if command[:3] == ["systemctl", "--user", "start"]:
- self.units.setdefault(command[-1], {})["active"] = True
- return subprocess.CompletedProcess(command, 0, "", "")
- if command[:3] == ["systemctl", "--user", "restart"]:
- self.units.setdefault(command[-1], {})["active"] = True
- return subprocess.CompletedProcess(command, 0, "", "")
- if command and command[0] == sys.executable:
- is_reconcile = "--reconcile-once" in command
- return subprocess.CompletedProcess(
- command,
- self.observer_result if is_reconcile else 0,
- (
- '{"differences": []}\n'
- if is_reconcile
- else json.dumps({"differences": self.final_differences}) + "\n"
- ),
- "",
- )
- return subprocess.CompletedProcess(command, 0, "", "")
- class ResetEngineTests(unittest.TestCase):
- def setUp(self):
- self.deployment = deploy.load_deployment()
- patch.object(deploy, "mod_host_port_is_open", return_value=False).start()
- patch.object(deploy, "mod_ui_port_is_open", return_value=False).start()
- self.addCleanup(patch.stopall)
- self.registry = {}
- def all_engine_units(self, timer_active=True):
- units = [
- ("pipewire.service", True, True),
- ("wireplumber.service", True, True),
- ("filter-chain.service", True, True),
- ("rigforge-pipewire-quantum.service", True, True),
- ("rigforge-mod-host-ports.service", True, True),
- ("rigforge-mod-host.service", True, True),
- ("rigforge-mod-ui.service", True, True),
- ("rigforge-midi-bridge.service", True, True),
- ("rigforge-sooperlooper.service", True, True),
- ("rigforge-slweb.service", True, True),
- ("rigforge-pipewire-reconcile.service", False, False),
- ("rigforge-pipewire-reconcile.timer", timer_active, True),
- ]
- return units
- def test_resets_services_in_order_restores_pedalboard_and_timer(self):
- runner = FakeSystemd(self.all_engine_units())
- with patch.object(reset_engine.deploy, "reset_mod_ui_reference") as reset_board:
- reset_engine.reset_engine(
- self.deployment, {"mod_ui": {}}, runner=runner
- )
- commands = runner.commands
- stop_timer = commands.index(
- ["systemctl", "--user", "stop", "rigforge-pipewire-reconcile.timer"]
- )
- stop_slweb = commands.index(
- ["systemctl", "--user", "stop", "rigforge-slweb.service"]
- )
- stop_wireplumber = commands.index(
- ["systemctl", "--user", "stop", "wireplumber.service"]
- )
- restart_pipewire = commands.index(
- ["systemctl", "--user", "restart", "pipewire.service"]
- )
- start_wireplumber = commands.index(
- ["systemctl", "--user", "start", "wireplumber.service"]
- )
- start_filter_chain = commands.index(
- ["systemctl", "--user", "start", "filter-chain.service"]
- )
- start_ports = commands.index(
- ["systemctl", "--user", "start", "rigforge-mod-host-ports.service"]
- )
- start_looper = commands.index(
- ["systemctl", "--user", "start", "rigforge-sooperlooper.service"]
- )
- start_mod_host = commands.index(
- ["systemctl", "--user", "start", "rigforge-mod-host.service"]
- )
- start_mod_ui = commands.index(
- ["systemctl", "--user", "start", "rigforge-mod-ui.service"]
- )
- start_midi = commands.index(
- ["systemctl", "--user", "start", "rigforge-midi-bridge.service"]
- )
- reconcile = next(
- index for index, command in enumerate(commands)
- if command and command[0] == sys.executable and "--reconcile-once" in command
- )
- start_timer = commands.index(
- ["systemctl", "--user", "start", "rigforge-pipewire-reconcile.timer"]
- )
- self.assertLess(stop_timer, stop_slweb)
- self.assertLess(stop_wireplumber, restart_pipewire)
- self.assertLess(restart_pipewire, start_wireplumber)
- self.assertLess(start_wireplumber, start_filter_chain)
- self.assertLess(start_filter_chain, start_ports)
- self.assertLess(start_ports, start_looper)
- self.assertLess(start_looper, start_mod_host)
- self.assertLess(start_mod_host, start_mod_ui)
- self.assertLess(start_mod_ui, start_midi)
- self.assertLess(start_midi, reconcile)
- self.assertLess(reconcile, start_timer)
- reset_board.assert_called_once_with(
- self.deployment["mod_ui"], {"mod_ui": {}}, manage_services=False
- )
- self.assertFalse(any("disable" in command for command in commands))
- def test_fails_without_resuming_reconciliation_if_graph_repair_fails(self):
- runner = FakeSystemd(self.all_engine_units(), observer_result=1)
- with patch.object(reset_engine.deploy, "reset_mod_ui_reference"):
- with self.assertRaisesRegex(RuntimeError, "command failed"):
- reset_engine.reset_engine(
- self.deployment, {"mod_ui": {}}, runner=runner
- )
- self.assertFalse(
- any(
- command == [
- "systemctl",
- "--user",
- "start",
- "rigforge-pipewire-reconcile.timer",
- ]
- for command in runner.commands
- )
- )
- def test_fails_if_read_only_post_check_reports_graph_differences(self):
- runner = FakeSystemd(
- self.all_engine_units(),
- final_differences=[{"kind": "missing", "from": "a", "to": "b"}],
- )
- with patch.object(reset_engine.deploy, "reset_mod_ui_reference"):
- with self.assertRaisesRegex(RuntimeError, "final PipeWire graph still differs"):
- reset_engine.reset_engine(
- self.deployment, {"mod_ui": {}}, runner=runner
- )
- self.assertFalse(
- any(
- command == [
- "systemctl",
- "--user",
- "start",
- "rigforge-pipewire-reconcile.timer",
- ]
- for command in runner.commands
- )
- )
- def test_only_enabled_or_active_services_are_restarted(self):
- runner = FakeSystemd(
- [
- ("pipewire.service", True, True),
- ("wireplumber.service", True, True),
- ("filter-chain.service", False, False),
- ("rigforge-pipewire-quantum.service", False, False),
- ("rigforge-mod-host-ports.service", False, False),
- ("rigforge-mod-host.service", False, False),
- ("rigforge-mod-ui.service", False, False),
- ("rigforge-midi-bridge.service", False, False),
- ("rigforge-sooperlooper.service", False, False),
- ("rigforge-slweb.service", False, False),
- ("rigforge-pipewire-reconcile.service", False, False),
- ("rigforge-pipewire-reconcile.timer", False, False),
- ]
- )
- with patch.object(reset_engine.deploy, "reset_mod_ui_reference"):
- reset_engine.reset_engine(self.deployment, {}, runner=runner)
- self.assertIn(
- ["systemctl", "--user", "restart", "pipewire.service"], runner.commands
- )
- self.assertFalse(
- any(
- command[:3] == ["systemctl", "--user", "start"]
- and command[-1].startswith("rigforge-")
- for command in runner.commands
- )
- )
- def test_refuses_to_reset_an_unmanaged_active_mod_ui(self):
- runner = FakeSystemd(self.all_engine_units())
- with self.assertRaisesRegex(RuntimeError, "not deployment-managed"):
- reset_engine.reset_engine(self.deployment, {}, runner=runner)
- self.assertFalse(
- any(command[:3] == ["systemctl", "--user", "stop"] for command in runner.commands)
- )
- if __name__ == "__main__":
- unittest.main()
|