|
| 1 | +import pytest |
| 2 | +from unittest.mock import patch, AsyncMock, MagicMock |
| 3 | +from io import BytesIO |
| 4 | + |
| 5 | + |
| 6 | +# Use the client fixture from conftest.py - no need to create our own |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def gpu_orchestration_env_vars(monkeypatch): |
| 11 | + """Set up GPU orchestration environment variables for testing""" |
| 12 | + monkeypatch.setenv("GPU_ORCHESTRATION_SERVER", "http://test-orchestrator.example.com") |
| 13 | + monkeypatch.setenv("GPU_ORCHESTRATION_SERVER_PORT", "8080") |
| 14 | + yield |
| 15 | + # Cleanup - remove env vars after test |
| 16 | + monkeypatch.delenv("GPU_ORCHESTRATION_SERVER", raising=False) |
| 17 | + monkeypatch.delenv("GPU_ORCHESTRATION_SERVER_PORT", raising=False) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def no_gpu_orchestration_env(monkeypatch): |
| 22 | + """Ensure GPU orchestration environment variables are not set""" |
| 23 | + monkeypatch.delenv("GPU_ORCHESTRATION_SERVER", raising=False) |
| 24 | + monkeypatch.delenv("GPU_ORCHESTRATION_SERVER_PORT", raising=False) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def mock_experiment_id(client): |
| 29 | + """Create a test experiment and return its ID, cleaning up after the test""" |
| 30 | + import os |
| 31 | + import time |
| 32 | + import uuid |
| 33 | + from transformerlab.services import experiment_service |
| 34 | + |
| 35 | + # Use a unique name to avoid conflicts - add UUID for better uniqueness |
| 36 | + unique_name = f"test_exp_remote_{os.getpid()}_{int(time.time())}_{uuid.uuid4().hex[:8]}" |
| 37 | + |
| 38 | + # Check if experiment already exists and delete it if it does |
| 39 | + existing = experiment_service.experiment_get(unique_name) |
| 40 | + if existing: |
| 41 | + experiment_service.experiment_delete(unique_name) |
| 42 | + |
| 43 | + # Create the experiment |
| 44 | + exp_id = experiment_service.experiment_create(unique_name, {}) |
| 45 | + |
| 46 | + yield exp_id |
| 47 | + |
| 48 | + # Cleanup: delete all jobs in the experiment, then delete the experiment |
| 49 | + try: |
| 50 | + from transformerlab.services import job_service |
| 51 | + job_service.job_delete_all(exp_id) |
| 52 | + except Exception: |
| 53 | + pass |
| 54 | + |
| 55 | + try: |
| 56 | + experiment_service.experiment_delete(exp_id) |
| 57 | + except Exception: |
| 58 | + # Ignore errors during cleanup |
| 59 | + pass |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture |
| 63 | +def job_cleanup(): |
| 64 | + """Fixture to track and cleanup jobs created during tests""" |
| 65 | + created_jobs = [] # List of (job_id, experiment_id) tuples |
| 66 | + |
| 67 | + yield created_jobs |
| 68 | + |
| 69 | + # Cleanup: delete all tracked jobs |
| 70 | + from transformerlab.services import job_service |
| 71 | + for job_id, experiment_id in created_jobs: |
| 72 | + try: |
| 73 | + job_service.job_delete(job_id, experiment_id) |
| 74 | + except Exception: |
| 75 | + # Ignore errors during cleanup |
| 76 | + pass |
| 77 | + |
| 78 | + |
| 79 | +class TestValidateGPUOrchestratorEnvVars: |
| 80 | + """Test the validate_gpu_orchestrator_env_vars function""" |
| 81 | + |
| 82 | + def test_validate_env_vars_with_both_set(self, gpu_orchestration_env_vars): |
| 83 | + """Test validation when both env vars are set""" |
| 84 | + from transformerlab.routers.remote import validate_gpu_orchestrator_env_vars |
| 85 | + |
| 86 | + url, port = validate_gpu_orchestrator_env_vars() |
| 87 | + assert url == "http://test-orchestrator.example.com" |
| 88 | + assert port == "8080" |
| 89 | + |
| 90 | + def test_validate_env_vars_missing_url(self, monkeypatch): |
| 91 | + """Test validation when GPU_ORCHESTRATION_SERVER is missing""" |
| 92 | + monkeypatch.delenv("GPU_ORCHESTRATION_SERVER", raising=False) |
| 93 | + monkeypatch.setenv("GPU_ORCHESTRATION_SERVER_PORT", "8080") |
| 94 | + |
| 95 | + from transformerlab.routers.remote import validate_gpu_orchestrator_env_vars |
| 96 | + |
| 97 | + result = validate_gpu_orchestrator_env_vars() |
| 98 | + url, error_response = result |
| 99 | + assert url is None |
| 100 | + assert isinstance(error_response, dict) |
| 101 | + assert error_response["status"] == "error" |
| 102 | + assert "GPU_ORCHESTRATION_SERVER" in error_response["message"] |
| 103 | + |
| 104 | + def test_validate_env_vars_missing_port(self, monkeypatch): |
| 105 | + """Test validation when GPU_ORCHESTRATION_SERVER_PORT is missing""" |
| 106 | + monkeypatch.setenv("GPU_ORCHESTRATION_SERVER", "http://test-orchestrator.example.com") |
| 107 | + monkeypatch.delenv("GPU_ORCHESTRATION_SERVER_PORT", raising=False) |
| 108 | + |
| 109 | + from transformerlab.routers.remote import validate_gpu_orchestrator_env_vars |
| 110 | + |
| 111 | + result = validate_gpu_orchestrator_env_vars() |
| 112 | + url, error_response = result |
| 113 | + # When port is missing, the function returns None as the URL |
| 114 | + assert url is None |
| 115 | + assert isinstance(error_response, dict) |
| 116 | + assert error_response["status"] == "error" |
| 117 | + assert "GPU_ORCHESTRATION_SERVER_PORT" in error_response["message"] |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +class TestStopRemote: |
| 123 | + """Test the /remote/stop endpoint""" |
| 124 | + |
| 125 | + |
| 126 | + def test_stop_remote_missing_env_vars(self, client, no_gpu_orchestration_env): |
| 127 | + """Test stopping when GPU orchestration env vars are not set""" |
| 128 | + response = client.post( |
| 129 | + "/remote/stop", |
| 130 | + data={ |
| 131 | + "job_id": "test-job-id", |
| 132 | + "cluster_name": "test-cluster", |
| 133 | + }, |
| 134 | + ) |
| 135 | + assert response.status_code == 200 |
| 136 | + data = response.json() |
| 137 | + assert data["status"] == "error" |
| 138 | + assert "GPU_ORCHESTRATION_SERVER" in data["message"] |
| 139 | + |
| 140 | + |
| 141 | +class TestUploadDirectory: |
| 142 | + """Test the /remote/upload endpoint""" |
| 143 | + |
| 144 | + @patch("transformerlab.routers.remote.httpx.AsyncClient") |
| 145 | + def test_upload_directory_success(self, mock_client_class, client, gpu_orchestration_env_vars): |
| 146 | + """Test uploading a directory successfully""" |
| 147 | + # Create test files using BytesIO for proper file-like objects |
| 148 | + files = [ |
| 149 | + ("dir_files", ("test1.txt", BytesIO(b"content1"), "text/plain")), |
| 150 | + ("dir_files", ("test2.txt", BytesIO(b"content2"), "text/plain")), |
| 151 | + ] |
| 152 | + |
| 153 | + # Mock the async client and response |
| 154 | + mock_response = MagicMock() |
| 155 | + mock_response.status_code = 200 |
| 156 | + mock_response.json.return_value = { |
| 157 | + "status": "uploaded", |
| 158 | + "upload_path": "/remote/path", |
| 159 | + } |
| 160 | + |
| 161 | + mock_httpx_client = AsyncMock() |
| 162 | + mock_httpx_client.post = AsyncMock(return_value=mock_response) |
| 163 | + mock_httpx_client.__aenter__.return_value = mock_httpx_client |
| 164 | + mock_httpx_client.__aexit__.return_value = None |
| 165 | + mock_client_class.return_value = mock_httpx_client |
| 166 | + |
| 167 | + response = client.post( |
| 168 | + "/remote/upload", |
| 169 | + files=files, |
| 170 | + data={"dir_name": "test-dir"}, |
| 171 | + ) |
| 172 | + assert response.status_code == 200 |
| 173 | + data = response.json() |
| 174 | + assert data["status"] == "success" |
| 175 | + assert "local_storage_path" in data |
| 176 | + |
| 177 | + def test_upload_directory_missing_env_vars(self, client, no_gpu_orchestration_env): |
| 178 | + """Test uploading when GPU orchestration env vars are not set""" |
| 179 | + files = [("dir_files", ("test.txt", BytesIO(b"content"), "text/plain"))] |
| 180 | + |
| 181 | + response = client.post( |
| 182 | + "/remote/upload", |
| 183 | + files=files, |
| 184 | + ) |
| 185 | + assert response.status_code == 200 |
| 186 | + data = response.json() |
| 187 | + assert data["status"] == "error" |
| 188 | + assert "GPU_ORCHESTRATION_SERVER" in data["message"] |
| 189 | + |
| 190 | + |
| 191 | +class TestCheckRemoteJobStatus: |
| 192 | + """Test the /remote/check-status endpoint""" |
| 193 | + |
| 194 | + @patch("transformerlab.routers.remote.httpx.AsyncClient") |
| 195 | + def test_check_status_no_launching_jobs(self, mock_client_class, client, gpu_orchestration_env_vars): |
| 196 | + """Test checking status when there are no LAUNCHING jobs""" |
| 197 | + response = client.get("/remote/check-status") |
| 198 | + assert response.status_code == 200 |
| 199 | + data = response.json() |
| 200 | + assert "updated_jobs" in data |
| 201 | + assert data["updated_jobs"] == [] |
| 202 | + |
| 203 | + |
| 204 | +class TestGetOrchestratorLogs: |
| 205 | + """Test the /remote/logs/{request_id} endpoint""" |
| 206 | + |
| 207 | + @patch("transformerlab.routers.remote.httpx.AsyncClient") |
| 208 | + def test_get_logs_success(self, mock_client_class, client, gpu_orchestration_env_vars): |
| 209 | + """Test getting logs successfully""" |
| 210 | + request_id = "test-request-123" |
| 211 | + |
| 212 | + # Mock streaming response |
| 213 | + mock_stream_response = MagicMock() |
| 214 | + mock_stream_response.status_code = 200 |
| 215 | + mock_stream_response.aiter_bytes = AsyncMock(return_value=iter([b"log line 1\n", b"log line 2\n"])) |
| 216 | + |
| 217 | + mock_httpx_client = AsyncMock() |
| 218 | + mock_httpx_client.stream = AsyncMock(return_value=mock_stream_response) |
| 219 | + mock_httpx_client.__aenter__.return_value = mock_httpx_client |
| 220 | + mock_httpx_client.__aexit__.return_value = None |
| 221 | + mock_client_class.return_value = mock_httpx_client |
| 222 | + |
| 223 | + response = client.get(f"/remote/logs/{request_id}") |
| 224 | + # Streaming responses return 200 with appropriate headers |
| 225 | + assert response.status_code == 200 |
| 226 | + |
| 227 | + def test_get_logs_missing_env_vars(self, client, no_gpu_orchestration_env): |
| 228 | + """Test getting logs when GPU orchestration env vars are not set""" |
| 229 | + response = client.get("/remote/logs/test-request-123") |
| 230 | + # The endpoint should return an error response |
| 231 | + assert response.status_code in [200, 500] # May return error as JSON or raise exception |
| 232 | + |
0 commit comments