| 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 contextmanager |
| 18 |
from secrets import token_hex |
| 19 |
import sqlite3 |
| 20 |
|
| 21 |
import pytest |
| 22 |
|
| 23 |
from cylc.flow.exceptions import HostSelectException, ServiceFileError |
| 24 |
from cylc.flow.scheduler_cli import ( |
| 25 |
RunOptions, |
| 26 |
_distribute, |
| 27 |
_version_check, |
| 28 |
) |
| 29 |
|
| 30 |
from .conftest import MonkeyMock |
| 31 |
|
| 32 |
|
| 33 |
@pytest.fixture |
| 34 |
def stopped_workflow_db(tmp_path): |
| 35 |
"""Returns a workflow DB with the `cylc_version` set to the provided |
| 36 |
string. |
| 37 |
|
| 38 |
def test_x(stopped_workflow_db): |
| 39 |
db_file = stopped_workflow_db(version) |
| 40 |
|
| 41 |
""" |
| 42 |
def _stopped_workflow_db(version): |
| 43 |
db_file = tmp_path / 'db' |
| 44 |
conn = sqlite3.connect(db_file) |
| 45 |
conn.execute(''' |
| 46 |
CREATE TABLE |
| 47 |
workflow_params(key TEXT, value TEXT, PRIMARY KEY(key)) |
| 48 |
''') |
| 49 |
conn.execute( |
| 50 |
''' |
| 51 |
INSERT INTO |
| 52 |
workflow_params |
| 53 |
VALUES (?, ?) |
| 54 |
''', |
| 55 |
('cylc_version', version) |
| 56 |
) |
| 57 |
conn.commit() |
| 58 |
conn.close() |
| 59 |
return db_file |
| 60 |
|
| 61 |
return _stopped_workflow_db |
| 62 |
|
| 63 |
|
| 64 |
@pytest.fixture |
| 65 |
def set_cylc_version(monkeypatch): |
| 66 |
"""Set the cylc.flow.__version__ attribute. |
| 67 |
|
| 68 |
def test_x(set_cylc_version): |
| 69 |
set_cylc_version('1.2.3') |
| 70 |
|
| 71 |
""" |
| 72 |
def _set_cylc_version(version): |
| 73 |
monkeypatch.setattr( |
| 74 |
'cylc.flow.scheduler_cli.__version__', |
| 75 |
version, |
| 76 |
) |
| 77 |
return _set_cylc_version |
| 78 |
|
| 79 |
|
| 80 |
@pytest.fixture |
| 81 |
def answer(monkeypatch): |
| 82 |
"""Answer a `cylc play` CLI prompt. |
| 83 |
|
| 84 |
def test_x(answer): |
| 85 |
answer(users_response) |
| 86 |
|
| 87 |
It also adds an assert on the number of times the prompt interface was |
| 88 |
called. 0 if response is None, else 1. |
| 89 |
|
| 90 |
""" |
| 91 |
@contextmanager |
| 92 |
def _answer(response): |
| 93 |
calls = 0 |
| 94 |
|
| 95 |
def prompt(*args, **kwargs): |
| 96 |
nonlocal calls |
| 97 |
calls += 1 |
| 98 |
return response |
| 99 |
|
| 100 |
monkeypatch.setattr( |
| 101 |
'cylc.flow.scheduler_cli.prompt', |
| 102 |
prompt, |
| 103 |
) |
| 104 |
|
| 105 |
yield |
| 106 |
|
| 107 |
expected_calls = 1 |
| 108 |
if response is None: |
| 109 |
expected_calls = 0 |
| 110 |
assert calls == expected_calls |
| 111 |
|
| 112 |
return _answer |
| 113 |
|
| 114 |
|
| 115 |
@pytest.fixture |
| 116 |
def interactive(monkeypatch): |
| 117 |
monkeypatch.setattr( |
| 118 |
'cylc.flow.scheduler_cli.is_terminal', |
| 119 |
lambda: True, |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
@pytest.fixture |
| 124 |
def non_interactive(monkeypatch): |
| 125 |
monkeypatch.setattr( |
| 126 |
'cylc.flow.scripts.reinstall.is_terminal', |
| 127 |
lambda: False, |
| 128 |
) |
| 129 |
|
| 130 |
|
| 131 |
@pytest.mark.parametrize( |
| 132 |
'before, after, downgrade, response, outcome', [ |
| 133 |
# no change |
| 134 |
('8.0.0', '8.0.0', False, None, True), |
| 135 |
# upgrading |
| 136 |
('8.0rc4.dev', '8.0.0', False, None, True), |
| 137 |
('8.0.0', '8.0.1', False, None, True), |
| 138 |
('8.0.0', '8.1.0', False, False, False), |
| 139 |
('8.0.0', '8.1.0', False, True, True), |
| 140 |
('8.0.0', '9.0.0', False, False, False), |
| 141 |
('8.0.0', '9.0.0', False, True, True), |
| 142 |
# downgrading |
| 143 |
('8.1.1', '8.1.0', False, None, False), |
| 144 |
('8.1.1', '8.1.0', True, None, True), |
| 145 |
('8.1.0', '8.0.0', False, None, False), |
| 146 |
('8.1.0', '8.0.0', True, None, True), |
| 147 |
('8.1.0', '8.0rc4.dev', True, None, True), |
| 148 |
('9.1.0', '8.0.0', False, None, False), |
| 149 |
('9.1.0', '8.0.0', True, None, True), |
| 150 |
# truncated versions |
| 151 |
('8.1.1', '8', False, None, False), |
| 152 |
('9.1.1', '8', True, None, True), |
| 153 |
], |
| 154 |
) |
| 155 |
def test_version_check_interactive( |
| 156 |
stopped_workflow_db, |
| 157 |
set_cylc_version, |
| 158 |
interactive, |
| 159 |
answer, |
| 160 |
before, |
| 161 |
after, |
| 162 |
response, |
| 163 |
downgrade, |
| 164 |
outcome, |
| 165 |
): |
| 166 |
"""It should check compatibility with the Cylc version of the prior run. |
| 167 |
|
| 168 |
When workflows are restarted we need to perform some checks to make sure |
| 169 |
it is safe and sensible to restart with this version of Cylc. |
| 170 |
|
| 171 |
Pytest Params: |
| 172 |
before: |
| 173 |
The Cylc version the workflow ran with previously. |
| 174 |
after: |
| 175 |
The version of Cylc being used to restart the workflow. |
| 176 |
downgrade: |
| 177 |
The --downgrade option of `cylc play`. |
| 178 |
response: |
| 179 |
The user's response the any CLI prompts. |
| 180 |
If `None` it will assert that no prompts were raised. |
| 181 |
outcome: |
| 182 |
The response of _version_check, True means safe to restart. |
| 183 |
|
| 184 |
""" |
| 185 |
db_file = stopped_workflow_db(before) |
| 186 |
set_cylc_version(after) |
| 187 |
with answer(response): |
| 188 |
assert ( |
| 189 |
_version_check( |
| 190 |
db_file, RunOptions(downgrade=downgrade) |
| 191 |
) |
| 192 |
is outcome |
| 193 |
) |
| 194 |
|
| 195 |
|
| 196 |
def test_version_check_interactive_upgrade( |
| 197 |
stopped_workflow_db, |
| 198 |
set_cylc_version, |
| 199 |
interactive, |
| 200 |
answer, |
| 201 |
): |
| 202 |
"""If a user interactively upgrades, it should set the upgrade option.""" |
| 203 |
db_file = stopped_workflow_db('8.0.0') |
| 204 |
set_cylc_version('8.1.0') |
| 205 |
opts = RunOptions() |
| 206 |
assert opts.upgrade is False |
| 207 |
with answer(True): |
| 208 |
assert _version_check(db_file, opts) is True |
| 209 |
assert opts.upgrade is True |
| 210 |
|
| 211 |
|
| 212 |
def test_version_check_non_interactive( |
| 213 |
stopped_workflow_db, |
| 214 |
set_cylc_version, |
| 215 |
non_interactive, |
| 216 |
): |
| 217 |
"""It should not prompt in non-interactive mode. |
| 218 |
|
| 219 |
* The --upgrade argument should permit upgrade. |
| 220 |
* The --downgrade argument should permit downgrade. |
| 221 |
""" |
| 222 |
# upgrade |
| 223 |
db_file = stopped_workflow_db('8.0.0') |
| 224 |
set_cylc_version('8.1.0') |
| 225 |
assert _version_check(db_file, RunOptions()) is False |
| 226 |
assert ( |
| 227 |
_version_check(db_file, RunOptions(upgrade=True)) is True |
| 228 |
) # CLI --upgrade |
| 229 |
|
| 230 |
# downgrade |
| 231 |
db_file.unlink() |
| 232 |
db_file = stopped_workflow_db('8.1.0') |
| 233 |
set_cylc_version('8.0.0') |
| 234 |
assert _version_check(db_file, RunOptions()) is False |
| 235 |
assert ( |
| 236 |
_version_check(db_file, RunOptions(downgrade=True)) is True |
| 237 |
) # CLI --downgrade |
| 238 |
|
| 239 |
|
| 240 |
def test_version_check_incompat(tmp_path): |
| 241 |
"""It should fail for a corrupted or invalid database file.""" |
| 242 |
db_file = tmp_path / 'db' # invalid DB file |
| 243 |
db_file.touch() |
| 244 |
with pytest.raises(ServiceFileError): |
| 245 |
_version_check(db_file, RunOptions()) |
| 246 |
|
| 247 |
|
| 248 |
def test_version_check_no_db(tmp_path): |
| 249 |
"""It should pass if there is no DB file (e.g. on workflow first start).""" |
| 250 |
db_file = tmp_path / 'db' # non-existent file |
| 251 |
assert _version_check(db_file, RunOptions()) |
| 252 |
|
| 253 |
|
| 254 |
@pytest.mark.parametrize( |
| 255 |
'cli_colour, is_terminal, distribute_colour', |
| 256 |
[ |
| 257 |
('never', True, '--color=never'), |
| 258 |
('auto', True, '--color=always'), |
| 259 |
('always', True, '--color=always'), |
| 260 |
('never', False, '--color=never'), |
| 261 |
('auto', False, '--color=never'), |
| 262 |
('always', False, '--color=never'), |
| 263 |
] |
| 264 |
) |
| 265 |
def test_distribute_colour( |
| 266 |
monkeymock, |
| 267 |
cli_colour, |
| 268 |
is_terminal, |
| 269 |
distribute_colour, |
| 270 |
): |
| 271 |
"""It should start detached workflows with the correct --colour option. |
| 272 |
|
| 273 |
The is_terminal test will fail for detached scheduler processes which means |
| 274 |
that the colour formatting will be stripped for startup. This includes |
| 275 |
the Cylc header logo and any warnings/errors raised during config parsing. |
| 276 |
|
| 277 |
In order to preserver colour formatting we must set the `--colour` arg to |
| 278 |
`always` when we want the detached process to start in colour mode. |
| 279 |
|
| 280 |
See https://github.com/cylc/cylc-flow/issues/5159 |
| 281 |
""" |
| 282 |
_is_terminal = monkeymock('cylc.flow.scheduler_cli.is_terminal') |
| 283 |
_is_terminal.return_value = is_terminal |
| 284 |
_cylc_server_cmd = monkeymock('cylc.flow.scheduler_cli.cylc_server_cmd') |
| 285 |
_cylc_server_cmd.return_value = 0 |
| 286 |
opts = RunOptions(host='myhost', color=cli_colour) |
| 287 |
with pytest.raises(SystemExit) as excinfo: |
| 288 |
_distribute('foo', 'foo/run1', opts) |
| 289 |
assert excinfo.value.code == 0 |
| 290 |
assert distribute_colour in _cylc_server_cmd.call_args[0][0] |
| 291 |
|
| 292 |
|
| 293 |
def test_distribute_upgrade( |
| 294 |
monkeymock: MonkeyMock, monkeypatch: pytest.MonkeyPatch |
| 295 |
): |
| 296 |
"""It should start detached workflows with the --upgrade option if the user |
| 297 |
has interactively chosen to upgrade (typed 'y' at prompt). |
| 298 |
""" |
| 299 |
monkeypatch.setattr( |
| 300 |
'sys.argv', ['cylc', 'play', 'foo'] # no upgrade option here |
| 301 |
) |
| 302 |
_cylc_server_cmd = monkeymock('cylc.flow.scheduler_cli.cylc_server_cmd') |
| 303 |
_cylc_server_cmd.return_value = 0 |
| 304 |
opts = RunOptions( |
| 305 |
host='myhost', |
| 306 |
upgrade=True, # added by interactive upgrade |
| 307 |
) |
| 308 |
with pytest.raises(SystemExit) as excinfo: |
| 309 |
_distribute('foo', 'foo/run1', opts) |
| 310 |
assert excinfo.value.code == 0 |
| 311 |
assert '--upgrade' in _cylc_server_cmd.call_args[0][0] |
| 312 |
|
| 313 |
|
| 314 |
def test_distribute_invalid_host( |
| 315 |
mock_glbl_cfg, caplog: pytest.LogCaptureFixture |
| 316 |
): |
| 317 |
"""It handles a socket error when the host is invalid.""" |
| 318 |
mock_glbl_cfg( |
| 319 |
'cylc.flow.host_select.glbl_cfg', |
| 320 |
f''' |
| 321 |
[scheduler] |
| 322 |
[[run hosts]] |
| 323 |
available = non_exist_{token_hex(4)} |
| 324 |
''' |
| 325 |
) |
| 326 |
with pytest.raises(HostSelectException): |
| 327 |
_distribute('foo', 'foo/run1', RunOptions()) |