import importlib.util import json import os from pathlib import Path import socket import tempfile import unittest from unittest import mock import install import manage class InstallationTests(unittest.TestCase): def test_launcher_forwards_application_arguments_unchanged(self): config = dict(prefix="/owned", server_port=12345, opencode="/host/opencode", path="/owned/bin:/usr/bin") for command in (["edit", "a name", "change X", "--json"], ["--url", "ws://other/connect", "list", "--json"], ["--help"], ["run", "workflow", "task", "--yes", "--json"]): with mock.patch.object(manage,"load",return_value=config), mock.patch.object(manage.sys,"argv",["manage.py","--prefix","/owned",*command]), mock.patch.object(manage.os,"execve") as execute: manage.main() binary,args,env=execute.call_args.args self.assertEqual(args,["/owned/bin/pipelines",*command]) self.assertEqual(env["PIPELINES_URL"],"ws://127.0.0.1:12345/connect") def test_compose_has_only_ntfy_and_no_credentials(self): config = dict(timezone="Europe/London", public_url="http://100.1.2.3:8090", ntfy_port=8090) text = install.ntfy_compose(config) self.assertIn("binwiederhier/ntfy:v2.29.0", text) self.assertIn("deny-all", text) self.assertNotIn("opencode", text) self.assertNotIn("password", text.lower()) self.assertNotIn("pipelines:", text) def test_detect_free_port_avoids_listener(self): with socket.socket() as listener: listener.bind(("0.0.0.0", 0)); listener.listen() occupied = listener.getsockname()[1] self.assertNotEqual(install.choose_port(occupied), occupied) def test_secret_files_private_and_not_overwritten(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory, "secrets/token") install.write_private(path, "private") self.assertEqual(path.stat().st_mode & 0o777, 0o600) with self.assertRaises(FileExistsError): install.write_private(path, "replacement") self.assertEqual(path.read_text(), "private") def test_management_requires_owned_installation_marker(self): with tempfile.TemporaryDirectory() as directory: with self.assertRaises(ValueError): manage.load(directory) root = Path(directory).resolve() (root / manage.MARKER).write_text(json.dumps(dict(format=1, prefix=str(root)))) self.assertEqual(manage.load(root)["prefix"], str(root)) (root / manage.MARKER).write_text(json.dumps(dict(format=1, prefix="/"))) with self.assertRaises(ValueError): manage.load(root) def test_pid_reuse_cannot_signal_unrelated_process(self): with tempfile.TemporaryDirectory() as directory: Path(directory, "supervisor.pid").write_text(str(os.getpid())) self.assertIsNone(manage.managed_pid(dict(prefix=directory))) def test_default_uninstall_preserves_data_and_open_code(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory, "app"); root.mkdir() for name in ("bin", "lib", "data", "secrets"): (root / name).mkdir() (root / "data/pipelines.db").write_text("history") (root / "secrets/publisher-token").write_text("credential") config = dict(format=1, prefix=str(root), launcher=str(Path(directory, "pipelines")), registration=None) Path(config["launcher"]).symlink_to(root / "bin/pipelines") with mock.patch.object(manage, "stop"), mock.patch.object(manage, "compose") as compose: manage.uninstall(config, yes=True) compose.assert_called_once_with(config, "down", quiet=True) self.assertEqual((root / "data/pipelines.db").read_text(), "history") self.assertEqual((root / "secrets/publisher-token").read_text(), "credential") self.assertFalse((root / "bin").exists()) self.assertFalse(Path(config["launcher"]).is_symlink()) def test_service_registration_quotes_paths(self): with tempfile.TemporaryDirectory(prefix="path with spaces ") as directory: root = Path(directory);(root / "logs").mkdir() config = dict(prefix=str(root), python="/usr/bin/python3", label="io.pipelines.test") with mock.patch.object(install.platform,"system",return_value="Linux"),mock.patch.object(install.Path,"home",return_value=root): path = Path(install.make_registration(config)) self.assertIn('"'+str(root / 'lib/manage.py')+'"', path.read_text()) self.assertIn("Restart=on-failure", path.read_text()) if __name__ == "__main__": unittest.main()