"""scripts/review-bringup.sh runs nisse-review from its image (M7.6): built from the code under test, with the reviewer's folder at its own path.""" import subprocess from pathlib import Path SCRIPT = Path(__file__).parent.parent.parent / "scripts" / "review-bringup.sh" def _setup(tmp_path: Path, *, running: bool = False) -> dict[str, str]: """A source repository with one commit, the reviewer's folder with its config, and stand-ins for docker, systemd-run and systemctl that log.""" src = tmp_path / "src" src.mkdir() (src / "Dockerfile").write_text("FROM scratch\n") for args in ( ["init", "-q"], ["add", "."], ["-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "c"], ): subprocess.run(["git", "-C", str(src), *args], check=True) review = tmp_path / "review" review.mkdir() (review / "nisse-review.yaml").write_text("state_db: x\n") (review / ".env").write_text("") bin_ = tmp_path / "bin" bin_.mkdir() log = tmp_path / "calls" stubs = { "docker": f'echo "docker $*" >> {log}', "systemd-run": f'echo "systemd-run $*" >> {log}', "systemctl": f'echo "systemctl $*" >> {log}; ' f'[ "$1" = is-active ] && exit {0 if running else 3}; exit 0', } for name, body in stubs.items(): path = bin_ / name path.write_text(f"#!/usr/bin/env bash\n{body}\n") path.chmod(0o755) return { "PATH": f"{bin_}:/usr/bin:/bin", "HOME": str(tmp_path), "NISSE_REVIEW": str(review), "NISSE_SRC": str(src), "LOG": str(log), } def _run(env: dict[str, str], *args: str) -> subprocess.CompletedProcess[str]: return subprocess.run( ["bash", str(SCRIPT), *args], env=env, capture_output=True, text=True, timeout=30, check=False, ) def test_start_builds_the_review_image_and_runs_it(tmp_path: Path) -> None: env = _setup(tmp_path) review = env["NISSE_REVIEW"] done = _run(env, "start") assert done.returncode == 0, done.stderr calls = Path(env["LOG"]).read_text().splitlines() [build] = [c for c in calls if c.startswith("docker build")] assert "--target review" in build and build.endswith(f"{review}/code") assert (Path(review) / "code" / "Dockerfile").exists() # the code exported [run] = [c for c in calls if c.startswith("systemd-run")] assert "--unit=nisse-review-bringup" in run assert "--property=KillSignal=SIGTERM" in run assert "docker run --rm --name nisse-review-bringup" in run assert f"-v {review}:{review}" in run assert f"-e NISSE_CONFIG={review}/nisse-review.yaml" in run assert f"-e NISSE_ENV={review}/.env" in run assert run.endswith("nisse-review:bringup") sha = subprocess.run( ["git", "-C", env["NISSE_SRC"], "rev-parse", "HEAD"], capture_output=True, text=True, check=True, ).stdout.strip() assert (Path(review) / "run-info").read_text().strip() == sha def test_it_wont_start_a_second_reviewer(tmp_path: Path) -> None: env = _setup(tmp_path, running=True) done = _run(env, "start") assert done.returncode != 0 and "already running" in done.stderr assert "docker" not in Path(env["LOG"]).read_text() def test_plan_shows_what_start_would_run(tmp_path: Path) -> None: env = _setup(tmp_path) done = _run(env, "plan") assert done.returncode == 0, done.stderr assert "docker build --target review -t nisse-review:bringup" in done.stdout assert not Path(env["LOG"]).exists() # nothing run