Files
pipelines/scripts/test_manage_native.py
T
alan 8adc6b8080 Bind the native server to the LAN so the phone client can reach it
The native install hardcoded -addr 127.0.0.1, so the server was unreachable from
the phone: the emulator worked because it proxies to the host loopback, but a
real device could not connect even though ntfy (bound to 0.0.0.0:8090) worked.

The bind address is now a manifest setting (server_address() in manage.py, with
older manifests still defaulting to loopback) and setup-native.py records it,
defaulting to 0.0.0.0 like the Docker install did and offering
--bind-address 127.0.0.1 for a loopback-only server. An invalid address is
rejected, and setup prints the phone client URL.

Verified: scripts test suite 116 tests OK, and after reinstalling the server
listens on *:8080 and answers {"healthy":true} on 127.0.0.1, 10.0.0.75,
10.0.0.249 and the tailnet address.
2026-10-15 01:57:27 +01:00

105 lines
5.0 KiB
Python

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()