|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16 | 16 |
|
17 | 17 | import logging
|
| 18 | +from types import SimpleNamespace |
18 | 19 | from typing import Any as Fixture
|
19 | 20 |
|
20 |
| -from cylc.flow.run_modes import RunMode |
21 |
| -from cylc.flow.task_events_mgr import TaskJobLogsRetrieveContext |
22 |
| -from cylc.flow.scheduler import Scheduler |
| 21 | +import pytest |
| 22 | + |
23 | 23 | from cylc.flow.data_store_mgr import (
|
24 | 24 | JOBS,
|
25 | 25 | TASK_STATUS_WAITING,
|
26 | 26 | )
|
| 27 | +from cylc.flow.id import Tokens |
| 28 | +from cylc.flow.run_modes import RunMode |
| 29 | +from cylc.flow.scheduler import Scheduler |
| 30 | +from cylc.flow.task_events_mgr import ( |
| 31 | + EventKey, |
| 32 | + TaskJobLogsRetrieveContext, |
| 33 | +) |
| 34 | + |
| 35 | +from .test_workflow_events import TEMPLATES |
| 36 | + |
| 37 | + |
| 38 | +# NOTE: we do not test custom event handlers here because these are tested |
| 39 | +# as a part of workflow validation (now also performed by cylc play) |
27 | 40 |
|
28 | 41 |
|
29 | 42 | async def test_process_job_logs_retrieval_warns_no_platform(
|
@@ -208,3 +221,79 @@ async def test__process_message_failed_with_retry(one, start, log_filter):
|
208 | 221 | fail_once, None, 'failed', False, 'failed/OOK')
|
209 | 222 | failed_record = log_filter(level=logging.ERROR)[-1]
|
210 | 223 | assert 'failed/OOK' in failed_record[1]
|
| 224 | + |
| 225 | + |
| 226 | +@pytest.mark.parametrize('template', TEMPLATES) |
| 227 | +async def test_mail_footer_template( |
| 228 | + mod_one, # use the same scheduler for each test |
| 229 | + start, |
| 230 | + mock_glbl_cfg, |
| 231 | + log_filter, |
| 232 | + capcall, |
| 233 | + template, |
| 234 | +): |
| 235 | + """It should handle templating issues with the mail footer.""" |
| 236 | + # prevent emails from being sent |
| 237 | + mail_calls = capcall( |
| 238 | + 'cylc.flow.task_events_mgr.TaskEventsManager._send_mail' |
| 239 | + ) |
| 240 | + |
| 241 | + # configure mail footer |
| 242 | + mock_glbl_cfg( |
| 243 | + 'cylc.flow.workflow_events.glbl_cfg', |
| 244 | + f''' |
| 245 | + [scheduler] |
| 246 | + [[mail]] |
| 247 | + footer = 'footer={template}' |
| 248 | + ''', |
| 249 | + ) |
| 250 | + |
| 251 | + # start the workflow and get it to send an email |
| 252 | + ctx = SimpleNamespace(mail_to=None, mail_from=None) |
| 253 | + id_keys = [EventKey('none', 'failed', 'failed', Tokens('//1/a'))] |
| 254 | + async with start(mod_one): |
| 255 | + mod_one.task_events_mgr._process_event_email(mod_one, ctx, id_keys) |
| 256 | + |
| 257 | + # warnings should appear only when the template is invalid |
| 258 | + should_log = 'workflow' not in template |
| 259 | + |
| 260 | + # check that template issues are handled correctly |
| 261 | + assert bool(log_filter( |
| 262 | + contains='Ignoring bad mail footer template', |
| 263 | + )) == should_log |
| 264 | + assert bool(log_filter( |
| 265 | + contains=template, |
| 266 | + )) == should_log |
| 267 | + |
| 268 | + # check that the mail is sent even if there are issues with the footer |
| 269 | + assert len(mail_calls) == 1 |
| 270 | + |
| 271 | + |
| 272 | +async def test_event_email_body( |
| 273 | + mod_one, |
| 274 | + start, |
| 275 | + capcall, |
| 276 | +): |
| 277 | + """It should send an email with the event context.""" |
| 278 | + mail_calls = capcall( |
| 279 | + 'cylc.flow.task_events_mgr.TaskEventsManager._send_mail' |
| 280 | + ) |
| 281 | + |
| 282 | + # start the workflow and get it to send an email |
| 283 | + ctx = SimpleNamespace(mail_to=None, mail_from=None) |
| 284 | + async with start(mod_one): |
| 285 | + # send a custom task message with the warning severity level |
| 286 | + id_keys = [ |
| 287 | + EventKey('none', 'warning', 'warning message', Tokens('//1/a/01')) |
| 288 | + ] |
| 289 | + mod_one.task_events_mgr._process_event_email(mod_one, ctx, id_keys) |
| 290 | + |
| 291 | + # test the email which would have been sent for this message |
| 292 | + email_body = mail_calls[0][0][3] |
| 293 | + assert 'event: warning' |
| 294 | + assert 'job: 1/a/01' in email_body |
| 295 | + assert 'message: warning message' in email_body |
| 296 | + assert f'workflow: {mod_one.tokens["workflow"]}' in email_body |
| 297 | + assert f'host: {mod_one.host}' in email_body |
| 298 | + assert f'port: {mod_one.server.port}' in email_body |
| 299 | + assert f'owner: {mod_one.owner}' in email_body |
0 commit comments