|
| 1 | +"""The supervisor canary boots the image with an empty /data: role dispatch loads on first use.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | +from types import SimpleNamespace |
| 9 | + |
| 10 | +import pytest |
| 11 | +from fastapi.testclient import TestClient |
| 12 | + |
| 13 | +from hypertrain.challenge import app as app_module |
| 14 | +from hypertrain.challenge.store import ChallengeError |
| 15 | +from hypertrain.gpu_ops import role_dispatch |
| 16 | + |
| 17 | +from .conftest import OWNER, make_config |
| 18 | + |
| 19 | +TREE = Path(__file__).parents[2] |
| 20 | + |
| 21 | + |
| 22 | +def _app(tmp_path: Path, dispatch: Path) -> app_module.FastAPI: |
| 23 | + # Canary: empty state dir, no secret files at all. |
| 24 | + config = make_config(tmp_path / "state", tmp_path / "no-secrets") |
| 25 | + config.role_dispatch_file = dispatch |
| 26 | + return app_module.create_app(config) |
| 27 | + |
| 28 | + |
| 29 | +def _write_valid(path: Path, tmp_path: Path) -> None: |
| 30 | + path.write_text( |
| 31 | + json.dumps( |
| 32 | + { |
| 33 | + "tree": str(TREE), |
| 34 | + "run_dir": str(tmp_path / "lifecycle"), |
| 35 | + "owner_key_file": str(tmp_path / "owner"), |
| 36 | + "roles": {OWNER.ss58: "h1"}, |
| 37 | + "run_id": "a" * 64, |
| 38 | + } |
| 39 | + ) |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def _foreign(launch: role_dispatch.RoleLaunch, tmp_path: Path) -> None: |
| 44 | + # Foreign run: the real factory refuses before any I/O, proving the file was loaded. |
| 45 | + with pytest.raises(RuntimeError, match="not the authorized run"): |
| 46 | + launch("audit", SimpleNamespace(run_id="b" * 64), tmp_path, {}) # type: ignore[arg-type] |
| 47 | + |
| 48 | + |
| 49 | +def test_boots_and_answers_version_without_dispatch_file(tmp_path: Path) -> None: |
| 50 | + with TestClient(_app(tmp_path, tmp_path / "missing" / "dispatch.json")) as client: |
| 51 | + response = client.get("/version") |
| 52 | + assert response.status_code == 200 |
| 53 | + assert response.json()["slug"] == "hypertrain" |
| 54 | + |
| 55 | + |
| 56 | +def test_missing_then_fixed_file_without_restart(tmp_path: Path) -> None: |
| 57 | + dispatch = tmp_path / "dispatch.json" |
| 58 | + launch = _app(tmp_path, dispatch).state.store._role_launch |
| 59 | + assert launch is not None |
| 60 | + for broken in (None, "{not json", json.dumps({"tree": str(TREE), "extra": 1})): |
| 61 | + if broken is not None: |
| 62 | + dispatch.write_text(broken) |
| 63 | + with pytest.raises(ChallengeError) as error: |
| 64 | + launch("audit", SimpleNamespace(run_id="a" * 64), tmp_path, {}) |
| 65 | + assert (error.value.status, error.value.detail) == (503, "ROLE_DISPATCH_UNAVAILABLE") |
| 66 | + _write_valid(dispatch, tmp_path) |
| 67 | + _foreign(launch, tmp_path) |
| 68 | + |
| 69 | + |
| 70 | +def test_valid_file_loaded_once_same_behaviour( |
| 71 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 72 | +) -> None: |
| 73 | + dispatch = tmp_path / "dispatch.json" |
| 74 | + _write_valid(dispatch, tmp_path) |
| 75 | + calls: list[Path] = [] |
| 76 | + real = role_dispatch.load |
| 77 | + |
| 78 | + def spy(path: Path) -> role_dispatch.RoleLaunch: |
| 79 | + calls.append(path) |
| 80 | + return real(path) |
| 81 | + |
| 82 | + monkeypatch.setattr(role_dispatch, "load", spy) |
| 83 | + monkeypatch.setattr(sys, "path", [p for p in sys.path if p != str(TREE / "scripts")]) |
| 84 | + launch = _app(tmp_path, dispatch).state.store._role_launch |
| 85 | + assert launch is not None |
| 86 | + _foreign(launch, tmp_path) |
| 87 | + assert sys.path[0] == str(TREE / "scripts") |
| 88 | + _foreign(launch, tmp_path) |
| 89 | + _foreign(real(dispatch), tmp_path) # same refusal as the eager loader |
| 90 | + assert calls == [dispatch] |