import importlib.util import os from pathlib import Path import signal import tempfile import unittest from unittest.mock import patch spec = importlib.util.spec_from_file_location("manage_native", Path(__file__).with_name("manage.py")) manage = importlib.util.module_from_spec(spec) spec.loader.exec_module(manage) class FakeProcess: def __init__(self, pid=4321): self.pid = pid def poll(self): return None class ManageNativeTests(unittest.TestCase): def config(self, root, mode="native"): return { "format": 1, "prefix": str(root), "python": "python3", "server_port": 8080, "ntfy_port": 8090, "opencode": "opencode", "opencode_env": {}, "path": "", "topic": "pipelines", "public_url": "http://192.168.1.20:8090", "username": "phone-reader", "workspace": str(root), "launcher": str(root / "bin/pipelines"), "registration": None, "label": "net.a-wan.pipelines", "docker": "docker", "project": "pipelines", "timezone": "UTC", "ntfy": {"mode": mode, "bin": str(root / "bin/ntfy"), "config": str(root / "data/ntfy/server.yml"), "base_url": "http://192.168.1.20:8090"}, } def test_mode_selection_defaults_to_docker(self): self.assertEqual(manage.ntfy_mode({}), "docker") self.assertEqual(manage.ntfy_mode({"ntfy": {}}), "docker") self.assertEqual(manage.ntfy_mode({"ntfy": {"mode": "native"}}), "native") self.assertEqual(manage.ntfy_health_url({"ntfy_port": 8090}), "http://127.0.0.1:8090/v1/health") def test_server_address_uses_the_manifest_bind_and_defaults_to_loopback(self): self.assertEqual(manage.server_address({"server_port": 8080, "bind_address": "0.0.0.0"}), "0.0.0.0:8080") self.assertEqual(manage.server_address({"server_port": 8080, "bind_address": "10.0.0.75"}), "10.0.0.75:8080") # A manifest written before the setting existed must stay loopback-only. self.assertEqual(manage.server_address({"server_port": 8080}), "127.0.0.1:8080") self.assertEqual(manage.server_address({"server_port": 8080, "bind_address": ""}), "127.0.0.1:8080") def test_ntfy_start_launches_binary_with_generated_config(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) (root / "bin").mkdir() (root / "logs").mkdir() config = self.config(root) with patch.object(manage.subprocess, "Popen", return_value=FakeProcess(pid=4321)) as popen: child = manage.ntfy_start(config) self.assertEqual(popen.call_args[0][0], [config["ntfy"]["bin"], "serve", "--config", config["ntfy"]["config"]]) self.assertEqual(child.pid, 4321) self.assertEqual((root / "ntfy.pid").read_text(), "4321") def test_ntfy_stop_terminates_managed_process_and_removes_pid(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) (root / "bin").mkdir() (root / "bin/ntfy").write_text("") (root / "ntfy.pid").write_text(str(os.getpid())) config = self.config(root) killed = [] def fake_kill(pid, sig): if sig == 0: raise ProcessLookupError killed.append((pid, sig)) with patch.object(manage.subprocess, "check_output", return_value=str(root / "bin/ntfy") + " serve --config x"), \ patch.object(manage.os, "kill", side_effect=fake_kill), \ patch.object(manage, "compose", side_effect=AssertionError("compose used for native ntfy")): manage.ntfy_stop(config) self.assertIn((os.getpid(), signal.SIGTERM), killed) self.assertFalse((root / "ntfy.pid").exists()) def test_native_stop_never_calls_compose(self): with tempfile.TemporaryDirectory() as directory: config = self.config(Path(directory)) with patch.object(manage, "registration", return_value=None), \ patch.object(manage, "managed_pid", return_value=None), \ patch.object(manage, "ntfy_stop") as stop, \ patch.object(manage, "compose", side_effect=AssertionError("compose used for native ntfy")): manage.stop(config) stop.assert_called_once_with(config) def test_uninstall_native_skips_compose_down(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) (root / "bin").mkdir() config = self.config(root) with patch.object(manage, "stop"), \ patch.object(manage, "registration", return_value=None), \ patch.object(manage, "compose", side_effect=AssertionError("compose used for native ntfy")): manage.uninstall(config, purge=False, yes=True) self.assertTrue((root / manage.MARKER).is_file()) if __name__ == "__main__": unittest.main()