| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 4 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from cylc.flow.option_parsers import Options |
| 20 |
from cylc.flow.scripts.validate_reinstall import ( |
| 21 |
get_option_parser as vr_gop, |
| 22 |
vr_cli, |
| 23 |
) |
| 24 |
|
| 25 |
ValidateReinstallOptions = Options(vr_gop()) |
| 26 |
|
| 27 |
|
| 28 |
def answer_prompts(monkeypatch, *responses): |
| 29 |
"""Hardcode responses to "cylc vr" interactive prompts.""" |
| 30 |
# make it look like we are running this command in a terminal |
| 31 |
monkeypatch.setattr( |
| 32 |
'cylc.flow.scripts.validate_reinstall.is_terminal', |
| 33 |
lambda: True |
| 34 |
) |
| 35 |
monkeypatch.setattr( |
| 36 |
'cylc.flow.scripts.reinstall.is_terminal', |
| 37 |
lambda: True |
| 38 |
) |
| 39 |
|
| 40 |
# patch user input |
| 41 |
count = -1 |
| 42 |
|
| 43 |
def _input(prompt): |
| 44 |
nonlocal count, responses |
| 45 |
responses = responses |
| 46 |
count += 1 |
| 47 |
print(prompt) # send the prompt to stdout for testing |
| 48 |
return responses[count] |
| 49 |
|
| 50 |
monkeypatch.setattr( |
| 51 |
'cylc.flow.scripts.validate_reinstall._input', |
| 52 |
_input, |
| 53 |
) |
| 54 |
monkeypatch.setattr( |
| 55 |
'cylc.flow.scripts.reinstall._input', |
| 56 |
_input, |
| 57 |
) |
| 58 |
|
| 59 |
|
| 60 |
async def test_prompt_for_running_workflow_with_no_changes( |
| 61 |
monkeypatch, |
| 62 |
capsys, |
| 63 |
one_run, |
| 64 |
capcall, |
| 65 |
): |
| 66 |
"""It should reinstall and restart the workflow with no changes. |
| 67 |
|
| 68 |
See: https://github.com/cylc/cylc-flow/issues/6261 |
| 69 |
|
| 70 |
We hope to get users into the habit of "cylc vip" to create a new run, |
| 71 |
and "cylc vr" to contine an old one (picking up any new changes in the |
| 72 |
process). |
| 73 |
|
| 74 |
If there are no changes to reinstall (or if the user chooses not to |
| 75 |
resintall) the "cylc vr" prompts whether to continue or do nothing. |
| 76 |
|
| 77 |
The "nothing to reinstall" situation can be interpreted two ways: |
| 78 |
1. Unexpected error, the user expected there to be something to reinstall, |
| 79 |
but there wasn't. E.g, they forgot to press save. |
| 80 |
2. Unexpected annoyance, I wanted to restart the workflow, just do it. |
| 81 |
|
| 82 |
To handle this we explain that there are no changes to reinstall and |
| 83 |
prompt the user to see if they want to press save or restart the workflow. |
| 84 |
""" |
| 85 |
# disable the clean_sysargv logic (this interferes with other tests) |
| 86 |
cleanup_sysargv_calls = capcall( |
| 87 |
'cylc.flow.scripts.validate_reinstall.cleanup_sysargv' |
| 88 |
) |
| 89 |
|
| 90 |
# answer "y" to prompt |
| 91 |
answer_prompts(monkeypatch, 'n', 'y') |
| 92 |
|
| 93 |
# attempt to restart it with "cylc vr" |
| 94 |
ret = await vr_cli( |
| 95 |
vr_gop(), ValidateReinstallOptions(), one_run |
| 96 |
) |
| 97 |
# the workflow should reinstall |
| 98 |
assert ret |
| 99 |
|
| 100 |
# the user should have been warned that there were no changes to reinstall |
| 101 |
outerr = capsys.readouterr()[0] |
| 102 |
assert 'Reinstall would make the above changes' in outerr |
| 103 |
|
| 104 |
# they should have been presented with a prompt |
| 105 |
# (to which we have hardcoded the response "y") |
| 106 |
assert 'Restart anyway?' in outerr |
| 107 |
|
| 108 |
# the workflow should have restarted |
| 109 |
assert len(cleanup_sysargv_calls) == 1 |
| 110 |
|
| 111 |
|
| 112 |
async def test_reinstall_abort( |
| 113 |
monkeypatch, |
| 114 |
capsys, |
| 115 |
one_run, |
| 116 |
): |
| 117 |
"""It should abort reinstallation according to user prompt.""" |
| 118 |
# answer 'n' to prompt |
| 119 |
answer_prompts(monkeypatch, 'n', 'n') |
| 120 |
|
| 121 |
# attempt to restart it with "cylc vr" |
| 122 |
ret = await vr_cli( |
| 123 |
vr_gop(), ValidateReinstallOptions(), one_run |
| 124 |
) |
| 125 |
assert ret is False |
| 126 |
|
| 127 |
# they should have been presented with a prompt |
| 128 |
# (to which we have hardcoded the response "n") |
| 129 |
assert 'Continue' in capsys.readouterr()[0] |