| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
from cylc.flow.job_runner_mgr import ( |
| 18 |
JobRunnerManager, JOB_FILES_REMOVED_MESSAGE) |
| 19 |
|
| 20 |
jrm = JobRunnerManager() |
| 21 |
|
| 22 |
|
| 23 |
SAMPLE_STATUS = """ |
| 24 |
ignore me, I have no = sign |
| 25 |
CYLC_JOB_RUNNER_NAME=pbs |
| 26 |
CYLC_JOB_ID=2361713 |
| 27 |
CYLC_JOB_RUNNER_SUBMIT_TIME=2025-01-28T14:46:04Z |
| 28 |
CYLC_JOB_PID=2361713 |
| 29 |
CYLC_JOB_INIT_TIME=2025-01-28T14:46:05Z |
| 30 |
CYLC_MESSAGE=2025-01-28T14:46:05Z|INFO|sleep 31 |
| 31 |
CYLC_JOB_RUNNER_EXIT_POLLED=2025-01-28T14:46:08Z |
| 32 |
CYLC_JOB_EXIT=SUCCEEDED |
| 33 |
CYLC_JOB_EXIT_TIME=2025-01-28T14:46:38Z |
| 34 |
""" |
| 35 |
|
| 36 |
|
| 37 |
def test__job_poll_status_files(tmp_path): |
| 38 |
"""Good Path: A valid job.status files exists""" |
| 39 |
(tmp_path / 'sub').mkdir() |
| 40 |
(tmp_path / 'sub' / 'job.status').write_text(SAMPLE_STATUS) |
| 41 |
ctx = jrm._jobs_poll_status_files(str(tmp_path), 'sub') |
| 42 |
assert ctx.job_runner_name == 'pbs' |
| 43 |
assert ctx.job_id == '2361713' |
| 44 |
assert ctx.job_runner_exit_polled == 1 |
| 45 |
assert ctx.pid == '2361713' |
| 46 |
assert ctx.time_submit_exit == '2025-01-28T14:46:04Z' |
| 47 |
assert ctx.time_run == '2025-01-28T14:46:05Z' |
| 48 |
assert ctx.time_run_exit == '2025-01-28T14:46:38Z' |
| 49 |
assert ctx.run_status == 0 |
| 50 |
assert ctx.messages == ['2025-01-28T14:46:05Z|INFO|sleep 31'] |
| 51 |
|
| 52 |
|
| 53 |
def test__job_poll_status_files_task_failed(tmp_path): |
| 54 |
"""Good Path: A valid job.status files exists""" |
| 55 |
(tmp_path / 'sub').mkdir() |
| 56 |
(tmp_path / 'sub' / 'job.status').write_text("CYLC_JOB_EXIT=FOO") |
| 57 |
ctx = jrm._jobs_poll_status_files(str(tmp_path), 'sub') |
| 58 |
assert ctx.run_status == 1 |
| 59 |
assert ctx.run_signal == 'FOO' |
| 60 |
|
| 61 |
|
| 62 |
def test__job_poll_status_files_deleted_logdir(): |
| 63 |
"""The log dir has been deleted whilst the task is still active. |
| 64 |
Return the context with the message that the task has failed. |
| 65 |
""" |
| 66 |
ctx = jrm._jobs_poll_status_files('foo', 'bar') |
| 67 |
assert ctx.run_signal == JOB_FILES_REMOVED_MESSAGE |
| 68 |
assert ctx.run_status == 1 |
| 69 |
assert ctx.job_runner_exit_polled == 1 |
| 70 |
|
| 71 |
|
| 72 |
def test__job_poll_status_files_ioerror(tmp_path, capsys): |
| 73 |
"""There is no readable file. |
| 74 |
""" |
| 75 |
(tmp_path / 'sub').mkdir() |
| 76 |
jrm._jobs_poll_status_files(str(tmp_path), 'sub') |
| 77 |
cap = capsys.readouterr() |
| 78 |
assert '[Errno 2] No such file or directory' in cap.err |