import importlib.util import os from pathlib import Path import subprocess import sys import tempfile import unittest from unittest.mock import patch spec = importlib.util.spec_from_file_location("setup_harness", Path(__file__).with_name("setup-harness.py")) setup = importlib.util.module_from_spec(spec) spec.loader.exec_module(setup) class HarnessSetupTests(unittest.TestCase): def test_missing_docker_installation_never_configures_host_harness(self): with tempfile.TemporaryDirectory() as directory: script = str(Path(directory, "scripts/setup-harness.py")) with patch.object(setup, "__file__", script), patch.object(sys, "argv", [script, "pi"]), patch.object(setup.subprocess, "run") as run: with self.assertRaisesRegex(SystemExit, "Run ./install first"): setup.main() run.assert_not_called() def test_reconfiguration_reuses_setup_docker_helpers_and_links_host(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) (root / ".env").write_text("PIPELINES_HOST_REPOSITORY=/tmp/repo\n") script = str(root / "scripts/setup-harness.py") calls = {} class Fake: def __init__(self, install_root): calls["root"] = install_root def reconfigure_harness(self, name, options): calls["name"] = name calls["options"] = tuple(options) def link_host(self, name, config=None): calls["linked"] = name def compose(self, *args, **kwargs): return subprocess.CompletedProcess(["docker", *args], 0, stdout='{"default":"pi","profiles":{"pi":{"model":"m","pipeline_model":"m"}}}', stderr="") with patch.object(setup, "__file__", script), \ patch.object(setup.setup_docker, "Setup", Fake), \ patch.object(setup.sys, "argv", [script, "pi", "--model", "fixture/model"]): setup.main() # /var/folders and /private/var/folders are the same real directory on # macOS; compare canonically instead of by path spelling. self.assertTrue(os.path.samefile(calls["root"], root)) self.assertEqual(calls["name"], "pi") self.assertEqual(calls["options"], ("--model", "fixture/model")) self.assertEqual(calls["linked"], "pi") def test_stale_image_stops_without_rebuilding_services(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) (root / ".env").write_text("PIPELINES_HOST_REPOSITORY=/tmp/repo\n") script = str(root / "scripts/setup-harness.py") class Fake: def __init__(self, install_root): pass def reconfigure_harness(self, name, options): raise ValueError("The Docker image does not match the current resolved releases; run ./install") with patch.object(setup, "__file__", script), \ patch.object(setup.setup_docker, "Setup", Fake), \ patch.object(setup.sys, "argv", [script, "pi"]): with self.assertRaisesRegex(ValueError, "run ./install"): setup.main() def test_install_harness_flags_point_to_the_single_setup_command(self): install = Path(__file__).resolve().parents[1] / "install.sh" for flag, name in (("--opencode", "opencode"), ("--pi", "pi")): result = subprocess.run(["sh", str(install), flag], capture_output=True, text=True) self.assertEqual(result.returncode, 2) self.assertEqual(result.stdout, "") self.assertEqual(result.stderr.strip(), f"./install {flag} was removed; run: pipelines harness setup {name}") if __name__ == "__main__": unittest.main()