| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372 |
- import copy
- 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, "user_unit_is_active", return_value=False).start()
- patch.object(deploy, "user_unit_is_enabled", return_value=False).start()
- patch.object(deploy, "mod_host_port_is_open", return_value=False).start()
- patch.object(deploy, "mod_ui_port_is_open", 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",
- "backup_directory": ".local/state/rigforge/namlv2-backups",
- }
- 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"<urn:plugin> a <urn:Plugin> .\\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 deploy.load_deployment()["pipewire"]
- def mod_host_config(self):
- return deploy.load_deployment()["mod_host"]
- def install_mod_host_ports_prerequisite(self):
- config = deploy.load_deployment()["mod_host_ports"]
- deploy.deploy_mod_host_ports("install", config, deploy.read_registry())
- self.commands.clear()
- def install_mod_ui_registry_prerequisite(self):
- registry = deploy.read_registry()
- registry["mod_ui"] = {"test_dependency": True}
- deploy.write_registry(registry)
- def sooperlooper_config(self):
- return deploy.load_deployment()["sooperlooper"]
- def slweb_config(self):
- return deploy.load_deployment()["slweb"]
- def reconcile_config(self):
- return deploy.load_deployment()["reconcile"]
- def midi_bridge_config(self):
- return deploy.load_deployment()["midi_bridge"]
- def make_archive(self, destination, files=None):
- files = files or {
- "manifest.ttl": b"<urn:nam> a <urn:Plugin> .\n",
- "neural_amp_modeler.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 make_slweb_archive(self, destination):
- files = {
- "tools/slweb.py": b"print('slweb')\n",
- "tools/slweb_web/index.html": b"<!doctype html>\n",
- "tools/slweb_web/app.css": b"body {}\n",
- "tools/slweb_web/app.js": b"// UI\n",
- "tools/other-tool.py": b"print('other tool')\n",
- }
- with tarfile.open(destination, "w:gz") as archive:
- root = tarfile.TarInfo("sooperlooper-mods-tag")
- root.type = tarfile.DIRTYPE
- archive.addfile(root)
- for relative_path, content in files.items():
- info = tarfile.TarInfo(f"sooperlooper-mods-tag/{relative_path}")
- info.size = len(content)
- info.mode = 0o644
- 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(), 256)
- 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", "256"],
- ["systemctl", "--user", "restart", "filter-chain.service"],
- ["systemctl", "--user", "is-active", "--quiet", "filter-chain.service"],
- ["systemctl", "--user", "daemon-reload"],
- ["systemctl", "--user", "enable", "rigforge-pipewire-quantum.service"],
- ["systemctl", "--user", "restart", "rigforge-pipewire-quantum.service"],
- ["systemctl", "--user", "is-active", "--quiet", "rigforge-pipewire-quantum.service"],
- ],
- )
- self.commands.clear()
- deploy.deploy_pipewire("install", config, deploy.read_registry(), 256)
- self.assertEqual(
- self.commands,
- [
- ["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "256"],
- ["systemctl", "--user", "daemon-reload"],
- ["systemctl", "--user", "enable", "rigforge-pipewire-quantum.service"],
- ["systemctl", "--user", "start", "rigforge-pipewire-quantum.service"],
- ["systemctl", "--user", "is-active", "--quiet", "rigforge-pipewire-quantum.service"],
- ],
- )
- self.assertFalse(any("pipewire.service" in command for command in self.commands))
- def test_pipewire_quantum_service_reapplies_desired_value_after_engine_restart(self):
- unit = deploy.render_pipewire_quantum_service(self.pipewire_config(), 256)
- content = unit["content"].decode("utf-8")
- self.assertIn("After=pipewire.service", content)
- self.assertIn("PartOf=pipewire.service", content)
- self.assertIn("RemainAfterExit=yes", content)
- self.assertIn(
- 'ExecStart="/usr/bin/pw-metadata" -n settings 0 clock.force-quantum 256',
- content,
- )
- self.assertIn("WantedBy=pipewire.service", content)
- def test_pipewire_reload_updates_quantum_service_without_restarting_engine(self):
- config = self.pipewire_config()
- deploy.deploy_pipewire("install", config, deploy.read_registry(), 256)
- self.commands.clear()
- deploy.deploy_pipewire("reload", config, deploy.read_registry(), 256)
- self.assertIn(
- ["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "256"],
- self.commands,
- )
- self.assertIn(
- ["systemctl", "--user", "restart", "filter-chain.service"],
- self.commands,
- )
- self.assertFalse(any(command[:4] == ["systemctl", "--user", "restart", "pipewire.service"]
- 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(), 256)
- self.commands.clear()
- deploy.deploy_pipewire("uninstall", config, deploy.read_registry(), 256)
- self.assertEqual(destination.read_text(encoding="utf-8"), "previous config")
- self.assertEqual(
- self.commands[:2],
- [
- ["systemctl", "--user", "disable", "--now", "rigforge-pipewire-quantum.service"],
- ["systemctl", "--user", "daemon-reload"],
- ],
- )
- self.assertIn(
- ["pw-metadata", "-n", "settings", "0", "clock.force-quantum", "0"],
- self.commands,
- )
- self.assertIn(
- ["systemctl", "--user", "restart", "filter-chain.service"],
- self.commands,
- )
- self.assertNotIn("pipewire", deploy.read_registry())
- self.assertNotIn("pipewire_quantum", 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")
- self.commands.clear()
- 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")
- self.assertNotIn(
- ["systemctl", "--user", "disable", "--now", "rigforge-pipewire-quantum.service"],
- self.commands,
- )
- self.assertIn("pipewire_quantum", deploy.read_registry())
- def test_mod_host_install_renders_and_activates_user_service(self):
- config = self.mod_host_config()
- self.install_mod_host_ports_prerequisite()
- 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 rigforge-mod-host-ports.service", content)
- self.assertIn("PartOf=pipewire.service wireplumber.service filter-chain.service rigforge-mod-host-ports.service", content)
- self.assertIn("ExecStartPre=", content)
- self.assertIn("wait_mod_host_ports.py", content)
- self.assertIn("--timeout 45", content)
- self.assertIn("TimeoutStartSec=90", content)
- self.assertIn("-n -p 5555 -f 5556", content)
- self.assertNotIn("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"])
- self.assertNotIn("reconcile", registry)
- def test_mod_host_install_is_idempotent_without_restarting_active_audio(self):
- config = self.mod_host_config()
- self.install_mod_host_ports_prerequisite()
- 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()
- self.install_mod_host_ports_prerequisite()
- 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_runtime_digest_ignores_global_reconcile_settings(self):
- config = self.mod_host_config()
- state_path = ROOT / "config" / "desired-state.yaml"
- with state_path.open(encoding="utf-8") as stream:
- original = deploy.yaml.safe_load(stream)
- changed_reconcile = copy.deepcopy(original)
- changed_reconcile["deployment"]["reconcile"]["interval_seconds"] = 30
- changed_mod_host = copy.deepcopy(original)
- changed_mod_host["deployment"]["mod_host"]["socket_port"] = 5556
- with patch.object(
- deploy.yaml,
- "safe_load",
- side_effect=[original, changed_reconcile],
- ):
- original_digest = deploy.mod_host_runtime_digest(config)
- reconcile_digest = deploy.mod_host_runtime_digest(config)
- self.assertEqual(original_digest, reconcile_digest)
- with patch.object(
- deploy.yaml,
- "safe_load",
- return_value=changed_mod_host,
- ):
- changed_digest = deploy.mod_host_runtime_digest(config)
- self.assertNotEqual(original_digest, changed_digest)
- def test_mod_host_uninstall_restores_previous_unit(self):
- config = self.mod_host_config()
- self.install_mod_host_ports_prerequisite()
- destination = self.home / config["service_destination"]
- destination.parent.mkdir(parents=True, exist_ok=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_reconcile_install_and_uninstall_restores_prior_units(self):
- config = self.reconcile_config()
- prior_contents = {
- config["service_destination"]: "prior reconcile service",
- config["timer_destination"]: "prior reconcile timer",
- }
- for relative_path, content in prior_contents.items():
- destination = self.home / relative_path
- destination.parent.mkdir(parents=True, exist_ok=True)
- destination.write_text(content, encoding="utf-8")
- deploy.deploy_reconcile("install", config, deploy.read_registry())
- reconcile_service = self.home / config["service_destination"]
- reconcile_timer = self.home / config["timer_destination"]
- self.assertIn("--reconcile-once --quiet-if-clean", reconcile_service.read_text(encoding="utf-8"))
- self.assertIn("SuccessExitStatus=1", reconcile_service.read_text(encoding="utf-8"))
- self.assertNotIn("rigforge-mod-host.service", reconcile_service.read_text(encoding="utf-8"))
- self.assertIn("OnUnitActiveSec=15s", reconcile_timer.read_text(encoding="utf-8"))
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "daemon-reload"],
- ["systemctl", "--user", "enable", "rigforge-pipewire-reconcile.timer"],
- ["systemctl", "--user", "restart", "rigforge-pipewire-reconcile.timer"],
- ["systemctl", "--user", "is-active", "--quiet", "rigforge-pipewire-reconcile.timer"],
- ],
- )
- self.commands.clear()
- deploy.deploy_reconcile("uninstall", config, deploy.read_registry())
- for relative_path, content in prior_contents.items():
- self.assertEqual((self.home / relative_path).read_text(encoding="utf-8"), content)
- self.assertNotIn("reconcile", deploy.read_registry())
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "disable", "--now", "rigforge-pipewire-reconcile.timer"],
- ["systemctl", "--user", "stop", "rigforge-pipewire-reconcile.service"],
- ["systemctl", "--user", "daemon-reload"],
- ],
- )
- def test_reconcile_uninstall_refuses_local_unit_edits(self):
- config = self.reconcile_config()
- deploy.deploy_reconcile("install", config, deploy.read_registry())
- timer = self.home / config["timer_destination"]
- timer.write_text("local edit", encoding="utf-8")
- self.commands.clear()
- with self.assertRaisesRegex(RuntimeError, "changed locally"):
- deploy.deploy_reconcile("uninstall", config, deploy.read_registry())
- self.assertEqual(timer.read_text(encoding="utf-8"), "local edit")
- self.assertEqual(self.commands, [])
- def test_reconcile_reload_runs_one_shot_without_restarting_mod_host(self):
- config = self.reconcile_config()
- deploy.deploy_reconcile("install", config, deploy.read_registry())
- self.commands.clear()
- deploy.deploy_reconcile("reload", config, deploy.read_registry())
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "start", "rigforge-pipewire-reconcile.timer"],
- ["systemctl", "--user", "is-active", "--quiet", "rigforge-pipewire-reconcile.timer"],
- ["systemctl", "--user", "start", "rigforge-pipewire-reconcile.service"],
- ],
- )
- def test_midi_bridge_install_and_uninstall_manage_only_user_service(self):
- self.install_mod_ui_registry_prerequisite()
- config = self.midi_bridge_config()
- destination = self.home / config["service_destination"]
- destination.parent.mkdir(parents=True)
- destination.write_text("prior MIDI unit", encoding="utf-8")
- deploy.deploy_midi_bridge("install", config, deploy.read_registry())
- installed = destination.read_text(encoding="utf-8")
- self.assertIn("Description=Rigforge MIDI action bridge", installed)
- self.assertIn("midi_action_bridge.py\" --state", installed)
- self.assertIn("After=rigforge-mod-ui.service", installed)
- self.assertIn("Restart=on-failure", installed)
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "daemon-reload"],
- ["systemctl", "--user", "enable", "rigforge-midi-bridge.service"],
- ["systemctl", "--user", "restart", "rigforge-midi-bridge.service"],
- ["systemctl", "--user", "is-active", "--quiet", "rigforge-midi-bridge.service"],
- ],
- )
- self.assertIn("midi_bridge", deploy.read_registry())
- self.commands.clear()
- deploy.deploy_midi_bridge("uninstall", config, deploy.read_registry())
- self.assertEqual(destination.read_text(encoding="utf-8"), "prior MIDI unit")
- self.assertNotIn("midi_bridge", deploy.read_registry())
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "disable", "--now", "rigforge-midi-bridge.service"],
- ["systemctl", "--user", "daemon-reload"],
- ],
- )
- def test_midi_bridge_install_is_idempotent_and_reload_restarts_only_bridge(self):
- self.install_mod_ui_registry_prerequisite()
- config = self.midi_bridge_config()
- deploy.deploy_midi_bridge("install", config, deploy.read_registry())
- self.commands.clear()
- deploy.deploy_midi_bridge("install", config, deploy.read_registry())
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "daemon-reload"],
- ["systemctl", "--user", "enable", "rigforge-midi-bridge.service"],
- ["systemctl", "--user", "start", "rigforge-midi-bridge.service"],
- ["systemctl", "--user", "is-active", "--quiet", "rigforge-midi-bridge.service"],
- ],
- )
- self.commands.clear()
- deploy.deploy_midi_bridge("reload", config, deploy.read_registry())
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "restart", "rigforge-midi-bridge.service"],
- ["systemctl", "--user", "is-active", "--quiet", "rigforge-midi-bridge.service"],
- ],
- )
- def test_midi_bridge_uninstall_refuses_local_unit_edits(self):
- self.install_mod_ui_registry_prerequisite()
- config = self.midi_bridge_config()
- deploy.deploy_midi_bridge("install", config, deploy.read_registry())
- destination = self.home / config["service_destination"]
- destination.write_text("local change", encoding="utf-8")
- self.commands.clear()
- with self.assertRaisesRegex(RuntimeError, "changed locally"):
- deploy.deploy_midi_bridge("uninstall", config, deploy.read_registry())
- self.assertEqual(destination.read_text(encoding="utf-8"), "local change")
- self.assertEqual(self.commands, [])
- def test_sooperlooper_service_uses_desired_counts_bindings_and_prefader_osc(self):
- config = self.sooperlooper_config()
- destination = self.home / config["service_destination"]
- destination.parent.mkdir(parents=True)
- destination.write_text("prior SooperLooper unit", encoding="utf-8")
- deploy.deploy_sooperlooper("install", config, deploy.read_registry())
- installed = destination.read_text(encoding="utf-8")
- binary = (ROOT / config["binary_source"]).resolve()
- binding_file = (ROOT / config["binding_file"]).resolve()
- self.assertIn(
- f'ExecStart="/usr/bin/pw-jack" "{binary}" -l 2 -c 4 -t 30 -m "{binding_file}"',
- installed,
- )
- self.assertIn(
- "ExecStartPost=/bin/sh -c 'sleep 4; exec \"/usr/bin/oscsend\" "
- "\"127.0.0.1\" 9951 /sl/0/set sf discrete_prefader 0'",
- installed,
- )
- self.assertIn("PartOf=pipewire.service wireplumber.service", installed)
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "daemon-reload"],
- ["systemctl", "--user", "enable", "rigforge-sooperlooper.service"],
- ["systemctl", "--user", "restart", "rigforge-sooperlooper.service"],
- ["systemctl", "--user", "is-active", "--quiet", "rigforge-sooperlooper.service"],
- ],
- )
- self.assertIn("sooperlooper", deploy.read_registry())
- self.commands.clear()
- deploy.deploy_sooperlooper("uninstall", config, deploy.read_registry())
- self.assertEqual(destination.read_text(encoding="utf-8"), "prior SooperLooper unit")
- self.assertNotIn("sooperlooper", deploy.read_registry())
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "disable", "--now", "rigforge-sooperlooper.service"],
- ["systemctl", "--user", "daemon-reload"],
- ],
- )
- def test_sooperlooper_reload_refuses_local_unit_edits(self):
- config = self.sooperlooper_config()
- deploy.deploy_sooperlooper("install", config, deploy.read_registry())
- destination = self.home / config["service_destination"]
- destination.write_text("local change", encoding="utf-8")
- self.commands.clear()
- with self.assertRaisesRegex(RuntimeError, "changed locally"):
- deploy.deploy_sooperlooper("reload", config, deploy.read_registry())
- self.assertEqual(destination.read_text(encoding="utf-8"), "local change")
- self.assertEqual(self.commands, [])
- def test_slweb_install_fetches_tools_and_activates_after_sooperlooper(self):
- config = self.slweb_config()
- sooperlooper = self.sooperlooper_config()
- registry = deploy.read_registry()
- registry["sooperlooper"] = {"installed_sha256": "engine"}
- deploy.write_registry(registry)
- archive_urls = []
- def download(url, destination):
- archive_urls.append(url)
- self.make_slweb_archive(destination)
- with patch.object(deploy, "download_archive", side_effect=download):
- deploy.deploy_slweb("install", config, sooperlooper, deploy.read_registry())
- installed_dir = self.home / config["install_directory"]
- self.assertTrue((installed_dir / "tools/slweb.py").is_file())
- self.assertTrue((installed_dir / "tools/slweb_web/index.html").is_file())
- self.assertTrue((installed_dir / "tools/other-tool.py").is_file())
- self.assertFalse((installed_dir / "src").exists())
- self.assertEqual(
- archive_urls,
- [
- "https://github.com/jojolb/sooperlooper-mods/archive/refs/tags/"
- "v1.7.9-mods.1-slweb.3.tar.gz"
- ],
- )
- unit_path = self.home / config["service_destination"]
- unit = unit_path.read_text(encoding="utf-8")
- self.assertIn("Requires=rigforge-sooperlooper.service", unit)
- self.assertIn("After=rigforge-sooperlooper.service", unit)
- self.assertIn("PartOf=rigforge-sooperlooper.service", unit)
- self.assertIn(f"WorkingDirectory={installed_dir}", unit)
- self.assertNotIn(f'WorkingDirectory="{installed_dir}"', unit)
- self.assertIn(
- f'ExecStart="/usr/bin/python3" tools/slweb.py --osc-host 127.0.0.1 '
- f'--osc-port {sooperlooper["osc"]["port"]} --http-host 0.0.0.0 --http-port 8080 '
- '--files-dir "/home/jojo/loops"',
- unit,
- )
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "daemon-reload"],
- ["systemctl", "--user", "enable", "rigforge-slweb.service"],
- ["systemctl", "--user", "restart", "rigforge-slweb.service"],
- ["systemctl", "--user", "is-active", "--quiet", "rigforge-slweb.service"],
- ],
- )
- self.assertEqual(
- deploy.systemd_path_value("/home/test user/rigforge%"),
- "/home/test\\x20user/rigforge%%",
- )
- self.assertIn("slweb", deploy.read_registry())
- self.assertIn("slweb_service", deploy.read_registry())
- self.commands.clear()
- with patch.object(deploy, "download_archive") as download:
- deploy.deploy_slweb("install", config, sooperlooper, deploy.read_registry())
- download.assert_not_called()
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "daemon-reload"],
- ["systemctl", "--user", "enable", "rigforge-slweb.service"],
- ["systemctl", "--user", "start", "rigforge-slweb.service"],
- ["systemctl", "--user", "is-active", "--quiet", "rigforge-slweb.service"],
- ],
- )
- self.commands.clear()
- deploy.deploy_slweb("uninstall", config, sooperlooper, deploy.read_registry())
- self.assertFalse(installed_dir.exists())
- self.assertNotIn("slweb", deploy.read_registry())
- self.assertNotIn("slweb_service", deploy.read_registry())
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "disable", "--now", "rigforge-slweb.service"],
- ["systemctl", "--user", "daemon-reload"],
- ],
- )
- def test_slweb_preserves_prior_tools_and_refuses_local_changes(self):
- config = self.slweb_config()
- sooperlooper = self.sooperlooper_config()
- destination = self.home / config["install_directory"]
- destination.mkdir(parents=True)
- (destination / "owner-file").write_text("keep me", encoding="utf-8")
- registry = deploy.read_registry()
- registry["sooperlooper"] = {"installed_sha256": "engine"}
- deploy.write_registry(registry)
- with patch.object(
- deploy, "download_archive", side_effect=lambda _url, path: self.make_slweb_archive(path)
- ):
- deploy.deploy_slweb("install", config, sooperlooper, deploy.read_registry())
- backup = destination.with_name(destination.name + ".rigforge-backup")
- self.assertEqual((backup / "owner-file").read_text(encoding="utf-8"), "keep me")
- (destination / "tools/slweb.py").write_text("local modification", encoding="utf-8")
- self.commands.clear()
- with self.assertRaisesRegex(RuntimeError, "changed locally"):
- deploy.deploy_slweb("uninstall", config, sooperlooper, deploy.read_registry())
- self.assertTrue((destination / "tools/slweb.py").exists())
- self.assertTrue(backup.is_dir())
- self.assertEqual(self.commands, [])
- (destination / "tools/slweb.py").write_text("print('slweb')\n", encoding="utf-8")
- deploy.deploy_slweb("uninstall", config, sooperlooper, deploy.read_registry())
- self.assertFalse((destination / "tools/slweb.py").exists())
- self.assertEqual((destination / "owner-file").read_text(encoding="utf-8"), "keep me")
- def test_slweb_install_requires_sooperlooper_service(self):
- with patch.object(deploy, "download_archive") as download:
- with self.assertRaisesRegex(RuntimeError, "install sooperlooper before"):
- deploy.deploy_slweb(
- "install",
- self.slweb_config(),
- self.sooperlooper_config(),
- deploy.read_registry(),
- )
- download.assert_not_called()
- def test_mod_host_install_and_uninstall_do_not_manage_reconcile_timer(self):
- reconcile_config = self.reconcile_config()
- mod_host_config = self.mod_host_config()
- self.install_mod_host_ports_prerequisite()
- deploy.deploy_reconcile("install", reconcile_config, deploy.read_registry())
- reconcile_paths = {
- reconcile_config["service_destination"],
- reconcile_config["timer_destination"],
- }
- self.commands.clear()
- with patch.object(deploy, "mod_host_port_is_open", return_value=False):
- deploy.deploy_mod_host("install", mod_host_config, deploy.read_registry())
- deploy.deploy_mod_host("uninstall", mod_host_config, deploy.read_registry())
- registry = deploy.read_registry()
- self.assertIn("reconcile", registry)
- for relative_path in reconcile_paths:
- self.assertTrue((self.home / relative_path).is_file())
- self.assertFalse(any("rigforge-pipewire-reconcile" in " ".join(command) for command in self.commands))
- def test_mod_host_pending_uninstall_recovers_after_prior_unit_is_restored(self):
- config = self.mod_host_config()
- self.install_mod_host_ports_prerequisite()
- destination = self.home / config["service_destination"]
- destination.parent.mkdir(parents=True, exist_ok=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()
- self.install_mod_host_ports_prerequisite()
- 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_ports_service_is_managed_scoped_and_ordered(self):
- config = deploy.load_deployment()["mod_host_ports"]
- destination = self.home / config["service_destination"]
- deploy.deploy_mod_host_ports("install", config, deploy.read_registry())
- content = destination.read_text(encoding="utf-8")
- self.assertIn("Description=Rigforge PipeWire audio boundary ports for mod-host", content)
- self.assertIn("run_mod_host_ports.py", content)
- self.assertIn("--pw-loopback \"/usr/bin/pw-loopback\"", content)
- self.assertIn("Wants=pipewire.service wireplumber.service", content)
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "daemon-reload"],
- ["systemctl", "--user", "enable", "rigforge-mod-host-ports.service"],
- ["systemctl", "--user", "restart", "rigforge-mod-host-ports.service"],
- ["systemctl", "--user", "is-active", "--quiet", "rigforge-mod-host-ports.service"],
- ],
- )
- self.assertIn("mod_host_ports", deploy.read_registry())
- self.commands.clear()
- deploy.deploy_mod_host_ports("uninstall", config, deploy.read_registry())
- self.assertNotIn("mod_host_ports", deploy.read_registry())
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "disable", "--now", "rigforge-mod-host-ports.service"],
- ["systemctl", "--user", "daemon-reload"],
- ],
- )
- def test_mod_ui_service_uses_reference_board_and_port_bridge(self):
- registry = {
- "mod_host_ports": {},
- "mod_host": {},
- }
- config = deploy.load_deployment()["mod_ui"]
- destination = self.home / config["service_destination"]
- deploy.deploy_mod_ui("install", config, registry)
- content = destination.read_text(encoding="utf-8")
- self.assertIn("run_mod_ui_service.py", content)
- self.assertIn("prepare_mod_ui_pedalboard.py", content)
- self.assertIn("ExecStartPre=", content)
- self.assertIn("--runtime-dir %t/rigforge-mod-ui", content)
- self.assertIn("MOD_DEFAULT_PEDALBOARD=", content)
- self.assertIn("MOD_RIGFORGE_CAPTURE_PORTS=", content)
- self.assertIn("MOD_RIGFORGE_PLAYBACK_PORTS=", content)
- self.assertIn(
- f'MOD_USER_FILES_DIR="{self.home / config["user_files_directory"]}"',
- content,
- )
- self.assertIn(
- "rigforge.mod-host-input.capture:capture_AUX0,rigforge.mod-host-input.capture:capture_AUX1,"
- "rigforge.mod-host-input.capture:capture_AUX2,rigforge.mod-host-input.capture:capture_AUX3",
- content,
- )
- self.assertIn(
- "rigforge.mod-host-output.playback:playback_AUX0,rigforge.mod-host-output.playback:playback_AUX1,"
- "rigforge.mod-host-output.playback:playback_AUX2,rigforge.mod-host-output.playback:playback_AUX3",
- content,
- )
- self.assertIn("Requires=rigforge-mod-host.service rigforge-mod-host-ports.service", content)
- self.assertIn("pw-jack", content)
- def test_mod_ui_manages_declared_nam_models_for_local_picker(self):
- registry = {
- "mod_host_ports": {},
- "mod_host": {},
- }
- config = deploy.load_deployment()["mod_ui"]
- deploy.deploy_mod_ui("install", config, registry)
- model_root = self.home / config["user_files_directory"] / "NAM Models" / "Rigforge"
- state = deploy.yaml.safe_load(
- (ROOT / "config" / "desired-state.yaml").read_text(encoding="utf-8")
- )
- model_names = set(deploy.mod_ui_nam_model_sources(state))
- self.assertEqual({path.name for path in model_root.iterdir()}, model_names)
- reference = Path(
- deploy.read_registry()["mod_ui"]["reference_bundle"]["destination"]
- )
- ttl = (reference / "rigforge-guitar.ttl").read_text(encoding="utf-8")
- self.assertIn(str(model_root / "Vox_AC15CH_Crystal_Clean_TB.nam"), ttl)
- deploy.deploy_mod_ui("uninstall", config, deploy.read_registry())
- self.assertFalse(model_root.exists())
- self.assertNotIn("mod_ui_nam_models", deploy.read_registry())
- def test_mod_ui_refuses_to_remove_locally_modified_nam_model(self):
- registry = {
- "mod_host_ports": {},
- "mod_host": {},
- }
- config = deploy.load_deployment()["mod_ui"]
- deploy.deploy_mod_ui("install", config, registry)
- model = (
- self.home
- / config["user_files_directory"]
- / "NAM Models"
- / "Rigforge"
- / "Vox_AC15CH_Crunch_TB.nam"
- )
- model.write_bytes(b"local change")
- with self.assertRaisesRegex(RuntimeError, "changed locally"):
- deploy.deploy_mod_ui("uninstall", config, deploy.read_registry())
- self.assertEqual(model.read_bytes(), b"local change")
- self.assertIn("mod_ui_nam_models", deploy.read_registry())
- def test_mod_ui_reference_bundle_is_immutable_after_install(self):
- config = deploy.load_deployment()["mod_ui"]
- registry = {"mod_host_ports": {}, "mod_host": {}}
- deploy.deploy_mod_ui("install", config, registry)
- reference = Path(deploy.read_registry()["mod_ui"]["reference_bundle"]["destination"])
- ttl = reference / "rigforge-guitar.ttl"
- ttl.write_bytes(ttl.read_bytes() + b"\n# local change\n")
- with self.assertRaisesRegex(RuntimeError, "changed locally"):
- deploy.deploy_mod_ui("install", config, deploy.read_registry())
- def test_mod_ui_install_repairs_registered_corrupt_reference_bundle(self):
- config = deploy.load_deployment()["mod_ui"]
- registry = {
- "mod_host_ports": {},
- "mod_host": {},
- }
- deploy.deploy_mod_ui("install", config, registry)
- registry = deploy.read_registry()
- reference = Path(registry["mod_ui"]["reference_bundle"]["destination"])
- graph = reference / "rigforge-guitar.ttl"
- graph.write_bytes(b"")
- registry["mod_ui"]["reference_bundle"]["installed_sha256"] = deploy.tree_sha256(
- reference
- )
- deploy.write_registry(registry)
- self.commands.clear()
- deploy.deploy_mod_ui("install", config, deploy.read_registry())
- repaired_registry = deploy.read_registry()
- repaired_reference = Path(
- repaired_registry["mod_ui"]["reference_bundle"]["destination"]
- )
- self.assertGreater((repaired_reference / "rigforge-guitar.ttl").stat().st_size, 0)
- corrupt_backup = repaired_reference.with_name(
- repaired_reference.name + ".rigforge-corrupt-backup"
- )
- self.assertEqual(
- (corrupt_backup / "rigforge-guitar.ttl").read_bytes(),
- b"",
- )
- self.assertEqual(
- repaired_registry["mod_ui"]["reference_bundle"]["installed_sha256"],
- deploy.tree_sha256(repaired_reference),
- )
- def test_mod_ui_force_install_backs_up_and_replaces_registry_hash_mismatch(self):
- config = deploy.load_deployment()["mod_ui"]
- registry = {
- "mod_host_ports": {},
- "mod_host": {},
- }
- deploy.deploy_mod_ui("install", config, registry)
- registry = deploy.read_registry()
- reference = Path(registry["mod_ui"]["reference_bundle"]["destination"])
- graph = reference / "rigforge-guitar.ttl"
- graph.write_bytes(b"changed outside deploy\n")
- original = graph.read_bytes()
- with self.assertRaisesRegex(RuntimeError, "changed locally"):
- deploy.deploy_mod_ui("install", config, deploy.read_registry())
- self.assertEqual(graph.read_bytes(), original)
- deploy.deploy_mod_ui(
- "install", config, deploy.read_registry(), force_reference=True
- )
- repaired_registry = deploy.read_registry()
- repaired_reference = Path(
- repaired_registry["mod_ui"]["reference_bundle"]["destination"]
- )
- backup = repaired_reference.with_name(
- repaired_reference.name + ".rigforge-force-backup"
- )
- self.assertEqual((backup / "rigforge-guitar.ttl").read_bytes(), original)
- self.assertGreater((repaired_reference / "rigforge-guitar.ttl").stat().st_size, 0)
- self.assertEqual(
- repaired_registry["mod_ui"]["reference_bundle"]["installed_sha256"],
- deploy.tree_sha256(repaired_reference),
- )
- def test_mod_ui_force_install_requires_a_registered_reference_bundle(self):
- config = deploy.load_deployment()["mod_ui"]
- registry = {
- "mod_host_ports": {},
- "mod_host": {},
- }
- state = deploy.yaml.safe_load(
- (ROOT / "config" / "desired-state.yaml").read_text(encoding="utf-8")
- )
- deploy.manage_mod_ui_nam_models("install", config, registry, state)
- reference = Path(deploy.render_mod_ui_service(config)["reference_bundle"])
- reference.mkdir(parents=True)
- (reference / "local-file").write_text("do not replace", encoding="utf-8")
- with self.assertRaisesRegex(RuntimeError, "requires a registry-managed"):
- deploy.deploy_mod_ui(
- "install", config, registry, force_reference=True
- )
- self.assertEqual(
- (reference / "local-file").read_text(encoding="utf-8"),
- "do not replace",
- )
- def test_mod_ui_reset_restores_reference_and_backs_up_user_state(self):
- registry = {
- "mod_host_ports": {},
- "mod_host": {},
- }
- config = deploy.load_deployment()["mod_ui"]
- deploy.deploy_mod_ui("install", config, registry)
- registry = deploy.read_registry()
- board = self.home / config["pedalboards_directory"] / "default.pedalboard"
- board.mkdir(parents=True)
- (board / "user-edit.txt").write_text("keep this backup", encoding="utf-8")
- last_state = self.home / config["last_state_file"]
- last_state.parent.mkdir(parents=True, exist_ok=True)
- last_state.write_text('{"bank":0,"pedalboard":"/old/board"}\n', encoding="utf-8")
- self.commands.clear()
- deploy.deploy_mod_ui("reset", config, registry)
- reference = Path(
- deploy.read_registry()["mod_ui"]["reference_bundle"]["destination"]
- )
- self.assertEqual((board / "rigforge-guitar.ttl").read_bytes(),
- (reference / "rigforge-guitar.ttl").read_bytes())
- self.assertTrue(
- (board.with_name("default.pedalboard.rigforge-reset-backup") / "user-edit.txt").is_file()
- )
- self.assertFalse(last_state.exists())
- self.assertTrue(
- last_state.with_name(last_state.name + ".rigforge-reset-backup").is_file()
- )
- self.assertEqual(
- self.commands,
- [
- ["systemctl", "--user", "stop", "rigforge-mod-ui.service"],
- ["systemctl", "--user", "start", "rigforge-mod-ui.service"],
- ["systemctl", "--user", "is-active", "--quiet", "rigforge-mod-ui.service"],
- ],
- )
- def test_mod_ui_reset_can_restore_board_without_managing_service_lifecycle(self):
- registry = {
- "mod_host_ports": {},
- "mod_host": {},
- }
- config = deploy.load_deployment()["mod_ui"]
- deploy.deploy_mod_ui("install", config, registry)
- registry = deploy.read_registry()
- self.commands.clear()
- deploy.reset_mod_ui_reference(config, registry, manage_services=False)
- self.assertEqual(self.commands, [])
- board = self.home / config["pedalboards_directory"] / "default.pedalboard"
- self.assertTrue((board / "rigforge-guitar.ttl").is_file())
- def test_mod_host_install_refuses_an_existing_control_socket(self):
- self.install_mod_host_ports_prerequisite()
- 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()
- self.install_mod_host_ports_prerequisite()
- 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 / "neural_amp_modeler.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"])
- backup = Path(registry["namlv2"]["backup"])
- self.assertEqual(
- backup,
- self.home / ".local/state/rigforge/namlv2-backups/neural_amp_modeler.lv2.rigforge-backup",
- )
- self.assertTrue(backup.is_dir())
- 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.assertFalse(backup.exists())
- self.assertNotIn("namlv2", deploy.read_registry())
- def test_nam_install_migrates_legacy_adjacent_backup_out_of_lv2_search_path(self):
- destination = self.home / ".lv2/neural_amp_modeler.lv2"
- destination.mkdir(parents=True)
- (destination / "old.txt").write_text("original plugin", encoding="utf-8")
- self.install_nam()
- config = self.nam_config()
- registry = deploy.read_registry()
- record = registry["namlv2"]
- old_backup = destination.with_name(destination.name + ".rigforge-backup")
- new_backup = (
- self.home
- / config["backup_directory"]
- / (destination.name + ".rigforge-backup")
- )
- os.replace(new_backup, old_backup)
- record["backup"] = str(old_backup)
- registry["namlv2"] = record
- deploy.write_registry(registry)
- deploy.deploy_namlv2("install", config, deploy.read_registry())
- self.assertFalse(old_backup.exists())
- self.assertTrue(new_backup.is_dir())
- self.assertEqual(
- deploy.read_registry()["namlv2"]["backup"],
- str(new_backup),
- )
- def test_nam_install_uses_the_dynamic_runtime_build_artifact(self):
- config = deploy.load_deployment()["namlv2"]
- destination = self.home / ".lv2/neural_amp_modeler.lv2"
- with patch.object(
- deploy,
- "download_archive",
- side_effect=AssertionError("built artifact install must not download the static release"),
- ):
- deploy.deploy_namlv2("install", config, deploy.read_registry())
- source = ROOT / config["artifact_directory"] / config["bundle_directory"]
- self.assertEqual(deploy.tree_sha256(destination), deploy.tree_sha256(source))
- registry = deploy.read_registry()["namlv2"]
- self.assertEqual(registry["tag"], "v0.2.3")
- self.assertIsNone(registry["archive_sha256"])
- self.assertEqual(registry["artifact_sha256"], deploy.tree_sha256(source))
- with patch.object(
- deploy,
- "download_archive",
- side_effect=AssertionError("idempotent build artifact install must not download"),
- ):
- deploy.deploy_namlv2("install", config, deploy.read_registry())
- self.assertEqual(deploy.tree_sha256(destination), deploy.tree_sha256(source))
- deploy.deploy_namlv2("uninstall", config, deploy.read_registry())
- self.assertFalse(destination.exists())
- 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()
|