Files
pipelines/scripts/test_setup_native.py
T
alan 914345cb31 Move harness profiles between host and Docker and stop silent draft retargeting
Native installs left Docker-era harness profiles in harnesses.json, so every
authoring launch failed trying to reach a Docker service, and a retained draft
bound to the old Docker server was silently projected onto the configured one.

- harness: add Profile.Mode and VerifyDockerLink as the single mode/verification
  helpers. `harness setup --host` configures or converts a host profile and
  clears docker_directory; reconfiguring a Docker-linked profile re-verifies the
  recorded directory still owns compose.yaml and fails naming it instead of
  saving a link that cannot launch. Setup's summary and `harness list` (via the
  new List/ListView shape) report the resulting mode and, for Docker, the
  directory, without changing the stored Config schema.
- setup-native.py: after build/install-skill it writes host (no docker_directory)
  profiles for the detected harnesses, defaulting to the detected OpenCode profile
  rather than a stale pi default, taking models from --model or the existing valid
  profile and never inventing one; it clears a stale Docker link and preserves
  unrelated keys/profiles. Detection-only: no harness install, upgrade or download.
- draft: add Draft.Resolve, the shared mismatch helper, which projects a draft
  onto the configured server and returns a dual-URL notice. CLI validate/submit
  and the TUI draft browser use it so a foreign draft is never silently
  retargeted; the TUI pre-wraps the notice to the panel width.
- Tests cover the Docker->host and host->host transitions, the missing-directory
  failure, installer write/migration (Python), and the CLI/TUI notice, using the
  real config file path in a temp dir.
- Update help text, README, docs and the packaged authoring skill reference to
  describe host as the default and the Docker-linked case.
2026-10-15 19:53:10 +01:00

446 lines
24 KiB
Python

import importlib.util
import json
import os
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)
TOKEN = "tk_" + "a" * 29
NTFY_VERSION = "2.11.0"
NTFY_MODULE = "heckel.io/ntfy/v2"
def resolution():
"""The shape resolve_ntfy returns: an exact tag to build the server from."""
return {"repository": "binwiederhier/ntfy", "module": NTFY_MODULE,
"version": NTFY_VERSION, "tag": "v" + NTFY_VERSION}
class Recorder:
def __init__(self, token=TOKEN, volumes=(), image="pipelines-pipelines:latest",
fail_create=False, fail_copy=False, fail_volume_ls=False, fail_build=False):
self.calls = []
self.token = token
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._module_cache = None
self.kwargs = []
def module_cache(self):
"""A stand-in Go module cache holding the pinned ntfy module source."""
if self._module_cache is None:
self._module_cache = tempfile.mkdtemp(prefix="gomodcache-")
source = Path(self._module_cache) / f"{NTFY_MODULE}@{resolution()['tag']}"
source.mkdir(parents=True)
(source / "go.mod").write_text(f"module {NTFY_MODULE}\n")
return self._module_cache
def __call__(self, command, **kwargs):
command = list(command)
self.calls.append(command)
self.kwargs.append(kwargs)
stdout, code = "", 0
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] == "cp":
if self.fail_copy:
code = 1
else:
Path(command[3]).write_bytes(b"migrated")
elif command[0] == "go" and command[1:3] == ["env", "GOMODCACHE"]:
stdout = self.module_cache() + "\n"
elif command[0] == "go" and command[1:2] == ["build"]:
if self.fail_build:
code = 1
elif "-o" in command:
# Only the ntfy build is inspected by the installer; its output
# is created so the atomic install has a real file to move.
output = Path(command[command.index("-o") + 1])
if output.name == "ntfy":
output.write_bytes(b"#!/bin/sh\n")
output.chmod(0o755)
if command[0].endswith("ntfy") and "token" in command:
stdout = f"token {self.token} created for pipeline-publisher\n"
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 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,
resolver=resolution,
environ={"XDG_CONFIG_HOME": str(Path(directory) / "config")})
@staticmethod
def which_all(opencode="/usr/bin/opencode", pi=None, cc="/usr/bin/cc"):
mapping = {"go": "/usr/bin/go", "python3": "/usr/bin/python3",
"git": "/usr/bin/git", "curl": "/usr/bin/curl"}
if opencode:
mapping["opencode"] = opencode
if pi:
mapping["pi"] = pi
if cc:
mapping["cc"] = cc
return lambda name: mapping.get(name)
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("never installs tools", message)
self.assertEqual(recorder.calls, [])
def test_build_installs_only_server_and_cli_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), 2)
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.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_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_tokens_and_config_without_harness_install(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.assertEqual(config["prefix"], str(prefix.resolve()))
self.assertEqual(config["server_port"], 8080)
self.assertEqual(config["bind_address"], "0.0.0.0")
self.assertEqual(config["ntfy"]["mode"], "native")
self.assertEqual(config["topic"], "pipelines")
self.assertEqual(config["opencode"], "/usr/bin/opencode")
self.assertEqual(config["pi"], "")
self.assertEqual((prefix / setup_native.MARKER).stat().st_mode & 0o777, 0o600)
token_path = prefix / "secrets/publisher-token"
self.assertEqual(token_path.read_text(), TOKEN + "\n")
self.assertEqual(token_path.stat().st_mode & 0o777, 0o600)
text = (prefix / "data/ntfy/server.yml").read_text()
for needle in ('listen-http:', 'base-url: "http://192.168.1.20:8090"', 'cache-file:', 'auth-file:',
'auth-default-access: "deny-all"', 'enable-login: true', 'attachment-cache-dir:',
'attachment-expiry-duration:', 'attachment-total-size-limit:',
'attachment-file-size-limit:'):
self.assertIn(needle, text)
self.assertNotIn("topic", text)
self.assertTrue(recorder.has(str(prefix.resolve() / "bin/pipelines"), "skill", "install"))
self.assertTrue(recorder.has("access", "pipeline-publisher", "pipelines", "write-only"))
self.assertTrue(recorder.has("access", "phone-reader", "pipelines", "read-only"))
self.assertFalse(any("opencode" in part for call in recorder.calls for part in call))
self.assertFalse(any("pipelines-tui" in part for call in recorder.calls for part in call))
self.assertFalse(any(TOKEN in part for call in recorder.calls for part in call))
def test_reinstall_keeps_existing_publisher_token(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([]))
before = (prefix / "secrets/publisher-token").read_text()
def refuse_resolution():
raise AssertionError("re-resolved ntfy despite a verified installed binary")
installer.resolver = refuse_resolution
recorder.calls.clear()
installer.run(setup_native.arguments([]))
self.assertEqual((prefix / "secrets/publisher-token").read_text(), before)
self.assertFalse(any("token" in call for call in recorder.calls), "reinstall re-created the token")
def test_ntfy_is_built_from_the_pinned_module(self):
with tempfile.TemporaryDirectory() as directory:
recorder = Recorder()
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()):
path = installer.build_ntfy(prefix, resolution())
self.assertTrue(path.is_file())
self.assertTrue(path.stat().st_mode & 0o100)
self.assertTrue(recorder.has("go", "mod", "download", f"{NTFY_MODULE}@v{NTFY_VERSION}"))
build = next(call for call in recorder.calls if call[:2] == ["go", "build"] and call[-1] == ".")
self.assertIn("-trimpath", build)
self.assertIn(setup_native.NTFY_BUILD_TAGS, build)
self.assertTrue(any(f"-X main.version=v{NTFY_VERSION}" in part for part in build))
self.assertTrue(build[build.index("-o") + 1].endswith("/ntfy"))
kwargs = next(kw for call, kw in zip(recorder.calls, recorder.kwargs)
if call[:2] == ["go", "build"] and call[-1] == ".")
self.assertEqual(kwargs.get("env", {}).get("CGO_ENABLED"), "1")
def test_ntfy_build_is_reused_for_the_same_version(self):
with tempfile.TemporaryDirectory() as directory:
recorder = Recorder()
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()):
installer.build_ntfy(prefix, resolution())
installer.write_manifest(prefix, {"ntfy": {"version": "v" + NTFY_VERSION}})
recorder.calls.clear()
installer.build_ntfy(prefix, resolution())
self.assertFalse(any(call[:2] == ["go", "build"] for call in recorder.calls),
"rebuilt ntfy for an already installed version")
def test_missing_c_compiler_fails_actionably(self):
installer = setup_native.Setup("/tmp", runner=Recorder(), resolver=resolution, environ={})
with patch.object(setup_native.shutil, "which", side_effect=self.which_all(cc=None)):
with self.assertRaisesRegex(ValueError, "No C compiler found"):
installer.require_compiler()
def test_ntfy_build_failure_is_reported(self):
with tempfile.TemporaryDirectory() as directory:
installer = self.installer(directory, Recorder(fail_build=True))
prefix = Path(directory) / "prefix"
installer.prepare_layout(prefix)
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()):
with self.assertRaisesRegex(ValueError, "build failed"):
installer.build_ntfy(prefix, resolution())
def test_publisher_token_is_never_overwritten(self):
with tempfile.TemporaryDirectory() as directory:
prefix = Path(directory) / "prefix"
(prefix / "secrets").mkdir(parents=True)
path = prefix / "secrets/publisher-token"
setup_native.tokens.save_token(f"token {TOKEN} created", path, 0o600)
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
with self.assertRaises(ValueError):
setup_native.tokens.save_token(f"token {TOKEN} created", path, 0o600)
self.assertEqual(path.read_text(), TOKEN + "\n")
def test_migration_copies_docker_data_only_when_absent(self):
with tempfile.TemporaryDirectory() as directory:
prefix = Path(directory) / "prefix"
(prefix / "data/ntfy").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")
self.assertEqual((prefix / "data/ntfy/auth.db").read_bytes(), b"migrated")
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, resolver=resolution, 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_full_run_migrates_docker_auth_store_before_native_accounts(self):
with tempfile.TemporaryDirectory() as directory:
recorder = Recorder(volumes=("pipelines_ntfy-data",))
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([]))
# The migrated store must survive bootstrap instead of being shadowed.
self.assertEqual((prefix / "data/ntfy/auth.db").read_bytes(), b"migrated")
copy_index = next(i for i, call in enumerate(recorder.calls)
if call[0] == "docker" and call[1:2] == ["cp"])
add_index = next(i for i, call in enumerate(recorder.calls)
if "pipeline-publisher" in call and "user" in call)
self.assertLess(copy_index, add_index, "auth store migrated after native accounts were created")
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_skips_without_a_valid_model(self):
with tempfile.TemporaryDirectory() as directory:
installer = self.installer(directory, Recorder())
path = installer.harness_config_path()
result = installer.configure_harnesses({"opencode": "/usr/bin/opencode", "pi": ""})
self.assertIsNone(result)
self.assertFalse(path.exists(), "an invalid profile was written")
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_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()