| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- 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()
- 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 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 make_archive(self, destination, files=None):
- files = files or {"manifest.ttl": b"<urn:nam> a <urn:Plugin> .\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_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()
|