Route native service commands and configure fresh authoring role
Select native installs only via explicit PIPELINES_PREFIX or the matching owned prefix/bin/pipelines executable. Route enable/status/disable to the existing native supervisor; validate registration ownership, label and command, keep preview read-only, and disable login registration before stopping. Never fall back to a standalone service for an invalid explicit prefix. Fresh native installs explicitly enable authoring, while reinstalls preserve false or missing legacy role settings. Add explicit --allow-authoring/--no-authoring overrides and pass the server flag only for manifest true. Print a copyable native enable command and update installation help/docs. No Docker configuration changes. Checks: 54 mocked native installer/lifecycle tests; 8 entrypoint tests; offline Go installation/CLI tests and vet; gofmt clean; shell syntax and diff checks. No live install, restart, deployment, permission/config changes, or native Linux verification.
This commit is contained in:
@@ -1,14 +1,20 @@
|
||||
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:
|
||||
@@ -20,6 +26,124 @@ class FakeProcess:
|
||||
|
||||
|
||||
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.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 config(self, root, mode="native"):
|
||||
return {
|
||||
"format": 1, "prefix": str(root), "python": "python3", "server_port": 8080,
|
||||
|
||||
Reference in New Issue
Block a user