| 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 typing import Optional |
| 18 |
from unittest.mock import Mock |
| 19 |
|
| 20 |
import pytest |
| 21 |
|
| 22 |
from cylc.flow.broadcast_mgr import BroadcastMgr |
| 23 |
from cylc.flow.task_events_mgr import TaskEventsManager |
| 24 |
from cylc.flow.task_proxy import TaskProxy |
| 25 |
from cylc.flow.taskdef import TaskDef |
| 26 |
|
| 27 |
|
| 28 |
@pytest.mark.parametrize( |
| 29 |
"broadcast, remote, platforms, expected", |
| 30 |
[ |
| 31 |
("hpc1", "a", "b", "hpc1"), |
| 32 |
(None, "hpc1", "b", "hpc1"), |
| 33 |
(None, None, "hpc1", "hpc1"), |
| 34 |
(None, None, None, None), |
| 35 |
] |
| 36 |
) |
| 37 |
def test_get_remote_conf(broadcast, remote, platforms, expected): |
| 38 |
"""Test TaskEventsManager._get_remote_conf().""" |
| 39 |
|
| 40 |
task_events_mgr = TaskEventsManager( |
| 41 |
None, None, None, None, None, None, None, None, None) |
| 42 |
|
| 43 |
task_events_mgr.broadcast_mgr = Mock( |
| 44 |
get_broadcast=lambda x: { |
| 45 |
"remote": { |
| 46 |
"host": broadcast |
| 47 |
} |
| 48 |
} |
| 49 |
) |
| 50 |
|
| 51 |
itask = Mock( |
| 52 |
identity='foo.1', |
| 53 |
tdef=Mock( |
| 54 |
rtconfig={ |
| 55 |
'remote': { |
| 56 |
'host': remote |
| 57 |
} |
| 58 |
} |
| 59 |
), |
| 60 |
platform={ |
| 61 |
'host': platforms |
| 62 |
} |
| 63 |
) |
| 64 |
|
| 65 |
assert task_events_mgr._get_remote_conf(itask, 'host') == expected |
| 66 |
|
| 67 |
|
| 68 |
@pytest.mark.parametrize( |
| 69 |
"broadcast, workflow, platforms, expected", |
| 70 |
[ |
| 71 |
([800], [700], [600], [800]), |
| 72 |
(None, [700], [600], [700]), |
| 73 |
(None, None, [600], [600]), |
| 74 |
] |
| 75 |
) |
| 76 |
def test_get_workflow_platforms_conf(broadcast, workflow, platforms, expected): |
| 77 |
"""Test TaskEventsManager._get_polling_interval_conf().""" |
| 78 |
|
| 79 |
task_events_mgr = TaskEventsManager( |
| 80 |
None, None, None, None, None, None, None, None, None) |
| 81 |
|
| 82 |
KEY = "execution polling intervals" |
| 83 |
|
| 84 |
task_events_mgr.broadcast_mgr = Mock( |
| 85 |
get_broadcast=lambda x: { |
| 86 |
KEY: broadcast |
| 87 |
} |
| 88 |
) |
| 89 |
|
| 90 |
itask = Mock( |
| 91 |
identity='foo.1', |
| 92 |
tdef=Mock( |
| 93 |
rtconfig={ |
| 94 |
KEY: workflow |
| 95 |
} |
| 96 |
), |
| 97 |
platform={ |
| 98 |
KEY: platforms |
| 99 |
} |
| 100 |
) |
| 101 |
|
| 102 |
assert ( |
| 103 |
task_events_mgr._get_workflow_platforms_conf(itask, KEY) == |
| 104 |
expected |
| 105 |
) |
| 106 |
|
| 107 |
|
| 108 |
@pytest.mark.parametrize( |
| 109 |
'rt_val, schd_val, glbl_val, expected', |
| 110 |
[ |
| 111 |
('rt', 'schd', 'glbl', 'rt'), |
| 112 |
(None, 'schd', 'glbl', 'schd'), |
| 113 |
(None, None, 'glbl', 'glbl'), |
| 114 |
(None, None, None, 'default'), |
| 115 |
] |
| 116 |
) |
| 117 |
def test_get_events_conf__mail_to_from( |
| 118 |
mock_glbl_cfg, |
| 119 |
rt_val: Optional[str], |
| 120 |
schd_val: Optional[str], |
| 121 |
glbl_val: Optional[str], |
| 122 |
expected: str |
| 123 |
): |
| 124 |
"""Test order of precedence for [mail]to/from.""" |
| 125 |
if glbl_val: |
| 126 |
mock_glbl_cfg( |
| 127 |
'cylc.flow.task_events_mgr.glbl_cfg', |
| 128 |
f''' |
| 129 |
[scheduler] |
| 130 |
[[mail]] |
| 131 |
from = {glbl_val} |
| 132 |
to = {glbl_val} |
| 133 |
''' |
| 134 |
) |
| 135 |
|
| 136 |
mock_task = Mock( |
| 137 |
spec=TaskProxy, |
| 138 |
tdef=Mock( |
| 139 |
spec=TaskDef, |
| 140 |
rtconfig={ |
| 141 |
'events': {}, |
| 142 |
'mail': {'to': rt_val, 'from': rt_val} if rt_val else {}, |
| 143 |
}, |
| 144 |
), |
| 145 |
) |
| 146 |
mock_task_events_mgr = Mock( |
| 147 |
spec=TaskEventsManager, |
| 148 |
workflow_cfg={ |
| 149 |
'scheduler': { |
| 150 |
'mail': {'to': schd_val, 'from': schd_val}, |
| 151 |
}, |
| 152 |
} if schd_val else {}, |
| 153 |
broadcast_mgr=Mock( |
| 154 |
spec_set=BroadcastMgr, |
| 155 |
get_broadcast=lambda *a, **k: {}, |
| 156 |
), |
| 157 |
) |
| 158 |
|
| 159 |
for key in ('to', 'from'): |
| 160 |
assert TaskEventsManager._get_events_conf( |
| 161 |
mock_task_events_mgr, itask=mock_task, key=key, default='default' |
| 162 |
) == expected |