import io import io import json import os import shutil import sys import tarfile import tempfile import unittest from pathlib import Path from unittest.mock import patch ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "scripts")) import deploy class DeploymentTests(unittest.TestCase): def setUp(self): self.temporary = tempfile.TemporaryDirectory() self.home = Path(self.temporary.name) / "home" self.home.mkdir() self.registry_path = self.home / ".local/state/rigforge/deployment.json" self.commands = [] self.run_patcher = patch.object(deploy, "run", side_effect=self.fake_run) self.run_patcher.start() self.home_patcher = patch.object(deploy.Path, "home", return_value=self.home) self.home_patcher.start() patch.object(deploy, "mod_host_unit_is_active", return_value=False).start() patch.object(deploy, "mod_host_unit_is_enabled", return_value=False).start() self.root_patcher = patch.object(deploy, "ROOT", ROOT) self.root_patcher.start() self.registry_patcher = patch.object(deploy, "STATE_PATH", self.registry_path) self.registry_patcher.start() def tearDown(self): patch.stopall() self.temporary.cleanup() def fake_run(self, command): self.commands.append(command) def nam_config(self, tag="v0.2.3"): return { "tag": tag, "archive_url": ( "https://github.com/mikeoliphant/neural-amp-modeler-lv2/releases/download/" f"{tag}/neural_amp_modeler_lv2_rpi5.tgz" ), "bundle_directory": "neural_amp_modeler.lv2", } def plugin_config(self, component): return deploy.load_deployment()[component] def make_plugin_archive(self, destination, config): if config["archive_format"] == "7z": destination.write_bytes(b"simulated 7z archive") return with tarfile.open(destination, "w:xz") as archive: for bundle_name, bundle_path in config["bundle_paths"].items(): directory = tarfile.TarInfo(bundle_path) directory.type = tarfile.DIRTYPE archive.addfile(directory) for filename, content in { "manifest.ttl": b" a .\\n", "plugin.so": b"binary", }.items(): info = tarfile.TarInfo(f"{bundle_path}/{filename}") info.size = len(content) info.mode = 0o755 if filename.endswith(".so") else 0o644 archive.addfile(info, io.BytesIO(content)) def install_plugin(self, component, config=None): config = config or self.plugin_config(component) def download(url, destination): self.make_plugin_archive(destination, config) with patch.object(deploy, "download_archive", side_effect=download): if config["archive_format"] == "7z": def extract_7z(archive_path, bundle_paths, extraction_root): extracted = {} for name in bundle_paths: bundle = extraction_root / name bundle.mkdir() (bundle / "manifest.ttl").write_text("plugin manifest", encoding="utf-8") (bundle / "plugin.so").write_bytes(b"binary") extracted[name] = bundle return extracted with patch.object(deploy, "extract_7z_bundles", side_effect=extract_7z): deploy.deploy_lv2_archives("install", component, config, deploy.read_registry()) else: deploy.deploy_lv2_archives("install", component, config, deploy.read_registry()) def pipewire_config(self): return { "fragment_source": "config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf", "fragment_destination": ".config/pipewire/filter-chain.conf.d/rigforge-direct-mix.conf", "user_service": "filter-chain.service", } def mod_host_config(self): return deploy.load_deployment()["mod_host"] def make_archive(self, destination, files=None): files = files or {"manifest.ttl": b" a .\n", "plugin.so": b"binary"} with tarfile.open(destination, "w:gz") as archive: directory = tarfile.TarInfo("neural_amp_modeler.lv2") directory.type = tarfile.DIRTYPE archive.addfile(directory) for name, content in files.items(): info = tarfile.TarInfo(f"neural_amp_modeler.lv2/{name}") info.size = len(content) archive.addfile(info, io.BytesIO(content)) def install_nam(self, config=None): config = config or self.nam_config() def download(url, destination): self.make_archive(destination) with patch.object(deploy, "download_archive", side_effect=download): deploy.deploy_namlv2("install", config, deploy.read_registry()) def test_pipewire_install_is_atomic_scoped_and_idempotent(self): config = self.pipewire_config() deploy.deploy_pipewire("install", config, deploy.read_registry(), 128) installed = self.home / config["fragment_destination"] self.assertEqual(installed.read_bytes(), (ROOT / config["fragment_source"]).read_bytes()) self.assertEqual( self.commands, [ ["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "128"], ["systemctl", "--user", "restart", "filter-chain.service"], ["systemctl", "--user", "is-active", "--quiet", "filter-chain.service"], ], ) self.commands.clear() deploy.deploy_pipewire("install", config, deploy.read_registry(), 128) self.assertEqual( self.commands, [["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "128"]], ) self.assertFalse(any("pipewire.service" in command for command in self.commands)) def test_pipewire_uninstall_restores_previous_file(self): config = self.pipewire_config() destination = self.home / config["fragment_destination"] destination.parent.mkdir(parents=True) destination.write_text("previous config", encoding="utf-8") deploy.deploy_pipewire("install", config, deploy.read_registry(), 128) self.commands.clear() deploy.deploy_pipewire("uninstall", config, deploy.read_registry(), 128) self.assertEqual(destination.read_text(encoding="utf-8"), "previous config") self.assertEqual(self.commands[0][-1], "0") self.assertEqual(self.commands[1], ["systemctl", "--user", "restart", "filter-chain.service"]) self.assertNotIn("pipewire", deploy.read_registry()) self.assertFalse(deploy.read_registry().get("pipewire_pending_reload")) def test_pipewire_pending_uninstall_recovers_after_prior_file_restored(self): config = self.pipewire_config() destination = self.home / config["fragment_destination"] destination.parent.mkdir(parents=True) destination.write_text("prior config", encoding="utf-8") deploy.deploy_pipewire("install", config, deploy.read_registry(), 128) registry = deploy.read_registry() registry["pipewire"]["pending_uninstall"] = True deploy.write_registry(registry) destination.write_text("prior config", encoding="utf-8") Path(registry["pipewire"]["backup"]).unlink() deploy.deploy_pipewire("uninstall", config, registry, 128) self.assertEqual(destination.read_text(encoding="utf-8"), "prior config") self.assertNotIn("pipewire", deploy.read_registry()) def test_pipewire_pending_uninstall_recovers_after_fragment_removal(self): config = self.pipewire_config() deploy.deploy_pipewire("install", config, deploy.read_registry(), 128) destination = self.home / config["fragment_destination"] destination.unlink() registry = deploy.read_registry() registry["pipewire"]["pending_uninstall"] = True deploy.write_registry(registry) deploy.deploy_pipewire("uninstall", config, registry, 128) self.assertFalse(destination.exists()) self.assertNotIn("pipewire", deploy.read_registry()) self.assertEqual(self.commands[-3:], [ ["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "0"], ["systemctl", "--user", "restart", "filter-chain.service"], ["systemctl", "--user", "is-active", "--quiet", "filter-chain.service"], ]) def test_pipewire_uninstall_refuses_local_changes(self): config = self.pipewire_config() deploy.deploy_pipewire("install", config, deploy.read_registry(), 128) destination = self.home / config["fragment_destination"] destination.write_text("local edit", encoding="utf-8") with self.assertRaisesRegex(RuntimeError, "changed locally"): deploy.deploy_pipewire("uninstall", config, deploy.read_registry(), 128) self.assertEqual(destination.read_text(encoding="utf-8"), "local edit") def test_mod_host_install_renders_and_activates_user_service(self): config = self.mod_host_config() with patch.object(deploy, "mod_host_port_is_open", return_value=False): deploy.deploy_mod_host("install", config, deploy.read_registry()) destination = self.home / config["service_destination"] content = destination.read_text(encoding="utf-8") self.assertIn("WantedBy=default.target", content) self.assertIn("After=pipewire.service wireplumber.service filter-chain.service", content) self.assertIn("-n -p 5555", content) self.assertIn("configure_mod_host.py", content) self.assertEqual( self.commands, [ ["systemctl", "--user", "daemon-reload"], ["systemctl", "--user", "enable", "rigforge-mod-host.service"], ["systemctl", "--user", "restart", "rigforge-mod-host.service"], ["systemctl", "--user", "is-active", "--quiet", "rigforge-mod-host.service"], ], ) registry = deploy.read_registry() self.assertTrue(registry["mod_host"]["installed_sha256"]) self.assertNotIn("pending_activation", registry["mod_host"]) def test_mod_host_install_is_idempotent_without_restarting_active_audio(self): config = self.mod_host_config() with patch.object(deploy, "mod_host_port_is_open", return_value=False): deploy.deploy_mod_host("install", config, deploy.read_registry()) self.commands.clear() deploy.deploy_mod_host("install", config, deploy.read_registry()) self.assertEqual( self.commands, [ ["systemctl", "--user", "daemon-reload"], ["systemctl", "--user", "enable", "rigforge-mod-host.service"], ["systemctl", "--user", "start", "rigforge-mod-host.service"], ["systemctl", "--user", "is-active", "--quiet", "rigforge-mod-host.service"], ], ) def test_mod_host_install_restarts_when_runtime_configuration_changes(self): config = self.mod_host_config() with ( patch.object(deploy, "mod_host_port_is_open", return_value=False), patch.object(deploy, "mod_host_runtime_digest", side_effect=["a" * 64, "b" * 64]), ): deploy.deploy_mod_host("install", config, deploy.read_registry()) self.commands.clear() deploy.deploy_mod_host("install", config, deploy.read_registry()) self.assertIn( ["systemctl", "--user", "restart", "rigforge-mod-host.service"], self.commands, ) def test_mod_host_uninstall_restores_previous_unit(self): config = self.mod_host_config() destination = self.home / config["service_destination"] destination.parent.mkdir(parents=True) destination.write_text("prior unit", encoding="utf-8") with patch.object(deploy, "mod_host_port_is_open", return_value=False): deploy.deploy_mod_host("install", config, deploy.read_registry()) self.commands.clear() deploy.deploy_mod_host("uninstall", config, deploy.read_registry()) self.assertEqual(destination.read_text(encoding="utf-8"), "prior unit") self.assertNotIn("mod_host", deploy.read_registry()) self.assertEqual( self.commands, [ ["systemctl", "--user", "disable", "--now", "rigforge-mod-host.service"], ["systemctl", "--user", "daemon-reload"], ], ) def test_mod_host_pending_uninstall_recovers_after_prior_unit_is_restored(self): config = self.mod_host_config() destination = self.home / config["service_destination"] destination.parent.mkdir(parents=True) destination.write_text("prior unit", encoding="utf-8") with patch.object(deploy, "mod_host_port_is_open", return_value=False): deploy.deploy_mod_host("install", config, deploy.read_registry()) registry = deploy.read_registry() backup = Path(registry["mod_host"]["backup"]) registry["mod_host"]["pending_uninstall"] = True deploy.write_registry(registry) destination.write_text("prior unit", encoding="utf-8") backup.unlink() self.commands.clear() deploy.deploy_mod_host("uninstall", config, registry) self.assertEqual(destination.read_text(encoding="utf-8"), "prior unit") self.assertNotIn("mod_host", deploy.read_registry()) self.assertEqual(self.commands, [["systemctl", "--user", "daemon-reload"]]) def test_mod_host_uninstall_refuses_local_unit_edits(self): config = self.mod_host_config() with patch.object(deploy, "mod_host_port_is_open", return_value=False): deploy.deploy_mod_host("install", config, deploy.read_registry()) destination = self.home / config["service_destination"] destination.write_text("local edit", encoding="utf-8") self.commands.clear() with self.assertRaisesRegex(RuntimeError, "changed locally"): deploy.deploy_mod_host("uninstall", config, deploy.read_registry()) self.assertEqual(destination.read_text(encoding="utf-8"), "local edit") self.assertEqual(self.commands, []) def test_mod_host_install_refuses_an_existing_control_socket(self): with patch.object(deploy, "mod_host_port_is_open", return_value=True): with self.assertRaisesRegex(RuntimeError, "already in use"): deploy.deploy_mod_host("install", self.mod_host_config(), deploy.read_registry()) self.assertEqual(self.commands, []) self.assertFalse((self.home / self.mod_host_config()["service_destination"]).exists()) def test_mod_host_reload_restarts_and_checks_the_user_service(self): config = self.mod_host_config() with patch.object(deploy, "mod_host_port_is_open", return_value=False): deploy.deploy_mod_host("install", config, deploy.read_registry()) self.commands.clear() deploy.deploy_mod_host("reload", config, deploy.read_registry()) self.assertEqual( self.commands, [ ["systemctl", "--user", "restart", "rigforge-mod-host.service"], ["systemctl", "--user", "is-active", "--quiet", "rigforge-mod-host.service"], ], ) def test_lsp_install_uninstall_restores_existing_bundle_and_is_idempotent(self): component = "lsp" config = self.plugin_config(component) destination = self.home / ".lv2/lsp-plugins.lv2" destination.mkdir(parents=True) (destination / "old.txt").write_text("prior plugin", encoding="utf-8") self.install_plugin(component, config) self.assertTrue((destination / "manifest.ttl").is_file()) registry = deploy.read_registry() self.assertEqual(registry[component]["tag"], "1.2.37") self.assertTrue(registry[component]["archive_sha256"]) self.commands.clear() with patch.object(deploy, "download_archive", side_effect=AssertionError("must not download")): deploy.deploy_lv2_archives("install", component, config, registry) deploy.deploy_lv2_archives("uninstall", component, config, registry) self.assertEqual((destination / "old.txt").read_text(encoding="utf-8"), "prior plugin") self.assertNotIn(component, deploy.read_registry()) def test_dragonfly_install_and_uninstall_manage_all_bundles(self): component = "dragonfly_reverb" config = self.plugin_config(component) first_name = next(iter(config["bundle_paths"])) first_destination = self.home / ".lv2" / first_name first_destination.mkdir(parents=True) (first_destination / "old.txt").write_text("prior plugin", encoding="utf-8") self.install_plugin(component, config) registry = deploy.read_registry() self.assertEqual(set(registry[component]["bundles"]), set(config["bundle_paths"])) for name in config["bundle_paths"]: self.assertTrue((self.home / ".lv2" / name / "manifest.ttl").is_file()) deploy.deploy_lv2_archives("uninstall", component, config, registry) self.assertEqual((first_destination / "old.txt").read_text(encoding="utf-8"), "prior plugin") self.assertNotIn(component, deploy.read_registry()) def test_plugin_update_preserves_old_bundles_when_staging_fails(self): component = "dragonfly_reverb" config = self.plugin_config(component) self.install_plugin(component, config) destinations = { name: self.home / ".lv2" / name for name in config["bundle_paths"] } original_hashes = {name: deploy.tree_sha256(path) for name, path in destinations.items()} updated = dict(config, tag="3.2.11") def fail_copy(source, destination, **kwargs): if Path(destination).name == "DragonflyPlateReverb.lv2.rigforge-new": raise OSError("simulated staging failure") return original_copytree(source, destination, **kwargs) original_copytree = deploy.shutil.copytree with patch.object(deploy, "download_archive", side_effect=lambda url, path: self.make_plugin_archive(path, config)): with patch.object(deploy, "extract_tar_bundles", return_value={name: self.home / "source" / name for name in config["bundle_paths"]}): source_root = self.home / "source" for name in config["bundle_paths"]: bundle = source_root / name bundle.mkdir(parents=True) (bundle / "manifest.ttl").write_text("new plugin", encoding="utf-8") with patch.object(deploy.shutil, "copytree", side_effect=fail_copy): with self.assertRaisesRegex(OSError, "simulated staging failure"): deploy.deploy_lv2_archives("install", component, updated, deploy.read_registry()) self.assertEqual( {name: deploy.tree_sha256(path) for name, path in destinations.items()}, original_hashes, ) leftover_names = {path.name for path in (self.home / ".lv2").iterdir()} self.assertFalse(any(name.endswith(".rigforge-new") or name.endswith(".rigforge-old") for name in leftover_names)) def test_plugin_pending_uninstall_recovers_after_bundle_removal(self): component = "dragonfly_reverb" config = self.plugin_config(component) self.install_plugin(component, config) registry = deploy.read_registry() registry[component]["pending_uninstall"] = True deploy.write_registry(registry) removed_name = "DragonflyEarlyReflections.lv2" destination = self.home / ".lv2" / removed_name shutil.rmtree(destination) deploy.deploy_lv2_archives("uninstall", component, config, registry) self.assertFalse(destination.exists()) self.assertNotIn(component, deploy.read_registry()) def test_plugin_install_refuses_local_edits_and_reload_is_noop(self): component = "dragonfly_reverb" config = self.plugin_config(component) self.install_plugin(component, config) destination = self.home / ".lv2/DragonflyHallReverb.lv2" (destination / "manifest.ttl").write_text("local edit", encoding="utf-8") with self.assertRaisesRegex(RuntimeError, "changed locally"): deploy.deploy_lv2_archives("uninstall", component, config, deploy.read_registry()) self.assertEqual((destination / "manifest.ttl").read_text(encoding="utf-8"), "local edit") self.commands.clear() deploy.deploy_lv2_archives("reload", component, config, deploy.read_registry()) self.assertEqual(self.commands, []) def test_release_archive_layouts_match_declarations(self): lsp_listing = """Path = /tmp/lsp.7z Type = 7z Physical Size = 456 Path = lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2 Attributes = D_ drwxr-xr-xr Path = lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2/manifest.ttl Attributes = A_ -rw-r--r-- """ self.assertEqual( deploy.inspect_7z_archive( self.plugin_config("lsp")["bundle_paths"], lsp_listing ), ( {Path("lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2")}, [ (Path("lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2"), True), (Path("lsp-plugins-1.2.37-Linux-aarch64/LV2/lsp-plugins.lv2/manifest.ttl"), False), ], ), ) def test_7z_plugin_archive_rejects_path_traversal(self): listing = """Path = /tmp/lsp.7z Type = 7z Physical Size = 456 Path = ../escape Attributes = A_ -rw-r--r-- """ with self.assertRaisesRegex(RuntimeError, "unsafe archive path"): deploy.inspect_7z_archive(self.plugin_config("lsp")["bundle_paths"], listing) def test_tar_plugin_archive_rejects_path_traversal(self): archive_path = self.home / "bad.tar.xz" with tarfile.open(archive_path, "w:xz") as archive: info = tarfile.TarInfo("../escape") info.size = 1 archive.addfile(info, io.BytesIO(b"x")) with self.assertRaisesRegex(RuntimeError, "unsafe archive path"): deploy.inspect_tar_archive(archive_path, self.plugin_config("dragonfly_reverb")["bundle_paths"]) def test_nam_install_and_uninstall_restore_existing_bundle(self): destination = self.home / ".lv2/neural_amp_modeler.lv2" destination.mkdir(parents=True) (destination / "old.txt").write_text("prior plugin", encoding="utf-8") self.install_nam() self.assertTrue((destination / "manifest.ttl").is_file()) self.assertTrue((destination / "plugin.so").is_file()) registry = deploy.read_registry() self.assertEqual(registry["namlv2"]["tag"], "v0.2.3") self.assertTrue(registry["namlv2"]["archive_sha256"]) self.assertTrue(registry["namlv2"]["installed_sha256"]) deploy.deploy_namlv2("uninstall", self.nam_config(), registry) self.assertEqual((destination / "old.txt").read_text(encoding="utf-8"), "prior plugin") self.assertNotIn("manifest.ttl", {path.name for path in destination.iterdir()}) self.assertNotIn("namlv2", deploy.read_registry()) def test_nam_pending_uninstall_recovers_after_bundle_removal(self): destination = self.home / ".lv2/neural_amp_modeler.lv2" destination.mkdir(parents=True) (destination / "old.txt").write_text("prior plugin", encoding="utf-8") self.install_nam() registry = deploy.read_registry() registry["namlv2"]["pending_uninstall"] = True deploy.write_registry(registry) shutil.rmtree(destination) deploy.deploy_namlv2("uninstall", self.nam_config(), registry) self.assertTrue((destination / "old.txt").is_file()) self.assertNotIn("namlv2", deploy.read_registry()) def test_nam_install_same_tag_is_idempotent(self): self.install_nam() self.commands.clear() with patch.object(deploy, "download_archive", side_effect=AssertionError("must not download")): deploy.deploy_namlv2("install", self.nam_config(), deploy.read_registry()) self.assertEqual(self.commands, []) def test_nam_install_update_refuses_local_changes_and_restore_baseline(self): self.install_nam() destination = self.home / ".lv2/neural_amp_modeler.lv2" (destination / "manifest.ttl").write_text("user edit", encoding="utf-8") with patch.object(deploy, "download_archive", side_effect=AssertionError("must not download")): with self.assertRaisesRegex(RuntimeError, "changed locally"): deploy.deploy_namlv2("install", self.nam_config("v0.2.4"), deploy.read_registry()) self.assertEqual((destination / "manifest.ttl").read_text(encoding="utf-8"), "user edit") def test_nam_reload_is_informational_only(self): deploy.deploy_namlv2("reload", self.nam_config(), {}) self.assertEqual(self.commands, []) def test_nam_archive_rejects_path_traversal(self): archive_path = self.home / "bad.tgz" with tarfile.open(archive_path, "w:gz") as archive: info = tarfile.TarInfo("../escape") info.size = 1 archive.addfile(info, io.BytesIO(b"x")) with self.assertRaisesRegex(RuntimeError, "unsafe archive path"): deploy.inspect_archive(archive_path, "neural_amp_modeler.lv2") def test_nam_registry_is_valid_json(self): self.install_nam() registry = json.loads(self.registry_path.read_text(encoding="utf-8")) self.assertIn("namlv2", registry) if __name__ == "__main__": unittest.main()