test_deploy.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import io
  2. import json
  3. import os
  4. import shutil
  5. import sys
  6. import tarfile
  7. import tempfile
  8. import unittest
  9. from pathlib import Path
  10. from unittest.mock import patch
  11. ROOT = Path(__file__).resolve().parents[1]
  12. sys.path.insert(0, str(ROOT / "scripts"))
  13. import deploy
  14. class DeploymentTests(unittest.TestCase):
  15. def setUp(self):
  16. self.temporary = tempfile.TemporaryDirectory()
  17. self.home = Path(self.temporary.name) / "home"
  18. self.home.mkdir()
  19. self.registry_path = self.home / ".local/state/rigforge/deployment.json"
  20. self.commands = []
  21. self.run_patcher = patch.object(deploy, "run", side_effect=self.fake_run)
  22. self.run_patcher.start()
  23. self.home_patcher = patch.object(deploy.Path, "home", return_value=self.home)
  24. self.home_patcher.start()
  25. self.root_patcher = patch.object(deploy, "ROOT", ROOT)
  26. self.root_patcher.start()
  27. self.registry_patcher = patch.object(deploy, "STATE_PATH", self.registry_path)
  28. self.registry_patcher.start()
  29. def tearDown(self):
  30. patch.stopall()
  31. self.temporary.cleanup()
  32. def fake_run(self, command):
  33. self.commands.append(command)
  34. def nam_config(self, tag="v0.2.3"):
  35. return {
  36. "tag": tag,
  37. "archive_url": (
  38. "https://github.com/mikeoliphant/neural-amp-modeler-lv2/releases/download/"
  39. f"{tag}/neural_amp_modeler_lv2_rpi5.tgz"
  40. ),
  41. "bundle_directory": "neural_amp_modeler.lv2",
  42. }
  43. def pipewire_config(self):
  44. return {
  45. "fragment_source": "config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf",
  46. "fragment_destination": ".config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf",
  47. "user_service": "filter-chain.service",
  48. }
  49. def make_archive(self, destination, files=None):
  50. files = files or {"manifest.ttl": b"<urn:nam> a <urn:Plugin> .\n", "plugin.so": b"binary"}
  51. with tarfile.open(destination, "w:gz") as archive:
  52. directory = tarfile.TarInfo("neural_amp_modeler.lv2")
  53. directory.type = tarfile.DIRTYPE
  54. archive.addfile(directory)
  55. for name, content in files.items():
  56. info = tarfile.TarInfo(f"neural_amp_modeler.lv2/{name}")
  57. info.size = len(content)
  58. archive.addfile(info, io.BytesIO(content))
  59. def install_nam(self, config=None):
  60. config = config or self.nam_config()
  61. def download(url, destination):
  62. self.make_archive(destination)
  63. with patch.object(deploy, "download_archive", side_effect=download):
  64. deploy.deploy_namlv2("install", config, deploy.read_registry())
  65. def test_pipewire_install_is_atomic_scoped_and_idempotent(self):
  66. config = self.pipewire_config()
  67. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  68. installed = self.home / config["fragment_destination"]
  69. self.assertEqual(installed.read_bytes(), (ROOT / config["fragment_source"]).read_bytes())
  70. self.assertEqual(
  71. self.commands,
  72. [
  73. ["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "128"],
  74. ["systemctl", "--user", "restart", "filter-chain.service"],
  75. ["systemctl", "--user", "is-active", "--quiet", "filter-chain.service"],
  76. ],
  77. )
  78. self.commands.clear()
  79. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  80. self.assertEqual(
  81. self.commands,
  82. [["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "128"]],
  83. )
  84. self.assertFalse(any("pipewire.service" in command for command in self.commands))
  85. def test_pipewire_uninstall_restores_previous_file(self):
  86. config = self.pipewire_config()
  87. destination = self.home / config["fragment_destination"]
  88. destination.parent.mkdir(parents=True)
  89. destination.write_text("previous config", encoding="utf-8")
  90. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  91. self.commands.clear()
  92. deploy.deploy_pipewire("uninstall", config, deploy.read_registry(), 128)
  93. self.assertEqual(destination.read_text(encoding="utf-8"), "previous config")
  94. self.assertEqual(self.commands[0][-1], "0")
  95. self.assertEqual(self.commands[1], ["systemctl", "--user", "restart", "filter-chain.service"])
  96. self.assertNotIn("pipewire", deploy.read_registry())
  97. self.assertFalse(deploy.read_registry().get("pipewire_pending_reload"))
  98. def test_pipewire_pending_uninstall_recovers_after_prior_file_restored(self):
  99. config = self.pipewire_config()
  100. destination = self.home / config["fragment_destination"]
  101. destination.parent.mkdir(parents=True)
  102. destination.write_text("prior config", encoding="utf-8")
  103. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  104. registry = deploy.read_registry()
  105. registry["pipewire"]["pending_uninstall"] = True
  106. deploy.write_registry(registry)
  107. destination.write_text("prior config", encoding="utf-8")
  108. Path(registry["pipewire"]["backup"]).unlink()
  109. deploy.deploy_pipewire("uninstall", config, registry, 128)
  110. self.assertEqual(destination.read_text(encoding="utf-8"), "prior config")
  111. self.assertNotIn("pipewire", deploy.read_registry())
  112. def test_pipewire_pending_uninstall_recovers_after_fragment_removal(self):
  113. config = self.pipewire_config()
  114. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  115. destination = self.home / config["fragment_destination"]
  116. destination.unlink()
  117. registry = deploy.read_registry()
  118. registry["pipewire"]["pending_uninstall"] = True
  119. deploy.write_registry(registry)
  120. deploy.deploy_pipewire("uninstall", config, registry, 128)
  121. self.assertFalse(destination.exists())
  122. self.assertNotIn("pipewire", deploy.read_registry())
  123. self.assertEqual(self.commands[-3:], [
  124. ["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "0"],
  125. ["systemctl", "--user", "restart", "filter-chain.service"],
  126. ["systemctl", "--user", "is-active", "--quiet", "filter-chain.service"],
  127. ])
  128. def test_pipewire_uninstall_refuses_local_changes(self):
  129. config = self.pipewire_config()
  130. deploy.deploy_pipewire("install", config, deploy.read_registry(), 128)
  131. destination = self.home / config["fragment_destination"]
  132. destination.write_text("local edit", encoding="utf-8")
  133. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  134. deploy.deploy_pipewire("uninstall", config, deploy.read_registry(), 128)
  135. self.assertEqual(destination.read_text(encoding="utf-8"), "local edit")
  136. def test_nam_install_and_uninstall_restore_existing_bundle(self):
  137. destination = self.home / ".lv2/neural_amp_modeler.lv2"
  138. destination.mkdir(parents=True)
  139. (destination / "old.txt").write_text("prior plugin", encoding="utf-8")
  140. self.install_nam()
  141. self.assertTrue((destination / "manifest.ttl").is_file())
  142. self.assertTrue((destination / "plugin.so").is_file())
  143. registry = deploy.read_registry()
  144. self.assertEqual(registry["namlv2"]["tag"], "v0.2.3")
  145. self.assertTrue(registry["namlv2"]["archive_sha256"])
  146. self.assertTrue(registry["namlv2"]["installed_sha256"])
  147. deploy.deploy_namlv2("uninstall", self.nam_config(), registry)
  148. self.assertEqual((destination / "old.txt").read_text(encoding="utf-8"), "prior plugin")
  149. self.assertNotIn("manifest.ttl", {path.name for path in destination.iterdir()})
  150. self.assertNotIn("namlv2", deploy.read_registry())
  151. def test_nam_pending_uninstall_recovers_after_bundle_removal(self):
  152. destination = self.home / ".lv2/neural_amp_modeler.lv2"
  153. destination.mkdir(parents=True)
  154. (destination / "old.txt").write_text("prior plugin", encoding="utf-8")
  155. self.install_nam()
  156. registry = deploy.read_registry()
  157. registry["namlv2"]["pending_uninstall"] = True
  158. deploy.write_registry(registry)
  159. shutil.rmtree(destination)
  160. deploy.deploy_namlv2("uninstall", self.nam_config(), registry)
  161. self.assertTrue((destination / "old.txt").is_file())
  162. self.assertNotIn("namlv2", deploy.read_registry())
  163. def test_nam_install_same_tag_is_idempotent(self):
  164. self.install_nam()
  165. self.commands.clear()
  166. with patch.object(deploy, "download_archive", side_effect=AssertionError("must not download")):
  167. deploy.deploy_namlv2("install", self.nam_config(), deploy.read_registry())
  168. self.assertEqual(self.commands, [])
  169. def test_nam_install_update_refuses_local_changes_and_restore_baseline(self):
  170. self.install_nam()
  171. destination = self.home / ".lv2/neural_amp_modeler.lv2"
  172. (destination / "manifest.ttl").write_text("user edit", encoding="utf-8")
  173. with patch.object(deploy, "download_archive", side_effect=AssertionError("must not download")):
  174. with self.assertRaisesRegex(RuntimeError, "changed locally"):
  175. deploy.deploy_namlv2("install", self.nam_config("v0.2.4"), deploy.read_registry())
  176. self.assertEqual((destination / "manifest.ttl").read_text(encoding="utf-8"), "user edit")
  177. def test_nam_reload_is_informational_only(self):
  178. deploy.deploy_namlv2("reload", self.nam_config(), {})
  179. self.assertEqual(self.commands, [])
  180. def test_nam_archive_rejects_path_traversal(self):
  181. archive_path = self.home / "bad.tgz"
  182. with tarfile.open(archive_path, "w:gz") as archive:
  183. info = tarfile.TarInfo("../escape")
  184. info.size = 1
  185. archive.addfile(info, io.BytesIO(b"x"))
  186. with self.assertRaisesRegex(RuntimeError, "unsafe archive path"):
  187. deploy.inspect_archive(archive_path, "neural_amp_modeler.lv2")
  188. def test_nam_registry_is_valid_json(self):
  189. self.install_nam()
  190. registry = json.loads(self.registry_path.read_text(encoding="utf-8"))
  191. self.assertIn("namlv2", registry)
  192. if __name__ == "__main__":
  193. unittest.main()