| 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 |
import errno |
| 18 |
import logging |
| 19 |
from pathlib import Path |
| 20 |
import re |
| 21 |
from textwrap import dedent |
| 22 |
|
| 23 |
from cylc.flow.job_runner_mgr import JobRunnerManager |
| 24 |
from cylc.flow.pathutil import get_workflow_run_job_dir |
| 25 |
from cylc.flow.task_state import TASK_STATUS_RUNNING |
| 26 |
from cylc.flow.subprocctx import SubProcContext |
| 27 |
from cylc.flow.task_job_logs import JOB_LOG_OUT, JOB_LOG_ERR |
| 28 |
|
| 29 |
|
| 30 |
async def test_kill_error(one, start, test_dir, capsys, log_filter): |
| 31 |
"""It should report the failure to kill a job.""" |
| 32 |
async with start(one): |
| 33 |
# make it look like the task is running |
| 34 |
itask = one.pool.get_tasks()[0] |
| 35 |
itask.submit_num += 1 |
| 36 |
itask.state_reset(TASK_STATUS_RUNNING) |
| 37 |
|
| 38 |
# fake job details |
| 39 |
workflow_job_log_dir = Path(get_workflow_run_job_dir(one.workflow)) |
| 40 |
job_id = itask.job_tokens.relative_id |
| 41 |
job_log_dir = Path(workflow_job_log_dir, job_id) |
| 42 |
|
| 43 |
# create job status file (give it a fake pid) |
| 44 |
job_log_dir.mkdir(parents=True) |
| 45 |
(job_log_dir / 'job.status').write_text(dedent(''' |
| 46 |
CYLC_JOB_RUNNER_NAME=background |
| 47 |
CYLC_JOB_ID=99999999 |
| 48 |
CYLC_JOB_PID=99999999 |
| 49 |
''')) |
| 50 |
|
| 51 |
# attempt to kill the job using the jobs-kill script |
| 52 |
# (note this is normally run via a subprocess) |
| 53 |
capsys.readouterr() |
| 54 |
JobRunnerManager().jobs_kill(str(workflow_job_log_dir), [job_id]) |
| 55 |
|
| 56 |
# the kill should fail, the failure should be written to stdout |
| 57 |
# (the jobs-kill callback will read this in and handle it) |
| 58 |
out, err = capsys.readouterr() |
| 59 |
assert re.search( |
| 60 |
# # NOTE: ESRCH = no such process |
| 61 |
rf'TASK JOB ERROR.*{job_id}.*Errno {errno.ESRCH}', |
| 62 |
out, |
| 63 |
) |
| 64 |
|
| 65 |
# feed this jobs-kill output into the scheduler |
| 66 |
# (as if we had run the jobs-kill script as a subprocess) |
| 67 |
one.task_job_mgr._kill_task_jobs_callback( |
| 68 |
# mock the subprocess |
| 69 |
SubProcContext( |
| 70 |
one.task_job_mgr.JOBS_KILL, |
| 71 |
['mock-cmd'], |
| 72 |
# provide it with the out/err the script produced |
| 73 |
out=out, |
| 74 |
err=err, |
| 75 |
), |
| 76 |
[itask], |
| 77 |
) |
| 78 |
|
| 79 |
# a warning should be logged |
| 80 |
assert log_filter( |
| 81 |
regex=r'1/one/01:running.*job kill failed', |
| 82 |
level=logging.WARNING, |
| 83 |
) |
| 84 |
assert itask.state(TASK_STATUS_RUNNING) |
| 85 |
|
| 86 |
|
| 87 |
async def test_create_nn_new(one, start): |
| 88 |
"""Test _create_nn. |
| 89 |
|
| 90 |
It should create the NN symlink. |
| 91 |
""" |
| 92 |
async with start(one): |
| 93 |
itask = one.pool.get_tasks()[0] |
| 94 |
|
| 95 |
workflow_job_log_dir = Path(get_workflow_run_job_dir(one.workflow)) |
| 96 |
job_id = itask.tokens.duplicate(job='01').relative_id |
| 97 |
job_log_dir = Path(workflow_job_log_dir, job_id) |
| 98 |
job_log_dir.mkdir(parents=True) |
| 99 |
|
| 100 |
# call _create_nn |
| 101 |
JobRunnerManager()._create_nn(job_log_dir / 'job') |
| 102 |
|
| 103 |
# check the symlink exists |
| 104 |
assert (job_log_dir.parent / "NN").is_symlink() |
| 105 |
|
| 106 |
|
| 107 |
async def test_create_nn_old(one, start): |
| 108 |
"""Test _create_nn. |
| 109 |
|
| 110 |
It should remove existing job logs, if the dir already exists. |
| 111 |
""" |
| 112 |
async with start(one): |
| 113 |
itask = one.pool.get_tasks()[0] |
| 114 |
|
| 115 |
# fake some old job logs |
| 116 |
workflow_job_log_dir = Path(get_workflow_run_job_dir(one.workflow)) |
| 117 |
job_id = itask.tokens.duplicate(job='01').relative_id |
| 118 |
job_log_dir = Path(workflow_job_log_dir, job_id) |
| 119 |
job_log_dir.mkdir(parents=True) |
| 120 |
|
| 121 |
job_logs = [] |
| 122 |
for name in JOB_LOG_OUT, JOB_LOG_ERR: |
| 123 |
job_logs.append(job_log_dir / name) |
| 124 |
|
| 125 |
# create the logs |
| 126 |
for job_log in job_logs: |
| 127 |
job_log.touch() |
| 128 |
|
| 129 |
# call _create_nn |
| 130 |
JobRunnerManager()._create_nn(job_log_dir / 'job') |
| 131 |
|
| 132 |
# check they were removed |
| 133 |
for job_log in job_logs: |
| 134 |
assert not job_log.is_file() |