|
| 1 | +import json |
| 2 | +import os |
1 | 3 | import tempfile |
| 4 | +from pathlib import Path |
2 | 5 | from unittest import mock |
3 | 6 |
|
4 | 7 | import pytest |
|
8 | 11 | from nemo_run.core.execution.skypilot_jobs import SkypilotJobsExecutor |
9 | 12 | from nemo_run.run.torchx_backend.schedulers.skypilot_jobs import ( |
10 | 13 | SkypilotJobsScheduler, |
| 14 | + _get_job_dirs, |
| 15 | + _save_job_dir, |
11 | 16 | create_scheduler, |
12 | 17 | ) |
13 | 18 |
|
@@ -142,3 +147,88 @@ def test_describe_with_past_jobs(skypilot_jobs_scheduler): |
142 | 147 | from torchx.specs import AppState |
143 | 148 |
|
144 | 149 | assert result.state == AppState.SUCCEEDED |
| 150 | + |
| 151 | + |
| 152 | +def test_save_job_dir_new_file(): |
| 153 | + """Test _save_job_dir when the job file doesn't exist.""" |
| 154 | + with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".json") as f: |
| 155 | + temp_path = f.name |
| 156 | + os.unlink(temp_path) # Remove file to test creation |
| 157 | + |
| 158 | + try: |
| 159 | + with mock.patch( |
| 160 | + "nemo_run.run.torchx_backend.schedulers.skypilot_jobs.SKYPILOT_JOB_DIRS", |
| 161 | + temp_path |
| 162 | + ): |
| 163 | + _save_job_dir("test_app_id", "RUNNING") |
| 164 | + |
| 165 | + # Verify the file was created and contains expected data |
| 166 | + assert os.path.exists(temp_path) |
| 167 | + with open(temp_path, "r") as f: |
| 168 | + data = json.load(f) |
| 169 | + |
| 170 | + assert "test_app_id" in data |
| 171 | + assert data["test_app_id"]["job_status"] == "RUNNING" |
| 172 | + finally: |
| 173 | + if os.path.exists(temp_path): |
| 174 | + os.unlink(temp_path) |
| 175 | + |
| 176 | + |
| 177 | +def test_save_job_dir_existing_file(): |
| 178 | + """Test _save_job_dir when the job file already exists with data.""" |
| 179 | + with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".json") as f: |
| 180 | + temp_path = f.name |
| 181 | + json.dump({"existing_app": {"job_status": "SUCCEEDED"}}, f) |
| 182 | + |
| 183 | + try: |
| 184 | + with mock.patch( |
| 185 | + "nemo_run.run.torchx_backend.schedulers.skypilot_jobs.SKYPILOT_JOB_DIRS", |
| 186 | + temp_path |
| 187 | + ): |
| 188 | + _save_job_dir("new_app_id", "PENDING") |
| 189 | + |
| 190 | + # Verify both old and new data exist |
| 191 | + with open(temp_path, "r") as f: |
| 192 | + data = json.load(f) |
| 193 | + |
| 194 | + assert "existing_app" in data |
| 195 | + assert data["existing_app"]["job_status"] == "SUCCEEDED" |
| 196 | + assert "new_app_id" in data |
| 197 | + assert data["new_app_id"]["job_status"] == "PENDING" |
| 198 | + finally: |
| 199 | + if os.path.exists(temp_path): |
| 200 | + os.unlink(temp_path) |
| 201 | + |
| 202 | + |
| 203 | +def test_get_job_dirs_existing_file(): |
| 204 | + """Test _get_job_dirs with an existing file containing data.""" |
| 205 | + test_data = { |
| 206 | + "app1": {"job_status": "RUNNING"}, |
| 207 | + "app2": {"job_status": "SUCCEEDED"}, |
| 208 | + } |
| 209 | + with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".json") as f: |
| 210 | + temp_path = f.name |
| 211 | + json.dump(test_data, f) |
| 212 | + |
| 213 | + try: |
| 214 | + with mock.patch( |
| 215 | + "nemo_run.run.torchx_backend.schedulers.skypilot_jobs.SKYPILOT_JOB_DIRS", |
| 216 | + temp_path |
| 217 | + ): |
| 218 | + result = _get_job_dirs() |
| 219 | + assert result == test_data |
| 220 | + finally: |
| 221 | + if os.path.exists(temp_path): |
| 222 | + os.unlink(temp_path) |
| 223 | + |
| 224 | + |
| 225 | +def test_get_job_dirs_file_not_found(): |
| 226 | + """Test _get_job_dirs when the file doesn't exist.""" |
| 227 | + non_existent_path = "/tmp/definitely_does_not_exist_12345.json" |
| 228 | + |
| 229 | + with mock.patch( |
| 230 | + "nemo_run.run.torchx_backend.schedulers.skypilot_jobs.SKYPILOT_JOB_DIRS", |
| 231 | + non_existent_path |
| 232 | + ): |
| 233 | + result = _get_job_dirs() |
| 234 | + assert result == {} |
0 commit comments