test_deploy.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. import io
  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, "mod_host_unit_is_active", return_value=False).start()
  27. patch.object(deploy, "mod_host_unit_is_enabled", return_value=False).start()
  28. self.root_patcher = patch.object(deploy, "ROOT", ROOT)
  29. self.root_patcher.start()
  30. self.registry_patcher = patch.object(deploy, "STATE_PATH", self.registry_path)
  31. self.registry_patcher.start()
  32. def tearDown(self):
  33. patch.stopall()
  34. self.temporary.cleanup()
  35. def fake_run(self, command):
  36. self.commands.append(command)
  37. def nam_config(self, tag="v0.2.3"):
  38. return {
  39. "tag": tag,
  40. "archive_url": (
  41. "https://github.com/mikeoliphant/neural-amp-modeler-lv2/releases/download/"
  42. f"{tag}/neural_amp_modeler_lv2_rpi5.tgz"
  43. ),
  44. "bundle_directory": "neural_amp_modeler.lv2",
  45. }
  46. def plugin_config(self, component):
  47. return deploy.load_deployment()[component]
  48. def make_plugin_archive(self, destination, config):
  49. if config["archive_format"] == "7z":
  50. destination.write_bytes(b"simulated 7z archive")
  51. return
  52. with tarfile.open(destination, "w:xz") as archive:
  53. for bundle_name, bundle_path in config["bundle_paths"].items():
  54. directory = tarfile.TarInfo(bundle_path)
  55. directory.type = tarfile.DIRTYPE
  56. archive.addfile(directory)
  57. for filename, content in {
  58. "manifest.ttl": b"<urn:plugin> a <urn:Plugin> .\\n",
  59. "plugin.so": b"binary",
  60. }.items():
  61. info = tarfile.TarInfo(f"{bundle_path}/{filename}")
  62. info.size = len(content)
  63. info.mode = 0o755 if filename.endswith(".so") else 0o644
  64. archive.addfile(info, io.BytesIO(content))
  65. def install_plugin(self, component, config=None):
  66. config = config or self.plugin_config(component)
  67. def download(url, destination):
  68. self.make_plugin_archive(destination, config)
  69. with patch.object(deploy, "download_archive", side_effect=download):
  70. if config["archive_format"] == "7z":
  71. def extract_7z(archive_path, bundle_paths, extraction_root):
  72. extracted = {}
  73. for name in bundle_paths:
  74. bundle = extraction_root / name
  75. bundle.mkdir()
  76. (bundle / "manifest.ttl").write_text("plugin manifest", encoding="utf-8")
  77. (bundle / "plugin.so").write_bytes(b"binary")
  78. extracted[name] = bundle
  79. return extracted
  80. with patch.object(deploy, "extract_7z_bundles", side_effect=extract_7z):
  81. deploy.deploy_lv2_archives("install", component, config, deploy.read_registry())
  82. else:
  83. deploy.deploy_lv2_archives("install", component, config, deploy.read_registry())
  84. def pipewire_config(self):
  85. return {
  86. "fragment_source": "config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf",
  87. "fragment_destination": ".config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf",
  88. "user_service": "filter-chain.service",
  89. }
  90. def mod_host_config(self):
  91. return deploy.load_deployment()["mod_host"]
  92. def make_archive(self, destination, files=None):
  93. files = files or {"manifest.ttl": b"<urn:nam> a <urn:Plugin> .\n", "plugin.so": b"binary"}
  94. with tarfile.open(destination, "w:gz") as archive:
  95. directory = tarfile.TarInfo("neural_amp_modeler.lv2")
  96. directory.type = tarfile.DIRTYPE
  97. archive.addfile(directory)
  98. for name, content in files.items():
  99. info = tarfile.TarInfo(f"neural_amp_modeler.lv2/{name}")
  100. info.size = len(content)
  101. archive.addfile(info, io.BytesIO(content))
  102. def install_nam(self, config=None):
  103. config = config or self.nam_config()
  104. def download(url, destination):
  105. self.make_archive(destination)
  106. with patch.object(deploy, "download_archive", side_effect=download):
  107. deploy.deploy_namlv2("install", config, deploy.read_registry())
  108. def test_pipewire_install_is_atomic_scoped_and_idempotent(self):
  109. config = self.pipewire_config()
  110. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  111. installed = self.home / config["fragment_destination"]
  112. self.assertEqual(installed.read_bytes(), (ROOT / config["fragment_source"]).read_bytes())
  113. self.assertEqual(
  114. self.commands,
  115. [
  116. ["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "128"],
  117. ["systemctl", "--user", "restart", "filter-chain.service"],
  118. ["systemctl", "--user", "is-active", "--quiet", "filter-chain.service"],
  119. ],
  120. )
  121. self.commands.clear()
  122. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  123. self.assertEqual(
  124. self.commands,
  125. [["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "128"]],
  126. )
  127. self.assertFalse(any("pipewire.service" in command for command in self.commands))
  128. def test_pipewire_uninstall_restores_previous_file(self):
  129. config = self.pipewire_config()
  130. destination = self.home / config["fragment_destination"]
  131. destination.parent.mkdir(parents=True)
  132. destination.write_text("previous config", encoding="utf-8")
  133. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  134. self.commands.clear()
  135. deploy.deploy_pipewire("uninstall", config, deploy.read_registry(), 128)
  136. self.assertEqual(destination.read_text(encoding="utf-8"), "previous config")
  137. self.assertEqual(self.commands[0][-1], "0")
  138. self.assertEqual(self.commands[1], ["systemctl", "--user", "restart", "filter-chain.service"])
  139. self.assertNotIn("pipewire", deploy.read_registry())
  140. self.assertFalse(deploy.read_registry().get("pipewire_pending_reload"))
  141. def test_pipewire_pending_uninstall_recovers_after_prior_file_restored(self):
  142. config = self.pipewire_config()
  143. destination = self.home / config["fragment_destination"]
  144. destination.parent.mkdir(parents=True)
  145. destination.write_text("prior config", encoding="utf-8")
  146. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  147. registry = deploy.read_registry()
  148. registry["pipewire"]["pending_uninstall"] = True
  149. deploy.write_registry(registry)
  150. destination.write_text("prior config", encoding="utf-8")
  151. Path(registry["pipewire"]["backup"]).unlink()
  152. deploy.deploy_pipewire("uninstall", config, registry, 128)
  153. self.assertEqual(destination.read_text(encoding="utf-8"), "prior config")
  154. self.assertNotIn("pipewire", deploy.read_registry())
  155. def test_pipewire_pending_uninstall_recovers_after_fragment_removal(self):
  156. config = self.pipewire_config()
  157. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  158. destination = self.home / config["fragment_destination"]
  159. destination.unlink()
  160. registry = deploy.read_registry()
  161. registry["pipewire"]["pending_uninstall"] = True
  162. deploy.write_registry(registry)
  163. deploy.deploy_pipewire("uninstall", config, registry, 128)
  164. self.assertFalse(destination.exists())
  165. self.assertNotIn("pipewire", deploy.read_registry())
  166. self.assertEqual(self.commands[-3:], [
  167. ["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "0"],
  168. ["systemctl", "--user", "restart", "filter-chain.service"],
  169. ["systemctl", "--user", "is-active", "--quiet", "filter-chain.service"],
  170. ])
  171. def test_pipewire_uninstall_refuses_local_changes(self):
  172. config = self.pipewire_config()
  173. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  174. destination = self.home / config["fragment_destination"]
  175. destination.write_text("local edit", encoding="utf-8")
  176. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  177. deploy.deploy_pipewire("uninstall", config, deploy.read_registry(), 128)
  178. self.assertEqual(destination.read_text(encoding="utf-8"), "local edit")
  179. def test_mod_host_install_renders_and_activates_user_service(self):
  180. config = self.mod_host_config()
  181. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  182. deploy.deploy_mod_host("install", config, deploy.read_registry())
  183. destination = self.home / config["service_destination"]
  184. content = destination.read_text(encoding="utf-8")
  185. self.assertIn("WantedBy=default.target", content)
  186. self.assertIn("After=pipewire.service wireplumber.service filter-chain.service", content)
  187. self.assertIn("PartOf=pipewire.service wireplumber.service filter-chain.service", content)
  188. self.assertIn("-n -p 5555", content)
  189. self.assertIn("configure_mod_host.py", content)
  190. reconcile_service = self.home / config["reconcile_service_destination"]
  191. reconcile_timer = self.home / config["reconcile_timer_destination"]
  192. self.assertIn("--reconcile-once --quiet-if-clean", reconcile_service.read_text(encoding="utf-8"))
  193. self.assertIn("SuccessExitStatus=1", reconcile_service.read_text(encoding="utf-8"))
  194. self.assertIn("OnUnitActiveSec=15s", reconcile_timer.read_text(encoding="utf-8"))
  195. self.assertEqual(
  196. self.commands,
  197. [
  198. ["systemctl", "--user", "daemon-reload"],
  199. ["systemctl", "--user", "enable", "rigforge-mod-host.service"],
  200. ["systemctl", "--user", "restart", "rigforge-mod-host.service"],
  201. ["systemctl", "--user", "is-active", "--quiet", "rigforge-mod-host.service"],
  202. ["systemctl", "--user", "daemon-reload"],
  203. ["systemctl", "--user", "enable", "rigforge-pipewire-reconcile.timer"],
  204. ["systemctl", "--user", "restart", "rigforge-pipewire-reconcile.timer"],
  205. ["systemctl", "--user", "is-active", "--quiet", "rigforge-pipewire-reconcile.timer"],
  206. ],
  207. )
  208. registry = deploy.read_registry()
  209. self.assertTrue(registry["mod_host"]["installed_sha256"])
  210. self.assertNotIn("pending_activation", registry["mod_host"])
  211. self.assertEqual(set(registry["mod_host_reconcile"]["files"]), {"service", "timer"})
  212. def test_mod_host_install_is_idempotent_without_restarting_active_audio(self):
  213. config = self.mod_host_config()
  214. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  215. deploy.deploy_mod_host("install", config, deploy.read_registry())
  216. self.commands.clear()
  217. deploy.deploy_mod_host("install", config, deploy.read_registry())
  218. self.assertEqual(
  219. self.commands,
  220. [
  221. ["systemctl", "--user", "daemon-reload"],
  222. ["systemctl", "--user", "enable", "rigforge-mod-host.service"],
  223. ["systemctl", "--user", "start", "rigforge-mod-host.service"],
  224. ["systemctl", "--user", "is-active", "--quiet", "rigforge-mod-host.service"],
  225. ["systemctl", "--user", "daemon-reload"],
  226. ["systemctl", "--user", "enable", "rigforge-pipewire-reconcile.timer"],
  227. ["systemctl", "--user", "start", "rigforge-pipewire-reconcile.timer"],
  228. ["systemctl", "--user", "is-active", "--quiet", "rigforge-pipewire-reconcile.timer"],
  229. ],
  230. )
  231. def test_mod_host_install_restarts_when_runtime_configuration_changes(self):
  232. config = self.mod_host_config()
  233. with (
  234. patch.object(deploy, "mod_host_port_is_open", return_value=False),
  235. patch.object(deploy, "mod_host_runtime_digest", side_effect=["a" * 64, "b" * 64]),
  236. ):
  237. deploy.deploy_mod_host("install", config, deploy.read_registry())
  238. self.commands.clear()
  239. deploy.deploy_mod_host("install", config, deploy.read_registry())
  240. self.assertIn(
  241. ["systemctl", "--user", "restart", "rigforge-mod-host.service"],
  242. self.commands,
  243. )
  244. def test_mod_host_uninstall_restores_previous_unit(self):
  245. config = self.mod_host_config()
  246. destination = self.home / config["service_destination"]
  247. destination.parent.mkdir(parents=True)
  248. destination.write_text("prior unit", encoding="utf-8")
  249. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  250. deploy.deploy_mod_host("install", config, deploy.read_registry())
  251. self.commands.clear()
  252. deploy.deploy_mod_host("uninstall", config, deploy.read_registry())
  253. self.assertEqual(destination.read_text(encoding="utf-8"), "prior unit")
  254. self.assertNotIn("mod_host", deploy.read_registry())
  255. self.assertEqual(
  256. self.commands,
  257. [
  258. ["systemctl", "--user", "disable", "--now", "rigforge-pipewire-reconcile.timer"],
  259. ["systemctl", "--user", "stop", "rigforge-pipewire-reconcile.service"],
  260. ["systemctl", "--user", "daemon-reload"],
  261. ["systemctl", "--user", "disable", "--now", "rigforge-mod-host.service"],
  262. ["systemctl", "--user", "daemon-reload"],
  263. ],
  264. )
  265. self.assertFalse((self.home / config["reconcile_service_destination"]).exists())
  266. self.assertFalse((self.home / config["reconcile_timer_destination"]).exists())
  267. def test_mod_host_uninstall_restores_prior_reconcile_units(self):
  268. config = self.mod_host_config()
  269. prior_contents = {
  270. config["reconcile_service_destination"]: "prior reconcile service",
  271. config["reconcile_timer_destination"]: "prior reconcile timer",
  272. }
  273. for relative_path, content in prior_contents.items():
  274. destination = self.home / relative_path
  275. destination.parent.mkdir(parents=True, exist_ok=True)
  276. destination.write_text(content, encoding="utf-8")
  277. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  278. deploy.deploy_mod_host("install", config, deploy.read_registry())
  279. deploy.deploy_mod_host("uninstall", config, deploy.read_registry())
  280. for relative_path, content in prior_contents.items():
  281. self.assertEqual((self.home / relative_path).read_text(encoding="utf-8"), content)
  282. self.assertNotIn("mod_host_reconcile", deploy.read_registry())
  283. def test_mod_host_uninstall_refuses_local_reconcile_unit_edits(self):
  284. config = self.mod_host_config()
  285. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  286. deploy.deploy_mod_host("install", config, deploy.read_registry())
  287. timer = self.home / config["reconcile_timer_destination"]
  288. timer.write_text("local edit", encoding="utf-8")
  289. self.commands.clear()
  290. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  291. deploy.deploy_mod_host("uninstall", config, deploy.read_registry())
  292. self.assertEqual(timer.read_text(encoding="utf-8"), "local edit")
  293. self.assertEqual(self.commands, [])
  294. def test_mod_host_pending_uninstall_recovers_after_prior_unit_is_restored(self):
  295. config = self.mod_host_config()
  296. destination = self.home / config["service_destination"]
  297. destination.parent.mkdir(parents=True)
  298. destination.write_text("prior unit", encoding="utf-8")
  299. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  300. deploy.deploy_mod_host("install", config, deploy.read_registry())
  301. registry = deploy.read_registry()
  302. backup = Path(registry["mod_host"]["backup"])
  303. registry["mod_host"]["pending_uninstall"] = True
  304. deploy.write_registry(registry)
  305. destination.write_text("prior unit", encoding="utf-8")
  306. backup.unlink()
  307. self.commands.clear()
  308. deploy.deploy_mod_host("uninstall", config, registry)
  309. self.assertEqual(destination.read_text(encoding="utf-8"), "prior unit")
  310. self.assertNotIn("mod_host", deploy.read_registry())
  311. self.assertEqual(
  312. self.commands,
  313. [
  314. ["systemctl", "--user", "disable", "--now", "rigforge-pipewire-reconcile.timer"],
  315. ["systemctl", "--user", "stop", "rigforge-pipewire-reconcile.service"],
  316. ["systemctl", "--user", "daemon-reload"],
  317. ["systemctl", "--user", "daemon-reload"],
  318. ],
  319. )
  320. def test_mod_host_uninstall_refuses_local_unit_edits(self):
  321. config = self.mod_host_config()
  322. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  323. deploy.deploy_mod_host("install", config, deploy.read_registry())
  324. destination = self.home / config["service_destination"]
  325. destination.write_text("local edit", encoding="utf-8")
  326. self.commands.clear()
  327. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  328. deploy.deploy_mod_host("uninstall", config, deploy.read_registry())
  329. self.assertEqual(destination.read_text(encoding="utf-8"), "local edit")
  330. self.assertEqual(self.commands, [])
  331. def test_mod_host_install_refuses_an_existing_control_socket(self):
  332. with patch.object(deploy, "mod_host_port_is_open", return_value=True):
  333. with self.assertRaisesRegex(RuntimeError, "already in use"):
  334. deploy.deploy_mod_host("install", self.mod_host_config(), deploy.read_registry())
  335. self.assertEqual(self.commands, [])
  336. self.assertFalse((self.home / self.mod_host_config()["service_destination"]).exists())
  337. def test_mod_host_reload_restarts_and_checks_the_user_service(self):
  338. config = self.mod_host_config()
  339. with patch.object(deploy, "mod_host_port_is_open", return_value=False):
  340. deploy.deploy_mod_host("install", config, deploy.read_registry())
  341. self.commands.clear()
  342. deploy.deploy_mod_host("reload", config, deploy.read_registry())
  343. self.assertEqual(
  344. self.commands,
  345. [
  346. ["systemctl", "--user", "restart", "rigforge-mod-host.service"],
  347. ["systemctl", "--user", "is-active", "--quiet", "rigforge-mod-host.service"],
  348. ],
  349. )
  350. def test_lsp_install_uninstall_restores_existing_bundle_and_is_idempotent(self):
  351. component = "lsp"
  352. config = self.plugin_config(component)
  353. destination = self.home / ".lv2/lsp-plugins.lv2"
  354. destination.mkdir(parents=True)
  355. (destination / "old.txt").write_text("prior plugin", encoding="utf-8")
  356. self.install_plugin(component, config)
  357. self.assertTrue((destination / "manifest.ttl").is_file())
  358. registry = deploy.read_registry()
  359. self.assertEqual(registry[component]["tag"], "1.2.37")
  360. self.assertTrue(registry[component]["archive_sha256"])
  361. self.commands.clear()
  362. with patch.object(deploy, "download_archive", side_effect=AssertionError("must not download")):
  363. deploy.deploy_lv2_archives("install", component, config, registry)
  364. deploy.deploy_lv2_archives("uninstall", component, config, registry)
  365. self.assertEqual((destination / "old.txt").read_text(encoding="utf-8"), "prior plugin")
  366. self.assertNotIn(component, deploy.read_registry())
  367. def test_dragonfly_install_and_uninstall_manage_all_bundles(self):
  368. component = "dragonfly_reverb"
  369. config = self.plugin_config(component)
  370. first_name = next(iter(config["bundle_paths"]))
  371. first_destination = self.home / ".lv2" / first_name
  372. first_destination.mkdir(parents=True)
  373. (first_destination / "old.txt").write_text("prior plugin", encoding="utf-8")
  374. self.install_plugin(component, config)
  375. registry = deploy.read_registry()
  376. self.assertEqual(set(registry[component]["bundles"]), set(config["bundle_paths"]))
  377. for name in config["bundle_paths"]:
  378. self.assertTrue((self.home / ".lv2" / name / "manifest.ttl").is_file())
  379. deploy.deploy_lv2_archives("uninstall", component, config, registry)
  380. self.assertEqual((first_destination / "old.txt").read_text(encoding="utf-8"), "prior plugin")
  381. self.assertNotIn(component, deploy.read_registry())
  382. def test_plugin_update_preserves_old_bundles_when_staging_fails(self):
  383. component = "dragonfly_reverb"
  384. config = self.plugin_config(component)
  385. self.install_plugin(component, config)
  386. destinations = {
  387. name: self.home / ".lv2" / name for name in config["bundle_paths"]
  388. }
  389. original_hashes = {name: deploy.tree_sha256(path) for name, path in destinations.items()}
  390. updated = dict(config, tag="3.2.11")
  391. def fail_copy(source, destination, **kwargs):
  392. if Path(destination).name == "DragonflyPlateReverb.lv2.rigforge-new":
  393. raise OSError("simulated staging failure")
  394. return original_copytree(source, destination, **kwargs)
  395. original_copytree = deploy.shutil.copytree
  396. with patch.object(deploy, "download_archive", side_effect=lambda url, path: self.make_plugin_archive(path, config)):
  397. with patch.object(deploy, "extract_tar_bundles", return_value={name: self.home / "source" / name for name in config["bundle_paths"]}):
  398. source_root = self.home / "source"
  399. for name in config["bundle_paths"]:
  400. bundle = source_root / name
  401. bundle.mkdir(parents=True)
  402. (bundle / "manifest.ttl").write_text("new plugin", encoding="utf-8")
  403. with patch.object(deploy.shutil, "copytree", side_effect=fail_copy):
  404. with self.assertRaisesRegex(OSError, "simulated staging failure"):
  405. deploy.deploy_lv2_archives("install", component, updated, deploy.read_registry())
  406. self.assertEqual(
  407. {name: deploy.tree_sha256(path) for name, path in destinations.items()},
  408. original_hashes,
  409. )
  410. leftover_names = {path.name for path in (self.home / ".lv2").iterdir()}
  411. self.assertFalse(any(name.endswith(".rigforge-new") or name.endswith(".rigforge-old") for name in leftover_names))
  412. def test_plugin_pending_uninstall_recovers_after_bundle_removal(self):
  413. component = "dragonfly_reverb"
  414. config = self.plugin_config(component)
  415. self.install_plugin(component, config)
  416. registry = deploy.read_registry()
  417. registry[component]["pending_uninstall"] = True
  418. deploy.write_registry(registry)
  419. removed_name = "DragonflyEarlyReflections.lv2"
  420. destination = self.home / ".lv2" / removed_name
  421. shutil.rmtree(destination)
  422. deploy.deploy_lv2_archives("uninstall", component, config, registry)
  423. self.assertFalse(destination.exists())
  424. self.assertNotIn(component, deploy.read_registry())
  425. def test_plugin_install_refuses_local_edits_and_reload_is_noop(self):
  426. component = "dragonfly_reverb"
  427. config = self.plugin_config(component)
  428. self.install_plugin(component, config)
  429. destination = self.home / ".lv2/DragonflyHallReverb.lv2"
  430. (destination / "manifest.ttl").write_text("local edit", encoding="utf-8")
  431. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  432. deploy.deploy_lv2_archives("uninstall", component, config, deploy.read_registry())
  433. self.assertEqual((destination / "manifest.ttl").read_text(encoding="utf-8"), "local edit")
  434. self.commands.clear()
  435. deploy.deploy_lv2_archives("reload", component, config, deploy.read_registry())
  436. self.assertEqual(self.commands, [])
  437. def test_release_archive_layouts_match_declarations(self):
  438. lsp_listing = """Path = /tmp/lsp.7z
  439. Type = 7z
  440. Physical Size = 456
  441. Path = lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2
  442. Attributes = D_ drwxr-xr-xr
  443. Path = lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2/manifest.ttl
  444. Attributes = A_ -rw-r--r--
  445. """
  446. self.assertEqual(
  447. deploy.inspect_7z_archive(
  448. self.plugin_config("lsp")["bundle_paths"], lsp_listing
  449. ),
  450. (
  451. {Path("lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2")},
  452. [
  453. (Path("lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2"), True),
  454. (Path("lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2/manifest.ttl"), False),
  455. ],
  456. ),
  457. )
  458. def test_7z_plugin_archive_rejects_path_traversal(self):
  459. listing = """Path = /tmp/lsp.7z
  460. Type = 7z
  461. Physical Size = 456
  462. Path = ../escape
  463. Attributes = A_ -rw-r--r--
  464. """
  465. with self.assertRaisesRegex(RuntimeError, "unsafe archive path"):
  466. deploy.inspect_7z_archive(self.plugin_config("lsp")["bundle_paths"], listing)
  467. def test_tar_plugin_archive_rejects_path_traversal(self):
  468. archive_path = self.home / "bad.tar.xz"
  469. with tarfile.open(archive_path, "w:xz") as archive:
  470. info = tarfile.TarInfo("../escape")
  471. info.size = 1
  472. archive.addfile(info, io.BytesIO(b"x"))
  473. with self.assertRaisesRegex(RuntimeError, "unsafe archive path"):
  474. deploy.inspect_tar_archive(archive_path, self.plugin_config("dragonfly_reverb")["bundle_paths"])
  475. def test_nam_install_and_uninstall_restore_existing_bundle(self):
  476. destination = self.home / ".lv2/neural_amp_modeler.lv2"
  477. destination.mkdir(parents=True)
  478. (destination / "old.txt").write_text("prior plugin", encoding="utf-8")
  479. self.install_nam()
  480. self.assertTrue((destination / "manifest.ttl").is_file())
  481. self.assertTrue((destination / "plugin.so").is_file())
  482. registry = deploy.read_registry()
  483. self.assertEqual(registry["namlv2"]["tag"], "v0.2.3")
  484. self.assertTrue(registry["namlv2"]["archive_sha256"])
  485. self.assertTrue(registry["namlv2"]["installed_sha256"])
  486. deploy.deploy_namlv2("uninstall", self.nam_config(), registry)
  487. self.assertEqual((destination / "old.txt").read_text(encoding="utf-8"), "prior plugin")
  488. self.assertNotIn("manifest.ttl", {path.name for path in destination.iterdir()})
  489. self.assertNotIn("namlv2", deploy.read_registry())
  490. def test_nam_pending_uninstall_recovers_after_bundle_removal(self):
  491. destination = self.home / ".lv2/neural_amp_modeler.lv2"
  492. destination.mkdir(parents=True)
  493. (destination / "old.txt").write_text("prior plugin", encoding="utf-8")
  494. self.install_nam()
  495. registry = deploy.read_registry()
  496. registry["namlv2"]["pending_uninstall"] = True
  497. deploy.write_registry(registry)
  498. shutil.rmtree(destination)
  499. deploy.deploy_namlv2("uninstall", self.nam_config(), registry)
  500. self.assertTrue((destination / "old.txt").is_file())
  501. self.assertNotIn("namlv2", deploy.read_registry())
  502. def test_nam_install_same_tag_is_idempotent(self):
  503. self.install_nam()
  504. self.commands.clear()
  505. with patch.object(deploy, "download_archive", side_effect=AssertionError("must not download")):
  506. deploy.deploy_namlv2("install", self.nam_config(), deploy.read_registry())
  507. self.assertEqual(self.commands, [])
  508. def test_nam_install_update_refuses_local_changes_and_restore_baseline(self):
  509. self.install_nam()
  510. destination = self.home / ".lv2/neural_amp_modeler.lv2"
  511. (destination / "manifest.ttl").write_text("user edit", encoding="utf-8")
  512. with patch.object(deploy, "download_archive", side_effect=AssertionError("must not download")):
  513. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  514. deploy.deploy_namlv2("install", self.nam_config("v0.2.4"), deploy.read_registry())
  515. self.assertEqual((destination / "manifest.ttl").read_text(encoding="utf-8"), "user edit")
  516. def test_nam_reload_is_informational_only(self):
  517. deploy.deploy_namlv2("reload", self.nam_config(), {})
  518. self.assertEqual(self.commands, [])
  519. def test_nam_archive_rejects_path_traversal(self):
  520. archive_path = self.home / "bad.tgz"
  521. with tarfile.open(archive_path, "w:gz") as archive:
  522. info = tarfile.TarInfo("../escape")
  523. info.size = 1
  524. archive.addfile(info, io.BytesIO(b"x"))
  525. with self.assertRaisesRegex(RuntimeError, "unsafe archive path"):
  526. deploy.inspect_archive(archive_path, "neural_amp_modeler.lv2")
  527. def test_nam_registry_is_valid_json(self):
  528. self.install_nam()
  529. registry = json.loads(self.registry_path.read_text(encoding="utf-8"))
  530. self.assertIn("namlv2", registry)
  531. if __name__ == "__main__":
  532. unittest.main()