Debian Codegraph @

tests/integration/test_task_job_mgr.py

raw
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 contextlib import suppress
18 import json
19 import logging
20 from typing import Any as Fixture
21 from unittest.mock import Mock
22
23 from cylc.flow import CYLC_LOG
24 from cylc.flow.job_runner_mgr import JOB_FILES_REMOVED_MESSAGE
25 from cylc.flow.scheduler import Scheduler
26 from cylc.flow.task_state import (
27 TASK_STATUS_FAILED,
28 TASK_STATUS_RUNNING,
29 )
30
31
32 async def test_run_job_cmd_no_hosts_error(
33 flow,
34 scheduler,
35 start,
36 mock_glbl_cfg,
37 log_filter,
38 ):
39 """It should catch NoHostsError.
40
41 NoHostsError's should be caught and handled rather than raised because
42 they will cause problems (i.e. trigger shutdown) if they make it to the
43 Scheduler.
44
45 NoHostError's can occur in the poll & kill logic, this test ensures that
46 these methods catch the NoHostsError and handle the event as a regular
47 SSH failure by pushing the issue down the 255 callback.
48
49 See https://github.com/cylc/cylc-flow/pull/5195
50 """
51 # define a platform
52 mock_glbl_cfg(
53 'cylc.flow.platforms.glbl_cfg',
54 '''
55 [platforms]
56 [[no-host-platform]]
57 ''',
58 )
59
60 # define a workflow with a task which runs on that platform
61 id_ = flow({
62 'scheduling': {
63 'graph': {
64 'R1': 'foo'
65 }
66 },
67 'runtime': {
68 'foo': {
69 'platform': 'no-host-platform'
70 }
71 }
72 })
73
74 # start the workflow
75 schd: Scheduler = scheduler(id_)
76 async with start(schd) as log:
77 # set logging to debug level
78 log.set_level(logging.DEBUG, CYLC_LOG)
79
80 # tell Cylc the task is running on that platform
81 schd.pool.get_tasks()[0].state_reset(TASK_STATUS_RUNNING)
82 schd.pool.get_tasks()[0].platform = {
83 'name': 'no-host-platform',
84 'hosts': ['no-host-platform'],
85 }
86
87 # tell Cylc that that platform is not contactable
88 # (i.e. all hosts are in bad_hosts)
89 # (this casuses the NoHostsError to be raised)
90 schd.task_job_mgr.bad_hosts.add('no-host-platform')
91
92 # polling the task should not result in an error...
93 schd.task_job_mgr.poll_task_jobs(schd.pool.get_tasks())
94
95 # ...but the failure should be logged
96 assert log_filter(
97 contains='No available hosts for no-host-platform',
98 )
99 log.clear()
100
101 # killing the task should not result in an error...
102 schd.task_job_mgr.kill_task_jobs(schd.pool.get_tasks())
103
104 # ...but the failure should be logged
105 assert log_filter(
106 contains='No available hosts for no-host-platform',
107 )
108
109
110 async def test__run_job_cmd_logs_platform_lookup_fail(
111 one_conf: Fixture, flow: Fixture, scheduler: Fixture, run: Fixture,
112 db_select: Fixture, caplog: Fixture
113 ) -> None:
114 """TaskJobMg._run_job_cmd handles failure to get platform."""
115 id_: str = flow(one_conf)
116 schd: 'Scheduler' = scheduler(id_, paused_start=True)
117 # Run
118 async with run(schd):
119 from types import SimpleNamespace
120 schd.task_job_mgr._run_job_cmd(
121 schd.task_job_mgr.JOBS_POLL,
122 [SimpleNamespace(platform={'name': 'culdee fell summit'})],
123 None,
124 None
125 )
126 warning = caplog.records[-1]
127 assert warning.levelname == 'ERROR'
128 assert 'Unable to run command jobs-poll' in warning.msg
129
130
131 async def test__prep_submit_task_job_impl_handles_execution_time_limit(
132 flow: Fixture,
133 scheduler: Fixture,
134 start: Fixture,
135 ):
136 """Ensure that emptying the execution time limit unsets it.
137
138 Previously unsetting the etl by either broadcast or reload
139 would not unset a previous etl.
140
141 See https://github.com/cylc/cylc-flow/issues/5891
142 """
143 id_ = flow({
144 "scheduling": {
145 "cycling mode": "integer",
146 "graph": {"R1": "a"}
147 },
148 "runtime": {
149 "root": {},
150 "a": {
151 "script": "sleep 10",
152 "execution time limit": 'PT5S'
153 }
154 }
155 })
156
157 # Run in live mode - function not called in sim mode.
158 schd = scheduler(id_, run_mode='live')
159 async with start(schd):
160 task_a = schd.pool.get_tasks()[0]
161 # We're not interested in the job file stuff, just
162 # in the summary state.
163 with suppress(FileExistsError):
164 schd.task_job_mgr._prep_submit_task_job_impl(
165 task_a, task_a.tdef.rtconfig
166 )
167 assert task_a.summary['execution_time_limit'] == 5.0
168
169 # If we delete the etl it gets deleted in the summary:
170 task_a.tdef.rtconfig['execution time limit'] = None
171 with suppress(FileExistsError):
172 schd.task_job_mgr._prep_submit_task_job_impl(
173 task_a, task_a.tdef.rtconfig
174 )
175 assert not task_a.summary.get('execution_time_limit', '')
176
177 # put everything back and test broadcast too.
178 task_a.tdef.rtconfig['execution time limit'] = 5.0
179 task_a.summary['execution_time_limit'] = 5.0
180 schd.broadcast_mgr.broadcasts = {
181 '1': {'a': {'execution time limit': None}}}
182 with suppress(FileExistsError):
183 # We run a higher level function here to ensure
184 # that the broadcast is applied.
185 schd.task_job_mgr._prep_submit_task_job(task_a)
186 assert not task_a.summary.get('execution_time_limit', '')
187
188
189 async def test_broadcast_platform_change(
190 mock_glbl_cfg,
191 flow,
192 scheduler,
193 start,
194 log_filter,
195 ):
196 """Broadcast can change task platform.
197
198 Even after host selection failure.
199
200 see https://github.com/cylc/cylc-flow/issues/6320
201 """
202 mock_glbl_cfg(
203 'cylc.flow.platforms.glbl_cfg',
204 '''
205 [platforms]
206 [[foo]]
207 hosts = food
208 ''')
209
210 id_ = flow({
211 "scheduling": {"graph": {"R1": "mytask"}},
212 # Platform = None doesn't cause this issue!
213 "runtime": {"mytask": {"platform": "localhost"}}})
214
215 schd: Scheduler = scheduler(id_, run_mode='live')
216
217 async with start(schd):
218 # Change the task platform with broadcast:
219 schd.broadcast_mgr.put_broadcast(
220 ['1'], ['mytask'], [{'platform': 'foo'}])
221
222 # Simulate prior failure to contact hosts:
223 schd.bad_hosts.add('food')
224
225 # Attempt job submission:
226 schd.submit_task_jobs(schd.pool.get_tasks())
227
228 # Check that task platform hasn't become "localhost":
229 assert schd.pool.get_tasks()[0].platform['name'] == 'foo'
230 # ... and that remote init failed because all hosts bad:
231 assert log_filter(regex=r"platform: foo .*\(no hosts were reachable\)")
232
233
234 async def test_poll_job_deleted_log_folder(
235 one_conf, flow, scheduler, start, log_filter
236 ):
237 """Capture a task error caused by polling finding the job log dir deleted.
238
239 https://github.com/cylc/cylc-flow/issues/6425
240 """
241 response = {
242 'run_signal': JOB_FILES_REMOVED_MESSAGE,
243 'run_status': 1,
244 'job_runner_exit_polled': 1,
245 }
246 schd: Scheduler = scheduler(flow(one_conf))
247 async with start(schd):
248 itask = schd.pool.get_tasks()[0]
249 itask.submit_num = 1
250 job_id = itask.job_tokens.relative_id
251 schd.task_job_mgr._poll_task_job_callback(
252 itask,
253 cmd_ctx=Mock(),
254 line=f'2025-02-13T12:08:30Z|{job_id}|{json.dumps(response)}',
255 )
256 assert itask.state(TASK_STATUS_FAILED)
257
258 assert log_filter(
259 logging.ERROR, f"job log directory {job_id} no longer exists"
260 )
261
262
263 async def test__prep_submit_task_job_impl_handles_all_old_platform_settings(
264 flow: Fixture,
265 scheduler: Fixture,
266 start: Fixture,
267 mock_glbl_cfg: Fixture,
268 ):
269 """Ensure that if the old host/batch system settings
270 are set that task job manager will wait for the any hostname to be
271 resolved.
272
273 https://github.com/cylc/cylc-flow/pull/6990
274 """
275 mock_glbl_cfg(
276 'cylc.flow.platforms.glbl_cfg',
277 '''
278 [platforms]
279 [[bakery]]
280 hosts = localhost
281 job runner = loaf
282 ''',
283 )
284 id_ = flow({
285 "scheduling": {"graph": {"R1": "a"}},
286 "runtime": {"a": {"job": {"batch system": "loaf"}}}
287 })
288
289 schd = scheduler(id_, run_mode='live')
290 async with start(schd):
291 task_a = schd.pool.get_tasks()[0]
292 with suppress(FileExistsError):
293 schd.task_job_mgr._prep_submit_task_job(task_a)
294
295 assert task_a.platform['name'] == 'bakery'