Skip to content

Commit 85658d5

Browse files
committed
more unit tests for br
1 parent 5b3e801 commit 85658d5

File tree

1 file changed

+103
-1
lines changed

1 file changed

+103
-1
lines changed

tests/test_br.py

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import argparse
66

77
import batchtools.build_yaml
8-
from batchtools.br import CreateJobCommand
8+
9+
from batchtools.br import CreateJobCommand, get_pod_status, log_job_output
910
from tests.helpers import DictToObject
1011

1112

@@ -124,3 +125,104 @@ def test_create_job_nowait(
124125
CreateJobCommand.run(args)
125126

126127
assert mock_create.call_args.args[0] == expected
128+
129+
130+
@mock.patch("openshift_client.create", name="create")
131+
@mock.patch("openshift_client.selector", name="selector")
132+
@mock.patch("socket.gethostname", name="gethostname")
133+
@mock.patch("os.getcwd", name="getcwd")
134+
def test_create_job_raises_from_oc_create(
135+
mock_getcwd,
136+
mock_gethostname,
137+
mock_selector,
138+
mock_create,
139+
parser,
140+
subparsers,
141+
tmp_path,
142+
):
143+
"""OpenShiftPythonException from oc.create should exit with a helpful message."""
144+
CreateJobCommand.build_parser(subparsers)
145+
args = parser.parse_args(["br"])
146+
args.wait = False
147+
args.job_id = "boom"
148+
args.gpu = "v100"
149+
args.image = "img"
150+
args.command = ["true"]
151+
152+
mock_getcwd.return_value = str(tmp_path)
153+
mock_gethostname.return_value = "devpod"
154+
155+
devpod = DictToObject(
156+
{
157+
"model": {
158+
"metadata": {"name": "devpod"},
159+
"spec": {"containers": [{"name": "c"}]},
160+
}
161+
}
162+
)
163+
mock_selector.return_value = mock.Mock(**{"object.return_value": devpod})
164+
165+
import openshift_client as oc
166+
167+
mock_create.side_effect = oc.OpenShiftPythonException("kaboom")
168+
169+
with pytest.raises(SystemExit) as err:
170+
CreateJobCommand.run(args)
171+
assert "Error occurred while creating job: kaboom" in str(err.value)
172+
173+
174+
@mock.patch("openshift_client.selector", name="selector")
175+
def test_get_pod_status_running(mock_selector):
176+
pod = DictToObject({"model": {"status": {"phase": "Running"}}})
177+
mock_selector.return_value = mock.Mock(**{"object.return_value": pod})
178+
assert get_pod_status("mypod") == "Running"
179+
180+
181+
@mock.patch("batchtools.br.oc_delete")
182+
@mock.patch("batchtools.br.pretty_print", return_value="LOGS")
183+
@mock.patch("batchtools.br.get_pod_status")
184+
@mock.patch("openshift_client.selector", name="selector")
185+
def test_log_job_output_success(
186+
mock_selector, mock_get_pod_status, mock_pretty_print, mock_oc_delete, capsys
187+
):
188+
# pod list for job
189+
pod = DictToObject({"model": {"metadata": {"name": "pod-1"}}})
190+
mock_selector.return_value = mock.Mock(**{"objects.return_value": [pod]})
191+
192+
# simulate states until success
193+
mock_get_pod_status.side_effect = ["Pending", "Running", "Succeeded"]
194+
195+
# avoid real sleep
196+
with mock.patch("time.sleep", return_value=None):
197+
log_job_output("job-abc", wait=True, timeout=30)
198+
199+
out = capsys.readouterr().out
200+
assert "finished with phase=Succeeded" in out
201+
assert "LOGS" in out
202+
mock_oc_delete.assert_not_called()
203+
204+
205+
@mock.patch("batchtools.br.oc_delete")
206+
@mock.patch("batchtools.br.get_pod_status", return_value="Running")
207+
@mock.patch("openshift_client.selector", name="selector")
208+
def test_log_job_output_timeout_deletes_job(
209+
mock_selector, mock_get_pod_status, mock_oc_delete, capsys
210+
):
211+
pod = DictToObject({"model": {"metadata": {"name": "pod-timeout"}}})
212+
mock_selector.return_value = mock.Mock(**{"objects.return_value": [pod]})
213+
214+
# make time.monotonic jump past the timeout quickly
215+
times = [0.0, 2.0]
216+
217+
def fake_monotonic():
218+
return times.pop(0) if times else 2.0
219+
220+
with (
221+
mock.patch("time.monotonic", side_effect=fake_monotonic),
222+
mock.patch("time.sleep", return_value=None),
223+
):
224+
log_job_output("job-timeout", wait=True, timeout=1)
225+
226+
out = capsys.readouterr().out
227+
assert "Timeout waiting for pod pod-timeout to complete" in out
228+
mock_oc_delete.assert_called_once_with("job", "job-timeout")

0 commit comments

Comments
 (0)