Files
pipelines/scripts/test_setup_harness.py

75 lines
3.3 KiB
Python

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()
if __name__ == "__main__":
unittest.main()