Files
pipelines/scripts/test_manage_native.py
T
alan ae6ba6ae10 Install authenticated native web proxy with user autostart
Register the existing native supervisor with launchd/systemd, include the dependency-free authenticated web proxy, detect Node, and keep the API loopback by default. Preserve existing web credentials and configured host harness defaults. Clarify single-step setup, lifecycle selection, and optional ntfy limits.

Checks: 44 mocked native installer/lifecycle tests; 8 entrypoint tests; 42 Docker installer tests; GOPROXY=off GOTOOLCHAIN=local go test ./internal/installation ./cmd/pipelines; sh -n install install.sh; git diff --check. No live install, deployment, restart, downloads, real autostart/login verification or native Linux verification.
2026-10-20 21:31:36 +01:00

160 lines
8.5 KiB
Python

import importlib.util
import os
from pathlib import Path
import signal
import tempfile
import unittest
from unittest.mock import MagicMock, 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_registered_start_commands_on_both_platforms(self):
for system in ("Darwin", "Linux"):
with self.subTest(system=system), tempfile.TemporaryDirectory() as directory:
config = self.config(Path(directory))
config["registration"] = str(Path(directory) / "service")
with patch.object(manage.platform, "system", return_value=system), \
patch.object(manage, "run") as run, patch.object(manage, "wait_health"):
manage.start(config)
calls = [call.args[0] for call in run.call_args_list]
if system == "Linux":
self.assertEqual(calls, [["systemctl", "--user", "daemon-reload"],
["systemctl", "--user", "enable", "--now", "net.a-wan.pipelines.service"]])
else:
self.assertEqual(calls[0], ["launchctl", "enable", f"gui/{os.getuid()}/net.a-wan.pipelines"])
self.assertIn(["launchctl", "kickstart", f"gui/{os.getuid()}/net.a-wan.pipelines"], calls)
def test_supervisor_spawns_authenticated_web_and_cleans_up_on_exit(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / "logs").mkdir()
config = self.config(root)
config["web"] = {"node": "/bin/node", "script": "/prefix/lib/web/serve.mjs",
"host": "0.0.0.0", "port": 8082, "auth_file": "/private/auth.json"}
ntfy, server, web = MagicMock(), MagicMock(), MagicMock()
ntfy.poll.return_value = None
server.poll.return_value = None
web.poll.side_effect = [1, None]
with patch.object(manage, "ntfy_start", return_value=ntfy), \
patch.object(manage, "wait_health"), patch.object(manage.signal, "signal"), \
patch.dict(os.environ, {"PIPELINES_WEB_AUTH": "disabled"}), \
patch.object(manage.subprocess, "Popen", side_effect=[server, web]) as popen:
with self.assertRaisesRegex(RuntimeError, "web proxy exited"):
manage.supervise(config)
call = popen.call_args_list[1]
self.assertEqual(call.args[0], ["/bin/node", "/prefix/lib/web/serve.mjs", "--port", "8082",
"--host", "0.0.0.0", "--pipelines", "127.0.0.1:8080"])
self.assertNotIn("PIPELINES_WEB_AUTH", call.kwargs["env"])
self.assertEqual(call.kwargs["env"]["PIPELINES_WEB_AUTH_FILE"], "/private/auth.json")
web.terminate.assert_called_once()
server.terminate.assert_called_once()
ntfy.terminate.assert_called_once()
def test_start_checks_web_login_readiness(self):
with tempfile.TemporaryDirectory() as directory:
config = self.config(Path(directory))
config["registration"] = "/scratch/service"
config["web"] = {"host": "0.0.0.0", "port": 8123}
response = MagicMock()
response.__enter__.return_value.status = 200
with patch.object(manage.platform, "system", return_value="Linux"), \
patch.object(manage, "run"), patch.object(manage, "wait_health"), \
patch.object(manage.urllib.request, "urlopen", return_value=response) as open_url:
manage.start(config)
open_url.assert_called_once_with("http://127.0.0.1:8123/login", timeout=2)
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()