test_deploy.py 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. import copy
  2. import io
  3. import json
  4. import os
  5. import shutil
  6. import sys
  7. import tarfile
  8. import tempfile
  9. import unittest
  10. from pathlib import Path
  11. from unittest.mock import patch
  12. ROOT = Path(__file__).resolve().parents[1]
  13. sys.path.insert(0, str(ROOT / "scripts"))
  14. import deploy
  15. class DeploymentTests(unittest.TestCase):
  16. def setUp(self):
  17. self.temporary = tempfile.TemporaryDirectory()
  18. self.home = Path(self.temporary.name) / "home"
  19. self.home.mkdir()
  20. self.registry_path = self.home / ".local/state/rigforge/deployment.json"
  21. self.commands = []
  22. self.run_patcher = patch.object(deploy, "run", side_effect=self.fake_run)
  23. self.run_patcher.start()
  24. self.home_patcher = patch.object(deploy.Path, "home", return_value=self.home)
  25. self.home_patcher.start()
  26. patch.object(deploy, "user_unit_is_active", return_value=False).start()
  27. patch.object(deploy, "user_unit_is_enabled", return_value=False).start()
  28. patch.object(deploy, "mod_host_port_is_open", return_value=False).start()
  29. patch.object(deploy, "mod_ui_port_is_open", return_value=False).start()
  30. self.root_patcher = patch.object(deploy, "ROOT", ROOT)
  31. self.root_patcher.start()
  32. self.registry_patcher = patch.object(deploy, "STATE_PATH", self.registry_path)
  33. self.registry_patcher.start()
  34. def tearDown(self):
  35. patch.stopall()
  36. self.temporary.cleanup()
  37. def fake_run(self, command):
  38. self.commands.append(command)
  39. def nam_config(self, tag="v0.2.3"):
  40. return {
  41. "tag": tag,
  42. "archive_url": (
  43. "https://github.com/mikeoliphant/neural-amp-modeler-lv2/releases/download/"
  44. f"{tag}/neural_amp_modeler_lv2_rpi5.tgz"
  45. ),
  46. "bundle_directory": "neural_amp_modeler.lv2",
  47. "backup_directory": ".local/state/rigforge/namlv2-backups",
  48. }
  49. def plugin_config(self, component):
  50. return deploy.load_deployment()[component]
  51. def make_plugin_archive(self, destination, config):
  52. if config["archive_format"] == "7z":
  53. destination.write_bytes(b"simulated 7z archive")
  54. return
  55. with tarfile.open(destination, "w:xz") as archive:
  56. for bundle_name, bundle_path in config["bundle_paths"].items():
  57. directory = tarfile.TarInfo(bundle_path)
  58. directory.type = tarfile.DIRTYPE
  59. archive.addfile(directory)
  60. for filename, content in {
  61. "manifest.ttl": b"<urn:plugin> a <urn:Plugin> .\\n",
  62. "plugin.so": b"binary",
  63. }.items():
  64. info = tarfile.TarInfo(f"{bundle_path}/{filename}")
  65. info.size = len(content)
  66. info.mode = 0o755 if filename.endswith(".so") else 0o644
  67. archive.addfile(info, io.BytesIO(content))
  68. def install_plugin(self, component, config=None):
  69. config = config or self.plugin_config(component)
  70. def download(url, destination):
  71. self.make_plugin_archive(destination, config)
  72. with patch.object(deploy, "download_archive", side_effect=download):
  73. if config["archive_format"] == "7z":
  74. def extract_7z(archive_path, bundle_paths, extraction_root):
  75. extracted = {}
  76. for name in bundle_paths:
  77. bundle = extraction_root / name
  78. bundle.mkdir()
  79. (bundle / "manifest.ttl").write_text("plugin manifest", encoding="utf-8")
  80. (bundle / "plugin.so").write_bytes(b"binary")
  81. extracted[name] = bundle
  82. return extracted
  83. with patch.object(deploy, "extract_7z_bundles", side_effect=extract_7z):
  84. deploy.deploy_lv2_archives("install", component, config, deploy.read_registry())
  85. else:
  86. deploy.deploy_lv2_archives("install", component, config, deploy.read_registry())
  87. def pipewire_config(self):
  88. return deploy.load_deployment()["pipewire"]
  89. def mod_host_config(self):
  90. return deploy.load_deployment()["mod_host"]
  91. def install_mod_host_ports_prerequisite(self):
  92. config = deploy.load_deployment()["mod_host_ports"]
  93. deploy.deploy_mod_host_ports("install", config, deploy.read_registry())
  94. self.commands.clear()
  95. def install_mod_ui_registry_prerequisite(self):
  96. registry = deploy.read_registry()
  97. registry["mod_ui"] = {"test_dependency": True}
  98. deploy.write_registry(registry)
  99. def sooperlooper_config(self):
  100. return deploy.load_deployment()["sooperlooper"]
  101. def slweb_config(self):
  102. return deploy.load_deployment()["slweb"]
  103. def reconcile_config(self):
  104. return deploy.load_deployment()["reconcile"]
  105. def midi_bridge_config(self):
  106. return deploy.load_deployment()["midi_bridge"]
  107. def make_archive(self, destination, files=None):
  108. files = files or {
  109. "manifest.ttl": b"<urn:nam> a <urn:Plugin> .\n",
  110. "neural_amp_modeler.so": b"binary",
  111. }
  112. with tarfile.open(destination, "w:gz") as archive:
  113. directory = tarfile.TarInfo("neural_amp_modeler.lv2")
  114. directory.type = tarfile.DIRTYPE
  115. archive.addfile(directory)
  116. for name, content in files.items():
  117. info = tarfile.TarInfo(f"neural_amp_modeler.lv2/{name}")
  118. info.size = len(content)
  119. archive.addfile(info, io.BytesIO(content))
  120. def make_slweb_archive(self, destination):
  121. files = {
  122. "tools/slweb.py": b"print('slweb')\n",
  123. "tools/slweb_web/index.html": b"<!doctype html>\n",
  124. "tools/slweb_web/app.css": b"body {}\n",
  125. "tools/slweb_web/app.js": b"// UI\n",
  126. "tools/other-tool.py": b"print('other tool')\n",
  127. }
  128. with tarfile.open(destination, "w:gz") as archive:
  129. root = tarfile.TarInfo("sooperlooper-mods-tag")
  130. root.type = tarfile.DIRTYPE
  131. archive.addfile(root)
  132. for relative_path, content in files.items():
  133. info = tarfile.TarInfo(f"sooperlooper-mods-tag/{relative_path}")
  134. info.size = len(content)
  135. info.mode = 0o644
  136. archive.addfile(info, io.BytesIO(content))
  137. def install_nam(self, config=None):
  138. config = config or self.nam_config()
  139. def download(url, destination):
  140. self.make_archive(destination)
  141. with patch.object(deploy, "download_archive", side_effect=download):
  142. deploy.deploy_namlv2("install", config, deploy.read_registry())
  143. def test_pipewire_install_is_atomic_scoped_and_idempotent(self):
  144. config = self.pipewire_config()
  145. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  146. installed = self.home / config["fragment_destination"]
  147. self.assertEqual(installed.read_bytes(), (ROOT / config["fragment_source"]).read_bytes())
  148. self.assertEqual(
  149. self.commands,
  150. [
  151. ["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "128"],
  152. ["systemctl", "--user", "restart", "filter-chain.service"],
  153. ["systemctl", "--user", "is-active", "--quiet", "filter-chain.service"],
  154. ["systemctl", "--user", "daemon-reload"],
  155. ["systemctl", "--user", "enable", "rigforge-pipewire-quantum.service"],
  156. ["systemctl", "--user", "restart", "rigforge-pipewire-quantum.service"],
  157. ["systemctl", "--user", "is-active", "--quiet", "rigforge-pipewire-quantum.service"],
  158. ],
  159. )
  160. self.commands.clear()
  161. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  162. self.assertEqual(
  163. self.commands,
  164. [
  165. ["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "128"],
  166. ["systemctl", "--user", "daemon-reload"],
  167. ["systemctl", "--user", "enable", "rigforge-pipewire-quantum.service"],
  168. ["systemctl", "--user", "start", "rigforge-pipewire-quantum.service"],
  169. ["systemctl", "--user", "is-active", "--quiet", "rigforge-pipewire-quantum.service"],
  170. ],
  171. )
  172. self.assertFalse(any("pipewire.service" in command for command in self.commands))
  173. def test_pipewire_quantum_service_reapplies_desired_value_after_engine_restart(self):
  174. unit = deploy.render_pipewire_quantum_service(self.pipewire_config(), 128)
  175. content = unit["content"].decode("utf-8")
  176. self.assertIn("After=pipewire.service", content)
  177. self.assertIn("PartOf=pipewire.service", content)
  178. self.assertIn("RemainAfterExit=yes", content)
  179. self.assertIn(
  180. 'ExecStart="/usr/bin/pw-metadata" -n settings 0 clock.force-quantum 128',
  181. content,
  182. )
  183. self.assertIn("WantedBy=pipewire.service", content)
  184. def test_pipewire_reload_updates_quantum_service_without_restarting_engine(self):
  185. config = self.pipewire_config()
  186. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  187. self.commands.clear()
  188. deploy.deploy_pipewire("reload", config, deploy.read_registry(), 128)
  189. self.assertIn(
  190. ["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "128"],
  191. self.commands,
  192. )
  193. self.assertIn(
  194. ["systemctl", "--user", "restart", "filter-chain.service"],
  195. self.commands,
  196. )
  197. self.assertFalse(any(command[:4] == ["systemctl", "--user", "restart", "pipewire.service"]
  198. for command in self.commands))
  199. def test_pipewire_uninstall_restores_previous_file(self):
  200. config = self.pipewire_config()
  201. destination = self.home / config["fragment_destination"]
  202. destination.parent.mkdir(parents=True)
  203. destination.write_text("previous config", encoding="utf-8")
  204. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  205. self.commands.clear()
  206. deploy.deploy_pipewire("uninstall", config, deploy.read_registry(), 128)
  207. self.assertEqual(destination.read_text(encoding="utf-8"), "previous config")
  208. self.assertEqual(
  209. self.commands[:2],
  210. [
  211. ["systemctl", "--user", "disable", "--now", "rigforge-pipewire-quantum.service"],
  212. ["systemctl", "--user", "daemon-reload"],
  213. ],
  214. )
  215. self.assertIn(
  216. ["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "0"],
  217. self.commands,
  218. )
  219. self.assertIn(
  220. ["systemctl", "--user", "restart", "filter-chain.service"],
  221. self.commands,
  222. )
  223. self.assertNotIn("pipewire", deploy.read_registry())
  224. self.assertNotIn("pipewire_quantum", deploy.read_registry())
  225. self.assertFalse(deploy.read_registry().get("pipewire_pending_reload"))
  226. def test_pipewire_pending_uninstall_recovers_after_prior_file_restored(self):
  227. config = self.pipewire_config()
  228. destination = self.home / config["fragment_destination"]
  229. destination.parent.mkdir(parents=True)
  230. destination.write_text("prior config", encoding="utf-8")
  231. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  232. registry = deploy.read_registry()
  233. registry["pipewire"]["pending_uninstall"] = True
  234. deploy.write_registry(registry)
  235. destination.write_text("prior config", encoding="utf-8")
  236. Path(registry["pipewire"]["backup"]).unlink()
  237. deploy.deploy_pipewire("uninstall", config, registry, 128)
  238. self.assertEqual(destination.read_text(encoding="utf-8"), "prior config")
  239. self.assertNotIn("pipewire", deploy.read_registry())
  240. def test_pipewire_pending_uninstall_recovers_after_fragment_removal(self):
  241. config = self.pipewire_config()
  242. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  243. destination = self.home / config["fragment_destination"]
  244. destination.unlink()
  245. registry = deploy.read_registry()
  246. registry["pipewire"]["pending_uninstall"] = True
  247. deploy.write_registry(registry)
  248. deploy.deploy_pipewire("uninstall", config, registry, 128)
  249. self.assertFalse(destination.exists())
  250. self.assertNotIn("pipewire", deploy.read_registry())
  251. self.assertEqual(self.commands[-3:], [
  252. ["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "0"],
  253. ["systemctl", "--user", "restart", "filter-chain.service"],
  254. ["systemctl", "--user", "is-active", "--quiet", "filter-chain.service"],
  255. ])
  256. def test_pipewire_uninstall_refuses_local_changes(self):
  257. config = self.pipewire_config()
  258. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  259. destination = self.home / config["fragment_destination"]
  260. destination.write_text("local edit", encoding="utf-8")
  261. self.commands.clear()
  262. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  263. deploy.deploy_pipewire("uninstall", config, deploy.read_registry(), 128)
  264. self.assertEqual(destination.read_text(encoding="utf-8"), "local edit")
  265. self.assertNotIn(
  266. ["systemctl", "--user", "disable", "--now", "rigforge-pipewire-quantum.service"],
  267. self.commands,
  268. )
  269. self.assertIn("pipewire_quantum", deploy.read_registry())
  270. def test_mod_host_install_renders_and_activates_user_service(self):
  271. config = self.mod_host_config()
  272. self.install_mod_host_ports_prerequisite()
  273. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  274. deploy.deploy_mod_host("install", config, deploy.read_registry())
  275. destination = self.home / config["service_destination"]
  276. content = destination.read_text(encoding="utf-8")
  277. self.assertIn("WantedBy=default.target", content)
  278. self.assertIn("After=pipewire.service wireplumber.service filter-chain.service rigforge-mod-host-ports.service", content)
  279. self.assertIn("PartOf=pipewire.service wireplumber.service filter-chain.service rigforge-mod-host-ports.service", content)
  280. self.assertIn("ExecStartPre=", content)
  281. self.assertIn("wait_mod_host_ports.py", content)
  282. self.assertIn("--timeout 45", content)
  283. self.assertIn("TimeoutStartSec=90", content)
  284. self.assertIn("-n -p 5555 -f 5556", content)
  285. self.assertNotIn("configure_mod_host.py", content)
  286. self.assertEqual(
  287. self.commands,
  288. [
  289. ["systemctl", "--user", "daemon-reload"],
  290. ["systemctl", "--user", "enable", "rigforge-mod-host.service"],
  291. ["systemctl", "--user", "restart", "rigforge-mod-host.service"],
  292. ["systemctl", "--user", "is-active", "--quiet", "rigforge-mod-host.service"],
  293. ],
  294. )
  295. registry = deploy.read_registry()
  296. self.assertTrue(registry["mod_host"]["installed_sha256"])
  297. self.assertNotIn("pending_activation", registry["mod_host"])
  298. self.assertNotIn("reconcile", registry)
  299. def test_mod_host_install_is_idempotent_without_restarting_active_audio(self):
  300. config = self.mod_host_config()
  301. self.install_mod_host_ports_prerequisite()
  302. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  303. deploy.deploy_mod_host("install", config, deploy.read_registry())
  304. self.commands.clear()
  305. deploy.deploy_mod_host("install", config, deploy.read_registry())
  306. self.assertEqual(
  307. self.commands,
  308. [
  309. ["systemctl", "--user", "daemon-reload"],
  310. ["systemctl", "--user", "enable", "rigforge-mod-host.service"],
  311. ["systemctl", "--user", "start", "rigforge-mod-host.service"],
  312. ["systemctl", "--user", "is-active", "--quiet", "rigforge-mod-host.service"],
  313. ],
  314. )
  315. def test_mod_host_install_restarts_when_runtime_configuration_changes(self):
  316. config = self.mod_host_config()
  317. self.install_mod_host_ports_prerequisite()
  318. with (
  319. patch.object(deploy, "mod_host_port_is_open", return_value=False),
  320. patch.object(deploy, "mod_host_runtime_digest", side_effect=["a" * 64, "b" * 64]),
  321. ):
  322. deploy.deploy_mod_host("install", config, deploy.read_registry())
  323. self.commands.clear()
  324. deploy.deploy_mod_host("install", config, deploy.read_registry())
  325. self.assertIn(
  326. ["systemctl", "--user", "restart", "rigforge-mod-host.service"],
  327. self.commands,
  328. )
  329. def test_mod_host_runtime_digest_ignores_global_reconcile_settings(self):
  330. config = self.mod_host_config()
  331. state_path = ROOT / "config" / "desired-state.yaml"
  332. with state_path.open(encoding="utf-8") as stream:
  333. original = deploy.yaml.safe_load(stream)
  334. changed_reconcile = copy.deepcopy(original)
  335. changed_reconcile["deployment"]["reconcile"]["interval_seconds"] = 30
  336. changed_mod_host = copy.deepcopy(original)
  337. changed_mod_host["deployment"]["mod_host"]["socket_port"] = 5556
  338. with patch.object(
  339. deploy.yaml,
  340. "safe_load",
  341. side_effect=[original, changed_reconcile],
  342. ):
  343. original_digest = deploy.mod_host_runtime_digest(config)
  344. reconcile_digest = deploy.mod_host_runtime_digest(config)
  345. self.assertEqual(original_digest, reconcile_digest)
  346. with patch.object(
  347. deploy.yaml,
  348. "safe_load",
  349. return_value=changed_mod_host,
  350. ):
  351. changed_digest = deploy.mod_host_runtime_digest(config)
  352. self.assertNotEqual(original_digest, changed_digest)
  353. def test_mod_host_uninstall_restores_previous_unit(self):
  354. config = self.mod_host_config()
  355. self.install_mod_host_ports_prerequisite()
  356. destination = self.home / config["service_destination"]
  357. destination.parent.mkdir(parents=True, exist_ok=True)
  358. destination.write_text("prior unit", encoding="utf-8")
  359. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  360. deploy.deploy_mod_host("install", config, deploy.read_registry())
  361. self.commands.clear()
  362. deploy.deploy_mod_host("uninstall", config, deploy.read_registry())
  363. self.assertEqual(destination.read_text(encoding="utf-8"), "prior unit")
  364. self.assertNotIn("mod_host", deploy.read_registry())
  365. self.assertEqual(
  366. self.commands,
  367. [
  368. ["systemctl", "--user", "disable", "--now", "rigforge-mod-host.service"],
  369. ["systemctl", "--user", "daemon-reload"],
  370. ],
  371. )
  372. def test_reconcile_install_and_uninstall_restores_prior_units(self):
  373. config = self.reconcile_config()
  374. prior_contents = {
  375. config["service_destination"]: "prior reconcile service",
  376. config["timer_destination"]: "prior reconcile timer",
  377. }
  378. for relative_path, content in prior_contents.items():
  379. destination = self.home / relative_path
  380. destination.parent.mkdir(parents=True, exist_ok=True)
  381. destination.write_text(content, encoding="utf-8")
  382. deploy.deploy_reconcile("install", config, deploy.read_registry())
  383. reconcile_service = self.home / config["service_destination"]
  384. reconcile_timer = self.home / config["timer_destination"]
  385. self.assertIn("--reconcile-once --quiet-if-clean", reconcile_service.read_text(encoding="utf-8"))
  386. self.assertIn("SuccessExitStatus=1", reconcile_service.read_text(encoding="utf-8"))
  387. self.assertNotIn("rigforge-mod-host.service", reconcile_service.read_text(encoding="utf-8"))
  388. self.assertIn("OnUnitActiveSec=15s", reconcile_timer.read_text(encoding="utf-8"))
  389. self.assertEqual(
  390. self.commands,
  391. [
  392. ["systemctl", "--user", "daemon-reload"],
  393. ["systemctl", "--user", "enable", "rigforge-pipewire-reconcile.timer"],
  394. ["systemctl", "--user", "restart", "rigforge-pipewire-reconcile.timer"],
  395. ["systemctl", "--user", "is-active", "--quiet", "rigforge-pipewire-reconcile.timer"],
  396. ],
  397. )
  398. self.commands.clear()
  399. deploy.deploy_reconcile("uninstall", config, deploy.read_registry())
  400. for relative_path, content in prior_contents.items():
  401. self.assertEqual((self.home / relative_path).read_text(encoding="utf-8"), content)
  402. self.assertNotIn("reconcile", deploy.read_registry())
  403. self.assertEqual(
  404. self.commands,
  405. [
  406. ["systemctl", "--user", "disable", "--now", "rigforge-pipewire-reconcile.timer"],
  407. ["systemctl", "--user", "stop", "rigforge-pipewire-reconcile.service"],
  408. ["systemctl", "--user", "daemon-reload"],
  409. ],
  410. )
  411. def test_reconcile_uninstall_refuses_local_unit_edits(self):
  412. config = self.reconcile_config()
  413. deploy.deploy_reconcile("install", config, deploy.read_registry())
  414. timer = self.home / config["timer_destination"]
  415. timer.write_text("local edit", encoding="utf-8")
  416. self.commands.clear()
  417. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  418. deploy.deploy_reconcile("uninstall", config, deploy.read_registry())
  419. self.assertEqual(timer.read_text(encoding="utf-8"), "local edit")
  420. self.assertEqual(self.commands, [])
  421. def test_reconcile_reload_runs_one_shot_without_restarting_mod_host(self):
  422. config = self.reconcile_config()
  423. deploy.deploy_reconcile("install", config, deploy.read_registry())
  424. self.commands.clear()
  425. deploy.deploy_reconcile("reload", config, deploy.read_registry())
  426. self.assertEqual(
  427. self.commands,
  428. [
  429. ["systemctl", "--user", "start", "rigforge-pipewire-reconcile.timer"],
  430. ["systemctl", "--user", "is-active", "--quiet", "rigforge-pipewire-reconcile.timer"],
  431. ["systemctl", "--user", "start", "rigforge-pipewire-reconcile.service"],
  432. ],
  433. )
  434. def test_midi_bridge_install_and_uninstall_manage_only_user_service(self):
  435. self.install_mod_ui_registry_prerequisite()
  436. config = self.midi_bridge_config()
  437. destination = self.home / config["service_destination"]
  438. destination.parent.mkdir(parents=True)
  439. destination.write_text("prior MIDI unit", encoding="utf-8")
  440. deploy.deploy_midi_bridge("install", config, deploy.read_registry())
  441. installed = destination.read_text(encoding="utf-8")
  442. self.assertIn("Description=Rigforge MIDI action bridge", installed)
  443. self.assertIn("midi_action_bridge.py\" --state", installed)
  444. self.assertIn("After=rigforge-mod-ui.service", installed)
  445. self.assertIn("Restart=on-failure", installed)
  446. self.assertEqual(
  447. self.commands,
  448. [
  449. ["systemctl", "--user", "daemon-reload"],
  450. ["systemctl", "--user", "enable", "rigforge-midi-bridge.service"],
  451. ["systemctl", "--user", "restart", "rigforge-midi-bridge.service"],
  452. ["systemctl", "--user", "is-active", "--quiet", "rigforge-midi-bridge.service"],
  453. ],
  454. )
  455. self.assertIn("midi_bridge", deploy.read_registry())
  456. self.commands.clear()
  457. deploy.deploy_midi_bridge("uninstall", config, deploy.read_registry())
  458. self.assertEqual(destination.read_text(encoding="utf-8"), "prior MIDI unit")
  459. self.assertNotIn("midi_bridge", deploy.read_registry())
  460. self.assertEqual(
  461. self.commands,
  462. [
  463. ["systemctl", "--user", "disable", "--now", "rigforge-midi-bridge.service"],
  464. ["systemctl", "--user", "daemon-reload"],
  465. ],
  466. )
  467. def test_midi_bridge_install_is_idempotent_and_reload_restarts_only_bridge(self):
  468. self.install_mod_ui_registry_prerequisite()
  469. config = self.midi_bridge_config()
  470. deploy.deploy_midi_bridge("install", config, deploy.read_registry())
  471. self.commands.clear()
  472. deploy.deploy_midi_bridge("install", config, deploy.read_registry())
  473. self.assertEqual(
  474. self.commands,
  475. [
  476. ["systemctl", "--user", "daemon-reload"],
  477. ["systemctl", "--user", "enable", "rigforge-midi-bridge.service"],
  478. ["systemctl", "--user", "start", "rigforge-midi-bridge.service"],
  479. ["systemctl", "--user", "is-active", "--quiet", "rigforge-midi-bridge.service"],
  480. ],
  481. )
  482. self.commands.clear()
  483. deploy.deploy_midi_bridge("reload", config, deploy.read_registry())
  484. self.assertEqual(
  485. self.commands,
  486. [
  487. ["systemctl", "--user", "restart", "rigforge-midi-bridge.service"],
  488. ["systemctl", "--user", "is-active", "--quiet", "rigforge-midi-bridge.service"],
  489. ],
  490. )
  491. def test_midi_bridge_uninstall_refuses_local_unit_edits(self):
  492. self.install_mod_ui_registry_prerequisite()
  493. config = self.midi_bridge_config()
  494. deploy.deploy_midi_bridge("install", config, deploy.read_registry())
  495. destination = self.home / config["service_destination"]
  496. destination.write_text("local change", encoding="utf-8")
  497. self.commands.clear()
  498. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  499. deploy.deploy_midi_bridge("uninstall", config, deploy.read_registry())
  500. self.assertEqual(destination.read_text(encoding="utf-8"), "local change")
  501. self.assertEqual(self.commands, [])
  502. def test_sooperlooper_service_uses_desired_counts_bindings_and_prefader_osc(self):
  503. config = self.sooperlooper_config()
  504. destination = self.home / config["service_destination"]
  505. destination.parent.mkdir(parents=True)
  506. destination.write_text("prior SooperLooper unit", encoding="utf-8")
  507. deploy.deploy_sooperlooper("install", config, deploy.read_registry())
  508. installed = destination.read_text(encoding="utf-8")
  509. binary = (ROOT / config["binary_source"]).resolve()
  510. binding_file = (ROOT / config["binding_file"]).resolve()
  511. self.assertIn(
  512. f'ExecStart="/usr/bin/pw-jack" "{binary}" -l 1 -c 4 -t 30 -m "{binding_file}"',
  513. installed,
  514. )
  515. self.assertIn(
  516. "ExecStartPost=/bin/sh -c 'sleep 4; exec \"/usr/bin/oscsend\" "
  517. "\"127.0.0.1\" 9951 /sl/0/set sf discrete_prefader 0'",
  518. installed,
  519. )
  520. self.assertIn("PartOf=pipewire.service wireplumber.service", installed)
  521. self.assertEqual(
  522. self.commands,
  523. [
  524. ["systemctl", "--user", "daemon-reload"],
  525. ["systemctl", "--user", "enable", "rigforge-sooperlooper.service"],
  526. ["systemctl", "--user", "restart", "rigforge-sooperlooper.service"],
  527. ["systemctl", "--user", "is-active", "--quiet", "rigforge-sooperlooper.service"],
  528. ],
  529. )
  530. self.assertIn("sooperlooper", deploy.read_registry())
  531. self.commands.clear()
  532. deploy.deploy_sooperlooper("uninstall", config, deploy.read_registry())
  533. self.assertEqual(destination.read_text(encoding="utf-8"), "prior SooperLooper unit")
  534. self.assertNotIn("sooperlooper", deploy.read_registry())
  535. self.assertEqual(
  536. self.commands,
  537. [
  538. ["systemctl", "--user", "disable", "--now", "rigforge-sooperlooper.service"],
  539. ["systemctl", "--user", "daemon-reload"],
  540. ],
  541. )
  542. def test_sooperlooper_reload_refuses_local_unit_edits(self):
  543. config = self.sooperlooper_config()
  544. deploy.deploy_sooperlooper("install", config, deploy.read_registry())
  545. destination = self.home / config["service_destination"]
  546. destination.write_text("local change", encoding="utf-8")
  547. self.commands.clear()
  548. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  549. deploy.deploy_sooperlooper("reload", config, deploy.read_registry())
  550. self.assertEqual(destination.read_text(encoding="utf-8"), "local change")
  551. self.assertEqual(self.commands, [])
  552. def test_slweb_install_fetches_tools_and_activates_after_sooperlooper(self):
  553. config = self.slweb_config()
  554. sooperlooper = self.sooperlooper_config()
  555. registry = deploy.read_registry()
  556. registry["sooperlooper"] = {"installed_sha256": "engine"}
  557. deploy.write_registry(registry)
  558. archive_urls = []
  559. def download(url, destination):
  560. archive_urls.append(url)
  561. self.make_slweb_archive(destination)
  562. with patch.object(deploy, "download_archive", side_effect=download):
  563. deploy.deploy_slweb("install", config, sooperlooper, deploy.read_registry())
  564. installed_dir = self.home / config["install_directory"]
  565. self.assertTrue((installed_dir / "tools/slweb.py").is_file())
  566. self.assertTrue((installed_dir / "tools/slweb_web/index.html").is_file())
  567. self.assertTrue((installed_dir / "tools/other-tool.py").is_file())
  568. self.assertFalse((installed_dir / "src").exists())
  569. self.assertEqual(
  570. archive_urls,
  571. [
  572. "https://github.com/jojolb/sooperlooper-mods/archive/refs/tags/"
  573. "v1.7.9-mods.1-slweb.tar.gz"
  574. ],
  575. )
  576. unit_path = self.home / config["service_destination"]
  577. unit = unit_path.read_text(encoding="utf-8")
  578. self.assertIn("Requires=rigforge-sooperlooper.service", unit)
  579. self.assertIn("After=rigforge-sooperlooper.service", unit)
  580. self.assertIn("PartOf=rigforge-sooperlooper.service", unit)
  581. self.assertIn(f"WorkingDirectory={installed_dir}", unit)
  582. self.assertNotIn(f'WorkingDirectory="{installed_dir}"', unit)
  583. self.assertIn(
  584. f'ExecStart="/usr/bin/python3" tools/slweb.py --osc-host 127.0.0.1 '
  585. f'--osc-port {sooperlooper["osc"]["port"]} --http-host 0.0.0.0 --http-port 8080',
  586. unit,
  587. )
  588. self.assertEqual(
  589. self.commands,
  590. [
  591. ["systemctl", "--user", "daemon-reload"],
  592. ["systemctl", "--user", "enable", "rigforge-slweb.service"],
  593. ["systemctl", "--user", "restart", "rigforge-slweb.service"],
  594. ["systemctl", "--user", "is-active", "--quiet", "rigforge-slweb.service"],
  595. ],
  596. )
  597. self.assertEqual(
  598. deploy.systemd_path_value("/home/test user/rigforge%"),
  599. "/home/test\\x20user/rigforge%%",
  600. )
  601. self.assertIn("slweb", deploy.read_registry())
  602. self.assertIn("slweb_service", deploy.read_registry())
  603. self.commands.clear()
  604. with patch.object(deploy, "download_archive") as download:
  605. deploy.deploy_slweb("install", config, sooperlooper, deploy.read_registry())
  606. download.assert_not_called()
  607. self.assertEqual(
  608. self.commands,
  609. [
  610. ["systemctl", "--user", "daemon-reload"],
  611. ["systemctl", "--user", "enable", "rigforge-slweb.service"],
  612. ["systemctl", "--user", "start", "rigforge-slweb.service"],
  613. ["systemctl", "--user", "is-active", "--quiet", "rigforge-slweb.service"],
  614. ],
  615. )
  616. self.commands.clear()
  617. deploy.deploy_slweb("uninstall", config, sooperlooper, deploy.read_registry())
  618. self.assertFalse(installed_dir.exists())
  619. self.assertNotIn("slweb", deploy.read_registry())
  620. self.assertNotIn("slweb_service", deploy.read_registry())
  621. self.assertEqual(
  622. self.commands,
  623. [
  624. ["systemctl", "--user", "disable", "--now", "rigforge-slweb.service"],
  625. ["systemctl", "--user", "daemon-reload"],
  626. ],
  627. )
  628. def test_slweb_preserves_prior_tools_and_refuses_local_changes(self):
  629. config = self.slweb_config()
  630. sooperlooper = self.sooperlooper_config()
  631. destination = self.home / config["install_directory"]
  632. destination.mkdir(parents=True)
  633. (destination / "owner-file").write_text("keep me", encoding="utf-8")
  634. registry = deploy.read_registry()
  635. registry["sooperlooper"] = {"installed_sha256": "engine"}
  636. deploy.write_registry(registry)
  637. with patch.object(
  638. deploy, "download_archive", side_effect=lambda _url, path: self.make_slweb_archive(path)
  639. ):
  640. deploy.deploy_slweb("install", config, sooperlooper, deploy.read_registry())
  641. backup = destination.with_name(destination.name + ".rigforge-backup")
  642. self.assertEqual((backup / "owner-file").read_text(encoding="utf-8"), "keep me")
  643. (destination / "tools/slweb.py").write_text("local modification", encoding="utf-8")
  644. self.commands.clear()
  645. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  646. deploy.deploy_slweb("uninstall", config, sooperlooper, deploy.read_registry())
  647. self.assertTrue((destination / "tools/slweb.py").exists())
  648. self.assertTrue(backup.is_dir())
  649. self.assertEqual(self.commands, [])
  650. (destination / "tools/slweb.py").write_text("print('slweb')\n", encoding="utf-8")
  651. deploy.deploy_slweb("uninstall", config, sooperlooper, deploy.read_registry())
  652. self.assertFalse((destination / "tools/slweb.py").exists())
  653. self.assertEqual((destination / "owner-file").read_text(encoding="utf-8"), "keep me")
  654. def test_slweb_install_requires_sooperlooper_service(self):
  655. with patch.object(deploy, "download_archive") as download:
  656. with self.assertRaisesRegex(RuntimeError, "install sooperlooper before"):
  657. deploy.deploy_slweb(
  658. "install",
  659. self.slweb_config(),
  660. self.sooperlooper_config(),
  661. deploy.read_registry(),
  662. )
  663. download.assert_not_called()
  664. def test_mod_host_install_and_uninstall_do_not_manage_reconcile_timer(self):
  665. reconcile_config = self.reconcile_config()
  666. mod_host_config = self.mod_host_config()
  667. self.install_mod_host_ports_prerequisite()
  668. deploy.deploy_reconcile("install", reconcile_config, deploy.read_registry())
  669. reconcile_paths = {
  670. reconcile_config["service_destination"],
  671. reconcile_config["timer_destination"],
  672. }
  673. self.commands.clear()
  674. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  675. deploy.deploy_mod_host("install", mod_host_config, deploy.read_registry())
  676. deploy.deploy_mod_host("uninstall", mod_host_config, deploy.read_registry())
  677. registry = deploy.read_registry()
  678. self.assertIn("reconcile", registry)
  679. for relative_path in reconcile_paths:
  680. self.assertTrue((self.home / relative_path).is_file())
  681. self.assertFalse(any("rigforge-pipewire-reconcile" in " ".join(command) for command in self.commands))
  682. def test_mod_host_pending_uninstall_recovers_after_prior_unit_is_restored(self):
  683. config = self.mod_host_config()
  684. self.install_mod_host_ports_prerequisite()
  685. destination = self.home / config["service_destination"]
  686. destination.parent.mkdir(parents=True, exist_ok=True)
  687. destination.write_text("prior unit", encoding="utf-8")
  688. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  689. deploy.deploy_mod_host("install", config, deploy.read_registry())
  690. registry = deploy.read_registry()
  691. backup = Path(registry["mod_host"]["backup"])
  692. registry["mod_host"]["pending_uninstall"] = True
  693. deploy.write_registry(registry)
  694. destination.write_text("prior unit", encoding="utf-8")
  695. backup.unlink()
  696. self.commands.clear()
  697. deploy.deploy_mod_host("uninstall", config, registry)
  698. self.assertEqual(destination.read_text(encoding="utf-8"), "prior unit")
  699. self.assertNotIn("mod_host", deploy.read_registry())
  700. self.assertEqual(self.commands, [["systemctl", "--user", "daemon-reload"]])
  701. def test_mod_host_uninstall_refuses_local_unit_edits(self):
  702. config = self.mod_host_config()
  703. self.install_mod_host_ports_prerequisite()
  704. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  705. deploy.deploy_mod_host("install", config, deploy.read_registry())
  706. destination = self.home / config["service_destination"]
  707. destination.write_text("local edit", encoding="utf-8")
  708. self.commands.clear()
  709. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  710. deploy.deploy_mod_host("uninstall", config, deploy.read_registry())
  711. self.assertEqual(destination.read_text(encoding="utf-8"), "local edit")
  712. self.assertEqual(self.commands, [])
  713. def test_mod_host_ports_service_is_managed_scoped_and_ordered(self):
  714. config = deploy.load_deployment()["mod_host_ports"]
  715. destination = self.home / config["service_destination"]
  716. deploy.deploy_mod_host_ports("install", config, deploy.read_registry())
  717. content = destination.read_text(encoding="utf-8")
  718. self.assertIn("Description=Rigforge PipeWire audio boundary ports for mod-host", content)
  719. self.assertIn("run_mod_host_ports.py", content)
  720. self.assertIn("--pw-loopback \"/usr/bin/pw-loopback\"", content)
  721. self.assertIn("Wants=pipewire.service wireplumber.service", content)
  722. self.assertEqual(
  723. self.commands,
  724. [
  725. ["systemctl", "--user", "daemon-reload"],
  726. ["systemctl", "--user", "enable", "rigforge-mod-host-ports.service"],
  727. ["systemctl", "--user", "restart", "rigforge-mod-host-ports.service"],
  728. ["systemctl", "--user", "is-active", "--quiet", "rigforge-mod-host-ports.service"],
  729. ],
  730. )
  731. self.assertIn("mod_host_ports", deploy.read_registry())
  732. self.commands.clear()
  733. deploy.deploy_mod_host_ports("uninstall", config, deploy.read_registry())
  734. self.assertNotIn("mod_host_ports", deploy.read_registry())
  735. self.assertEqual(
  736. self.commands,
  737. [
  738. ["systemctl", "--user", "disable", "--now", "rigforge-mod-host-ports.service"],
  739. ["systemctl", "--user", "daemon-reload"],
  740. ],
  741. )
  742. def test_mod_ui_service_uses_reference_board_and_port_bridge(self):
  743. registry = {
  744. "mod_host_ports": {},
  745. "mod_host": {},
  746. }
  747. config = deploy.load_deployment()["mod_ui"]
  748. destination = self.home / config["service_destination"]
  749. deploy.deploy_mod_ui("install", config, registry)
  750. content = destination.read_text(encoding="utf-8")
  751. self.assertIn("run_mod_ui_service.py", content)
  752. self.assertIn("prepare_mod_ui_pedalboard.py", content)
  753. self.assertIn("ExecStartPre=", content)
  754. self.assertIn("--runtime-dir %t/rigforge-mod-ui", content)
  755. self.assertIn("MOD_DEFAULT_PEDALBOARD=", content)
  756. self.assertIn("MOD_RIGFORGE_CAPTURE_PORTS=", content)
  757. self.assertIn("MOD_RIGFORGE_PLAYBACK_PORTS=", content)
  758. self.assertIn(
  759. f'MOD_USER_FILES_DIR="{self.home / config["user_files_directory"]}"',
  760. content,
  761. )
  762. self.assertIn(
  763. "rigforge.mod-host-input.capture:capture_AUX0,rigforge.mod-host-input.capture:capture_AUX1,"
  764. "rigforge.mod-host-input.capture:capture_AUX2,rigforge.mod-host-input.capture:capture_AUX3",
  765. content,
  766. )
  767. self.assertIn(
  768. "rigforge.mod-host-output.playback:playback_AUX0,rigforge.mod-host-output.playback:playback_AUX1,"
  769. "rigforge.mod-host-output.playback:playback_AUX2,rigforge.mod-host-output.playback:playback_AUX3",
  770. content,
  771. )
  772. self.assertIn("Requires=rigforge-mod-host.service rigforge-mod-host-ports.service", content)
  773. self.assertIn("pw-jack", content)
  774. def test_mod_ui_manages_declared_nam_models_for_local_picker(self):
  775. registry = {
  776. "mod_host_ports": {},
  777. "mod_host": {},
  778. }
  779. config = deploy.load_deployment()["mod_ui"]
  780. deploy.deploy_mod_ui("install", config, registry)
  781. model_root = self.home / config["user_files_directory"] / "NAM Models" / "Rigforge"
  782. state = deploy.yaml.safe_load(
  783. (ROOT / "config" / "desired-state.yaml").read_text(encoding="utf-8")
  784. )
  785. model_names = set(deploy.mod_ui_nam_model_sources(state))
  786. self.assertEqual({path.name for path in model_root.iterdir()}, model_names)
  787. reference = Path(
  788. deploy.read_registry()["mod_ui"]["reference_bundle"]["destination"]
  789. )
  790. ttl = (reference / "rigforge-guitar.ttl").read_text(encoding="utf-8")
  791. self.assertIn(str(model_root / "Vox_AC15CH_Crystal_Clean_TB.nam"), ttl)
  792. deploy.deploy_mod_ui("uninstall", config, deploy.read_registry())
  793. self.assertFalse(model_root.exists())
  794. self.assertNotIn("mod_ui_nam_models", deploy.read_registry())
  795. def test_mod_ui_refuses_to_remove_locally_modified_nam_model(self):
  796. registry = {
  797. "mod_host_ports": {},
  798. "mod_host": {},
  799. }
  800. config = deploy.load_deployment()["mod_ui"]
  801. deploy.deploy_mod_ui("install", config, registry)
  802. model = (
  803. self.home
  804. / config["user_files_directory"]
  805. / "NAM Models"
  806. / "Rigforge"
  807. / "Vox_AC15CH_Crunch_TB.nam"
  808. )
  809. model.write_bytes(b"local change")
  810. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  811. deploy.deploy_mod_ui("uninstall", config, deploy.read_registry())
  812. self.assertEqual(model.read_bytes(), b"local change")
  813. self.assertIn("mod_ui_nam_models", deploy.read_registry())
  814. def test_mod_ui_reference_bundle_is_immutable_after_install(self):
  815. config = deploy.load_deployment()["mod_ui"]
  816. registry = {"mod_host_ports": {}, "mod_host": {}}
  817. deploy.deploy_mod_ui("install", config, registry)
  818. reference = Path(deploy.read_registry()["mod_ui"]["reference_bundle"]["destination"])
  819. ttl = reference / "rigforge-guitar.ttl"
  820. ttl.write_bytes(ttl.read_bytes() + b"\n# local change\n")
  821. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  822. deploy.deploy_mod_ui("install", config, deploy.read_registry())
  823. def test_mod_ui_install_repairs_registered_corrupt_reference_bundle(self):
  824. config = deploy.load_deployment()["mod_ui"]
  825. registry = {
  826. "mod_host_ports": {},
  827. "mod_host": {},
  828. }
  829. deploy.deploy_mod_ui("install", config, registry)
  830. registry = deploy.read_registry()
  831. reference = Path(registry["mod_ui"]["reference_bundle"]["destination"])
  832. graph = reference / "rigforge-guitar.ttl"
  833. graph.write_bytes(b"")
  834. registry["mod_ui"]["reference_bundle"]["installed_sha256"] = deploy.tree_sha256(
  835. reference
  836. )
  837. deploy.write_registry(registry)
  838. self.commands.clear()
  839. deploy.deploy_mod_ui("install", config, deploy.read_registry())
  840. repaired_registry = deploy.read_registry()
  841. repaired_reference = Path(
  842. repaired_registry["mod_ui"]["reference_bundle"]["destination"]
  843. )
  844. self.assertGreater((repaired_reference / "rigforge-guitar.ttl").stat().st_size, 0)
  845. corrupt_backup = repaired_reference.with_name(
  846. repaired_reference.name + ".rigforge-corrupt-backup"
  847. )
  848. self.assertEqual(
  849. (corrupt_backup / "rigforge-guitar.ttl").read_bytes(),
  850. b"",
  851. )
  852. self.assertEqual(
  853. repaired_registry["mod_ui"]["reference_bundle"]["installed_sha256"],
  854. deploy.tree_sha256(repaired_reference),
  855. )
  856. def test_mod_ui_force_install_backs_up_and_replaces_registry_hash_mismatch(self):
  857. config = deploy.load_deployment()["mod_ui"]
  858. registry = {
  859. "mod_host_ports": {},
  860. "mod_host": {},
  861. }
  862. deploy.deploy_mod_ui("install", config, registry)
  863. registry = deploy.read_registry()
  864. reference = Path(registry["mod_ui"]["reference_bundle"]["destination"])
  865. graph = reference / "rigforge-guitar.ttl"
  866. graph.write_bytes(b"changed outside deploy\n")
  867. original = graph.read_bytes()
  868. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  869. deploy.deploy_mod_ui("install", config, deploy.read_registry())
  870. self.assertEqual(graph.read_bytes(), original)
  871. deploy.deploy_mod_ui(
  872. "install", config, deploy.read_registry(), force_reference=True
  873. )
  874. repaired_registry = deploy.read_registry()
  875. repaired_reference = Path(
  876. repaired_registry["mod_ui"]["reference_bundle"]["destination"]
  877. )
  878. backup = repaired_reference.with_name(
  879. repaired_reference.name + ".rigforge-force-backup"
  880. )
  881. self.assertEqual((backup / "rigforge-guitar.ttl").read_bytes(), original)
  882. self.assertGreater((repaired_reference / "rigforge-guitar.ttl").stat().st_size, 0)
  883. self.assertEqual(
  884. repaired_registry["mod_ui"]["reference_bundle"]["installed_sha256"],
  885. deploy.tree_sha256(repaired_reference),
  886. )
  887. def test_mod_ui_force_install_requires_a_registered_reference_bundle(self):
  888. config = deploy.load_deployment()["mod_ui"]
  889. registry = {
  890. "mod_host_ports": {},
  891. "mod_host": {},
  892. }
  893. state = deploy.yaml.safe_load(
  894. (ROOT / "config" / "desired-state.yaml").read_text(encoding="utf-8")
  895. )
  896. deploy.manage_mod_ui_nam_models("install", config, registry, state)
  897. reference = Path(deploy.render_mod_ui_service(config)["reference_bundle"])
  898. reference.mkdir(parents=True)
  899. (reference / "local-file").write_text("do not replace", encoding="utf-8")
  900. with self.assertRaisesRegex(RuntimeError, "requires a registry-managed"):
  901. deploy.deploy_mod_ui(
  902. "install", config, registry, force_reference=True
  903. )
  904. self.assertEqual(
  905. (reference / "local-file").read_text(encoding="utf-8"),
  906. "do not replace",
  907. )
  908. def test_mod_ui_reset_restores_reference_and_backs_up_user_state(self):
  909. registry = {
  910. "mod_host_ports": {},
  911. "mod_host": {},
  912. }
  913. config = deploy.load_deployment()["mod_ui"]
  914. deploy.deploy_mod_ui("install", config, registry)
  915. registry = deploy.read_registry()
  916. board = self.home / config["pedalboards_directory"] / "default.pedalboard"
  917. board.mkdir(parents=True)
  918. (board / "user-edit.txt").write_text("keep this backup", encoding="utf-8")
  919. last_state = self.home / config["last_state_file"]
  920. last_state.parent.mkdir(parents=True, exist_ok=True)
  921. last_state.write_text('{"bank":0,"pedalboard":"/old/board"}\n', encoding="utf-8")
  922. self.commands.clear()
  923. deploy.deploy_mod_ui("reset", config, registry)
  924. reference = Path(
  925. deploy.read_registry()["mod_ui"]["reference_bundle"]["destination"]
  926. )
  927. self.assertEqual((board / "rigforge-guitar.ttl").read_bytes(),
  928. (reference / "rigforge-guitar.ttl").read_bytes())
  929. self.assertTrue(
  930. (board.with_name("default.pedalboard.rigforge-reset-backup") / "user-edit.txt").is_file()
  931. )
  932. self.assertFalse(last_state.exists())
  933. self.assertTrue(
  934. last_state.with_name(last_state.name + ".rigforge-reset-backup").is_file()
  935. )
  936. self.assertEqual(
  937. self.commands,
  938. [
  939. ["systemctl", "--user", "stop", "rigforge-mod-ui.service"],
  940. ["systemctl", "--user", "start", "rigforge-mod-ui.service"],
  941. ["systemctl", "--user", "is-active", "--quiet", "rigforge-mod-ui.service"],
  942. ],
  943. )
  944. def test_mod_host_install_refuses_an_existing_control_socket(self):
  945. self.install_mod_host_ports_prerequisite()
  946. with patch.object(deploy, "mod_host_port_is_open", return_value=True):
  947. with self.assertRaisesRegex(RuntimeError, "already in use"):
  948. deploy.deploy_mod_host("install", self.mod_host_config(), deploy.read_registry())
  949. self.assertEqual(self.commands, [])
  950. self.assertFalse((self.home / self.mod_host_config()["service_destination"]).exists())
  951. def test_mod_host_reload_restarts_and_checks_the_user_service(self):
  952. config = self.mod_host_config()
  953. self.install_mod_host_ports_prerequisite()
  954. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  955. deploy.deploy_mod_host("install", config, deploy.read_registry())
  956. self.commands.clear()
  957. deploy.deploy_mod_host("reload", config, deploy.read_registry())
  958. self.assertEqual(
  959. self.commands,
  960. [
  961. ["systemctl", "--user", "restart", "rigforge-mod-host.service"],
  962. ["systemctl", "--user", "is-active", "--quiet", "rigforge-mod-host.service"],
  963. ],
  964. )
  965. def test_lsp_install_uninstall_restores_existing_bundle_and_is_idempotent(self):
  966. component = "lsp"
  967. config = self.plugin_config(component)
  968. destination = self.home / ".lv2/lsp-plugins.lv2"
  969. destination.mkdir(parents=True)
  970. (destination / "old.txt").write_text("prior plugin", encoding="utf-8")
  971. self.install_plugin(component, config)
  972. self.assertTrue((destination / "manifest.ttl").is_file())
  973. registry = deploy.read_registry()
  974. self.assertEqual(registry[component]["tag"], "1.2.37")
  975. self.assertTrue(registry[component]["archive_sha256"])
  976. self.commands.clear()
  977. with patch.object(deploy, "download_archive", side_effect=AssertionError("must not download")):
  978. deploy.deploy_lv2_archives("install", component, config, registry)
  979. deploy.deploy_lv2_archives("uninstall", component, config, registry)
  980. self.assertEqual((destination / "old.txt").read_text(encoding="utf-8"), "prior plugin")
  981. self.assertNotIn(component, deploy.read_registry())
  982. def test_dragonfly_install_and_uninstall_manage_all_bundles(self):
  983. component = "dragonfly_reverb"
  984. config = self.plugin_config(component)
  985. first_name = next(iter(config["bundle_paths"]))
  986. first_destination = self.home / ".lv2" / first_name
  987. first_destination.mkdir(parents=True)
  988. (first_destination / "old.txt").write_text("prior plugin", encoding="utf-8")
  989. self.install_plugin(component, config)
  990. registry = deploy.read_registry()
  991. self.assertEqual(set(registry[component]["bundles"]), set(config["bundle_paths"]))
  992. for name in config["bundle_paths"]:
  993. self.assertTrue((self.home / ".lv2" / name / "manifest.ttl").is_file())
  994. deploy.deploy_lv2_archives("uninstall", component, config, registry)
  995. self.assertEqual((first_destination / "old.txt").read_text(encoding="utf-8"), "prior plugin")
  996. self.assertNotIn(component, deploy.read_registry())
  997. def test_plugin_update_preserves_old_bundles_when_staging_fails(self):
  998. component = "dragonfly_reverb"
  999. config = self.plugin_config(component)
  1000. self.install_plugin(component, config)
  1001. destinations = {
  1002. name: self.home / ".lv2" / name for name in config["bundle_paths"]
  1003. }
  1004. original_hashes = {name: deploy.tree_sha256(path) for name, path in destinations.items()}
  1005. updated = dict(config, tag="3.2.11")
  1006. def fail_copy(source, destination, **kwargs):
  1007. if Path(destination).name == "DragonflyPlateReverb.lv2.rigforge-new":
  1008. raise OSError("simulated staging failure")
  1009. return original_copytree(source, destination, **kwargs)
  1010. original_copytree = deploy.shutil.copytree
  1011. with patch.object(deploy, "download_archive", side_effect=lambda url, path: self.make_plugin_archive(path, config)):
  1012. with patch.object(deploy, "extract_tar_bundles", return_value={name: self.home / "source" / name for name in config["bundle_paths"]}):
  1013. source_root = self.home / "source"
  1014. for name in config["bundle_paths"]:
  1015. bundle = source_root / name
  1016. bundle.mkdir(parents=True)
  1017. (bundle / "manifest.ttl").write_text("new plugin", encoding="utf-8")
  1018. with patch.object(deploy.shutil, "copytree", side_effect=fail_copy):
  1019. with self.assertRaisesRegex(OSError, "simulated staging failure"):
  1020. deploy.deploy_lv2_archives("install", component, updated, deploy.read_registry())
  1021. self.assertEqual(
  1022. {name: deploy.tree_sha256(path) for name, path in destinations.items()},
  1023. original_hashes,
  1024. )
  1025. leftover_names = {path.name for path in (self.home / ".lv2").iterdir()}
  1026. self.assertFalse(any(name.endswith(".rigforge-new") or name.endswith(".rigforge-old") for name in leftover_names))
  1027. def test_plugin_pending_uninstall_recovers_after_bundle_removal(self):
  1028. component = "dragonfly_reverb"
  1029. config = self.plugin_config(component)
  1030. self.install_plugin(component, config)
  1031. registry = deploy.read_registry()
  1032. registry[component]["pending_uninstall"] = True
  1033. deploy.write_registry(registry)
  1034. removed_name = "DragonflyEarlyReflections.lv2"
  1035. destination = self.home / ".lv2" / removed_name
  1036. shutil.rmtree(destination)
  1037. deploy.deploy_lv2_archives("uninstall", component, config, registry)
  1038. self.assertFalse(destination.exists())
  1039. self.assertNotIn(component, deploy.read_registry())
  1040. def test_plugin_install_refuses_local_edits_and_reload_is_noop(self):
  1041. component = "dragonfly_reverb"
  1042. config = self.plugin_config(component)
  1043. self.install_plugin(component, config)
  1044. destination = self.home / ".lv2/DragonflyHallReverb.lv2"
  1045. (destination / "manifest.ttl").write_text("local edit", encoding="utf-8")
  1046. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  1047. deploy.deploy_lv2_archives("uninstall", component, config, deploy.read_registry())
  1048. self.assertEqual((destination / "manifest.ttl").read_text(encoding="utf-8"), "local edit")
  1049. self.commands.clear()
  1050. deploy.deploy_lv2_archives("reload", component, config, deploy.read_registry())
  1051. self.assertEqual(self.commands, [])
  1052. def test_release_archive_layouts_match_declarations(self):
  1053. lsp_listing = """Path = /tmp/lsp.7z
  1054. Type = 7z
  1055. Physical Size = 456
  1056. Path = lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2
  1057. Attributes = D_ drwxr-xr-xr
  1058. Path = lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2/manifest.ttl
  1059. Attributes = A_ -rw-r--r--
  1060. """
  1061. self.assertEqual(
  1062. deploy.inspect_7z_archive(
  1063. self.plugin_config("lsp")["bundle_paths"], lsp_listing
  1064. ),
  1065. (
  1066. {Path("lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2")},
  1067. [
  1068. (Path("lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2"), True),
  1069. (Path("lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2/manifest.ttl"), False),
  1070. ],
  1071. ),
  1072. )
  1073. def test_7z_plugin_archive_rejects_path_traversal(self):
  1074. listing = """Path = /tmp/lsp.7z
  1075. Type = 7z
  1076. Physical Size = 456
  1077. Path = ../escape
  1078. Attributes = A_ -rw-r--r--
  1079. """
  1080. with self.assertRaisesRegex(RuntimeError, "unsafe archive path"):
  1081. deploy.inspect_7z_archive(self.plugin_config("lsp")["bundle_paths"], listing)
  1082. def test_tar_plugin_archive_rejects_path_traversal(self):
  1083. archive_path = self.home / "bad.tar.xz"
  1084. with tarfile.open(archive_path, "w:xz") as archive:
  1085. info = tarfile.TarInfo("../escape")
  1086. info.size = 1
  1087. archive.addfile(info, io.BytesIO(b"x"))
  1088. with self.assertRaisesRegex(RuntimeError, "unsafe archive path"):
  1089. deploy.inspect_tar_archive(archive_path, self.plugin_config("dragonfly_reverb")["bundle_paths"])
  1090. def test_nam_install_and_uninstall_restore_existing_bundle(self):
  1091. destination = self.home / ".lv2/neural_amp_modeler.lv2"
  1092. destination.mkdir(parents=True)
  1093. (destination / "old.txt").write_text("prior plugin", encoding="utf-8")
  1094. self.install_nam()
  1095. self.assertTrue((destination / "manifest.ttl").is_file())
  1096. self.assertTrue((destination / "neural_amp_modeler.so").is_file())
  1097. registry = deploy.read_registry()
  1098. self.assertEqual(registry["namlv2"]["tag"], "v0.2.3")
  1099. self.assertTrue(registry["namlv2"]["archive_sha256"])
  1100. self.assertTrue(registry["namlv2"]["installed_sha256"])
  1101. backup = Path(registry["namlv2"]["backup"])
  1102. self.assertEqual(
  1103. backup,
  1104. self.home / ".local/state/rigforge/namlv2-backups/neural_amp_modeler.lv2.rigforge-backup",
  1105. )
  1106. self.assertTrue(backup.is_dir())
  1107. deploy.deploy_namlv2("uninstall", self.nam_config(), registry)
  1108. self.assertEqual((destination / "old.txt").read_text(encoding="utf-8"), "prior plugin")
  1109. self.assertNotIn("manifest.ttl", {path.name for path in destination.iterdir()})
  1110. self.assertFalse(backup.exists())
  1111. self.assertNotIn("namlv2", deploy.read_registry())
  1112. def test_nam_install_migrates_legacy_adjacent_backup_out_of_lv2_search_path(self):
  1113. destination = self.home / ".lv2/neural_amp_modeler.lv2"
  1114. destination.mkdir(parents=True)
  1115. (destination / "old.txt").write_text("original plugin", encoding="utf-8")
  1116. self.install_nam()
  1117. config = self.nam_config()
  1118. registry = deploy.read_registry()
  1119. record = registry["namlv2"]
  1120. old_backup = destination.with_name(destination.name + ".rigforge-backup")
  1121. new_backup = (
  1122. self.home
  1123. / config["backup_directory"]
  1124. / (destination.name + ".rigforge-backup")
  1125. )
  1126. os.replace(new_backup, old_backup)
  1127. record["backup"] = str(old_backup)
  1128. registry["namlv2"] = record
  1129. deploy.write_registry(registry)
  1130. deploy.deploy_namlv2("install", config, deploy.read_registry())
  1131. self.assertFalse(old_backup.exists())
  1132. self.assertTrue(new_backup.is_dir())
  1133. self.assertEqual(
  1134. deploy.read_registry()["namlv2"]["backup"],
  1135. str(new_backup),
  1136. )
  1137. def test_nam_install_uses_the_dynamic_runtime_build_artifact(self):
  1138. config = deploy.load_deployment()["namlv2"]
  1139. destination = self.home / ".lv2/neural_amp_modeler.lv2"
  1140. with patch.object(
  1141. deploy,
  1142. "download_archive",
  1143. side_effect=AssertionError("built artifact install must not download the static release"),
  1144. ):
  1145. deploy.deploy_namlv2("install", config, deploy.read_registry())
  1146. source = ROOT / config["artifact_directory"] / config["bundle_directory"]
  1147. self.assertEqual(deploy.tree_sha256(destination), deploy.tree_sha256(source))
  1148. registry = deploy.read_registry()["namlv2"]
  1149. self.assertEqual(registry["tag"], "v0.2.3")
  1150. self.assertIsNone(registry["archive_sha256"])
  1151. self.assertEqual(registry["artifact_sha256"], deploy.tree_sha256(source))
  1152. with patch.object(
  1153. deploy,
  1154. "download_archive",
  1155. side_effect=AssertionError("idempotent build artifact install must not download"),
  1156. ):
  1157. deploy.deploy_namlv2("install", config, deploy.read_registry())
  1158. self.assertEqual(deploy.tree_sha256(destination), deploy.tree_sha256(source))
  1159. deploy.deploy_namlv2("uninstall", config, deploy.read_registry())
  1160. self.assertFalse(destination.exists())
  1161. self.assertNotIn("namlv2", deploy.read_registry())
  1162. def test_nam_pending_uninstall_recovers_after_bundle_removal(self):
  1163. destination = self.home / ".lv2/neural_amp_modeler.lv2"
  1164. destination.mkdir(parents=True)
  1165. (destination / "old.txt").write_text("prior plugin", encoding="utf-8")
  1166. self.install_nam()
  1167. registry = deploy.read_registry()
  1168. registry["namlv2"]["pending_uninstall"] = True
  1169. deploy.write_registry(registry)
  1170. shutil.rmtree(destination)
  1171. deploy.deploy_namlv2("uninstall", self.nam_config(), registry)
  1172. self.assertTrue((destination / "old.txt").is_file())
  1173. self.assertNotIn("namlv2", deploy.read_registry())
  1174. def test_nam_install_same_tag_is_idempotent(self):
  1175. self.install_nam()
  1176. self.commands.clear()
  1177. with patch.object(deploy, "download_archive", side_effect=AssertionError("must not download")):
  1178. deploy.deploy_namlv2("install", self.nam_config(), deploy.read_registry())
  1179. self.assertEqual(self.commands, [])
  1180. def test_nam_install_update_refuses_local_changes_and_restore_baseline(self):
  1181. self.install_nam()
  1182. destination = self.home / ".lv2/neural_amp_modeler.lv2"
  1183. (destination / "manifest.ttl").write_text("user edit", encoding="utf-8")
  1184. with patch.object(deploy, "download_archive", side_effect=AssertionError("must not download")):
  1185. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  1186. deploy.deploy_namlv2("install", self.nam_config("v0.2.4"), deploy.read_registry())
  1187. self.assertEqual((destination / "manifest.ttl").read_text(encoding="utf-8"), "user edit")
  1188. def test_nam_reload_is_informational_only(self):
  1189. deploy.deploy_namlv2("reload", self.nam_config(), {})
  1190. self.assertEqual(self.commands, [])
  1191. def test_nam_archive_rejects_path_traversal(self):
  1192. archive_path = self.home / "bad.tgz"
  1193. with tarfile.open(archive_path, "w:gz") as archive:
  1194. info = tarfile.TarInfo("../escape")
  1195. info.size = 1
  1196. archive.addfile(info, io.BytesIO(b"x"))
  1197. with self.assertRaisesRegex(RuntimeError, "unsafe archive path"):
  1198. deploy.inspect_archive(archive_path, "neural_amp_modeler.lv2")
  1199. def test_nam_registry_is_valid_json(self):
  1200. self.install_nam()
  1201. registry = json.loads(self.registry_path.read_text(encoding="utf-8"))
  1202. self.assertIn("namlv2", registry)
  1203. if __name__ == "__main__":
  1204. unittest.main()