Files
pipelines/scripts/test_setup_docker.py
T
alan fc7dbb0ac0 Remove ntfy: notifications go only through the app notification stream
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>
2026-12-03 23:24:24 +00:00

723 lines
43 KiB
Python

import importlib.util
import io
from contextlib import redirect_stderr, redirect_stdout
import os
from pathlib import Path
import shutil
import signal
import subprocess
import sys
import tempfile
import time
import unittest
from unittest.mock import patch
spec = importlib.util.spec_from_file_location("setup", Path(__file__).with_name("setup-docker.py"))
setup = importlib.util.module_from_spec(spec)
spec.loader.exec_module(setup)
class DockerSetupTests(unittest.TestCase):
def test_host_update_installs_clients_not_server(self):
calls = []
def runner(command, **kwargs):
calls.append(command)
return subprocess.CompletedProcess(command, 0)
with patch.object(setup.shutil, "which", return_value="go"):
setup.Setup(".", runner=runner).update_host_client()
self.assertEqual(calls[0], ["go", "install", "-trimpath", "./cmd/pipelines", "./cmd/pipelines-tui"])
self.assertFalse(any("./cmd/pipelines-server" in command or "./cmd/..." in command for command in calls))
def test_fuzzy_model_picker_runs_in_container_with_private_files_and_terminal_input(self):
with tempfile.TemporaryDirectory() as directory:
calls = []
def runner(command, **kwargs):
calls.append(command)
self.assertNotIn("-T", command, "interactive picker lost its terminal")
self.assertFalse(kwargs.get("capture_output"), "picker output must not be captured")
catalog = Path(command[command.index("--options-file")+1])
self.assertEqual(setup.json.loads(catalog.read_text()), ["fixture/first", "fixture/second"])
self.assertEqual(catalog.stat().st_mode & 0o777, 0o600)
Path(command[command.index("--output")+1]).write_text("fixture/second\n")
return subprocess.CompletedProcess(command, 0)
installer = setup.Setup(directory, runner=runner)
with patch.object(setup.shutil, "which", return_value="go"):
self.assertEqual(installer.choose_model(["fixture/first", "fixture/second"], "Choose model"), "fixture/second")
self.assertEqual(calls[-1][:5], ["docker", "compose", "run", "--rm", "--no-deps"])
self.assertNotIn("go", calls[-1], "picker fell back to a host runtime")
self.assertEqual(list(Path(directory).iterdir()), [])
def test_unidentifiable_source_fails_before_settings_or_startup(self):
calls = []
def runner(command, **kwargs):
calls.append(command)
return subprocess.CompletedProcess(command, 1 if command[0] == "git" else 0, stdout="", stderr="")
installer = setup.Setup(".", runner=runner)
with patch.object(installer, "settings", side_effect=AssertionError("must not modify settings")):
with self.assertRaisesRegex(ValueError, "Cannot identify source"):
installer.run(setup.arguments([]))
self.assertFalse(any("up" in command or "build" in command for command in calls))
def test_running_build_must_match_compiled_source(self):
expected = {"PIPELINES_VERSION": "a" * 12, "PIPELINES_COMMIT": "a" * 40, "PIPELINES_DIRTY": "false"}
good = {"type": "pong", "build": {"version": "a" * 12, "commit": "a" * 40, "dirty": False}}
for report, accepted in [(good, True), ({"type": "pong"}, False),
({**good, "build": {**good["build"], "commit": "b" * 40}}, False),
({**good, "build": {**good["build"], "dirty": None}}, False),
({**good, "type": "error"}, False), ([], False)]:
def runner(command, **kwargs):
return subprocess.CompletedProcess(command, 0, stdout=setup.json.dumps(report), stderr="")
installer = setup.Setup(".", runner=runner)
if accepted:
installer.verify_build(expected)
else:
with self.assertRaisesRegex(ValueError, "setup is NOT complete"):
installer.verify_build(expected)
def setUp(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
home = tempfile.TemporaryDirectory()
self.addCleanup(home.cleanup)
# Fixtures must never see the container default /home/pipelines; inject a
# disposable HOME/USERPROFILE (host_workspaces still rejects unsafe paths).
environment = patch.dict(os.environ, {"XDG_CONFIG_HOME": directory.name,
"HOME": home.name, "USERPROFILE": home.name})
environment.start()
self.addCleanup(environment.stop)
def test_conditional_rebuild_uses_receipt_and_verified_image_identity(self):
with tempfile.TemporaryDirectory() as directory:
releases = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64}, "pi": {"version": "1.1.0"}}
identity = {"PIPELINES_VERSION": "b" * 12, "PIPELINES_COMMIT": "b" * 40, "PIPELINES_DIRTY": "false"}
image_labels = {}
def runner(command, **kwargs):
if command[2:4] == ["images", "-q"]:
return subprocess.CompletedProcess(command, 0, stdout="imageid\n", stderr="")
if command[:2] == ["docker", "image"]:
return subprocess.CompletedProcess(command, 0, stdout=setup.json.dumps(image_labels), stderr="")
return subprocess.CompletedProcess(command, 0, stdout="", stderr="")
installer = setup.Setup(directory, runner=runner)
self.assertTrue(installer.needs_image_build(releases, identity), "missing image must build")
installer.record_release_receipt(releases, identity)
image_labels.update(installer.expected_labels(releases, identity))
self.assertFalse(installer.needs_image_build(releases, identity), "unchanged image/source rebuilt")
changed_source = {**identity, "PIPELINES_VERSION": "b" * 12 + "-dirty", "PIPELINES_DIRTY": "true"}
self.assertTrue(installer.needs_image_build(releases, changed_source, force_dirty=True), "changed source skipped install rebuild")
changed = {"opencode": {"version": "2.0.27", "digest": "sha256:" + "c" * 64}, "pi": {"version": "1.1.0"}}
self.assertTrue(installer.needs_image_build(changed, identity), "changed release not rebuilt")
# The receipt now matches, but the actual image is unrelated: labels win.
installer.record_release_receipt(changed, identity)
self.assertTrue(installer.needs_image_build(changed, identity), "unrelated image accepted on receipt alone")
receipt = setup.json.loads((Path(directory) / ".pipelines-releases.json").read_text())
self.assertEqual(receipt["opencode"]["version"], "2.0.27")
self.assertEqual(receipt["build"], identity)
self.assertEqual((Path(directory) / ".pipelines-releases.json").stat().st_mode & 0o777, 0o600)
build_args = installer.release_build_args(releases)
self.assertIn("OPENCODE_VERSION=2.0.26", build_args)
self.assertIn("OPENCODE_DIGEST=sha256:" + "a" * 64, build_args)
def test_dirty_source_forces_install_rebuild_but_not_reconfiguration(self):
with tempfile.TemporaryDirectory() as directory:
releases = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64}, "pi": {"version": "1.1.0"}}
dirty = {"PIPELINES_VERSION": "b" * 12, "PIPELINES_COMMIT": "b" * 40, "PIPELINES_DIRTY": "true"}
image_labels = {}
def runner(command, **kwargs):
if command[2:4] == ["images", "-q"]:
return subprocess.CompletedProcess(command, 0, stdout="imageid\n", stderr="")
if command[:2] == ["docker", "image"]:
return subprocess.CompletedProcess(command, 0, stdout=setup.json.dumps(image_labels), stderr="")
return subprocess.CompletedProcess(command, 0, stdout="", stderr="")
installer = setup.Setup(directory, runner=runner)
installer.record_release_receipt(releases, dirty)
image_labels.update(installer.expected_labels(releases, dirty))
# Identical dirty identity must not let changed source skip the build.
self.assertTrue(installer.needs_image_build(releases, dirty, force_dirty=True))
# Reconfiguration reuses the verified release image without a rebuild.
self.assertFalse(installer.needs_image_build(releases, dirty))
def test_reconfiguration_ignores_source_identity_but_install_builds(self):
with tempfile.TemporaryDirectory() as directory:
releases = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64}, "pi": {"version": "1.1.0"}}
built_with = {"PIPELINES_VERSION": "b" * 12, "PIPELINES_COMMIT": "b" * 40, "PIPELINES_DIRTY": "false"}
other_source = {"PIPELINES_VERSION": "c" * 12, "PIPELINES_COMMIT": "c" * 40, "PIPELINES_DIRTY": "false"}
image_labels = {}
def runner(command, **kwargs):
if command[2:4] == ["images", "-q"]:
return subprocess.CompletedProcess(command, 0, stdout="imageid\n", stderr="")
if command[:2] == ["docker", "image"]:
return subprocess.CompletedProcess(command, 0, stdout=setup.json.dumps(image_labels), stderr="")
return subprocess.CompletedProcess(command, 0, stdout="", stderr="")
installer = setup.Setup(directory, runner=runner)
installer.record_release_receipt(releases, other_source)
image_labels.update(installer.expected_labels(releases, other_source))
# Release identity matches; a model/login-only reconfigure must not rebuild.
self.assertFalse(installer.needs_image_build(releases, built_with),
"source identity forced a reconfiguration rebuild")
# A full install with a different source identity must rebuild.
self.assertTrue(installer.needs_image_build(releases, built_with, force_dirty=True),
"different source identity skipped the install rebuild")
def test_corrupted_receipt_fails_closed_and_rebuilds(self):
releases = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64}, "pi": {"version": "1.1.0"}}
identity = {"PIPELINES_VERSION": "b" * 12}
installer = setup.Setup(".")
for receipt in ({"opencode": "x"}, {"pi": []}, {"opencode": {}, "pi": {}}, {"opencode": {"version": "2.0.26"}}, None, []):
self.assertFalse(installer.receipt_matches(receipt, releases, identity), receipt)
matching = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64},
"pi": {"version": "1.1.0"}, "build": identity}
self.assertTrue(installer.receipt_matches(matching, releases, identity))
def test_link_host_reuses_container_profile_and_falls_back_to_pipelines_client(self):
calls = []
def runner(command, **kwargs):
if command[0] == "docker":
return subprocess.CompletedProcess(command, 0,
stdout='{"default":"pi","profiles":{"pi":{"model":"m","pipeline_model":"pm"}}}', stderr="")
calls.append(command)
return subprocess.CompletedProcess(command, 0)
installer = setup.Setup(".", runner=runner)
with patch.object(setup.shutil, "which", side_effect=lambda name: None if name == "go" else "/usr/bin/pipelines"):
installer.link_host()
self.assertEqual(calls, [["/usr/bin/pipelines", "harness", "link", "pi",
"--docker-directory", str(Path(".").resolve()),
"--model", "m", "--pipeline-model", "pm", "--default"]])
def test_reconfigure_reuses_resolution_and_refuses_stale_image(self):
releases = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64},
"pi": {"version": "1.1.0", "engine": ">=22.19.0"}}
calls = []
def runner(command, **kwargs):
calls.append(command)
return subprocess.CompletedProcess(command, 0, stdout="", stderr="")
installer = setup.Setup(".", runner=runner)
installer.resolve_releases = lambda: releases
installer.build_identity = lambda: {"PIPELINES_VERSION": "b" * 12}
installer.needs_image_build = lambda resolved, identity: True
with self.assertRaisesRegex(ValueError, "run ./install"):
installer.reconfigure_harness("pi", [])
self.assertEqual(calls, [], "stale reconfigure must not touch Docker")
def test_reconfigure_verifies_and_configures_without_rebuilding(self):
releases = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64},
"pi": {"version": "1.1.0", "engine": ">=22.19.0"}}
calls = []
def runner(command, **kwargs):
calls.append(command)
return subprocess.CompletedProcess(command, 0, stdout="", stderr="")
installer = setup.Setup(".", runner=runner)
installer.resolve_releases = lambda: releases
installer.build_identity = lambda: {"PIPELINES_VERSION": "b" * 12}
installer.needs_image_build = lambda resolved, identity: False
verified = []
installer.verify_harness_versions = lambda resolved: verified.append(resolved)
installer.reconfigure_harness("opencode", ["--model", "fixture/model"])
self.assertEqual(verified, [releases])
self.assertEqual(calls[0][:4], ["docker", "compose", "exec", "pipelines"])
self.assertIn("harness", calls[0])
self.assertFalse(any("build" in call for call in calls))
def test_stale_image_version_mismatch_fails_closed(self):
def runner(command, **kwargs):
return subprocess.CompletedProcess(command, 0, stdout="opencode=2.0.20\npi=0.87.1\nnode=v22.19.0\n", stderr="")
installer = setup.Setup(".", runner=runner)
with self.assertRaisesRegex(ValueError, "stale or mismatched"):
installer.verify_harness_versions({"opencode": {"version": "2.0.26"}, "pi": {"version": "1.1.0", "engine": ">=22.19.0"}})
def test_exact_version_token_and_node_engine_are_enforced(self):
releases = {"opencode": {"version": "2.0.26"}, "pi": {"version": "1.1.0", "engine": ">=22.19.0"}}
rejected = ["opencode=v2.0.260\npi=1.1.0\nnode=v22.19.0\n", # parsed 260 != expected 26 (exact equality)
"opencode=2.0.26\npi=1.1.0\n", # missing Node fails closed
"opencode=2.0.26\npi=1.1.0\nnode=v20.0.0\n", # unsatisfied engine
"opencode=v2.0.26-rc.1\npi=1.1.0\nnode=v22.19.0\n", # arbitrary suffix
"opencode=OpenCode v2.0.26\npi=1.1.0\nnode=v22.19.0\n", # banner, not an exact token
"opencode=v2.0.26 extra\npi=1.1.0\nnode=v22.19.0\n", # extra token after version
"opencode=v2.0.26\npi=v1.1.0\nnode=v22.19.0\n", # Pi's format has no leading v
"opencode=v2.0.26\npi=1.1.0\nnode=node v22.19.0\n"] # Node banner, not an exact token
for output in rejected:
def runner(command, **kwargs):
return subprocess.CompletedProcess(command, 0, stdout=output, stderr="")
with self.assertRaises(ValueError):
setup.Setup(".", runner=runner).verify_harness_versions(releases)
def good(command, **kwargs):
return subprocess.CompletedProcess(command, 0, stdout="opencode=opencode v2.0.26\npi=1.1.0\nnode=v24.18.1\n", stderr="")
setup.Setup(".", runner=good).verify_harness_versions(releases)
def test_three_digit_openCode_patch_is_compared_by_exact_equality(self):
three = {"opencode": {"version": "2.0.100"}, "pi": {"version": "1.1.0"}}
def matching(command, **kwargs):
return subprocess.CompletedProcess(command, 0, stdout="opencode=opencode v2.0.100\npi=1.1.0\nnode=v24.18.1\n", stderr="")
setup.Setup(".", runner=matching).verify_harness_versions(three)
def different(command, **kwargs):
return subprocess.CompletedProcess(command, 0, stdout="opencode=opencode v2.0.260\npi=1.1.0\nnode=v24.18.1\n", stderr="")
with self.assertRaises(ValueError):
setup.Setup(".", runner=different).verify_harness_versions(three)
def test_parse_harness_version_accepts_only_exact_known_formats(self):
accepted = {("opencode", "v2.0.26"): "2.0.26",
("opencode", "2.0.26"): "2.0.26",
("opencode", "opencode v2.0.26"): "2.0.26",
("opencode", "opencode 2.0.26"): "2.0.26",
("opencode", "opencode v2.0.260"): "2.0.260",
("opencode", "opencode v2.0.100"): "2.0.100",
("opencode", "v2.0.260"): "2.0.260",
("pi", "1.1.0"): "1.1.0",
("node", "v24.18.1"): "24.18.1",
("node", "24.18.1"): "24.18.1"}
for (name, value), normalized in accepted.items():
self.assertEqual(setup.parse_harness_version(name, value), normalized)
for name, value in (("opencode", "v2.0.26-rc.1"), ("opencode", "OpenCode v2.0.26"),
("opencode", "opencode v2.0.26 extra"), ("opencode", "opencode v2.0.26.1"),
("pi", "v1.1.0"), ("pi", "1.1.0 extra"), ("node", "22.19.0.1"),
("node", ""), ("unknown", "1.2.3")):
with self.assertRaises(ValueError):
setup.parse_harness_version(name, value)
def test_release_resolution_failure_keeps_verified_pins(self):
installer = setup.Setup(".")
def fail():
raise setup.release_lookup.ReleaseError("registry unreachable")
installer.resolver = fail
with self.assertRaisesRegex(ValueError, "keeping the last verified Dockerfile pins"):
installer.resolve_releases()
def test_verified_client_default_preserves_unrelated_settings_and_warns_on_override(self):
path = Path(os.environ["XDG_CONFIG_HOME"])/"pipelines/client.json"
path.parent.mkdir()
path.write_text('{"server_url":"ws://old/connect","custom":"keep"}')
with tempfile.TemporaryDirectory() as directory, patch.dict(os.environ, {"PIPELINES_URL":"ws://old/connect"}):
setup.Setup(directory).configure_client("ws://127.0.0.1:8081/connect")
self.assertEqual(setup.json.loads(path.read_text()), {"server_url":"ws://127.0.0.1:8081/connect","custom":"keep"})
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
def test_client_config_symlink_and_malformed_content_are_not_overwritten(self):
path = Path(os.environ["XDG_CONFIG_HOME"])/"pipelines/client.json"
path.parent.mkdir()
target = path.parent/"custom.json"
target.write_text("private custom settings")
path.symlink_to(target)
with self.assertRaises(ValueError): setup.Setup(".").configure_client("ws://127.0.0.1:8081/connect")
self.assertEqual(target.read_text(),"private custom settings")
path.unlink()
path.write_text("not JSON")
with self.assertRaises(ValueError): setup.Setup(".").configure_client("ws://127.0.0.1:8081/connect")
self.assertEqual(path.read_text(), "not JSON")
def test_host_verification_rejects_wrong_unconfigured_server(self):
def runner(command, **kwargs):
return subprocess.CompletedProcess(command, 0, stdout='[]', stderr="")
with patch.object(setup.shutil,"which",return_value="/fixture/go"), self.assertRaises(ValueError):
setup.Setup(".",runner=runner).verify_host_endpoint("ws://127.0.0.1:8081/connect")
def test_workspace_mount_uses_host_home_uid_and_preserves_settings(self):
with tempfile.TemporaryDirectory() as directory:
path=Path(directory,".env");path.write_text("CUSTOM=keep\nPIPELINES_UID=10001\n")
setup.Setup(directory).configure_workspaces()
content=path.read_text()
self.assertIn("CUSTOM=keep\n",content)
self.assertIn("PIPELINES_HOST_HOME='"+str(Path.home().resolve())+"'",content)
self.assertIn("PIPELINES_UID="+str(os.getuid()),content)
self.assertIn("PIPELINES_GID="+str(os.getgid()),content)
setup.Setup(directory).configure_workspaces()
self.assertEqual(path.read_text(),content)
def test_host_workspaces_rejects_unsafe_home_and_equal_repository(self):
with tempfile.TemporaryDirectory() as repository:
for home in ["/", "/data", "/workspace", "/home/pipelines"]:
with patch.dict(os.environ, {"HOME": home, "USERPROFILE": home}):
with self.assertRaises(ValueError):
setup.configure.host_workspaces(repository)
# home == repository must also be rejected, not mounted at itself.
with patch.dict(os.environ, {"HOME": repository, "USERPROFILE": repository}):
with self.assertRaises(ValueError):
setup.configure.host_workspaces(repository)
def test_ownership_migration_and_workspace_verification_are_scoped(self):
calls=[]
def runner(command,**kwargs):
calls.append(command)
config={"services":{"pipelines":{"volumes":[{"type":"volume","target":target} for target in ["/data","/home/pipelines","/workspace"]]}}}
return subprocess.CompletedProcess(command,0,stdout=setup.json.dumps(config) if "--format" in command else "",stderr="")
installer=setup.Setup(".",runner=runner)
installer.prepare_volume_ownership()
installer.verify_workspace()
self.assertIn("0:0",calls[1])
self.assertEqual(calls[1][-1], 'set -eu; chown -R "$PIPELINES_UID:$PIPELINES_GID" /data /home/pipelines /workspace')
self.assertEqual(calls[2][-1],str(Path(".").resolve()))
self.assertIn('cd "$1"',calls[2][-3])
def test_ownership_migration_never_chowns_host_bind_mounts(self):
calls=[]
def runner(command,**kwargs):
calls.append(command)
return subprocess.CompletedProcess(command,0,stdout='{"services":{"pipelines":{"volumes":[{"type":"bind","target":"/data"}]}}}',stderr="")
with self.assertRaises(ValueError): setup.Setup(".",runner=runner).prepare_volume_ownership()
self.assertEqual(len(calls),1)
self.assertFalse(any("chown" in part for part in calls[0]))
def test_validation_primes_its_own_service_and_passes_model_as_argument(self):
with tempfile.TemporaryDirectory() as directory:
root=Path(directory);(root/".env").write_text("CUSTOM=keep\n")
opencode=root/"opencode"
opencode.write_text('''#!/bin/sh
if [ "$1" = api ]; then
touch "$FIXTURE/primed"
echo '{"data":[]}'
elif [ "$1" = models ]; then
echo 'opencode-go/deepseek-v4.1-flash'
else
exit 9
fi
''');opencode.chmod(0o700)
pipelines=root/"pipelines"
pipelines.write_text('''#!/bin/sh
test -f "$FIXTURE/primed" || exit 8
test "$1" = provider-check || exit 9
printf '%s' "$3" > "$FIXTURE/model"
echo '{"healthy":true,"session_id":"ses_validation"}'
''');pipelines.chmod(0o700)
def runner(command,**kwargs):
if "harness" in command:
return subprocess.CompletedProcess(command,0,stdout="",stderr="")
index=command.index("-c")
# Each one-off container has a separate service/catalog.
(root/"primed").unlink(missing_ok=True)
return subprocess.run(["sh",*command[index:]],capture_output=True,text=True,
env=dict(os.environ,PATH=str(root)+os.pathsep+os.environ["PATH"],FIXTURE=str(root)))
installer=setup.Setup(directory,runner=runner)
installer.validate_provider(setup.arguments(["--model","opencode-go/deepseek-v4.1-flash","--harness-model","opencode-go/deepseek-v4.1-flash"]))
self.assertEqual((root/"model").read_text(),"opencode-go/deepseek-v4.1-flash")
self.assertIn("CUSTOM=keep\n",(root/".env").read_text())
def test_native_login_ui_uses_same_primed_container(self):
with tempfile.TemporaryDirectory() as directory:
root=Path(directory)
executable=root/"opencode"
executable.write_text('''#!/bin/sh
if [ "$1" = api ]; then
touch "$FIXTURE/primed"
echo '{"data":[]}'
elif [ "$1 $2" = 'auth login' ]; then
test -f "$FIXTURE/primed" || exit 8
touch "$FIXTURE/logged"
else
exit 9
fi
''')
executable.chmod(0o700)
logins=[]
def runner(command,**kwargs):
if command[-4:]==["auth","list","--format","json"]:
accounts='[{"id":"opencode-go","connections":[{"id":"fixture"}]}]' if (root/"logged").exists() else '[]'
return subprocess.CompletedProcess(command,0,stdout=accounts,stderr="")
logins.append(command)
self.assertNotIn("-T",command,"interactive login lost its terminal")
return subprocess.run(["sh","-c",command[-1]],
env=dict(os.environ,PATH=str(root)+os.pathsep+os.environ["PATH"],FIXTURE=str(root)))
setup.Setup(directory,runner=runner).authenticate()
self.assertEqual(len(logins),1)
self.assertTrue((root/"logged").exists())
def test_model_discovery_primes_catalog_and_retries_in_one_container(self):
with tempfile.TemporaryDirectory() as directory:
root=Path(directory)
executable=root/"opencode"
executable.write_text('''#!/bin/sh
if [ "$1" = api ]; then
touch "$FIXTURE/primed"
echo '{"data":[]}'
elif [ "$1" = models ]; then
test -f "$FIXTURE/primed" || exit 8
if [ ! -f "$FIXTURE/first-read" ]; then
touch "$FIXTURE/first-read"
else
echo 'opencode-go/deepseek-v4.1-flash'
fi
else
exit 9
fi
''')
executable.chmod(0o700)
calls=[]
def runner(command,**kwargs):
calls.append(command)
self.assertIn("sh",command)
return subprocess.run(["sh","-c",command[-1]],capture_output=True,text=True,
env=dict(os.environ,PATH=str(root)+os.pathsep+os.environ["PATH"],FIXTURE=str(root)))
models=setup.Setup(directory,runner=runner).discover_models()
self.assertEqual(models,["opencode-go/deepseek-v4.1-flash"])
self.assertEqual(len(calls),1,"retry started a fresh container/service")
def test_empty_catalog_does_not_blame_or_replace_saved_key(self):
with tempfile.TemporaryDirectory() as directory:
path=Path(directory,".env");path.write_text("CUSTOM=keep\n")
def runner(command,**kwargs):
return subprocess.CompletedProcess(command,0,stdout="",stderr="")
installer=setup.Setup(directory,runner=runner)
installer.accounts=[{"id":"opencode-go","connections":[{"id":"saved-account"}]}]
with self.assertRaises(ValueError) as raised:
installer.validate_provider(setup.arguments([]))
self.assertIn("NOT proof it is invalid",str(raised.exception))
self.assertIn("opencode-go",str(raised.exception))
self.assertNotIn("--relogin",str(raised.exception))
self.assertEqual(path.read_text(),"CUSTOM=keep\n")
def test_provider_validation_failure_preserves_saved_model(self):
for report in ['{"healthy":false,"diagnostic":"quota exhausted","session_id":"ses_failed"}',
'{"healthy":true}', '[]', 'not-json']:
with tempfile.TemporaryDirectory() as directory:
path=Path(directory,".env")
path.write_text("PIPELINES_DEFAULT_MODEL=fixture/previous\nCUSTOM=keep\n")
before=path.read_bytes()
calls=[]
def runner(command,**kwargs):
calls.append(command)
out="fixture/new\n" if "opencode models" in command[-1] else report
return subprocess.CompletedProcess(command,0,stdout=out,stderr="")
installer=setup.Setup(directory,runner=runner)
with patch("sys.stderr"),self.assertRaises(ValueError):
installer.validate_provider(setup.arguments(["--model","fixture/new","--harness-model","fixture/new"]))
self.assertEqual(before,path.read_bytes())
self.assertTrue(any(any("provider-check" in arg for arg in command) for command in calls))
def test_unavailable_model_does_not_attempt_generation(self):
with tempfile.TemporaryDirectory() as directory:
Path(directory,".env").write_text("CUSTOM=keep\n")
calls=[]
def runner(command,**kwargs):
calls.append(command)
return subprocess.CompletedProcess(command,0,stdout="fixture/available\n",stderr="")
with self.assertRaises(ValueError):
setup.Setup(directory,runner=runner).validate_provider(setup.arguments(["--model","fixture/unavailable","--harness-model","fixture/available"]))
self.assertFalse(any(any("provider-check" in arg for arg in command) for command in calls))
def test_ctrl_c_exits_cleanly_and_retains_configuration(self):
with tempfile.TemporaryDirectory() as directory:
root=Path(directory);scripts=root/"scripts";scripts.mkdir()
for name in ["setup-docker.py","lan-address.py","configure-compose.py","build_metadata.py","resolve-releases.py"]:
shutil.copy2(Path(__file__).with_name(name),scripts/name)
fixture=root/"releases.json"
fixture.write_text(setup.json.dumps({"opencode":{"image":"ghcr.io/anomalyco/opencode","version":"2.0.26","digest":"sha256:"+"a"*64},"pi":{"version":"1.1.0","engine":">=22.19.0"}}))
marker=root/"waiting"
docker=root/"docker"
docker.write_text('#!/bin/sh\ncase "$*" in *" config --format json"*) touch "'+str(marker)+'"; exec sleep 30;; *"opencode --version"*) printf "opencode=2.0.26\\npi=1.1.0\\nnode=v22.19.0\\n";; esac\n')
docker.chmod(0o700)
go=root/"go";go.write_text('#!/bin/sh\nexit 0\n');go.chmod(0o700)
git=root/"git";git.write_text('#!/bin/sh\nif [ "$1" = rev-parse ]; then echo '+"a"*40+'; fi\n');git.chmod(0o700)
# The container's default home (/home/pipelines) is rejected by the
# mount guard; give the subprocess its own disposable HOME.
process=subprocess.Popen([sys.executable,str(scripts/"setup-docker.py"),
"--timezone","UTC"],
env=dict(os.environ,PATH=str(root)+os.pathsep+os.environ["PATH"],
HOME=str(root/"home"),USERPROFILE=str(root/"home"),
PIPELINES_RELEASE_FIXTURE=str(fixture)),
stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,start_new_session=True)
try:
deadline=time.monotonic()+5
while not marker.exists() and process.poll() is None and time.monotonic()<deadline:
time.sleep(.02)
self.assertTrue(marker.exists(),"installer did not reach Docker startup")
os.killpg(process.pid,signal.SIGINT)
stdout,stderr=process.communicate(timeout=5)
self.assertEqual(process.returncode,130)
self.assertNotIn("Traceback",stdout+stderr)
self.assertIn("Setup canceled",stderr)
self.assertTrue((root/".env").exists())
finally:
if process.poll() is None:
os.killpg(process.pid,signal.SIGTERM)
process.communicate(timeout=5)
def test_fresh_defaults_configure_without_any_input(self):
with tempfile.TemporaryDirectory() as directory, \
patch.object(setup.configure,"host_timezone",return_value="Europe/London"), \
patch("builtins.input",side_effect=AssertionError("must not ask for network settings")):
setup.Setup(directory).settings(setup.arguments([]))
text=Path(directory,".env").read_text()
self.assertNotIn("NTFY",text)
self.assertIn("TZ=Europe/London\n",text)
self.assertFalse(Path(directory,"secrets").exists())
def test_timezone_fallback_does_not_prompt(self):
with tempfile.TemporaryDirectory() as directory, \
patch.object(setup.lan,"detect_lan",return_value="192.168.1.25"), \
patch.object(setup.configure,"host_timezone",side_effect=ValueError("unknown")):
setup.Setup(directory).settings(setup.arguments([]))
self.assertIn("TZ=UTC\n",Path(directory,".env").read_text())
def test_missing_lan_fails_without_partial_settings_or_prompt(self):
with tempfile.TemporaryDirectory() as directory:
with self.assertRaisesRegex(ValueError, "No LAN"):
self.exercise(directory, lan_error=ValueError("No LAN"))
self.assertFalse(Path(directory,".env").exists())
def exercise(self, directory, existing=False, fail=None, confirm="yes", harness="opencode",
lan_error=None, ntfy_leftovers=False):
calls = []
logged = existing
def runner(command, **kwargs):
nonlocal logged
if command[0] == "git":
out = "a" * 40 if command[1] == "rev-parse" else ""
return subprocess.CompletedProcess(command, 0, stdout=out, stderr="")
args = command[2:]
calls.append(args)
code, out = 0, ""
if ntfy_leftovers and command[:3] == ["docker", "ps", "-aq"] and "label=com.docker.compose.service=ntfy" in command:
out = "0123abcd\n"
if ntfy_leftovers and command[:3] == ["docker", "volume", "ls"]:
out = "pipelines_ntfy-data\n"
if args[-4:] == ["auth", "list", "--format", "json"]:
out = '[{"id":"fixture","connections":[{"id":"fixture"}]}]' if logged else '[]'
if "opencode models" in args[-1]:
out = "fixture/test-model\n"
if any("provider-check" in arg for arg in args):
out = '{"healthy":true,"session_id":"ses_fixture"}'
if "exec opencode auth login" in args[-1] and confirm == "yes":
logged = True
if args[-2:] == ["status", "--json"]:
out = '{"type":"status","runs":[]}'
if args[-2:] == ["ping", "--json"]:
out = setup.json.dumps({"type": "pong", "build": {"version": "a" * 12, "commit": "a" * 40, "dirty": False}})
if args[-3:]==["harness","list","--json"]:
out = setup.json.dumps({"default":harness,"profiles":{harness:{"model":"fixture/test-model","pipeline_model":"fixture/test-model"}}})
if args[:1] == ["port"]:
out = "127.0.0.1:8081\n"
if "opencode --version" in args[-1]:
out = "opencode=2.0.26\npi=1.1.0\nnode=v22.19.0\n"
if args == ["config", "--format", "json"]:
out = setup.json.dumps({"services":{"pipelines":{"volumes":[{"type":"volume","target":target} for target in ["/data","/home/pipelines","/workspace"]]}}})
if fail and (fail in args or (fail=="login" and "exec opencode auth login" in args[-1]) or (fail=="provider-check" and any("provider-check" in arg for arg in args))):
code, out = 9, "private provider data/token"
if fail == "host" and command[0] != "docker" and args[-2:] == ["status", "--json"]:
code, out = 9, '{}'
return subprocess.CompletedProcess(command, code, stdout=out, stderr="")
args = setup.arguments([] if existing else ["--timezone", "Europe/London"])
args.harness=harness
installer = setup.Setup(directory, runner=runner, prompt=lambda _: "1")
installer.resolver = lambda: {"opencode": {"image": "ghcr.io/anomalyco/opencode", "version": "2.0.26", "digest": "sha256:" + "a" * 64},
"pi": {"version": "1.1.0", "engine": ">=22.19.0"}}
installer.choose_model=lambda models, title, purpose="", step="", current="": models[0]
with patch.object(setup.lan,"detect_lan",return_value="192.168.1.20",side_effect=lan_error):
installer.run(args)
return calls
def test_fresh_pi_setup_skips_opencode_login_and_generation(self):
with tempfile.TemporaryDirectory() as directory:
calls = self.exercise(directory, harness="pi")
setup_call = next(c for c in calls if "setup" in c and "pi" in c)
self.assertIn("--login", setup_call)
self.assertIn("--default", setup_call)
self.assertIn("--versions", setup_call)
self.assertIn("OpenCode 2.0.26", setup_call[setup_call.index("--versions") + 1])
self.assertFalse(any("exec opencode auth login" in c[-1] for c in calls))
self.assertFalse(any(any("provider-check" in arg for arg in c) for c in calls))
def test_reconfigure_passes_verified_versions_to_onboarding(self):
releases = {"opencode": {"version": "2.0.26", "digest": "sha256:" + "a" * 64},
"pi": {"version": "1.1.0", "engine": ">=22.19.0"}}
calls = []
def runner(command, **kwargs):
calls.append(command)
return subprocess.CompletedProcess(command, 0, stdout="", stderr="")
installer = setup.Setup(".", runner=runner)
installer.resolve_releases = lambda: releases
installer.build_identity = lambda: {"PIPELINES_VERSION": "b" * 12}
installer.needs_image_build = lambda resolved, identity: False
installer.verify_harness_versions = lambda resolved: {"opencode": "2.0.26", "pi": "1.1.0", "node": "22.19.0"}
installer.reconfigure_harness("pi", ["--model", "fixture/model"])
setup_call = next(c for c in calls if "setup" in c)
# The in-container setup signs in only when needed; --login is opt-in.
self.assertNotIn("--login", setup_call)
self.assertIn("--versions", setup_call)
label = setup_call[setup_call.index("--versions") + 1]
for want in ("OpenCode 2.0.26", "Pi 1.1.0", "Node 22.19.0"):
self.assertIn(want, label)
def test_fresh_setup_runs_container_login_without_ntfy(self):
with tempfile.TemporaryDirectory() as directory:
calls = self.exercise(directory)
self.assertTrue(any("exec opencode auth login" in c[-1] for c in calls))
recreate = next(c for c in calls if "--force-recreate" in c)
# The old ntfy container is an orphan once compose.yaml drops it.
self.assertIn("--remove-orphans", recreate)
build = next(c for c in calls if "build" in c)
self.assertIn("PIPELINES_COMMIT=" + "a" * 40, build)
self.assertIn("PIPELINES_DIRTY=false", build)
self.assertLess(next(i for i,c in enumerate(calls) if "exec opencode auth login" in c[-1]), next(i for i,c in enumerate(calls) if "--force-recreate" in c))
self.assertFalse(any(c[:1] == ["up"] and "ntfy" in c for c in calls))
self.assertFalse(any(c[:1] == ["exec"] and "ntfy" in c for c in calls))
self.assertFalse(Path(directory, "secrets/ntfy-token.txt").exists())
self.assertNotIn("NTFY", Path(directory, ".env").read_text())
config = Path(os.environ["XDG_CONFIG_HOME"])/"pipelines/client.json"
self.assertEqual(setup.json.loads(config.read_text())["server_url"],"ws://127.0.0.1:8081/connect")
def test_reinstall_preserves_configuration_and_provider_setup(self):
with tempfile.TemporaryDirectory() as directory:
self.exercise(directory)
settings = Path(directory, ".env")
before = settings.read_bytes()
calls = self.exercise(directory, existing=True)
self.assertFalse(any("exec opencode auth login" in c[-1] for c in calls))
self.assertEqual(settings.read_bytes(), before + b"PIPELINES_BIND_ADDRESS=0.0.0.0\n")
normalized = settings.read_bytes()
self.exercise(directory, existing=True)
self.assertEqual(normalized, settings.read_bytes())
def test_reinstall_retires_the_old_ntfy_container_volume_token_and_settings(self):
with tempfile.TemporaryDirectory() as directory:
self.exercise(directory)
settings = Path(directory, ".env")
settings.write_text(settings.read_text() + "NTFY_BASE_URL=http://192.168.1.20:8090\nNTFY_BIND_ADDRESS=0.0.0.0\n")
Path(directory, "secrets").mkdir()
Path(directory, "secrets/ntfy-token.txt").write_text("tk_old\n")
with redirect_stdout(io.StringIO()) as output:
calls = self.exercise(directory, existing=True, ntfy_leftovers=True)
self.assertIn(["-f", "0123abcd"], calls)
self.assertIn(["rm", "pipelines_ntfy-data"], calls)
self.assertFalse(Path(directory, "secrets/ntfy-token.txt").exists())
self.assertNotIn("NTFY", settings.read_text())
self.assertIn("Retired the old ntfy notification server", output.getvalue())
self.assertNotIn("tk_old", output.getvalue())
# Retirement happens before anything is (re)started.
self.assertLess(calls.index(["-f", "0123abcd"]), next(i for i, c in enumerate(calls) if "--force-recreate" in c))
def test_login_failure_or_cancel_does_not_mark_complete_or_start_server(self):
for fail, confirm in [("login", "yes"), (None, "no")]:
with tempfile.TemporaryDirectory() as directory:
with self.assertRaises(ValueError) as raised:
self.exercise(directory, fail=fail, confirm=confirm)
self.assertNotIn("private provider", str(raised.exception))
self.assertTrue(Path(directory, ".env").stat().st_size)
def test_status_and_publication_failures_are_not_success(self):
for failure in ["status", "sh", "provider-check", "host"]:
with tempfile.TemporaryDirectory() as directory, self.assertRaises(ValueError):
self.exercise(directory, fail=failure)
def test_existing_env_is_not_replaced_by_flags(self):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory, ".env")
path.write_text("existing settings")
with self.assertRaises(ValueError):
setup.Setup(directory).settings(setup.arguments(["--timezone", "UTC"]))
self.assertEqual(path.read_text(), "existing settings")
def test_ntfy_flags_are_gone(self):
for flag in (["--ntfy-url", "http://192.168.1.20:8090"], ["--bind-address", "0.0.0.0"]):
with self.assertRaises(SystemExit), redirect_stderr(io.StringIO()):
setup.arguments(flag)
def test_existing_env_symlink_is_not_modified(self):
with tempfile.TemporaryDirectory() as directory:
target=Path(directory,"custom.env");target.write_text("keep")
Path(directory,".env").symlink_to(target)
with self.assertRaises(ValueError):
setup.Setup(directory).settings(setup.arguments([]))
self.assertEqual(target.read_text(),"keep")
if __name__ == "__main__":
unittest.main()