test_reset_engine.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import json
  2. import subprocess
  3. import sys
  4. import unittest
  5. from pathlib import Path
  6. from unittest.mock import patch
  7. ROOT = Path(__file__).resolve().parents[1]
  8. sys.path.insert(0, str(ROOT / "scripts"))
  9. import deploy
  10. import reset_engine
  11. class FakeSystemd:
  12. def __init__(self, units, observer_result=0, final_differences=None):
  13. self.units = {
  14. name: {
  15. "present": True,
  16. "active": active,
  17. "enabled": enabled,
  18. }
  19. for name, active, enabled in units
  20. }
  21. self.commands = []
  22. self.observer_result = observer_result
  23. self.final_differences = final_differences or []
  24. def __call__(self, command, check=False, capture_output=False, text=False):
  25. self.commands.append(command)
  26. if command[:3] == ["systemctl", "--user", "show"]:
  27. unit = command[-1]
  28. state = self.units.get(unit, {"present": False})
  29. output = "loaded\n" if state["present"] else "not-found\n"
  30. return subprocess.CompletedProcess(command, 0, output, "")
  31. if command[:3] == ["systemctl", "--user", "is-active"]:
  32. active = self.units.get(command[-1], {}).get("active", False)
  33. return subprocess.CompletedProcess(command, 0 if active else 3, "", "")
  34. if command[:3] == ["systemctl", "--user", "is-enabled"]:
  35. enabled = self.units.get(command[-1], {}).get("enabled", False)
  36. return subprocess.CompletedProcess(command, 0 if enabled else 1, "", "")
  37. if command[:3] == ["systemctl", "--user", "stop"]:
  38. self.units.setdefault(command[-1], {})["active"] = False
  39. return subprocess.CompletedProcess(command, 0, "", "")
  40. if command[:3] == ["systemctl", "--user", "start"]:
  41. self.units.setdefault(command[-1], {})["active"] = True
  42. return subprocess.CompletedProcess(command, 0, "", "")
  43. if command[:3] == ["systemctl", "--user", "restart"]:
  44. self.units.setdefault(command[-1], {})["active"] = True
  45. return subprocess.CompletedProcess(command, 0, "", "")
  46. if command and command[0] == sys.executable:
  47. is_reconcile = "--reconcile-once" in command
  48. return subprocess.CompletedProcess(
  49. command,
  50. self.observer_result if is_reconcile else 0,
  51. (
  52. '{"differences": []}\n'
  53. if is_reconcile
  54. else json.dumps({"differences": self.final_differences}) + "\n"
  55. ),
  56. "",
  57. )
  58. return subprocess.CompletedProcess(command, 0, "", "")
  59. class ResetEngineTests(unittest.TestCase):
  60. def setUp(self):
  61. self.deployment = deploy.load_deployment()
  62. patch.object(deploy, "mod_host_port_is_open", return_value=False).start()
  63. patch.object(deploy, "mod_ui_port_is_open", return_value=False).start()
  64. self.addCleanup(patch.stopall)
  65. self.registry = {}
  66. def all_engine_units(self, timer_active=True):
  67. units = [
  68. ("pipewire.service", True, True),
  69. ("wireplumber.service", True, True),
  70. ("filter-chain.service", True, True),
  71. ("rigforge-pipewire-quantum.service", True, True),
  72. ("rigforge-mod-host-ports.service", True, True),
  73. ("rigforge-mod-host.service", True, True),
  74. ("rigforge-mod-ui.service", True, True),
  75. ("rigforge-midi-bridge.service", True, True),
  76. ("rigforge-sooperlooper.service", True, True),
  77. ("rigforge-slweb.service", True, True),
  78. ("rigforge-pipewire-reconcile.service", False, False),
  79. ("rigforge-pipewire-reconcile.timer", timer_active, True),
  80. ]
  81. return units
  82. def test_resets_services_in_order_restores_pedalboard_and_timer(self):
  83. runner = FakeSystemd(self.all_engine_units())
  84. with patch.object(reset_engine.deploy, "reset_mod_ui_reference") as reset_board:
  85. reset_engine.reset_engine(
  86. self.deployment, {"mod_ui": {}}, runner=runner
  87. )
  88. commands = runner.commands
  89. stop_timer = commands.index(
  90. ["systemctl", "--user", "stop", "rigforge-pipewire-reconcile.timer"]
  91. )
  92. stop_slweb = commands.index(
  93. ["systemctl", "--user", "stop", "rigforge-slweb.service"]
  94. )
  95. stop_wireplumber = commands.index(
  96. ["systemctl", "--user", "stop", "wireplumber.service"]
  97. )
  98. restart_pipewire = commands.index(
  99. ["systemctl", "--user", "restart", "pipewire.service"]
  100. )
  101. start_wireplumber = commands.index(
  102. ["systemctl", "--user", "start", "wireplumber.service"]
  103. )
  104. start_filter_chain = commands.index(
  105. ["systemctl", "--user", "start", "filter-chain.service"]
  106. )
  107. start_ports = commands.index(
  108. ["systemctl", "--user", "start", "rigforge-mod-host-ports.service"]
  109. )
  110. start_looper = commands.index(
  111. ["systemctl", "--user", "start", "rigforge-sooperlooper.service"]
  112. )
  113. start_mod_host = commands.index(
  114. ["systemctl", "--user", "start", "rigforge-mod-host.service"]
  115. )
  116. start_mod_ui = commands.index(
  117. ["systemctl", "--user", "start", "rigforge-mod-ui.service"]
  118. )
  119. start_midi = commands.index(
  120. ["systemctl", "--user", "start", "rigforge-midi-bridge.service"]
  121. )
  122. reconcile = next(
  123. index for index, command in enumerate(commands)
  124. if command and command[0] == sys.executable and "--reconcile-once" in command
  125. )
  126. start_timer = commands.index(
  127. ["systemctl", "--user", "start", "rigforge-pipewire-reconcile.timer"]
  128. )
  129. self.assertLess(stop_timer, stop_slweb)
  130. self.assertLess(stop_wireplumber, restart_pipewire)
  131. self.assertLess(restart_pipewire, start_wireplumber)
  132. self.assertLess(start_wireplumber, start_filter_chain)
  133. self.assertLess(start_filter_chain, start_ports)
  134. self.assertLess(start_ports, start_looper)
  135. self.assertLess(start_looper, start_mod_host)
  136. self.assertLess(start_mod_host, start_mod_ui)
  137. self.assertLess(start_mod_ui, start_midi)
  138. self.assertLess(start_midi, reconcile)
  139. self.assertLess(reconcile, start_timer)
  140. reset_board.assert_called_once_with(
  141. self.deployment["mod_ui"], {"mod_ui": {}}, manage_services=False
  142. )
  143. self.assertFalse(any("disable" in command for command in commands))
  144. def test_fails_without_resuming_reconciliation_if_graph_repair_fails(self):
  145. runner = FakeSystemd(self.all_engine_units(), observer_result=1)
  146. with patch.object(reset_engine.deploy, "reset_mod_ui_reference"):
  147. with self.assertRaisesRegex(RuntimeError, "command failed"):
  148. reset_engine.reset_engine(
  149. self.deployment, {"mod_ui": {}}, runner=runner
  150. )
  151. self.assertFalse(
  152. any(
  153. command == [
  154. "systemctl",
  155. "--user",
  156. "start",
  157. "rigforge-pipewire-reconcile.timer",
  158. ]
  159. for command in runner.commands
  160. )
  161. )
  162. def test_fails_if_read_only_post_check_reports_graph_differences(self):
  163. runner = FakeSystemd(
  164. self.all_engine_units(),
  165. final_differences=[{"kind": "missing", "from": "a", "to": "b"}],
  166. )
  167. with patch.object(reset_engine.deploy, "reset_mod_ui_reference"):
  168. with self.assertRaisesRegex(RuntimeError, "final PipeWire graph still differs"):
  169. reset_engine.reset_engine(
  170. self.deployment, {"mod_ui": {}}, runner=runner
  171. )
  172. self.assertFalse(
  173. any(
  174. command == [
  175. "systemctl",
  176. "--user",
  177. "start",
  178. "rigforge-pipewire-reconcile.timer",
  179. ]
  180. for command in runner.commands
  181. )
  182. )
  183. def test_only_enabled_or_active_services_are_restarted(self):
  184. runner = FakeSystemd(
  185. [
  186. ("pipewire.service", True, True),
  187. ("wireplumber.service", True, True),
  188. ("filter-chain.service", False, False),
  189. ("rigforge-pipewire-quantum.service", False, False),
  190. ("rigforge-mod-host-ports.service", False, False),
  191. ("rigforge-mod-host.service", False, False),
  192. ("rigforge-mod-ui.service", False, False),
  193. ("rigforge-midi-bridge.service", False, False),
  194. ("rigforge-sooperlooper.service", False, False),
  195. ("rigforge-slweb.service", False, False),
  196. ("rigforge-pipewire-reconcile.service", False, False),
  197. ("rigforge-pipewire-reconcile.timer", False, False),
  198. ]
  199. )
  200. with patch.object(reset_engine.deploy, "reset_mod_ui_reference"):
  201. reset_engine.reset_engine(self.deployment, {}, runner=runner)
  202. self.assertIn(
  203. ["systemctl", "--user", "restart", "pipewire.service"], runner.commands
  204. )
  205. self.assertFalse(
  206. any(
  207. command[:3] == ["systemctl", "--user", "start"]
  208. and command[-1].startswith("rigforge-")
  209. for command in runner.commands
  210. )
  211. )
  212. def test_refuses_to_reset_an_unmanaged_active_mod_ui(self):
  213. runner = FakeSystemd(self.all_engine_units())
  214. with self.assertRaisesRegex(RuntimeError, "not deployment-managed"):
  215. reset_engine.reset_engine(self.deployment, {}, runner=runner)
  216. self.assertFalse(
  217. any(command[:3] == ["systemctl", "--user", "stop"] for command in runner.commands)
  218. )
  219. if __name__ == "__main__":
  220. unittest.main()