fc7dbb0ac0
Server: drop ntfy publishing (stage hooks, run outbox, chat attention, chat tool notify, auto-switch and schedule notices), the chat push outbox (its table is dropped at startup), ntfy status/endpoints and PIPELINES_NTFY_*. The app_notifications stream, subscribe/clear and kinds are unchanged. Notification settings keep run_finished/chat_needs_you/chat_reply_finished; ntfy_enabled is accepted and ignored and its stored row is never read. Stage type=ntfy is rejected: "ntfy stages were removed; notifications go to the Pipelines apps — use the stage notify hook". The notify hook remains. Installer: no ntfy build, accounts, config or supervisor child; manifest gets top-level "mode" (legacy ntfy.mode still read). Reinstall retires an old ntfy (safe PID-file stop, deletes binary, config, data dir, token, log, with a note in logs/install.log); uninstall cleans leftovers. App: ntfy settings section replaced by "Notification events"; texts updated. Docs: docs/ntfy.md folded into docs/notifications.md; install, docker, cli, author skill references updated; security review M1 marked resolved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1032 lines
62 KiB
Python
1032 lines
62 KiB
Python
import importlib.util
|
|
import io
|
|
import json
|
|
import os
|
|
import plistlib
|
|
import shutil
|
|
from contextlib import redirect_stdout
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
spec = importlib.util.spec_from_file_location("setup_native", Path(__file__).with_name("setup-native.py"))
|
|
setup_native = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(setup_native)
|
|
|
|
manage_spec = importlib.util.spec_from_file_location("pipelines_manage", Path(__file__).with_name("manage.py"))
|
|
manage = importlib.util.module_from_spec(manage_spec)
|
|
manage_spec.loader.exec_module(manage)
|
|
|
|
OPENSSL = shutil.which("openssl")
|
|
|
|
class Recorder:
|
|
def __init__(self, volumes=(), image="pipelines-pipelines:latest",
|
|
fail_create=False, fail_copy=False, fail_volume_ls=False, fail_build=False, fail_web=False,
|
|
host_model="fixture/host-default"):
|
|
self.calls = []
|
|
self.volumes = volumes
|
|
self.image = image
|
|
self.fail_create = fail_create
|
|
self.fail_copy = fail_copy
|
|
self.fail_volume_ls = fail_volume_ls
|
|
self.fail_build = fail_build
|
|
self.fail_web = fail_web
|
|
self.host_model = host_model
|
|
self.kwargs = []
|
|
|
|
def __call__(self, command, **kwargs):
|
|
command = list(command)
|
|
self.calls.append(command)
|
|
self.kwargs.append(kwargs)
|
|
stdout, code = "", 0
|
|
if command[0].endswith("node") and command[1:] == ["--version"]:
|
|
stdout = "v22.19.0\n"
|
|
if command[0].endswith("opencode") and command[1:] == ["api", "get", "/api/model/default"]:
|
|
model = None
|
|
if self.host_model:
|
|
provider, name = self.host_model.split("/", 1)
|
|
model = {"providerID": provider, "modelID": name, "enabled": True, "status": "active"}
|
|
stdout = json.dumps({"location": {"directory": "/scratch/home"}, "data": model})
|
|
if command[0] == "docker":
|
|
if command[1:3] == ["volume", "ls"]:
|
|
if self.fail_volume_ls:
|
|
code = 1
|
|
else:
|
|
stdout = "\n".join(self.volumes)
|
|
elif command[1:3] == ["image", "ls"]:
|
|
stdout = self.image + "\n" if self.image else ""
|
|
elif command[1] == "create" and self.fail_create:
|
|
code = 1
|
|
elif command[1] == "run" and "pipelines-app-build" in command:
|
|
# The fake app build writes its output to the /out mount.
|
|
out = next(v for v in command if v.endswith(":/out")).rsplit(":", 1)[0]
|
|
if not self.fail_web:
|
|
(Path(out) / "index.html").write_text("app")
|
|
elif command[1] == "cp":
|
|
if self.fail_copy:
|
|
code = 1
|
|
else:
|
|
Path(command[3]).write_bytes(b"migrated")
|
|
elif command[0] == "go" and command[1:2] == ["build"]:
|
|
if self.fail_build:
|
|
code = 1
|
|
elif "-o" in command:
|
|
# Every build output is created so the atomic installs have real
|
|
# files to move.
|
|
output = Path(command[command.index("-o") + 1])
|
|
output.write_bytes(b"#!/bin/sh\n")
|
|
output.chmod(0o755)
|
|
return subprocess.CompletedProcess(command, code, stdout=stdout, stderr="")
|
|
|
|
def has(self, *parts):
|
|
return any(all(part in call for part in parts) for call in self.calls)
|
|
|
|
|
|
class SetupNativeTests(unittest.TestCase):
|
|
def setUp(self):
|
|
# All LaunchAgent files, including full-run tests, stay in scratch HOME.
|
|
self.home = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self.home.cleanup)
|
|
home = patch.object(setup_native.Path, "home", return_value=Path(self.home.name))
|
|
home.start()
|
|
self.addCleanup(home.stop)
|
|
|
|
def installer(self, directory, recorder):
|
|
# A disposable XDG config home keeps the client default out of the real
|
|
# user configuration during tests.
|
|
return setup_native.Setup(directory, prefix=Path(directory) / "prefix", runner=recorder,
|
|
environ={"XDG_CONFIG_HOME": str(Path(directory) / "config"),
|
|
"PIPELINES_WEB_PASSWORD": "chosen tulip orbit canyon",
|
|
# The update signing key stays in scratch too, made
|
|
# with the host OpenSSL (shutil.which is patched).
|
|
"PIPELINES_UPDATE_SIGNING_KEY": str(Path(directory) / "update-key.pem"),
|
|
"PIPELINES_OPENSSL": OPENSSL or ""})
|
|
|
|
@staticmethod
|
|
def which_all(opencode="/usr/bin/opencode", pi=None):
|
|
mapping = {"go": "/usr/bin/go", "python3": "/usr/bin/python3",
|
|
"git": "/usr/bin/git", "curl": "/usr/bin/curl", "node": "/usr/bin/node",
|
|
"launchctl": "/bin/launchctl", "systemctl": "/usr/bin/systemctl"}
|
|
if opencode:
|
|
mapping["opencode"] = opencode
|
|
if pi:
|
|
mapping["pi"] = pi
|
|
return lambda name: mapping.get(name)
|
|
|
|
def test_reinstall_preserves_network_settings_and_explicit_flags_override(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
prefix = installer.resolve_prefix()
|
|
prefix.mkdir()
|
|
manifest = {"server_port": 8090,
|
|
"bind_address": "127.0.0.2",
|
|
"web": {"port": 8096, "host": "127.0.0.1"}}
|
|
(prefix / setup_native.MARKER).write_text(json.dumps(manifest))
|
|
args = setup_native.arguments([])
|
|
installer.preserve_network_settings(prefix, args)
|
|
self.assertEqual((args.port, args.web_port), (8090, 8096))
|
|
self.assertEqual((args.bind_address, args.web_host), ("127.0.0.2", "127.0.0.1"))
|
|
args = setup_native.arguments(["--web-port=8082", "--port", "8080"])
|
|
installer.preserve_network_settings(prefix, args)
|
|
self.assertEqual((args.port, args.web_port), (8080, 8082))
|
|
(prefix / setup_native.MARKER).unlink()
|
|
args = setup_native.arguments([])
|
|
installer.preserve_network_settings(prefix, args)
|
|
self.assertEqual(args.web_port, 8082)
|
|
|
|
def test_require_tools_reports_each_missing_tool_without_installing(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder()
|
|
installer = self.installer(directory, recorder)
|
|
mapping = {"go": "/usr/bin/go", "python3": "/usr/bin/python3", "git": None, "curl": None}
|
|
with patch.object(setup_native.shutil, "which", side_effect=lambda name: mapping.get(name)):
|
|
with self.assertRaises(ValueError) as raised:
|
|
installer.require_tools()
|
|
message = str(raised.exception)
|
|
self.assertIn("git", message)
|
|
self.assertIn("curl", message)
|
|
self.assertIn("node", message)
|
|
self.assertIn("never installs tools", message)
|
|
self.assertEqual(recorder.calls, [])
|
|
|
|
def test_registration_uses_native_supervisor_on_both_platforms(self):
|
|
for system in ("Darwin", "Linux"):
|
|
with self.subTest(system=system), tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
prefix = Path(directory) / "prefix with spaces"
|
|
manifest = {"prefix": str(prefix), "python": "/usr/bin/python3", "path": "/bin"}
|
|
with patch.object(setup_native.platform, "system", return_value=system), \
|
|
patch.object(setup_native.shutil, "which", side_effect=self.which_all()):
|
|
path = installer.service_path()
|
|
installer.register_service(path, manifest)
|
|
self.assertEqual(manifest["registration"], str(path))
|
|
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
|
|
if system == "Darwin":
|
|
data = plistlib.loads(path.read_bytes())
|
|
self.assertEqual(data["ProgramArguments"], ["/usr/bin/python3", str(prefix / "lib/manage.py"),
|
|
"supervise", "--prefix", str(prefix)])
|
|
self.assertTrue(data["RunAtLoad"])
|
|
else:
|
|
text = path.read_text()
|
|
self.assertIn('"supervise" "--prefix"', text)
|
|
self.assertIn('"' + str(prefix) + '"', text)
|
|
self.assertIn("WantedBy=default.target", text)
|
|
|
|
def test_unmanaged_service_is_never_replaced(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
with patch.object(setup_native.platform, "system", return_value="Linux"), \
|
|
patch.object(setup_native.shutil, "which", side_effect=self.which_all()):
|
|
path = installer.service_path()
|
|
path.parent.mkdir(parents=True)
|
|
path.write_text("operator service")
|
|
with self.assertRaisesRegex(ValueError, "unmanaged"):
|
|
installer.service_path()
|
|
self.assertEqual(path.read_text(), "operator service")
|
|
|
|
def test_missing_service_manager_fails_before_build_or_config(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
with patch.object(setup_native.platform, "system", return_value="Linux"), \
|
|
patch.object(setup_native.shutil, "which", return_value=None):
|
|
with self.assertRaisesRegex(ValueError, "systemctl is required"):
|
|
installer.service_path()
|
|
self.assertFalse((Path(directory) / "prefix").exists())
|
|
|
|
def test_web_install_copies_assets_and_preserves_credentials(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
prefix = Path(directory) / "prefix"
|
|
installer.prepare_layout(prefix)
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()):
|
|
web = installer.configure_web(prefix, setup_native.arguments([]))
|
|
auth = Path(web["auth_file"])
|
|
before = auth.read_bytes()
|
|
installer.configure_web(prefix, setup_native.arguments([]))
|
|
self.assertEqual(auth.read_bytes(), before)
|
|
self.assertEqual(auth.stat().st_mode & 0o777, 0o600)
|
|
stored = json.loads(before)
|
|
self.assertNotIn("password", stored)
|
|
self.assertNotIn(b"chosen tulip", before)
|
|
self.assertTrue(setup_native.web_password.verify_password(stored["password_hash"], "chosen tulip orbit canyon"))
|
|
self.assertTrue(Path(web["script"]).is_file())
|
|
# The web app is built in Docker once and installed for the proxy.
|
|
self.assertTrue(installer.runner.has("docker", "run", "pipelines-app-build", "web"))
|
|
self.assertEqual(web["root"], str(prefix / "lib/app-web"))
|
|
self.assertTrue(Path(web["root"]).is_dir())
|
|
self.assertTrue(Path(web["updates"]).is_dir())
|
|
self.assertTrue((prefix / "lib/web/src/proxySecurity.js").is_file())
|
|
self.assertEqual(web["port"], 8082)
|
|
|
|
def test_server_token_is_created_private_once_beside_the_web_auth(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
prefix = Path(directory) / "prefix"
|
|
installer.prepare_layout(prefix)
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()):
|
|
web = installer.configure_web(prefix, setup_native.arguments([]))
|
|
token = Path(web["server_token_file"])
|
|
before = token.read_bytes()
|
|
installer.configure_web(prefix, setup_native.arguments([]))
|
|
self.assertEqual(token.parent, Path(web["auth_file"]).parent)
|
|
self.assertEqual(token.read_bytes(), before)
|
|
self.assertEqual(token.stat().st_mode & 0o777, 0o600)
|
|
self.assertGreaterEqual(len(before.strip()), 32)
|
|
token.chmod(0o644)
|
|
with self.assertRaises(ValueError):
|
|
installer.ensure_server_token(token)
|
|
|
|
def test_new_web_password_must_be_chosen(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
prefix = Path(directory) / "prefix"
|
|
installer.prepare_layout(prefix)
|
|
installer.environ.pop("PIPELINES_WEB_PASSWORD")
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
|
patch.object(setup_native.sys.stdin, "isatty", return_value=False):
|
|
with self.assertRaisesRegex(ValueError, "PIPELINES_WEB_PASSWORD"):
|
|
installer.configure_web(prefix, setup_native.arguments([]))
|
|
installer.environ["PIPELINES_WEB_PASSWORD"] = "short"
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()):
|
|
with self.assertRaisesRegex(ValueError, "too weak"):
|
|
installer.configure_web(prefix, setup_native.arguments([]))
|
|
installer.environ.pop("PIPELINES_WEB_PASSWORD")
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
|
patch.object(setup_native.sys.stdin, "isatty", return_value=True), \
|
|
patch.object(setup_native.getpass, "getpass",
|
|
side_effect=["short", "typed tulip orbit canyon", "other tulip orbit canyon",
|
|
"typed tulip orbit canyon", "typed tulip orbit canyon"]):
|
|
web = installer.configure_web(prefix, setup_native.arguments([]))
|
|
stored = json.loads(Path(web["auth_file"]).read_text())["password_hash"]
|
|
self.assertTrue(setup_native.web_password.verify_password(stored, "typed tulip orbit canyon"))
|
|
|
|
def test_fresh_install_skips_docker_migration(self):
|
|
args = setup_native.arguments(["--fresh"])
|
|
self.assertTrue(args.fresh)
|
|
self.assertFalse(setup_native.arguments([]).fresh)
|
|
|
|
def test_invalid_web_auth_is_retained(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
auth = Path(directory) / "auth.json"
|
|
auth.write_text('{"password":"short"}')
|
|
installer.environ["PIPELINES_WEB_AUTH_FILE"] = str(auth)
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()):
|
|
with self.assertRaisesRegex(ValueError, "Invalid existing web auth"):
|
|
installer.configure_web(Path(directory) / "prefix", setup_native.arguments([]))
|
|
self.assertEqual(auth.read_text(), '{"password":"short"}')
|
|
|
|
def test_node_tool_manager_shim_keeps_its_invoked_name(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder()
|
|
installer = self.installer(directory, recorder)
|
|
prefix = Path(directory) / "prefix"
|
|
installer.prepare_layout(prefix)
|
|
target = Path(directory) / "tool-manager-shim"
|
|
target.write_text("fixture")
|
|
shim = Path(directory) / "node"
|
|
shim.symlink_to(target)
|
|
with patch.object(setup_native.shutil, "which", return_value=str(shim)):
|
|
web = installer.configure_web(prefix, setup_native.arguments([]))
|
|
self.assertEqual(web["node"], str(shim))
|
|
self.assertIn([str(shim), "--version"], recorder.calls)
|
|
|
|
def test_old_node_is_rejected(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
def old_node(command, **kwargs):
|
|
return subprocess.CompletedProcess(command, 0, stdout="v16.0.0\n")
|
|
installer = self.installer(directory, old_node)
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()):
|
|
with self.assertRaisesRegex(ValueError, "Node.js 18"):
|
|
installer.configure_web(Path(directory) / "prefix", setup_native.arguments([]))
|
|
|
|
def test_build_installs_server_cli_and_detachable_hosts_with_trimpath(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder()
|
|
installer = self.installer(directory, recorder)
|
|
prefix = Path(directory) / "prefix"
|
|
installer.prepare_layout(prefix)
|
|
installer.build(prefix)
|
|
builds = [call for call in recorder.calls if call[:2] == ["go", "build"]]
|
|
self.assertEqual(len(builds), 4)
|
|
for call in builds:
|
|
self.assertIn("-trimpath", call)
|
|
self.assertTrue(any(call[-1] == "./cmd/pipelines-server" for call in builds))
|
|
self.assertTrue(any(call[-1] == "./cmd/pipelines" for call in builds))
|
|
self.assertTrue(any(call[-1] == "./cmd/pipelines-claude-host" for call in builds))
|
|
self.assertTrue(any(call[-1] == "./cmd/pipelines-codex-host" for call in builds))
|
|
self.assertFalse(any("pipelines-tui" in part for call in recorder.calls for part in call))
|
|
|
|
def test_layout_creates_private_prefix_and_copies_manage(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
prefix = Path(directory) / "prefix"
|
|
installer.prepare_layout(prefix)
|
|
for name in ("bin", "lib", "data", "logs", "secrets"):
|
|
self.assertTrue((prefix / name).is_dir(), name)
|
|
self.assertEqual((prefix / "secrets").stat().st_mode & 0o777, 0o700)
|
|
self.assertTrue((prefix / "lib/manage.py").is_file())
|
|
|
|
def test_prefix_honours_pipelines_prefix_environment(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
custom = Path(directory) / "custom"
|
|
installer = setup_native.Setup(directory, runner=Recorder(), environ={"PIPELINES_PREFIX": str(custom)})
|
|
self.assertEqual(installer.resolve_prefix(), custom.resolve())
|
|
|
|
def test_authoring_role_defaults_only_fresh_install_to_enabled(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
prefix = Path(directory) / "prefix"
|
|
self.assertTrue(installer.authoring_role(prefix, None))
|
|
prefix.mkdir()
|
|
for previous, expected in (({}, False), ({"allow_authoring": False}, False),
|
|
({"allow_authoring": True}, True)):
|
|
with self.subTest(previous=previous):
|
|
manifest = prefix / setup_native.MARKER
|
|
manifest.write_text(json.dumps(previous))
|
|
before = manifest.read_bytes()
|
|
self.assertIs(installer.authoring_role(prefix, None), expected)
|
|
self.assertTrue(installer.authoring_role(prefix, True))
|
|
self.assertFalse(installer.authoring_role(prefix, False))
|
|
self.assertEqual(manifest.read_bytes(), before)
|
|
manifest.write_text('{"allow_authoring":"true"}')
|
|
with self.assertRaisesRegex(ValueError, "Invalid existing authoring role"):
|
|
installer.authoring_role(prefix, None)
|
|
|
|
def test_authoring_flags_are_explicit_optional_overrides(self):
|
|
self.assertIsNone(setup_native.arguments([]).allow_authoring)
|
|
self.assertTrue(setup_native.arguments(["--allow-authoring"]).allow_authoring)
|
|
self.assertFalse(setup_native.arguments(["--no-authoring"]).allow_authoring)
|
|
|
|
def test_full_run_keeps_existing_execution_only_and_accepts_fresh_opt_out(self):
|
|
for previous, arguments in (({}, []), ({"allow_authoring": False}, []), (None, ["--no-authoring"])):
|
|
with self.subTest(previous=previous), tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
prefix = Path(directory) / "prefix"
|
|
if previous is not None:
|
|
prefix.mkdir()
|
|
(prefix / setup_native.MARKER).write_text(json.dumps(previous))
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
|
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
|
|
patch.object(setup_native.configure, "host_timezone", return_value="UTC"), \
|
|
redirect_stdout(io.StringIO()):
|
|
installer.run(setup_native.arguments(arguments))
|
|
self.assertFalse(manage.load(prefix)["allow_authoring"])
|
|
|
|
def test_notification_web_url_is_preserved_and_can_be_overridden(self):
|
|
for override in (None, "https://new-pipelines.example.com"):
|
|
with self.subTest(override=override), tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
prefix = Path(directory) / "prefix"
|
|
prefix.mkdir()
|
|
(prefix / setup_native.MARKER).write_text(json.dumps({"opencode_env": {
|
|
"PIPELINES_WEB_PUBLIC_URL": "https://pipelines.example.com"}}))
|
|
if override:
|
|
installer.environ["PIPELINES_WEB_PUBLIC_URL"] = override
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
|
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
|
|
patch.object(setup_native.configure, "host_timezone", return_value="UTC"), \
|
|
redirect_stdout(io.StringIO()):
|
|
installer.run(setup_native.arguments([]))
|
|
self.assertEqual(manage.load(prefix)["opencode_env"]["PIPELINES_WEB_PUBLIC_URL"],
|
|
override or "https://pipelines.example.com")
|
|
|
|
def test_invalid_notification_web_url_refused_before_install(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
installer.environ["PIPELINES_WEB_PUBLIC_URL"] = "https://user:secret@example.com"
|
|
with self.assertRaisesRegex(ValueError, "PIPELINES_WEB_PUBLIC_URL"):
|
|
installer.run(setup_native.arguments([]))
|
|
|
|
def test_missing_opencode_fails_actionably(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all(opencode=None)):
|
|
with self.assertRaisesRegex(ValueError, "OpenCode was not found"):
|
|
installer.harness()
|
|
|
|
def test_full_run_writes_manifest_and_config_without_harness_install_or_ntfy(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder()
|
|
installer = self.installer(directory, recorder)
|
|
prefix = Path(directory) / "prefix"
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
|
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
|
|
patch.object(setup_native.configure, "host_timezone", return_value="Europe/London"):
|
|
installer.run(setup_native.arguments([]))
|
|
config = manage.load(prefix)
|
|
self.assertEqual(config["format"], 1)
|
|
self.assertTrue(config["allow_authoring"])
|
|
self.assertEqual(config["prefix"], str(prefix.resolve()))
|
|
self.assertEqual(config["server_port"], 8080)
|
|
self.assertEqual(config["bind_address"], "127.0.0.1")
|
|
self.assertEqual(config["mode"], "native")
|
|
self.assertEqual(manage.install_mode(config), "native")
|
|
for key in ("ntfy", "ntfy_port", "topic", "username", "public_url"):
|
|
self.assertNotIn(key, config)
|
|
self.assertEqual(config["opencode"], "/usr/bin/opencode")
|
|
self.assertEqual(config["pi"], "")
|
|
self.assertEqual(config["opencode_env"]["PIPELINES_DEFAULT_MODEL"], "fixture/host-default")
|
|
# A fresh install's proxy admits loopback and tailnet only, so the
|
|
# default web URL is not the LAN address.
|
|
self.assertEqual(config["opencode_env"]["PIPELINES_WEB_PUBLIC_URL"], "http://127.0.0.1:8082")
|
|
self.assertEqual((config["web"]["host"], config["web"]["lan"], config["web"]["lan_plain_login"]),
|
|
("tailnet", False, False))
|
|
host = json.loads(installer.harness_config_path().read_text())["profiles"]["opencode"]
|
|
self.assertEqual(host["model"], "fixture/host-default")
|
|
self.assertEqual(host["pipeline_model"], "fixture/host-default")
|
|
self.assertTrue(Path(config["registration"]).is_file())
|
|
self.assertTrue(recorder.has("start", "--prefix", str(prefix.resolve())))
|
|
self.assertEqual((prefix / setup_native.MARKER).stat().st_mode & 0o777, 0o600)
|
|
self.assertFalse((prefix / "secrets/publisher-token").exists())
|
|
self.assertFalse((prefix / "data/ntfy").exists())
|
|
self.assertFalse((prefix / "bin/ntfy").exists())
|
|
self.assertNotIn("ntfy", Path(config["registration"]).read_text())
|
|
self.assertTrue(recorder.has(str(prefix.resolve() / "bin/pipelines"), "skill", "install"))
|
|
self.assertFalse(any("ntfy" in part for call in recorder.calls for part in call))
|
|
discovery = [call for call in recorder.calls if call[0].endswith("opencode")]
|
|
self.assertEqual(discovery, [["/usr/bin/opencode", "api", "get", "/api/model/default"]])
|
|
self.assertFalse(any("pipelines-tui" in part for call in recorder.calls for part in call))
|
|
|
|
def test_reinstall_keeps_settings_and_prints_web_urls_last(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder()
|
|
installer = self.installer(directory, recorder)
|
|
prefix = Path(directory) / "prefix"
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
|
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
|
|
patch.object(setup_native.configure, "host_timezone", return_value="Europe/London"):
|
|
installer.run(setup_native.arguments(["--web-port", "8096"]))
|
|
recorder.calls.clear()
|
|
with redirect_stdout(io.StringIO()) as output:
|
|
installer.run(setup_native.arguments([]))
|
|
self.assertEqual(manage.load(prefix)["web"]["port"], 8096)
|
|
self.assertEqual(output.getvalue().splitlines()[-4:], [
|
|
" http://localhost:8096/", " http://127.0.0.1:8096/",
|
|
"OpenCode is required (default stage and chat harness) and is set up by ./install.",
|
|
"Next: set up your agent harnesses: pipelines harness setup claude|codex|opencode|pi"])
|
|
self.assertNotIn("ntfy", output.getvalue())
|
|
|
|
def legacy_install(self, installer, prefix):
|
|
"""An install from before ntfy was removed, on disk."""
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
|
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
|
|
patch.object(setup_native.configure, "host_timezone", return_value="Europe/London"), \
|
|
redirect_stdout(io.StringIO()):
|
|
installer.run(setup_native.arguments([]))
|
|
manifest = json.loads((prefix / setup_native.MARKER).read_text())
|
|
manifest.pop("mode")
|
|
manifest.update(ntfy_port=8090, topic="pipelines", public_url="http://192.168.1.20:8090",
|
|
username="phone-reader",
|
|
ntfy={"mode": "native", "bin": str(prefix.resolve() / "bin/ntfy"), "version": "v2.38.1",
|
|
"config": str(prefix.resolve() / "data/ntfy/server.yml"),
|
|
"base_url": "http://192.168.1.20:8090"})
|
|
(prefix / setup_native.MARKER).write_text(json.dumps(manifest))
|
|
(prefix / "data/ntfy/attachments").mkdir(parents=True)
|
|
for name in ("bin/ntfy", "logs/ntfy.log", "data/ntfy/server.yml", "data/ntfy/auth.db"):
|
|
(prefix / name).write_text("old")
|
|
(prefix / "secrets/publisher-token").write_text("tk_secretsecret\n")
|
|
(prefix / "ntfy.pid").write_text("4321")
|
|
return manifest
|
|
|
|
def test_reinstall_retires_the_old_ntfy(self):
|
|
for running in (True, False):
|
|
with self.subTest(running=running), tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
prefix = Path(directory) / "prefix"
|
|
self.legacy_install(installer, prefix)
|
|
installer.install_log = prefix / "logs/install.log"
|
|
installer.install_log.write_text("earlier\n")
|
|
alive = [running]
|
|
killed = []
|
|
|
|
def command_line(pid):
|
|
return f"{prefix.resolve()}/bin/ntfy serve --config x" if pid == 4321 and alive[0] else None
|
|
|
|
def kill(pid, sig):
|
|
killed.append((pid, sig))
|
|
alive[0] = False
|
|
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
|
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
|
|
patch.object(setup_native.configure, "host_timezone", return_value="Europe/London"), \
|
|
patch.object(setup_native.manage, "process_command", side_effect=command_line), \
|
|
patch.object(setup_native.manage.os, "kill", side_effect=kill), \
|
|
redirect_stdout(io.StringIO()) as output:
|
|
installer.run(setup_native.arguments([]))
|
|
self.assertEqual(killed, [(4321, setup_native.manage.signal.SIGTERM)] if running else [])
|
|
for path in setup_native.manage.legacy_ntfy_paths(prefix):
|
|
self.assertFalse(path.exists(), path)
|
|
self.assertTrue((prefix / "data").is_dir())
|
|
config = manage.load(prefix)
|
|
self.assertEqual(config["mode"], "native")
|
|
for key in ("ntfy", "ntfy_port", "topic", "username", "public_url"):
|
|
self.assertNotIn(key, config)
|
|
log = installer.install_log.read_text()
|
|
self.assertIn("Retiring ntfy (version v2.38.1)", log)
|
|
self.assertIn(str(prefix.resolve() / "bin/ntfy"), log)
|
|
self.assertNotIn("tk_secretsecret", log)
|
|
self.assertEqual("Stopped the old ntfy server process." in log, running)
|
|
self.assertIn("Retired the old ntfy notification server", output.getvalue())
|
|
# A second reinstall has nothing left to retire.
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
|
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
|
|
patch.object(setup_native.configure, "host_timezone", return_value="Europe/London"), \
|
|
redirect_stdout(io.StringIO()) as output:
|
|
installer.run(setup_native.arguments([]))
|
|
self.assertEqual(installer.install_log.read_text().count("Retiring ntfy"), 1)
|
|
self.assertNotIn("ntfy", output.getvalue())
|
|
|
|
def test_reinstall_never_signals_a_reused_ntfy_pid(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
prefix = Path(directory) / "prefix"
|
|
self.legacy_install(installer, prefix)
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
|
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
|
|
patch.object(setup_native.configure, "host_timezone", return_value="Europe/London"), \
|
|
patch.object(setup_native.manage, "process_command", return_value="/usr/bin/sleep 1000"), \
|
|
patch.object(setup_native.manage.os, "kill", side_effect=AssertionError("signalled a reused PID")), \
|
|
redirect_stdout(io.StringIO()):
|
|
installer.run(setup_native.arguments([]))
|
|
self.assertFalse((prefix / "ntfy.pid").exists())
|
|
self.assertFalse((prefix / "bin/ntfy").exists())
|
|
|
|
def test_upgrade_keeps_an_all_interfaces_web_host_with_a_warning_and_lan_is_opt_in(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
prefix = Path(directory) / "prefix"
|
|
|
|
def install(argv):
|
|
with redirect_stdout(io.StringIO()) as output:
|
|
installer.run(setup_native.arguments(argv))
|
|
return manage.load(prefix)["web"], output.getvalue()
|
|
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
|
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
|
|
patch.object(setup_native.configure, "host_timezone", return_value="Europe/London"):
|
|
# An install from before: the proxy on every interface.
|
|
install(["--web-host", "0.0.0.0"])
|
|
web, text = install([])
|
|
self.assertEqual(web["host"], "0.0.0.0")
|
|
self.assertIn("WARNING: the web proxy listens on 0.0.0.0 over plain HTTP", text)
|
|
self.assertIn("./install --web-host tailnet", text)
|
|
self.assertIn(" http://192.168.1.20:8082/", text)
|
|
# Opting into the LAN listener selects the new model; sign-in
|
|
# over the plain-HTTP LAN stays off until asked for.
|
|
web, text = install(["--web-lan"])
|
|
self.assertEqual((web["host"], web["lan"], web["lan_plain_login"]), ("tailnet", True, False))
|
|
self.assertNotIn("WARNING: the web proxy listens", text)
|
|
self.assertIn("Web proxy access: loopback + tailnet + LAN", text)
|
|
# The URL recorded by the old install is kept, and flagged.
|
|
self.assertIn("WARNING: the recorded web URL http://192.168.1.20:8082 is a plain-HTTP LAN", text)
|
|
web, text = install(["--web-lan-plain-login"])
|
|
self.assertEqual((web["host"], web["lan"], web["lan_plain_login"]), ("tailnet", True, True))
|
|
self.assertIn("WARNING: web.lan_plain_login is on", text)
|
|
web, _ = install(["--no-web-lan"])
|
|
self.assertEqual((web["host"], web["lan"], web["lan_plain_login"]), ("tailnet", False, False))
|
|
web, _ = install([])
|
|
self.assertEqual((web["host"], web["lan"]), ("tailnet", False))
|
|
with self.assertRaisesRegex(ValueError, "--web-lan applies to --web-host tailnet"):
|
|
installer.run(setup_native.arguments(["--web-host", "0.0.0.0", "--web-lan"]))
|
|
with self.assertRaisesRegex(ValueError, "Invalid --web-host"):
|
|
installer.run(setup_native.arguments(["--web-host", "everywhere"]))
|
|
|
|
def test_migration_copies_docker_data_only_when_absent(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
prefix = Path(directory) / "prefix"
|
|
(prefix / "data").mkdir(parents=True)
|
|
recorder = Recorder(volumes=("pipelines_pipelines-data", "pipelines_ntfy-data"))
|
|
installer = self.installer(directory, recorder)
|
|
self.assertTrue(installer.migrate_docker(prefix, replace=False))
|
|
self.assertEqual((prefix / "data/pipelines.db").read_bytes(), b"migrated")
|
|
self.assertTrue(any(call[:2] == ["docker", "cp"] and call[2].endswith("-wal")
|
|
for call in installer.runner.calls),
|
|
"the SQLite write-ahead log sidecar must be copied with the database")
|
|
# ntfy's old Docker volume is not migrated: nothing uses it.
|
|
self.assertFalse((prefix / "data/ntfy").exists())
|
|
self.assertFalse(any("pipelines_ntfy-data" in part for call in recorder.calls
|
|
if call[1:2] == ["create"] for part in call))
|
|
|
|
def test_migration_keeps_existing_native_data_without_opt_in(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
prefix = Path(directory) / "prefix"
|
|
(prefix / "data").mkdir(parents=True)
|
|
(prefix / "data/pipelines.db").write_bytes(b"native")
|
|
recorder = Recorder(volumes=("pipelines_pipelines-data",))
|
|
installer = self.installer(directory, recorder)
|
|
self.assertTrue(installer.migrate_docker(prefix, replace=False))
|
|
self.assertEqual((prefix / "data/pipelines.db").read_bytes(), b"native")
|
|
self.assertFalse(any(call[1:2] == ["cp"] for call in recorder.calls))
|
|
|
|
def test_absent_docker_volumes_is_a_noop(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder(volumes=())
|
|
installer = self.installer(directory, recorder)
|
|
self.assertFalse(installer.migrate_docker(Path(directory) / "prefix", replace=False))
|
|
self.assertFalse(any(call[0] == "docker" and call[1] == "create" for call in recorder.calls))
|
|
|
|
def test_uninspectable_docker_volumes_abort_instead_of_skipping_migration(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder(fail_volume_ls=True)
|
|
installer = self.installer(directory, recorder)
|
|
with self.assertRaisesRegex(ValueError, "Could not inspect Docker volumes"):
|
|
installer.migrate_docker(Path(directory) / "prefix", replace=False)
|
|
|
|
def test_missing_docker_cli_is_a_noop_not_a_failure(self):
|
|
def no_docker(command, **kwargs):
|
|
raise FileNotFoundError(command[0])
|
|
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = setup_native.Setup(directory, prefix=Path(directory) / "prefix",
|
|
runner=no_docker, environ={})
|
|
self.assertEqual(installer.docker_volumes(), ())
|
|
|
|
def test_requested_migration_failure_aborts(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
prefix = Path(directory) / "prefix"
|
|
(prefix / "data").mkdir(parents=True)
|
|
recorder = Recorder(volumes=("pipelines_pipelines-data",), fail_copy=True)
|
|
installer = self.installer(directory, recorder)
|
|
with self.assertRaisesRegex(ValueError, "install aborted"):
|
|
installer.migrate_docker(prefix, replace=False)
|
|
|
|
def test_missing_local_image_aborts_migration(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
prefix = Path(directory) / "prefix"
|
|
(prefix / "data").mkdir(parents=True)
|
|
recorder = Recorder(volumes=("pipelines_pipelines-data",), image=None)
|
|
installer = self.installer(directory, recorder)
|
|
with self.assertRaisesRegex(ValueError, "no local Pipelines image"):
|
|
installer.migrate_docker(prefix, replace=False)
|
|
|
|
def test_configure_harnesses_writes_host_profiles_for_detected_harness(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder()
|
|
installer = self.installer(directory, recorder)
|
|
prefix = Path(directory) / "prefix"
|
|
installer.configure_harnesses({"opencode": "/usr/bin/opencode", "pi": ""}, "fixture/harness")
|
|
path = installer.harness_config_path()
|
|
self.assertEqual(path, Path(directory) / "config/pipelines/harnesses.json")
|
|
config = json.loads(path.read_text())
|
|
self.assertEqual(config["default"], "opencode")
|
|
profile = config["profiles"]["opencode"]
|
|
self.assertEqual(profile["executable"], "/usr/bin/opencode")
|
|
self.assertNotIn("docker_directory", profile)
|
|
self.assertTrue(installer._valid_model(profile["model"]))
|
|
self.assertTrue(installer._valid_model(profile["pipeline_model"]))
|
|
self.assertEqual(profile["model"], "fixture/harness")
|
|
# Detection-only: no harness install/upgrade/download was invoked.
|
|
self.assertFalse(any("opencode" in part for call in recorder.calls for part in call))
|
|
|
|
def test_configure_harnesses_fails_without_a_valid_model_in_nonterminal(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder(host_model=None))
|
|
path = installer.harness_config_path()
|
|
with patch.object(setup_native.sys.stdin, "isatty", return_value=False):
|
|
with self.assertRaisesRegex(ValueError, "setup is not ready.*--model PROVIDER/MODEL"):
|
|
installer.configure_harnesses({"opencode": "/usr/bin/opencode", "pi": ""})
|
|
self.assertFalse(path.exists(), "an invalid profile was written")
|
|
|
|
def test_opencode_default_reads_model_info_without_inference_or_jsonc_parsing(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
calls = []
|
|
|
|
def api(command, **kwargs):
|
|
calls.append((command, kwargs))
|
|
return subprocess.CompletedProcess(command, 0, stdout=json.dumps({"data": {
|
|
"id": "internal-id-not-a-model", "providerID": "configured-provider",
|
|
"modelID": "configured-model", "enabled": True, "status": "active"}}))
|
|
|
|
installer = self.installer(directory, api)
|
|
path = installer.configure_harnesses({"opencode": "/existing/opencode", "pi": ""})
|
|
profile = json.loads(path.read_text())["profiles"]["opencode"]
|
|
self.assertEqual(profile["model"], "configured-provider/configured-model")
|
|
self.assertEqual(profile["pipeline_model"], profile["model"])
|
|
self.assertEqual(len(calls), 1)
|
|
self.assertEqual(calls[0][0], ["/existing/opencode", "api", "get", "/api/model/default"])
|
|
self.assertEqual(calls[0][1]["cwd"], str(Path.home()))
|
|
self.assertEqual(calls[0][1]["env"], installer.environ)
|
|
self.assertEqual(calls[0][1]["timeout"], 30)
|
|
|
|
def test_opencode_default_rejects_missing_disabled_deprecated_or_placeholder_models(self):
|
|
valid = {"providerID": "fixture", "modelID": "host-default", "enabled": True, "status": "active"}
|
|
responses = [None, {}, {"data": None}, {"data": {**valid, "enabled": False}},
|
|
{"data": {**valid, "status": "deprecated"}},
|
|
{"data": {**valid, "providerID": "default", "modelID": "default"}},
|
|
{"data": {"id": "fixture/looks-valid", "enabled": True}}]
|
|
for response in responses:
|
|
with self.subTest(response=response), tempfile.TemporaryDirectory() as directory:
|
|
def api(command, **kwargs):
|
|
return subprocess.CompletedProcess(command, 0, stdout=json.dumps(response))
|
|
installer = self.installer(directory, api)
|
|
self.assertEqual(installer.opencode_default_model("/existing/opencode"), "")
|
|
|
|
def test_configure_harnesses_discovery_fills_only_missing_model_fields(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
path = installer.harness_config_path()
|
|
path.parent.mkdir(parents=True)
|
|
path.write_text(json.dumps({"default": "opencode", "future": "preserve", "profiles": {
|
|
"opencode": {"model": "fixture/keep-interactive", "pipeline_model": "", "future": "keep"}}}))
|
|
installer.configure_harnesses({"opencode": "/existing/opencode", "pi": ""})
|
|
saved = json.loads(path.read_text())
|
|
self.assertEqual(saved["future"], "preserve")
|
|
self.assertEqual(saved["profiles"]["opencode"]["model"], "fixture/keep-interactive")
|
|
self.assertEqual(saved["profiles"]["opencode"]["pipeline_model"], "fixture/host-default")
|
|
self.assertEqual(saved["profiles"]["opencode"]["future"], "keep")
|
|
|
|
def test_missing_default_runs_existing_picker_inside_install_only_in_terminal(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder(host_model=None)
|
|
installer = self.installer(directory, recorder)
|
|
path = installer.harness_config_path()
|
|
|
|
def runner(command, **kwargs):
|
|
result = recorder(command, **kwargs)
|
|
if command[1:4] == ["harness", "setup", "opencode"]:
|
|
path.parent.mkdir(parents=True)
|
|
path.write_text(json.dumps({"default": "opencode", "profiles": {"opencode": {
|
|
"executable": "/existing/opencode", "model": "fixture/chosen-interactive",
|
|
"pipeline_model": "fixture/chosen-stages", "future": "preserve"}}}))
|
|
return result
|
|
|
|
installer.runner = runner
|
|
with patch.object(setup_native.sys.stdin, "isatty", return_value=True):
|
|
installer.configure_harnesses({"opencode": "/existing/opencode", "pi": ""})
|
|
picker = [call for call in recorder.calls if call[1:4] == ["harness", "setup", "opencode"]]
|
|
self.assertEqual(picker, [[str(installer.resolve_prefix() / "bin/pipelines"), "harness", "setup", "opencode",
|
|
"--host", "--executable", "/existing/opencode"]])
|
|
profile = json.loads(path.read_text())["profiles"]["opencode"]
|
|
self.assertEqual(profile["pipeline_model"], "fixture/chosen-stages")
|
|
self.assertEqual(profile["future"], "preserve")
|
|
|
|
def test_unresolved_model_aborts_full_run_before_start_or_ready_claim(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder(host_model=None)
|
|
installer = self.installer(directory, recorder)
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
|
patch.object(setup_native.sys.stdin, "isatty", return_value=False), \
|
|
redirect_stdout(io.StringIO()) as output:
|
|
with self.assertRaisesRegex(ValueError, "setup is not ready"):
|
|
installer.run(setup_native.arguments([]))
|
|
self.assertFalse((installer.resolve_prefix() / setup_native.MARKER).exists())
|
|
self.assertFalse(any("start" in call for call in recorder.calls))
|
|
self.assertNotIn("Native Pipelines installed", output.getvalue())
|
|
|
|
def test_legacy_standalone_web_agent_is_retired(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder()
|
|
installer = self.installer(directory, recorder)
|
|
agents = Path(self.home.name) / "Library/LaunchAgents"
|
|
agents.mkdir(parents=True)
|
|
legacy = agents / "net.awan.pipelines.web.plist"
|
|
legacy.write_text("legacy")
|
|
with patch.object(setup_native.platform, "system", return_value="Darwin"), \
|
|
redirect_stdout(io.StringIO()):
|
|
installer.retire_legacy_web()
|
|
installer.retire_legacy_web()
|
|
self.assertFalse(legacy.exists())
|
|
self.assertEqual((agents / "net.awan.pipelines.web.plist.disabled").read_text(), "legacy")
|
|
self.assertEqual(sum(call[:2] == ["launchctl", "bootout"] for call in recorder.calls), 1)
|
|
|
|
def test_tailnet_public_url_serves_the_installed_web_app(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder()
|
|
installer = self.installer(directory, recorder)
|
|
with patch.object(setup_native.shutil, "which", return_value="/usr/bin/tailscale"), \
|
|
redirect_stdout(io.StringIO()):
|
|
installer.serve_on_tailnet("https://box.tail1.ts.net:8443", 8083)
|
|
installer.serve_on_tailnet("http://10.0.0.5:8083", 8083)
|
|
self.assertEqual([call for call in recorder.calls if "serve" in call],
|
|
[["/usr/bin/tailscale", "serve", "--bg", "--https=8443", "http://127.0.0.1:8083"]])
|
|
|
|
def refusing(command, **kwargs):
|
|
return subprocess.CompletedProcess(command, 1, stdout="", stderr="serve: access denied")
|
|
|
|
installer = self.installer(directory, refusing)
|
|
with patch.object(setup_native.shutil, "which", return_value="/usr/bin/tailscale"):
|
|
with self.assertRaisesRegex(ValueError, "access denied"):
|
|
installer.serve_on_tailnet("https://box.tail1.ts.net", 8083)
|
|
|
|
def test_install_is_all_or_nothing_with_the_web_app(self):
|
|
cases = {"no docker": dict(docker=False), "web build fails": dict(fail_web=True),
|
|
"server build fails": dict(fail_build=True)}
|
|
for name, case in cases.items():
|
|
with self.subTest(case=name), tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder(fail_web=case.get("fail_web", False),
|
|
fail_build=case.get("fail_build", False))
|
|
installer = self.installer(directory, recorder)
|
|
prefix = installer.resolve_prefix()
|
|
(prefix / "bin").mkdir(parents=True)
|
|
(prefix / "bin/pipelines-server").write_text("old")
|
|
(prefix / "lib/app-web").mkdir(parents=True)
|
|
(prefix / "lib/app-web/index.html").write_text("old")
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
|
patch.object(setup_native.app_build, "docker_available",
|
|
return_value=case.get("docker", True)), \
|
|
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
|
|
redirect_stdout(io.StringIO()):
|
|
with self.assertRaises(ValueError):
|
|
installer.run(setup_native.arguments([]))
|
|
# Neither half was replaced and nothing claims an install.
|
|
self.assertEqual((prefix / "bin/pipelines-server").read_text(), "old")
|
|
self.assertEqual((prefix / "lib/app-web/index.html").read_text(), "old")
|
|
self.assertFalse((prefix / setup_native.MARKER).exists())
|
|
self.assertFalse((prefix / "bin/.next").exists())
|
|
|
|
def test_picker_cancellation_or_unsaved_models_never_claim_configuration_complete(self):
|
|
for returncode in (0, 1):
|
|
with self.subTest(returncode=returncode), tempfile.TemporaryDirectory() as directory:
|
|
calls = []
|
|
|
|
def runner(command, **kwargs):
|
|
calls.append(command)
|
|
if command[1:4] == ["harness", "setup", "opencode"]:
|
|
return subprocess.CompletedProcess(command, returncode)
|
|
return subprocess.CompletedProcess(command, 0, stdout='{"data":null}')
|
|
|
|
installer = self.installer(directory, runner)
|
|
with patch.object(setup_native.sys.stdin, "isatty", return_value=True), redirect_stdout(io.StringIO()):
|
|
with self.assertRaisesRegex(ValueError, "setup is not ready"):
|
|
installer.configure_harnesses({"opencode": "/existing/opencode", "pi": ""})
|
|
self.assertFalse(installer.harness_config_path().exists())
|
|
self.assertEqual(sum(call[1:4] == ["harness", "setup", "opencode"] for call in calls), 1)
|
|
|
|
def test_full_run_reuses_saved_stage_default_without_opencode_queries(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder(host_model=None)
|
|
installer = self.installer(directory, recorder)
|
|
path = installer.harness_config_path()
|
|
path.parent.mkdir(parents=True)
|
|
path.write_text(json.dumps({"default": "opencode", "profiles": {"opencode": {
|
|
"executable": "/old/opencode", "model": "fixture/interactive", "pipeline_model": "fixture/stages"}}}))
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
|
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
|
|
patch.object(setup_native.configure, "host_timezone", return_value="UTC"), \
|
|
redirect_stdout(io.StringIO()):
|
|
installer.run(setup_native.arguments([]))
|
|
manifest = manage.load(installer.resolve_prefix())
|
|
self.assertEqual(manifest["opencode_env"]["PIPELINES_DEFAULT_MODEL"], "fixture/stages")
|
|
self.assertFalse(any(call[0].endswith("opencode") for call in recorder.calls))
|
|
|
|
def test_configure_harnesses_migrates_stale_docker_link_and_default(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
path = installer.harness_config_path()
|
|
path.parent.mkdir(parents=True)
|
|
path.write_text(json.dumps({
|
|
"default": "pi",
|
|
"future": "preserve",
|
|
"profiles": {
|
|
"opencode": {"docker_directory": "/checkout", "executable": "opencode",
|
|
"model": "fixture/interactive", "pipeline_model": "fixture/stages"},
|
|
"pi": {"docker_directory": "/checkout", "executable": "pi",
|
|
"model": "provider/interactive", "pipeline_model": "provider/stages"},
|
|
},
|
|
}))
|
|
installer.configure_harnesses({"opencode": "/usr/bin/opencode", "pi": "/usr/bin/pi"})
|
|
saved = json.loads(path.read_text())
|
|
self.assertEqual(saved["default"], "opencode")
|
|
self.assertEqual(saved["future"], "preserve")
|
|
for name in ("opencode", "pi"):
|
|
self.assertNotIn("docker_directory", saved["profiles"][name], name)
|
|
self.assertTrue(installer._valid_model(saved["profiles"][name]["model"]))
|
|
self.assertEqual(saved["profiles"]["opencode"]["executable"], "/usr/bin/opencode")
|
|
|
|
def test_configure_harnesses_preserves_configured_host_default(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = self.installer(directory, Recorder())
|
|
path = installer.harness_config_path()
|
|
path.parent.mkdir(parents=True)
|
|
profiles = {name: {"model": "fixture/interactive", "pipeline_model": "fixture/stages"}
|
|
for name in ("opencode", "pi")}
|
|
profiles["pi"]["future"] = "preserve"
|
|
path.write_text(json.dumps({"default": "pi", "profiles": profiles}))
|
|
installer.configure_harnesses({"opencode": "/usr/bin/opencode", "pi": "/usr/bin/pi"})
|
|
saved = json.loads(path.read_text())
|
|
self.assertEqual(saved["default"], "pi")
|
|
self.assertEqual(saved["profiles"]["pi"]["model"], "fixture/interactive")
|
|
self.assertEqual(saved["profiles"]["pi"]["future"], "preserve")
|
|
|
|
def _fake_pipelines(self, path, version, commit):
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(f'#!/bin/sh\necho \'{{"version":"{version}","commit":"{commit}"}}\'\n')
|
|
path.chmod(0o755)
|
|
return path
|
|
|
|
def test_check_path_shadow_warns_naming_path_and_both_versions(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
directory = Path(directory)
|
|
prefix = directory / "prefix"
|
|
installed = self._fake_pipelines(prefix / "bin/pipelines", "new-version", "bbbb")
|
|
shadow_dir = directory / "earlier"
|
|
shadow = self._fake_pipelines(shadow_dir / "pipelines", "old-version", "aaaa")
|
|
before = shadow.read_bytes()
|
|
installer = setup_native.Setup(directory, prefix=prefix, runner=subprocess.run, environ={})
|
|
with patch.object(setup_native.shutil, "which", side_effect=lambda name: str(shadow) if name == "pipelines" else None):
|
|
output = io.StringIO()
|
|
with redirect_stdout(output):
|
|
result = installer.check_path_shadow(prefix)
|
|
warning = output.getvalue()
|
|
self.assertEqual(result, shadow.resolve())
|
|
self.assertIn("WARNING", warning)
|
|
self.assertIn(str(shadow), warning)
|
|
self.assertIn("old-version", warning)
|
|
self.assertIn("new-version", warning)
|
|
# The shadowing binary is only read; it is never modified or replaced.
|
|
self.assertEqual(shadow.read_bytes(), before)
|
|
self.assertEqual(installed.read_bytes().count(b"new-version"), 1)
|
|
|
|
def test_check_path_shadow_is_silent_when_prefix_binary_wins(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
directory = Path(directory)
|
|
prefix = directory / "prefix"
|
|
installed = self._fake_pipelines(prefix / "bin/pipelines", "same-version", "cccc")
|
|
installer = setup_native.Setup(directory, prefix=prefix, runner=subprocess.run, environ={})
|
|
with patch.object(setup_native.shutil, "which", return_value=str(installed)):
|
|
output = io.StringIO()
|
|
with redirect_stdout(output):
|
|
result = installer.check_path_shadow(prefix)
|
|
self.assertIsNone(result)
|
|
self.assertEqual(output.getvalue(), "")
|
|
|
|
def test_check_path_shadow_is_silent_without_a_path_binary(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
installer = setup_native.Setup(directory, prefix=Path(directory) / "prefix",
|
|
runner=subprocess.run, environ={})
|
|
with patch.object(setup_native.shutil, "which", return_value=None):
|
|
output = io.StringIO()
|
|
with redirect_stdout(output):
|
|
result = installer.check_path_shadow(Path(directory) / "prefix")
|
|
self.assertIsNone(result)
|
|
self.assertEqual(output.getvalue(), "")
|
|
|
|
def test_check_path_shadow_reads_each_binary_at_most_once(self):
|
|
class VersionRunner:
|
|
def __init__(self):
|
|
self.commands = []
|
|
|
|
def __call__(self, command, **kwargs):
|
|
command = list(command)
|
|
self.commands.append(command)
|
|
version = "old-version" if "earlier" in command[0] else "new-version"
|
|
return subprocess.CompletedProcess(command, 0,
|
|
stdout=f'{{"version":"{version}","commit":"ab"}}', stderr="")
|
|
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
directory = Path(directory)
|
|
prefix = directory / "prefix"
|
|
installed = self._fake_pipelines(prefix / "bin/pipelines", "new-version", "bbbb")
|
|
shadow = self._fake_pipelines(directory / "earlier/pipelines", "old-version", "aaaa")
|
|
runner = VersionRunner()
|
|
installer = setup_native.Setup(directory, prefix=prefix, runner=runner, environ={})
|
|
with patch.object(setup_native.shutil, "which", return_value=str(shadow)):
|
|
output = io.StringIO()
|
|
with redirect_stdout(output):
|
|
installer.check_path_shadow(prefix)
|
|
# A detected shadow costs exactly one bounded, read-only `version`
|
|
# read per binary (the shadow and the freshly installed CLI) so both
|
|
# versions can be named; no other subprocess runs.
|
|
self.assertEqual(len(runner.commands), 2)
|
|
self.assertTrue(all(command[1:] == ["version", "--json"] for command in runner.commands))
|
|
self.assertIn("old-version", output.getvalue())
|
|
self.assertIn("new-version", output.getvalue())
|
|
self.assertEqual(installed.read_bytes().count(b"new-version"), 1)
|
|
|
|
def test_full_run_migrates_docker_harness_config_to_host(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
recorder = Recorder()
|
|
installer = self.installer(directory, recorder)
|
|
prefix = Path(directory) / "prefix"
|
|
path = installer.harness_config_path()
|
|
path.parent.mkdir(parents=True)
|
|
path.write_text(json.dumps({
|
|
"default": "pi",
|
|
"profiles": {
|
|
"opencode": {"docker_directory": "/checkout", "executable": "opencode",
|
|
"model": "fixture/interactive", "pipeline_model": "fixture/stages"},
|
|
"pi": {"docker_directory": "/checkout", "executable": "pi",
|
|
"model": "provider/interactive", "pipeline_model": "provider/stages"},
|
|
},
|
|
}))
|
|
with patch.object(setup_native.shutil, "which", side_effect=self.which_all(opencode="/usr/bin/opencode", pi="/usr/bin/pi")), \
|
|
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
|
|
patch.object(setup_native.configure, "host_timezone", return_value="Europe/London"):
|
|
installer.run(setup_native.arguments([]))
|
|
saved = json.loads(path.read_text())
|
|
self.assertEqual(saved["default"], "opencode", "stale Docker-era pi default survived")
|
|
self.assertNotIn("docker_directory", saved["profiles"]["opencode"])
|
|
self.assertTrue(saved["profiles"]["opencode"]["executable"].endswith("opencode"))
|
|
self.assertTrue(installer._valid_model(saved["profiles"]["opencode"]["model"]))
|
|
# Detection-only: no harness install/upgrade/download command recorded.
|
|
self.assertFalse(any("opencode" in part for call in recorder.calls for part in call))
|
|
self.assertFalse(any(part == "pi" for call in recorder.calls for part in call))
|
|
self.assertFalse(any("npm" == part for call in recorder.calls for part in call))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|