7ecc12b747
Server: internal/discovery publishes a DNS-SD record (hashicorp/mdns) with the port LAN clients dial (the web proxy via -mdns-port, which the supervisor passes, or a non-loopback -addr) and TXT v, name, path, tls, auth. On by default; -mdns=false or PIPELINES_MDNS=off disables it. Failures are logged and never block startup; the advert is withdrawn on shutdown. App: the Connect to Pipelines screen gets 'Find servers on this network' behind a ServerDiscovery interface (Bonsoir: Android NSD with multicast lock, iOS Bonjour with NSLocalNetworkUsageDescription and NSBonjourServices). Tapping a server fills the address and connects; manual entry stays. Web has no scan.
384 lines
22 KiB
Python
384 lines
22 KiB
Python
import importlib.util
|
|
import io
|
|
import os
|
|
from pathlib import Path
|
|
import signal
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from contextlib import redirect_stdout
|
|
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)
|
|
setup_spec = importlib.util.spec_from_file_location("setup_native_service", Path(__file__).with_name("setup-native.py"))
|
|
setup_native = importlib.util.module_from_spec(setup_spec)
|
|
setup_spec.loader.exec_module(setup_native)
|
|
|
|
|
|
class FakeProcess:
|
|
def __init__(self, pid=4321):
|
|
self.pid = pid
|
|
|
|
def poll(self):
|
|
return None
|
|
|
|
|
|
class ManageNativeTests(unittest.TestCase):
|
|
def service_config(self, home, system):
|
|
config = self.config(home / "prefix")
|
|
config["opencode_env"] = {"XDG_CONFIG_HOME": str(home / ".config")}
|
|
path = (home / "Library/LaunchAgents/net.a-wan.pipelines.plist" if system == "Darwin"
|
|
else home / ".config/systemd/user/net.a-wan.pipelines.service")
|
|
setup_native.Setup(home, environ={}).register_service(path, config)
|
|
return config
|
|
|
|
def test_native_service_preview_is_read_only_on_both_platforms(self):
|
|
for system in ("Darwin", "Linux"):
|
|
with self.subTest(system=system), tempfile.TemporaryDirectory() as directory, \
|
|
patch.object(manage.Path, "home", return_value=Path(directory)), \
|
|
patch.object(manage.platform, "system", return_value=system):
|
|
config = self.service_config(Path(directory), system)
|
|
path = Path(config["registration"])
|
|
before = path.read_bytes()
|
|
with patch.object(manage, "run", side_effect=AssertionError("preview invoked OS manager")), \
|
|
patch.object(manage, "start", side_effect=AssertionError("preview started services")), \
|
|
redirect_stdout(io.StringIO()) as output:
|
|
manage.service_command(config, ["enable", "--print"])
|
|
self.assertEqual(path.read_bytes(), before)
|
|
self.assertEqual(output.getvalue(), before.decode())
|
|
self.assertNotIn("publisher-token", output.getvalue())
|
|
|
|
def test_native_service_enable_and_disable_use_existing_registration(self):
|
|
for system in ("Darwin", "Linux"):
|
|
with self.subTest(system=system), tempfile.TemporaryDirectory() as directory, \
|
|
patch.object(manage.Path, "home", return_value=Path(directory)), \
|
|
patch.object(manage.platform, "system", return_value=system):
|
|
config = self.service_config(Path(directory), system)
|
|
before = Path(config["registration"]).read_bytes()
|
|
with patch.object(manage, "start") as start, redirect_stdout(io.StringIO()):
|
|
manage.service_command(config, ["enable"])
|
|
start.assert_called_once_with(config)
|
|
calls = []
|
|
with patch.object(manage, "run", side_effect=lambda args, **kw: calls.append(args)), \
|
|
patch.object(manage, "stop", side_effect=lambda cfg: calls.append("stop")), \
|
|
redirect_stdout(io.StringIO()):
|
|
manage.service_command(config, ["disable"])
|
|
expected = (["launchctl", "disable", f"gui/{os.getuid()}/net.a-wan.pipelines"] if system == "Darwin"
|
|
else ["systemctl", "--user", "disable", "net.a-wan.pipelines.service"])
|
|
self.assertEqual(calls, [expected, "stop"])
|
|
self.assertEqual(Path(config["registration"]).read_bytes(), before)
|
|
|
|
def test_native_service_status_does_not_print_raw_manager_output(self):
|
|
for system in ("Darwin", "Linux"):
|
|
with self.subTest(system=system), tempfile.TemporaryDirectory() as directory, \
|
|
patch.object(manage.Path, "home", return_value=Path(directory)), \
|
|
patch.object(manage.platform, "system", return_value=system):
|
|
config = self.service_config(Path(directory), system)
|
|
|
|
def manager(args, **kwargs):
|
|
self.assertTrue(kwargs["quiet"])
|
|
return subprocess.CompletedProcess(args, 0, stdout='"net.a-wan.pipelines" => true\nprivate-output')
|
|
|
|
with patch.object(manage, "run", side_effect=manager), redirect_stdout(io.StringIO()) as output:
|
|
manage.service_command(config, ["status"])
|
|
self.assertNotIn("private-output", output.getvalue())
|
|
self.assertIn("Autostart: " + ("disabled" if system == "Darwin" else "enabled"), output.getvalue())
|
|
|
|
def test_native_service_rejects_unmanaged_symlink_and_wrong_prefix_definitions(self):
|
|
for variant in ("unmanaged", "symlink", "wrong-prefix", "wrong-label"):
|
|
with self.subTest(variant=variant), tempfile.TemporaryDirectory() as directory, \
|
|
patch.object(manage.Path, "home", return_value=Path(directory)), \
|
|
patch.object(manage.platform, "system", return_value="Linux"):
|
|
config = self.service_config(Path(directory), "Linux")
|
|
path = Path(config["registration"])
|
|
if variant == "unmanaged":
|
|
path.write_text("Managed by pipelines service\nExecStart=developer-server")
|
|
elif variant == "symlink":
|
|
target = path.with_suffix(".other")
|
|
path.rename(target)
|
|
path.symlink_to(target)
|
|
elif variant == "wrong-prefix":
|
|
config["prefix"] += "-other"
|
|
else:
|
|
config["label"] = "pipelines"
|
|
with patch.object(manage, "run", side_effect=AssertionError("unsafe OS manager invocation")), \
|
|
patch.object(manage, "start", side_effect=AssertionError("unsafe startup")):
|
|
with self.assertRaises(ValueError):
|
|
manage.service_command(config, ["enable"])
|
|
|
|
def test_native_service_rejects_standalone_database_flags(self):
|
|
with redirect_stdout(io.StringIO()), patch("sys.stderr", new_callable=io.StringIO):
|
|
with self.assertRaises(SystemExit) as raised:
|
|
manage.service_command({}, ["enable", "--db", "/other.db"])
|
|
self.assertEqual(raised.exception.code, 2)
|
|
|
|
def test_manager_dispatches_internal_service_verb_without_forwarding_to_go(self):
|
|
config = {"prefix": "/scratch/native"}
|
|
with patch("sys.argv", ["manage.py", "--prefix", "/scratch/native", "native-service", "enable", "--print"]), \
|
|
patch.object(manage, "load", return_value=config), \
|
|
patch.object(manage, "service_command") as service, \
|
|
patch.object(manage.os, "execve", side_effect=AssertionError("recursive Go dispatch")):
|
|
manage.main()
|
|
service.assert_called_once_with(config, ["enable", "--print"])
|
|
|
|
def test_supervisor_authoring_flag_requires_explicit_manifest_true(self):
|
|
for role in (True, False, None, "true"):
|
|
with self.subTest(role=role), tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
(root / "logs").mkdir()
|
|
config = self.config(root)
|
|
if role is not None:
|
|
config["allow_authoring"] = role
|
|
ntfy, server = MagicMock(), MagicMock()
|
|
ntfy.poll.return_value = None
|
|
server.poll.return_value = 0
|
|
server.returncode = 0
|
|
with patch.object(manage, "ntfy_start", return_value=ntfy), \
|
|
patch.object(manage, "wait_health"), patch.object(manage.signal, "signal"), \
|
|
patch.object(manage.subprocess, "Popen", return_value=server) as popen:
|
|
manage.supervise(config)
|
|
command = popen.call_args_list[0].args[0]
|
|
self.assertEqual(command[:5], [str(root / "bin/pipelines-server"), "-addr", "127.0.0.1:8080",
|
|
"-db", str(root / "data/pipelines.db")])
|
|
self.assertEqual("-allow-authoring" in command, role is True)
|
|
|
|
def test_mdns_advertises_the_lan_web_proxy_port(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
config = self.config(Path(directory))
|
|
self.assertEqual(manage.mdns_args(config), [])
|
|
config["web"]["host"] = "0.0.0.0"
|
|
self.assertEqual(manage.mdns_args(config), ["-mdns-port", "8082"])
|
|
config.pop("web")
|
|
self.assertEqual(manage.mdns_args(config), [])
|
|
|
|
def config(self, root, mode="native"):
|
|
(root / "lib/app-web").mkdir(parents=True, exist_ok=True)
|
|
(root / "lib/app-web/index.html").write_text("app")
|
|
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"},
|
|
"web": {"node": "/bin/node", "script": str(root / "lib/web/serve.mjs"), "host": "127.0.0.1",
|
|
"port": 8082, "auth_file": str(root / "auth.json"), "root": str(root / "lib/app-web")},
|
|
}
|
|
|
|
def test_server_never_starts_without_the_web_app(self):
|
|
for missing in ("unconfigured", "not built"):
|
|
with self.subTest(missing=missing), tempfile.TemporaryDirectory() as directory:
|
|
config = self.config(Path(directory))
|
|
if missing == "unconfigured":
|
|
config.pop("web")
|
|
else:
|
|
(Path(directory) / "lib/app-web/index.html").unlink()
|
|
with patch.object(manage, "run") as run, patch.object(manage.subprocess, "Popen") as popen:
|
|
with self.assertRaisesRegex(RuntimeError, "refusing to start the server"):
|
|
manage.start(config)
|
|
with self.assertRaisesRegex(RuntimeError, "refusing to start the server"):
|
|
manage.supervise(config)
|
|
run.assert_not_called()
|
|
popen.assert_not_called()
|
|
|
|
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")
|
|
response = MagicMock()
|
|
response.__enter__.return_value.status = 200
|
|
with patch.object(manage.platform, "system", return_value=system), \
|
|
patch.object(manage, "run") as run, patch.object(manage, "wait_health"), \
|
|
patch.object(manage.urllib.request, "urlopen", return_value=response):
|
|
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_update_reinstalls_the_same_way_and_restarts(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
source = Path(directory) / "src"
|
|
source.mkdir()
|
|
(source / "install.sh").write_text("#!/bin/sh\n")
|
|
config = self.config(Path(directory))
|
|
config["source"] = str(source)
|
|
config["install_args"] = ["--port", "18180"]
|
|
calls = []
|
|
done = subprocess.CompletedProcess([], 0)
|
|
with patch.object(manage, "healthy", return_value=True), \
|
|
patch.object(manage, "active_runs", return_value=[]), \
|
|
patch.object(manage.subprocess, "run", side_effect=lambda cmd, **kw: calls.append(cmd) or done), \
|
|
patch.object(manage, "load", return_value=config), \
|
|
patch.object(manage, "stop") as stop, patch.object(manage, "start") as start, \
|
|
patch.object(manage, "report") as report:
|
|
manage.update(config)
|
|
self.assertEqual(calls[-1], ["sh", str(source / "install.sh"), "--port", "18180"])
|
|
stop.assert_called_once()
|
|
start.assert_called_once()
|
|
report.assert_called_once()
|
|
|
|
def test_update_waits_for_active_runs(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
source = Path(directory)
|
|
(source / "install.sh").write_text("#!/bin/sh\n")
|
|
config = self.config(Path(directory))
|
|
config["source"] = str(source)
|
|
with patch.object(manage, "healthy", return_value=True), \
|
|
patch.object(manage, "active_runs", return_value=[{"id": 1}]), \
|
|
patch.object(manage.subprocess, "run") as ran:
|
|
with self.assertRaisesRegex(RuntimeError, "still executing"):
|
|
manage.update(config)
|
|
ran.assert_not_called()
|
|
|
|
def test_update_needs_a_recorded_source(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
config = self.config(Path(directory))
|
|
with self.assertRaisesRegex(ValueError, "source checkout"):
|
|
manage.update(config)
|
|
|
|
def test_report_prints_the_server_block_and_access(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
(root / "logs").mkdir()
|
|
(root / "logs/server.log").write_text(
|
|
"== Pipelines server v1\n API x\n==\nold\n"
|
|
"== Pipelines server v2\n API y\n== Backends\n Chat opencode\n==\nlater line\n")
|
|
(root / "app-web").mkdir()
|
|
(root / "app-web/index.html").write_text("app")
|
|
(root / "updates").mkdir()
|
|
config = self.config(root)
|
|
config["web"] = {"host": "0.0.0.0", "port": 8082, "auth_file": "/private/auth.json",
|
|
"root": str(root / "app-web"), "updates": str(root / "updates")}
|
|
config["opencode_env"] = {"PIPELINES_WEB_PUBLIC_URL": "http://10.0.0.2:8082"}
|
|
output = io.StringIO()
|
|
with redirect_stdout(output):
|
|
manage.report(config)
|
|
text = output.getvalue()
|
|
self.assertIn("== Pipelines server v2\n API y\n== Backends\n Chat opencode\n==\n", text)
|
|
self.assertNotIn("v1", text)
|
|
self.assertIn("http://10.0.0.2:8082 (password in /private/auth.json)", text)
|
|
self.assertIn("Web app installed", text)
|
|
self.assertIn("not built; run ./install --apk", text)
|
|
|
|
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",
|
|
"root": str(root / "lib/app-web"), "updates": "/prefix/lib/app-updates"}
|
|
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",
|
|
"--root", str(root / "lib/app-web"),
|
|
"--updates", "/prefix/lib/app-updates"])
|
|
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, "root": config["web"]["root"]}
|
|
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()
|