| 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 |
tests for functions in cylc.flow.workflow_events.py |
| 18 |
""" |
| 19 |
|
| 20 |
import pytest |
| 21 |
|
| 22 |
from types import SimpleNamespace |
| 23 |
|
| 24 |
from cylc.flow.workflow_events import ( |
| 25 |
WorkflowEventHandler, |
| 26 |
get_template_variables, |
| 27 |
process_mail_footer, |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
@pytest.mark.parametrize( |
| 32 |
'key, workflow_cfg, glbl_cfg, expected', |
| 33 |
[ |
| 34 |
('handlers', True, True, ['stall']), |
| 35 |
('handlers', False, True, None), |
| 36 |
('handlers', False, False, None), |
| 37 |
('mail events', True, True, []), |
| 38 |
('mail events', False, True, ['abort']), |
| 39 |
('mail events', False, False, None), |
| 40 |
('from', True, True, 'docklands@railway'), |
| 41 |
('from', False, True, 'highway@mixture'), |
| 42 |
('from', False, False, None), |
| 43 |
('abort on workflow timeout', True, True, True), |
| 44 |
('abort on workflow timeout', False, True, True), |
| 45 |
('abort on workflow timeout', False, False, False), |
| 46 |
] |
| 47 |
) |
| 48 |
def test_get_events_handler( |
| 49 |
mock_glbl_cfg, key, workflow_cfg, glbl_cfg, expected |
| 50 |
): |
| 51 |
"""Test order of precedence for getting event handler configuration.""" |
| 52 |
mock_glbl_cfg( |
| 53 |
'cylc.flow.workflow_events.glbl_cfg', |
| 54 |
( |
| 55 |
''' |
| 56 |
[scheduler] |
| 57 |
[[mail]] |
| 58 |
from = highway@mixture |
| 59 |
[[events]] |
| 60 |
abort on workflow timeout = True |
| 61 |
mail events = abort |
| 62 |
''' |
| 63 |
if glbl_cfg else '' |
| 64 |
), |
| 65 |
) |
| 66 |
|
| 67 |
config = SimpleNamespace() |
| 68 |
config.cfg = { |
| 69 |
'scheduler': { |
| 70 |
'events': {'handlers': ['stall'], 'mail events': []}, |
| 71 |
'mail': {'from': 'docklands@railway'}, |
| 72 |
} if workflow_cfg else {'events': {}} |
| 73 |
} |
| 74 |
assert WorkflowEventHandler.get_events_conf(config, key) == expected |
| 75 |
|
| 76 |
|
| 77 |
def test_process_mail_footer(caplog, log_filter): |
| 78 |
schd = SimpleNamespace( |
| 79 |
config=SimpleNamespace(cfg={'meta': {}}), |
| 80 |
host='myhost', |
| 81 |
owner='me', |
| 82 |
server=SimpleNamespace(port=42), |
| 83 |
uuid_str=None, |
| 84 |
workflow='my_workflow', |
| 85 |
) |
| 86 |
template_vars = get_template_variables(schd, '', '') |
| 87 |
|
| 88 |
# test all variables |
| 89 |
assert process_mail_footer( |
| 90 |
'%(host)s|%(port)s|%(owner)s|%(suite)s|%(workflow)s', template_vars |
| 91 |
) == 'myhost|42|me|my_workflow|my_workflow\n' |
| 92 |
assert not log_filter(contains='Ignoring bad mail footer template') |
| 93 |
|
| 94 |
# test invalid variable |
| 95 |
assert process_mail_footer('%(invalid)s', template_vars) == '' |
| 96 |
assert log_filter(contains='Ignoring bad mail footer template') |
| 97 |
|
| 98 |
# test broken template |
| 99 |
caplog.clear() |
| 100 |
assert process_mail_footer('%(invalid)s', template_vars) == '' |
| 101 |
assert log_filter(contains='Ignoring bad mail footer template') |