| 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 asyncio |
| 18 |
from collections import deque |
| 19 |
import logging |
| 20 |
|
| 21 |
import pytest |
| 22 |
|
| 23 |
from cylc.flow import CYLC_LOG |
| 24 |
from cylc.flow.exceptions import CylcError |
| 25 |
from cylc.flow.main_loop import ( |
| 26 |
CoroTypes, |
| 27 |
MainLoopPluginException, |
| 28 |
_wrapper, |
| 29 |
get_runners, |
| 30 |
load, |
| 31 |
) |
| 32 |
from cylc.flow.main_loop.health_check import health_check as hc_during |
| 33 |
|
| 34 |
|
| 35 |
def test_load_plugins_blank(): |
| 36 |
"""Test that log_plugins works when no plugins are requested.""" |
| 37 |
conf = { |
| 38 |
'plugins': [] |
| 39 |
} |
| 40 |
assert load(conf) == { |
| 41 |
'config': conf, |
| 42 |
'state': {}, |
| 43 |
'timings': {} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
def test_load_plugins(): |
| 48 |
"""Test the loading of a built-in plugin.""" |
| 49 |
conf = { |
| 50 |
'plugins': ['health check'], |
| 51 |
'health check': { |
| 52 |
'interval': 1234 |
| 53 |
} |
| 54 |
} |
| 55 |
assert load(conf) == { |
| 56 |
CoroTypes.Periodic: { |
| 57 |
('health check', 'health_check'): hc_during |
| 58 |
}, |
| 59 |
'state': { |
| 60 |
'health check': { |
| 61 |
} |
| 62 |
}, |
| 63 |
'config': conf, |
| 64 |
'timings': { |
| 65 |
('health check', 'health_check'): deque([], maxlen=1) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
def test_wrapper_calls_function(): |
| 71 |
"""Ensure the wrapper calls coroutines.""" |
| 72 |
flag = False |
| 73 |
|
| 74 |
async def test_coro(arg1, arg2): |
| 75 |
nonlocal flag |
| 76 |
assert arg1 == 'arg1' |
| 77 |
assert arg2 == 'arg2' |
| 78 |
flag = True |
| 79 |
|
| 80 |
coro = _wrapper( |
| 81 |
test_coro, |
| 82 |
'arg1', |
| 83 |
'arg2' |
| 84 |
) |
| 85 |
asyncio.run(coro) |
| 86 |
assert flag |
| 87 |
|
| 88 |
|
| 89 |
def test_wrapper_logging(caplog): |
| 90 |
"""Ensure the wrapper logs each coroutine call.""" |
| 91 |
async def test_coro(*_): |
| 92 |
pass |
| 93 |
coro = _wrapper( |
| 94 |
test_coro, |
| 95 |
None, |
| 96 |
None |
| 97 |
) |
| 98 |
with caplog.at_level(logging.DEBUG, logger=CYLC_LOG): |
| 99 |
asyncio.run(coro) |
| 100 |
assert len(caplog.record_tuples) == 2 |
| 101 |
( |
| 102 |
(run_log, run_level, run_msg), |
| 103 |
(end_log, end_level, end_msg) |
| 104 |
) = caplog.record_tuples |
| 105 |
# we should have two messages, one sent before and one after |
| 106 |
# the function |
| 107 |
assert 'run' in run_msg |
| 108 |
assert 'end' in end_msg |
| 109 |
# both should contain the name of the function |
| 110 |
assert 'test_coro' in run_msg |
| 111 |
assert 'test_coro' in end_msg |
| 112 |
# and should be sent to the cylc logger at the debug level |
| 113 |
assert run_log == end_log == CYLC_LOG |
| 114 |
assert run_level == end_level == logging.DEBUG |
| 115 |
|
| 116 |
|
| 117 |
def test_wrapper_catches_exceptions(caplog): |
| 118 |
"""Ensure the wrapper catches Exception instances and logs them.""" |
| 119 |
async def test_coro(*_): |
| 120 |
raise Exception('foo') |
| 121 |
coro = _wrapper( |
| 122 |
test_coro, |
| 123 |
None, |
| 124 |
None |
| 125 |
) |
| 126 |
with caplog.at_level(logging.DEBUG, logger=CYLC_LOG): |
| 127 |
asyncio.run(coro) |
| 128 |
assert len(caplog.record_tuples) == 4 |
| 129 |
run, error, traceback, completed = caplog.record_tuples |
| 130 |
assert 'run' in run[2] |
| 131 |
assert error[1] == logging.ERROR |
| 132 |
assert traceback[1] == logging.ERROR |
| 133 |
assert 'foo' in traceback[2] |
| 134 |
assert completed[1] == logging.DEBUG |
| 135 |
|
| 136 |
|
| 137 |
def test_wrapper_passes_cylc_error(): |
| 138 |
"""Ensure the wrapper does not catch CylcError instances.""" |
| 139 |
async def test_coro(*_): |
| 140 |
raise CylcError('foo') |
| 141 |
coro = _wrapper( |
| 142 |
test_coro, |
| 143 |
None, |
| 144 |
None |
| 145 |
) |
| 146 |
with pytest.raises(MainLoopPluginException): |
| 147 |
asyncio.run(coro) |
| 148 |
|
| 149 |
|
| 150 |
@pytest.fixture |
| 151 |
def basic_plugins(): |
| 152 |
calls = [] |
| 153 |
|
| 154 |
def capture(*args): |
| 155 |
calls.append(args) |
| 156 |
|
| 157 |
plugins = { |
| 158 |
'config': { |
| 159 |
'periodic plugin': { |
| 160 |
'interval': 10 |
| 161 |
} |
| 162 |
}, |
| 163 |
'timings': { |
| 164 |
('periodic plugin', 'periodic_coro'): [], |
| 165 |
('startup plugin', 'startup_coro'): [], |
| 166 |
}, |
| 167 |
'state': { |
| 168 |
'periodic plugin': { |
| 169 |
'a': 1 |
| 170 |
}, |
| 171 |
'startup plugin': { |
| 172 |
'b': 2 |
| 173 |
} |
| 174 |
}, |
| 175 |
CoroTypes.Periodic: { |
| 176 |
('periodic plugin', 'periodic_coro'): capture |
| 177 |
}, |
| 178 |
CoroTypes.StartUp: { |
| 179 |
('startup plugin', 'startup_coro'): capture |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
return (plugins, calls, capture) |
| 184 |
|
| 185 |
|
| 186 |
def test_get_runners_startup(basic_plugins): |
| 187 |
"""IT should return runners for startup functions.""" |
| 188 |
plugins, calls, capture = basic_plugins |
| 189 |
runners = get_runners( |
| 190 |
plugins, |
| 191 |
CoroTypes.StartUp, |
| 192 |
'scheduler object' |
| 193 |
) |
| 194 |
assert len(runners) == 1 |
| 195 |
asyncio.run(runners[0]) |
| 196 |
assert calls == [('scheduler object', {'b': 2})] |
| 197 |
|
| 198 |
|
| 199 |
def test_get_runners_periodic(basic_plugins): |
| 200 |
"""It should return runners for periodic functions.""" |
| 201 |
plugins, calls, capture = basic_plugins |
| 202 |
runners = get_runners( |
| 203 |
plugins, |
| 204 |
CoroTypes.Periodic, |
| 205 |
'scheduler object' |
| 206 |
) |
| 207 |
assert len(runners) == 1 |
| 208 |
asyncio.run(runners[0]) |
| 209 |
assert calls == [('scheduler object', {'a': 1})] |
| 210 |
|
| 211 |
|
| 212 |
def test_get_runners_periodic_debounce(basic_plugins): |
| 213 |
"""It should run periodic functions based on the configured interval.""" |
| 214 |
plugins, calls, capture = basic_plugins |
| 215 |
|
| 216 |
# we should start with a blank timings object |
| 217 |
assert len(plugins['timings'][('periodic plugin', 'periodic_coro')]) == 0 |
| 218 |
|
| 219 |
runners = get_runners( |
| 220 |
plugins, |
| 221 |
CoroTypes.Periodic, |
| 222 |
'scheduler object' |
| 223 |
) |
| 224 |
assert len(runners) == 1 |
| 225 |
asyncio.run(runners[0]) |
| 226 |
assert calls == [('scheduler object', {'a': 1})] |
| 227 |
|
| 228 |
# the timings object should now contain the previous run |
| 229 |
assert len(plugins['timings'][('periodic plugin', 'periodic_coro')]) == 1 |
| 230 |
|
| 231 |
# the next run should be skipped because of the interval |
| 232 |
runners = get_runners( |
| 233 |
plugins, |
| 234 |
CoroTypes.Periodic, |
| 235 |
'scheduler object' |
| 236 |
) |
| 237 |
assert len(runners) == 0 |
| 238 |
|
| 239 |
# if we remove the interval the next run will not get skipped |
| 240 |
plugins['config']['periodic plugin']['interval'] = 0 |
| 241 |
runners = get_runners( |
| 242 |
plugins, |
| 243 |
CoroTypes.Periodic, |
| 244 |
'scheduler object' |
| 245 |
) |
| 246 |
assert len(runners) == 1 |
| 247 |
assert calls[-1] == ('scheduler object', {'a': 1}) |
| 248 |
|
| 249 |
# Clean up coroutines we didn't run |
| 250 |
for coro in runners: |
| 251 |
coro.close() |
| 252 |
|
| 253 |
|
| 254 |
def test_state(basic_plugins): |
| 255 |
"""It should pass the same state object with each function call. |
| 256 |
|
| 257 |
* Run the same plugin function twice. |
| 258 |
* Ensure that the state object recieved by each call is the same object. |
| 259 |
|
| 260 |
""" |
| 261 |
plugins, calls, capture = basic_plugins |
| 262 |
runners = get_runners( |
| 263 |
plugins, |
| 264 |
CoroTypes.StartUp, |
| 265 |
'scheduler object' |
| 266 |
) |
| 267 |
assert len(runners) == 1 |
| 268 |
asyncio.run(*runners) |
| 269 |
assert len(calls) == 1 |
| 270 |
|
| 271 |
runners = get_runners( |
| 272 |
plugins, |
| 273 |
CoroTypes.StartUp, |
| 274 |
'scheduler object' |
| 275 |
) |
| 276 |
assert len(runners) == 1 |
| 277 |
asyncio.run(*runners) |
| 278 |
assert len(calls) == 2 |
| 279 |
|
| 280 |
(_, state1), (_, state2) = calls |
| 281 |
assert id(state1) == id(state2) |