Skip to content

Commit cdf5406

Browse files
committed
Write unit tests for _save_job_dir and _get_job_dirs
Signed-off-by: Rahim Dharssi <[email protected]>
1 parent 4045098 commit cdf5406

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

test/run/torchx_backend/schedulers/test_skypilot_jobs.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import json
2+
import os
13
import tempfile
4+
from pathlib import Path
25
from unittest import mock
36

47
import pytest
@@ -8,6 +11,8 @@
811
from nemo_run.core.execution.skypilot_jobs import SkypilotJobsExecutor
912
from nemo_run.run.torchx_backend.schedulers.skypilot_jobs import (
1013
SkypilotJobsScheduler,
14+
_get_job_dirs,
15+
_save_job_dir,
1116
create_scheduler,
1217
)
1318

@@ -142,3 +147,88 @@ def test_describe_with_past_jobs(skypilot_jobs_scheduler):
142147
from torchx.specs import AppState
143148

144149
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

Comments
 (0)