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()