import importlib.util 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 setUp(self): directory = tempfile.TemporaryDirectory() self.addCleanup(directory.cleanup) environment = patch.dict(os.environ, {"XDG_CONFIG_HOME": directory.name}) environment.start() self.addCleanup(environment.stop) 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='{"ntfy":{"health":"not configured"}}', 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_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): 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"])) 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"])) 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"])) 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","save-ntfy-token.py"]: shutil.copy2(Path(__file__).with_name(name),scripts/name) marker=root/"waiting" docker=root/"docker" docker.write_text('#!/bin/sh\ncase "$*" in *" up "*) touch "'+str(marker)+'"; exec sleep 30;; esac\n') docker.chmod(0o700) go=root/"go";go.write_text('#!/bin/sh\nexit 0\n');go.chmod(0o700) process=subprocess.Popen([sys.executable,str(scripts/"setup-docker.py"), "--ntfy-url","http://192.168.1.25:8090","--bind-address","192.168.1.25","--timezone","UTC"], env=dict(os.environ,PATH=str(root)+os.pathsep+os.environ["PATH"]), 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()