"""A host gets a tag only if the tag has an image for the host's platform (NetOrk/netork#934). From 0.34.0 on, netOrk's releases (`X.Y.Z`, `latest`) are published for linux/amd64 and linux/arm64, so a Raspberry Pi with a 64-bit OS can run netOrk. Everything else is amd64 only: `latest-dev`, `main-`, `feature-`, and every release before 0.34.0. On a Pi such a tag fails at the first `docker pull` with "no matching manifest for linux/arm64/v8 in the manifest list entries", which names neither the cause nor a tag that would work. So right after the registry login the script asks the host for its architecture, and the registry for the platforms of the engine (and, on UI servers, the ui) image, and refuses before anything on the host changes. When the registry says nothing it can read, the check stays out of the way and the pull decides, as before. The remote side is a fake `ssh` that records every command and answers the platform question from FAKE_PLATFORMS. """ from __future__ import annotations import os import subprocess from pathlib import Path DEPLOY = Path(__file__).resolve().parent.parent / "deploy.sh" _FAKE_SSH = """#!/bin/sh for arg in "$@"; do cmd="$arg"; done # The host lock's holder runs for real, here (test_deploy_host_lock.py). case "$cmd" in *.deploy.lock*) exec sh -c "$cmd" ;; esac cat > /dev/null printf '%s\\n----\\n' "$cmd" >> "{state}/commands" case "$cmd" in *"docker manifest inspect"*) cat "{state}/platforms" ;; esac exit 0 """ def _index(*architectures: str) -> str: """`docker manifest inspect` output for an OCI index, as docker prints it.""" entries = ",\n".join( " {\n" ' "mediaType": "application/vnd.oci.image.manifest.v1+json",\n' ' "platform": {\n' f' "architecture": "{arch}",\n' ' "os": "linux"\n' " }\n" " }" for arch in architectures ) return ( '{\n "schemaVersion": 2,\n' ' "mediaType": "application/vnd.oci.image.index.v1+json",\n' f' "manifests": [\n{entries}\n ]\n}}\n' ) AMD64_ONLY = _index("amd64", "unknown") BOTH = _index("amd64", "unknown", "arm64", "unknown") def _deploy( tmp_path: Path, answer: str, *, version: str = "0.34.0", ui: bool = False ) -> tuple[subprocess.CompletedProcess[str], list[str]]: bin_dir = tmp_path / "bin" bin_dir.mkdir() fake = bin_dir / "ssh" fake.write_text(_FAKE_SSH.replace("{state}", str(tmp_path))) fake.chmod(0o755) (tmp_path / "platforms").write_text(answer) env = { "PATH": f"{bin_dir}:{os.environ['PATH']}", "HOME": str(tmp_path), "DEPLOY_ENV_FILE": "/dev/null", "REGISTRY_HOST": "registry.example", "NETORK_VERSION": version, } if ui: env["UI_SERVER"] = "testhost" result = subprocess.run( ["bash", str(DEPLOY), "testhost"], env=env, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=60, check=False, ) commands = (tmp_path / "commands").read_text().split("\n----\n") return result, commands def _fetched_compose_files(commands: list[str]) -> bool: return any("/app/deploy/" in c for c in commands) def test_an_amd64_only_tag_is_refused_on_an_arm64_host(tmp_path: Path) -> None: answer = "arch=arm64\nrepo=engine\n" + AMD64_ONLY result, commands = _deploy(tmp_path, answer, version="latest-dev") assert result.returncode != 0 assert "latest-dev" in result.stderr assert "linux/arm64" in result.stderr assert "engine" in result.stderr # Names what would work instead. assert "0.34.0" in result.stderr assert not _fetched_compose_files(commands), "the host was touched before the refusal" def test_a_release_with_arm64_deploys_on_an_arm64_host(tmp_path: Path) -> None: answer = "arch=arm64\nrepo=engine\n" + BOTH result, commands = _deploy(tmp_path, answer) assert result.returncode == 0, result.stderr assert _fetched_compose_files(commands) def test_an_amd64_host_deploys_an_amd64_only_tag(tmp_path: Path) -> None: answer = "arch=amd64\nrepo=engine\n" + AMD64_ONLY result, _ = _deploy(tmp_path, answer, version="latest-dev") assert result.returncode == 0, result.stderr def test_the_ui_is_checked_on_a_ui_server(tmp_path: Path) -> None: answer = "arch=arm64\nrepo=engine\n" + BOTH + "repo=ui\n" + AMD64_ONLY result, commands = _deploy(tmp_path, answer, ui=True) assert result.returncode != 0 assert "ui" in result.stderr assert not _fetched_compose_files(commands) (check,) = [c for c in commands if "docker manifest inspect" in c] assert "for r in engine ui;" in check def test_the_ui_is_not_asked_for_on_other_servers(tmp_path: Path) -> None: answer = "arch=arm64\nrepo=engine\n" + BOTH _, commands = _deploy(tmp_path, answer) (check,) = [c for c in commands if "docker manifest inspect" in c] assert "for r in engine;" in check def test_the_check_comes_after_the_login_and_before_the_compose_files(tmp_path: Path) -> None: """The registry may be private: the host can only ask it once logged in.""" answer = "arch=arm64\nrepo=engine\n" + BOTH _, commands = _deploy(tmp_path, answer) login = next(i for i, c in enumerate(commands) if "docker login" in c) check = next(i for i, c in enumerate(commands) if "docker manifest inspect" in c) fetch = next(i for i, c in enumerate(commands) if "/app/deploy/" in c) assert login < check < fetch def test_a_registry_answer_it_cannot_read_leaves_it_to_the_pull(tmp_path: Path) -> None: """An older docker without `manifest`, a missing tag, a single-platform manifest: the check says nothing, and the pull reports what is wrong.""" for answer in ("", "arch=arm64\nrepo=engine\n", 'arch=arm64\nrepo=engine\n{"config": {}}\n'): case = tmp_path / str(len(answer)) case.mkdir() result, commands = _deploy(case, answer) assert result.returncode == 0, (answer, result.stderr) assert _fetched_compose_files(commands)