| 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 |
"""Test cylc install.""" |
| 18 |
|
| 19 |
import pytest |
| 20 |
from pathlib import Path |
| 21 |
from typing import Callable, Tuple |
| 22 |
|
| 23 |
from cylc.flow.async_util import pipe |
| 24 |
from cylc.flow.scripts import scan |
| 25 |
from cylc.flow.workflow_files import WorkflowFiles |
| 26 |
from cylc.flow.scripts.install import ( |
| 27 |
InstallOptions, |
| 28 |
install_cli |
| 29 |
) |
| 30 |
|
| 31 |
from .network.test_scan import init_flows |
| 32 |
from .utils.entry_points import EntryPointWrapper |
| 33 |
|
| 34 |
SRV_DIR = Path(WorkflowFiles.Service.DIRNAME) |
| 35 |
CONTACT = Path(WorkflowFiles.Service.CONTACT) |
| 36 |
RUN_N = Path(WorkflowFiles.RUN_N) |
| 37 |
INSTALL = Path(WorkflowFiles.Install.DIRNAME) |
| 38 |
|
| 39 |
INSTALLED_MSG = "INSTALLED {wfrun} from" |
| 40 |
WF_ACTIVE_MSG = '1 run of "{wf}" is already active:' |
| 41 |
BAD_CONTACT_MSG = "Bad contact file:" |
| 42 |
|
| 43 |
|
| 44 |
@pytest.fixture() |
| 45 |
def patch_graphql_query( |
| 46 |
monkeypatch: pytest.MonkeyPatch |
| 47 |
): |
| 48 |
# Define a mocked graphql_query pipe function. |
| 49 |
@pipe |
| 50 |
async def _graphql_query(flow, fields, filters=None): |
| 51 |
flow.update({"status": "running"}) |
| 52 |
return flow |
| 53 |
|
| 54 |
# Swap out the function that cylc.flow.scripts.scan. |
| 55 |
monkeypatch.setattr( |
| 56 |
'cylc.flow.scripts.scan.graphql_query', |
| 57 |
_graphql_query, |
| 58 |
) |
| 59 |
|
| 60 |
|
| 61 |
@pytest.fixture() |
| 62 |
def src_run_dirs( |
| 63 |
mock_glbl_cfg: Callable, |
| 64 |
monkeypatch: pytest.MonkeyPatch, |
| 65 |
tmp_path: Path |
| 66 |
) -> Tuple[Path, Path]: |
| 67 |
"""Create some workflow source and run dirs for testing. |
| 68 |
|
| 69 |
Source dirs: |
| 70 |
<tmp-src>/w1 |
| 71 |
<tmp-src>/w2 |
| 72 |
|
| 73 |
Run dir: |
| 74 |
<tmp-run>/w1/run1 |
| 75 |
|
| 76 |
""" |
| 77 |
tmp_src_path = tmp_path / 'cylc-src' |
| 78 |
tmp_run_path = tmp_path / 'cylc-run' |
| 79 |
tmp_src_path.mkdir() |
| 80 |
tmp_run_path.mkdir() |
| 81 |
|
| 82 |
init_flows( |
| 83 |
tmp_run_path=tmp_run_path, |
| 84 |
running=('w1/run1',), |
| 85 |
tmp_src_path=tmp_src_path, |
| 86 |
src=('w1', 'w2') |
| 87 |
) |
| 88 |
mock_glbl_cfg( |
| 89 |
'cylc.flow.install.glbl_cfg', |
| 90 |
f''' |
| 91 |
[install] |
| 92 |
source dirs = {tmp_src_path} |
| 93 |
''' |
| 94 |
) |
| 95 |
monkeypatch.setattr('cylc.flow.pathutil._CYLC_RUN_DIR', tmp_run_path) |
| 96 |
|
| 97 |
return tmp_src_path, tmp_run_path |
| 98 |
|
| 99 |
|
| 100 |
async def test_install_scan_no_ping( |
| 101 |
src_run_dirs: Tuple[Path, Path], |
| 102 |
capsys: pytest.CaptureFixture, |
| 103 |
caplog: pytest.LogCaptureFixture |
| 104 |
) -> None: |
| 105 |
"""At install, running intances should be reported. |
| 106 |
|
| 107 |
Ping = False case: don't query schedulers. |
| 108 |
""" |
| 109 |
|
| 110 |
opts = InstallOptions() |
| 111 |
opts.no_ping = True |
| 112 |
|
| 113 |
await install_cli(opts, id_='w1') |
| 114 |
out = capsys.readouterr().out |
| 115 |
assert INSTALLED_MSG.format(wfrun='w1/run2') in out |
| 116 |
assert WF_ACTIVE_MSG.format(wf='w1') in out |
| 117 |
# Empty contact file faked with "touch": |
| 118 |
assert f"{BAD_CONTACT_MSG} w1/run1" in caplog.text |
| 119 |
|
| 120 |
await install_cli(opts, id_='w2') |
| 121 |
out = capsys.readouterr().out |
| 122 |
assert WF_ACTIVE_MSG.format(wf='w2') not in out |
| 123 |
assert INSTALLED_MSG.format(wfrun='w2/run1') in out |
| 124 |
|
| 125 |
|
| 126 |
async def test_install_scan_ping( |
| 127 |
src_run_dirs: Tuple[Path, Path], |
| 128 |
capsys: pytest.CaptureFixture, |
| 129 |
caplog: pytest.LogCaptureFixture, |
| 130 |
patch_graphql_query: Callable |
| 131 |
) -> None: |
| 132 |
"""At install, running intances should be reported. |
| 133 |
|
| 134 |
Ping = True case: but mock scan's scheduler query method. |
| 135 |
""" |
| 136 |
opts = InstallOptions() |
| 137 |
opts.no_ping = False |
| 138 |
|
| 139 |
await install_cli(opts, id_='w1') |
| 140 |
out = capsys.readouterr().out |
| 141 |
assert INSTALLED_MSG.format(wfrun='w1/run2') in out |
| 142 |
assert WF_ACTIVE_MSG.format(wf='w1') in out |
| 143 |
assert scan.FLOW_STATE_SYMBOLS["running"] in out |
| 144 |
# Empty contact file faked with "touch": |
| 145 |
assert f"{BAD_CONTACT_MSG} w1/run1" in caplog.text |
| 146 |
|
| 147 |
await install_cli(opts, id_='w2') |
| 148 |
out = capsys.readouterr().out |
| 149 |
assert INSTALLED_MSG.format(wfrun='w2/run1') in out |
| 150 |
assert WF_ACTIVE_MSG.format(wf='w2') not in out |
| 151 |
|
| 152 |
|
| 153 |
async def test_install_gets_back_compat_mode_for_plugins( |
| 154 |
src_run_dirs: Tuple[Path, Path], |
| 155 |
monkeypatch: pytest.MonkeyPatch, |
| 156 |
capcall, |
| 157 |
capsys: pytest.CaptureFixture, |
| 158 |
): |
| 159 |
"""Assert that cylc install will detect whether a workflow |
| 160 |
should use back compat mode _before_ running pre_configure plugins |
| 161 |
so that those plugins can use that information. |
| 162 |
""" |
| 163 |
# track calls of the check_deprecation method |
| 164 |
# (this is the thing that sets cylc.flow.flags.back_compat) |
| 165 |
check_deprecation_calls = capcall( |
| 166 |
'cylc.flow.scripts.install.check_deprecation' |
| 167 |
) |
| 168 |
|
| 169 |
@EntryPointWrapper |
| 170 |
def failIfDeprecated(*args, **kwargs): |
| 171 |
"""A fake Cylc Plugin entry point""" |
| 172 |
# print the number of times the check_deprecation method has been |
| 173 |
# called |
| 174 |
print(f'CALLS={len(check_deprecation_calls)}') |
| 175 |
# return a blank result |
| 176 |
return { |
| 177 |
'env': {}, |
| 178 |
'template_variables': {}, |
| 179 |
} |
| 180 |
|
| 181 |
# Monkeypatch our fake entry point into iter_entry_points: |
| 182 |
monkeypatch.setattr( |
| 183 |
'cylc.flow.plugins.iter_entry_points', |
| 184 |
lambda namespace: ( |
| 185 |
[failIfDeprecated] if namespace == 'cylc.pre_configure' else [] |
| 186 |
) |
| 187 |
) |
| 188 |
|
| 189 |
# install the workflow |
| 190 |
opts = InstallOptions() |
| 191 |
await install_cli(opts, id_='w1') |
| 192 |
|
| 193 |
# ensure the check_deprecation method was called before the plugin was run |
| 194 |
assert 'CALLS=1' in capsys.readouterr()[0] |