a70e3aef4b
Add scripts/setup-native.py and make ./install (and --native) build the server, the CLI and ntfy from source into ~/.local/share/pipelines, detect an existing host OpenCode and Pi instead of installing them, install the packaged authoring skill, migrate an existing Docker database and ntfy auth store, save the host client default and start both services. ./install --docker keeps the previous Docker server + ntfy installation unchanged. ntfy is compiled from its pinned Go module on every host because upstream publishes no macOS server binary (its darwin asset is built with -tags noserver); its cgo SQLite driver is why a C compiler is now required natively. The ntfy credential bootstrap and the client default writer are shared by both installers, installation.Load resolves symlinked prefixes, the Docker migration copies the SQLite write-ahead log so the newest definitions and runs are not lost, and the account subcommands read the generated configuration through NTFY_CONFIG_FILE. Verified on macOS: 115 Python tests, go build/vet/test, and scratch installs that migrate 9 pipelines and 31 runs, build ntfy v2.30.0 and start both services healthy on spare ports before stopping them.
98 lines
4.4 KiB
Python
98 lines
4.4 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_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()
|