File size: 9,945 Bytes
1ef1de7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | from __future__ import annotations
from pathlib import Path
import pytest
from ocrtech.errors import RemoteAuditError
from ocrtech.remote import audit_remote_host, bootstrap_remote_host, sync_remote_workspace
def test_audit_remote_host_passes_with_sufficient_probe(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
"ocrtech.remote._probe_remote",
lambda *args, **kwargs: {
"python_version": "3.11.9",
"python_version_info": [3, 11, 9],
"system": "Linux",
"machine": "x86_64",
"platform": "Linux-6.8.0-x86_64",
"cwd": "/home/k2/ocr-tech",
"free_bytes_cwd": 40 * 1024**3,
"free_bytes_home": 40 * 1024**3,
"commands": {"python3": "/usr/bin/python3", "git": "/usr/bin/git", "uv": None, "nvidia-smi": None},
"python_candidates": {
"python3.11": {"candidate": "python3.11", "path": "/usr/local/bin/python3.11", "version": "3.11.9", "version_info": [3, 11, 9]},
"python3": {"candidate": "python3", "path": "/usr/bin/python3", "version": "3.9.6", "version_info": [3, 9, 6]},
},
"paths": {"~/ocr-tech": {"expanded": "/home/k2/ocr-tech", "exists": True, "is_dir": True, "is_file": False}},
},
)
report = audit_remote_host(
"100.97.238.103",
tmp_path / "audit",
user="k2",
workdir="~/ocr-tech",
require_paths=["~/ocr-tech"],
)
assert report.passed
assert any(check.name == "gpu" and check.status == "warn" for check in report.checks)
assert (tmp_path / "audit" / "remote-audit.json").exists()
def test_audit_remote_host_prefers_newer_python_candidate(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
"ocrtech.remote._probe_remote",
lambda *args, **kwargs: {
"python_version": "3.9.6",
"python_version_info": [3, 9, 6],
"system": "Linux",
"machine": "x86_64",
"platform": "Linux-6.8.0-x86_64",
"cwd": "/home/k2",
"free_bytes_cwd": 40 * 1024**3,
"free_bytes_home": 40 * 1024**3,
"commands": {"git": "/usr/bin/git", "uv": "/Users/k2/.local/bin/uv", "nvidia-smi": None},
"python_candidates": {
"python3.11": {"candidate": "python3.11", "path": "/Users/k2/.local/bin/python3.11", "version": "3.11.14", "version_info": [3, 11, 14]},
"python3": {"candidate": "python3", "path": "/usr/bin/python3", "version": "3.9.6", "version_info": [3, 9, 6]},
},
"paths": {},
},
)
report = audit_remote_host("100.97.238.103", tmp_path / "audit", user="k2")
assert report.passed
python_check = next(check for check in report.checks if check.name == "python")
assert "python3.11" in python_check.summary
def test_audit_remote_host_fails_on_python_and_disk(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
"ocrtech.remote._probe_remote",
lambda *args, **kwargs: {
"python_version": "3.9.6",
"python_version_info": [3, 9, 6],
"system": "Linux",
"machine": "x86_64",
"platform": "Linux-6.8.0-x86_64",
"cwd": "/home/k2/ocr-tech",
"free_bytes_cwd": int(0.8 * 1024**3),
"free_bytes_home": int(0.8 * 1024**3),
"commands": {"python3": "/usr/bin/python3", "git": "/usr/bin/git", "uv": None, "nvidia-smi": None},
"python_candidates": {
"python3": {"candidate": "python3", "path": "/usr/bin/python3", "version": "3.9.6", "version_info": [3, 9, 6]},
},
"paths": {},
},
)
report = audit_remote_host("100.97.238.103", tmp_path / "audit", user="k2")
assert not report.passed
statuses = {check.name: check.status for check in report.checks}
assert statuses["python"] == "fail"
assert statuses["disk"] == "fail"
def test_audit_remote_host_requires_password_env_when_requested(tmp_path: Path) -> None:
with pytest.raises(RemoteAuditError):
audit_remote_host("100.97.238.103", tmp_path / "audit", user="k2", password_env="OCRTECH_REMOTE_PASSWORD")
def test_audit_remote_host_can_fail_on_unsupported_paddle_training_platform(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
"ocrtech.remote._probe_remote",
lambda *args, **kwargs: {
"python_version": "3.11.14",
"python_version_info": [3, 11, 14],
"system": "Darwin",
"machine": "arm64",
"platform": "macOS-14.6-arm64-arm-64bit",
"cwd": "/Users/k2/ocr-tech",
"free_bytes_cwd": 40 * 1024**3,
"free_bytes_home": 40 * 1024**3,
"commands": {"git": "/usr/bin/git", "uv": "/Users/k2/.local/bin/uv", "nvidia-smi": None},
"python_candidates": {
"python3.11": {"candidate": "python3.11", "path": "/Users/k2/.local/bin/python3.11", "version": "3.11.14", "version_info": [3, 11, 14]},
},
"paths": {},
},
)
report = audit_remote_host("100.97.238.103", tmp_path / "audit", user="k2", require_paddle_training=True)
assert not report.passed
platform_check = next(check for check in report.checks if check.name == "platform")
assert platform_check.status == "fail"
assert "arm64" in platform_check.summary
def test_bootstrap_remote_host_writes_report(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
"ocrtech.remote._probe_remote",
lambda *args, **kwargs: {
"python_version": "3.9.6",
"python_version_info": [3, 9, 6],
"system": "Darwin",
"machine": "arm64",
"platform": "macOS-14.6-arm64-arm-64bit",
"cwd": "/Users/k2",
"free_bytes_cwd": 40 * 1024**3,
"free_bytes_home": 40 * 1024**3,
"commands": {"git": "/usr/bin/git", "uv": "/Users/k2/.local/bin/uv", "nvidia-smi": None},
"python_candidates": {
"python3.11": {"candidate": "python3.11", "path": "/Users/k2/.local/bin/python3.11", "version": "3.11.14", "version_info": [3, 11, 14]},
},
"paths": {},
},
)
monkeypatch.setattr(
"ocrtech.remote._run_remote_json_script",
lambda *args, **kwargs: {
"workdir": "/Users/k2/ocr-tech",
"workdir_existed": False,
"venv_dir": "/Users/k2/ocr-tech/.venv",
"venv_existed": False,
"venv_recreated": False,
"python_command": "/Users/k2/.local/bin/python3.11",
"python_bin": "/Users/k2/ocr-tech/.venv/bin/python",
"pip_bin": "/Users/k2/ocr-tech/.venv/bin/pip",
},
)
report = bootstrap_remote_host("100.97.238.103", tmp_path / "bootstrap", user="k2", workdir="~/ocr-tech")
assert report.passed
assert report.python_command.endswith("python3.11")
assert (tmp_path / "bootstrap" / "remote-bootstrap.json").exists()
def test_bootstrap_remote_host_can_recreate_venv(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
"ocrtech.remote._probe_remote",
lambda *args, **kwargs: {
"python_version": "3.9.6",
"python_version_info": [3, 9, 6],
"system": "Darwin",
"machine": "arm64",
"platform": "macOS-14.6-arm64-arm-64bit",
"cwd": "/Users/k2",
"free_bytes_cwd": 40 * 1024**3,
"free_bytes_home": 40 * 1024**3,
"commands": {"git": "/usr/bin/git", "uv": "/Users/k2/.local/bin/uv", "nvidia-smi": None},
"python_candidates": {
"~/.local/bin/python3.11": {"candidate": "~/.local/bin/python3.11", "path": "/Users/k2/.local/bin/python3.11", "version": "3.11.14", "version_info": [3, 11, 14]},
},
"paths": {},
},
)
captured: dict[str, object] = {}
def fake_run_remote_json_script(*args, **kwargs):
captured["script"] = kwargs["script"]
return {
"workdir": "/Users/k2/ocr-tech",
"workdir_existed": True,
"venv_dir": "/Users/k2/ocr-tech/.venv",
"venv_existed": False,
"venv_recreated": True,
"python_command": "/Users/k2/.local/bin/python3.11",
"python_bin": "/Users/k2/ocr-tech/.venv/bin/python",
"pip_bin": "/Users/k2/ocr-tech/.venv/bin/pip",
}
monkeypatch.setattr("ocrtech.remote._run_remote_json_script", fake_run_remote_json_script)
report = bootstrap_remote_host("100.97.238.103", tmp_path / "bootstrap", user="k2", workdir="~/ocr-tech", recreate_venv=True)
assert report.passed
assert report.actions["venv_recreated"] is True
assert "shutil.rmtree" in str(captured["script"])
def test_sync_remote_workspace_writes_report(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
local_root = tmp_path / "workspace"
local_root.mkdir()
(local_root / "README.md").write_text("hello\n", encoding="utf-8")
monkeypatch.setattr("ocrtech.remote._create_workspace_archive", lambda *args, **kwargs: None)
monkeypatch.setattr("ocrtech.remote._copy_file_to_remote", lambda *args, **kwargs: None)
payloads = iter([{"expanded_path": "/Users/k2/ocr-tech"}, {"workdir": "/Users/k2/ocr-tech"}])
monkeypatch.setattr("ocrtech.remote._run_remote_json_script", lambda *args, **kwargs: next(payloads))
report = sync_remote_workspace(local_root, tmp_path / "sync", host="100.97.238.103", user="k2", remote_workdir="~/ocr-tech")
assert report.passed
assert report.remote_workdir == "/Users/k2/ocr-tech"
assert (tmp_path / "sync" / "remote-sync.json").exists()
|