"""`--version` takes only the tag formats netOrk's CI publishes (NetOrk/netork#55, #208). Every branch build is a `feature-` tag, with `/` turned into `-`: `feature/x` and `feat/x` become `feature-x`, `fix/x` becomes `feature-fix-x`. The registry keeps `feature-*` tags for 14 days and deletes a tag of any other form within a day, so a bare `fix-x` is refused like any other unknown form. Anything else is refused before a host is touched, so a typo cannot pull some ancient leftover tag. The fake `ssh` only notes that it was reached and fails, which ends the deploy right after the validation. """ from __future__ import annotations import os import subprocess from pathlib import Path import pytest DEPLOY = Path(__file__).resolve().parent.parent / "deploy.sh" def _deploy(tmp_path: Path, version: str) -> tuple[subprocess.CompletedProcess[str], bool]: bin_dir = tmp_path / "bin" bin_dir.mkdir() marker = tmp_path / "ssh-was-called" fake = bin_dir / "ssh" fake.write_text(f"#!/bin/sh\ntouch {marker}\nexit 255\n") fake.chmod(0o755) result = subprocess.run( ["bash", str(DEPLOY), f"--version={version}", "testhost"], env={ "PATH": f"{bin_dir}:{os.environ['PATH']}", "HOME": str(tmp_path), "DEPLOY_ENV_FILE": "/dev/null", "REGISTRY_HOST": "registry.example", }, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=30, check=False, ) return result, marker.exists() @pytest.mark.parametrize( "version", [ "latest", "latest-dev", "main-1a2b3c4", "feature-container-engine-api", "feature-fix-a-hook-below-the-early-returns", "feature-fix-ui-warning-icons", "0.37.0", ], ) def test_a_published_tag_is_accepted(tmp_path: Path, version: str) -> None: result, reached_ssh = _deploy(tmp_path, version) assert "Refusing to deploy version" not in result.stderr assert reached_ssh @pytest.mark.parametrize( "version", [ "bogus", "fix-a-hook", "fix/a-hook", "feature-", "feature-fix/ui/warning-icons", "feature-fix-a;id", "feature-$(id)", "main-xyz1234", ], ) def test_anything_else_is_refused_before_a_host_is_touched(tmp_path: Path, version: str) -> None: result, reached_ssh = _deploy(tmp_path, version) assert result.returncode != 0 assert f"Refusing to deploy version '{version}'" in result.stderr assert not reached_ssh def test_the_refusal_names_branch_builds(tmp_path: Path) -> None: result, _ = _deploy(tmp_path, "bogus") assert "feature- (also for fix/* branches)" in result.stderr