File: test_task_remote_mgr.py

package info (click to toggle)
cylc-flow 8.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,368 kB
  • sloc: python: 87,751; sh: 17,109; sql: 233; xml: 171; javascript: 78; lisp: 55; makefile: 11
file content (342 lines) | stat: -rw-r--r-- 11,194 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from contextlib import suppress
from pathlib import Path
from time import sleep
import pytest
from typing import (Any, Optional)
from unittest.mock import MagicMock, Mock

from cylc.flow.exceptions import PlatformError
from cylc.flow.network.client_factory import CommsMeth
from cylc.flow.task_remote_mgr import (
    REMOTE_FILE_INSTALL_DONE, REMOTE_INIT_IN_PROGRESS, TaskRemoteMgr)
from cylc.flow.workflow_files import WorkflowFiles, get_workflow_srv_dir


Fixture = Any


@pytest.mark.parametrize(
    'comms_meth, expected',
    [
        (CommsMeth.SSH, True),
        (CommsMeth.ZMQ, True),
        (CommsMeth.POLL, False)
    ]
)
def test__remote_init_items(comms_meth: CommsMeth, expected: bool):
    """Test _remote_init_items().

    Should only includes files under .service/
    """
    id_ = 'barclay'
    mock_mgr = Mock(workflow=id_)
    srv_dir = get_workflow_srv_dir(id_)
    items = TaskRemoteMgr._remote_init_items(mock_mgr, comms_meth)
    if expected:
        assert items
        for src_path, dst_path in items:
            Path(src_path).relative_to(srv_dir)
            Path(dst_path).relative_to(WorkflowFiles.Service.DIRNAME)
    else:
        assert not items


@pytest.mark.parametrize(
    'install_target, skip_expected, expected_status',
    [('localhost', True, REMOTE_FILE_INSTALL_DONE),
     ('something_else', False, REMOTE_INIT_IN_PROGRESS)]
)
def test_remote_init_skip(
        install_target: str, skip_expected: bool, expected_status: str,
        monkeypatch: Fixture):
    """Test the TaskRemoteMgr.remote_init() skips localhost install target.

    Params:
        install_target: The platform's install target.
        skip_expected: Whether remote init is expected to be skipped.
        expected_status: The expected value of
            TaskRemoteMgr.remote_init_map[install_target].
    """
    platform = {
        'install target': install_target,
        'communication method': CommsMeth.POLL,
        'hosts': ['localhost'],
        'selection': {'method': 'random'},
        'name': 'foo'
    }
    mock_task_remote_mgr = MagicMock(remote_init_map={}, bad_hosts=[])
    mock_construct_ssh_cmd = Mock()
    monkeypatch.setattr('cylc.flow.task_remote_mgr.construct_ssh_cmd',
                        mock_construct_ssh_cmd)
    for item in (
            'tarfile',
            'get_remote_workflow_run_dir',
            'get_dirs_to_symlink'):
        monkeypatch.setattr(f'cylc.flow.task_remote_mgr.{item}', MagicMock())

    TaskRemoteMgr.remote_init(mock_task_remote_mgr, platform)
    call_expected = not skip_expected
    assert mock_task_remote_mgr._remote_init_items.called is call_expected
    assert mock_construct_ssh_cmd.called is call_expected
    assert mock_task_remote_mgr.proc_pool.put_command.called is call_expected
    status = mock_task_remote_mgr.remote_init_map[install_target]
    assert status == expected_status


@pytest.mark.parametrize(
    'install_target, load_type, expected',
    [
        ('install_target', None, '03-start-install_target.log'),
        ('some_install_target',
         'restart',
         '03-restart-some_install_target.log'),
        ('another_install_target',
         'reload',
         '03-reload-another_install_target.log')
    ]
)
def test_get_log_file_name(tmp_path: Path,
                           install_target: str,
                           load_type: Optional[str],
                           expected: str):
    task_remote_mgr = TaskRemoteMgr('some_workflow', None, None, None, None)
    if load_type == 'restart':
        task_remote_mgr.is_restart = True
    elif load_type == 'reload':
        task_remote_mgr.is_reload = True
    # else load type is start (no flag required)
    run_dir = tmp_path
    log_dir = run_dir / 'some_workflow' / 'log' / 'remote-install'
    log_dir.mkdir(parents=True)
    for log_num in range(1, 3):
        Path(f"{log_dir}/{log_num:02d}-start-{install_target}.log").touch()
        sleep(0.1)
    log_name = task_remote_mgr.get_log_file_name(
        install_target, install_log_dir=log_dir)
    assert log_name == expected


@pytest.mark.parametrize(
    'platform_names, install_targets, glblcfg, expect',
    [
        pytest.param(
            # Two platforms share an install target. Both are reachable.
            ['sir_handel', 'peter_sam'],
            ['mountain_railway'],
            '''
            [platforms]
                [[peter_sam, sir_handel]]
                    install target = mountain_railway
            ''',
            {
                'targets': {'mountain_railway': ['peter_sam', 'sir_handel']},
                'unreachable': set()
            },
            id='basic'
        ),
        pytest.param(
            # Two platforms share an install target. Both are unreachable.
            set(),
            ['mountain_railway'],
            '''
            [platforms]
                [[peter_sam, sir_handel]]
                    install target = mountain_railway
            ''',
            {
                'targets': {'mountain_railway': []},
                'unreachable': {'mountain_railway'}
            },
            id='platform_unreachable'
        ),
        pytest.param(
            # One of our install targets matches one of our platforms,
            # but only implicitly; i.e. the platform name is the same as the
            # install target name.
            ['sir_handel'],
            ['sir_handel'],
            '''
            [platforms]
                [[sir_handel]]
            ''',
            {
                'targets': {'sir_handel': ['sir_handel']},
                'unreachable': set()
            },
            id='implicit-target'
        ),
        pytest.param(
            # One of our install targets matches one of our platforms,
            # but only implicitly, and the platform name is defined using a
            # regex.
            ['sir_handel42'],
            ['sir_handel42'],
            '''
            [platforms]
                [[sir_handel..]]
            ''',
            {
                'targets': {'sir_handel42': ['sir_handel42']},
                'unreachable': set()
            },
            id='implicit-target-regex'
        ),
        pytest.param(
            # One of our install targets (rusty) has no defined platforms
            # causing a PlatformLookupError.
            ['duncan', 'rusty'],
            ['mountain_railway', 'rusty'],
            '''
            [platforms]
                [[duncan]]
                    install target = mountain_railway
            ''',
            {
                'targets': {'mountain_railway': ['duncan']},
                'unreachable': {'rusty'}
            },
            id='PlatformLookupError'
        )
    ]
)
def test_map_platforms_used_for_install_targets(
    mock_glbl_cfg,
    platform_names, install_targets, glblcfg, expect, caplog
):
    def flatten_install_targets_map(itm):
        result = {}
        for target, platforms in itm.items():
            result[target] = sorted([p['name'] for p in platforms])
        return result

    mock_glbl_cfg('cylc.flow.platforms.glbl_cfg', glblcfg)

    install_targets_map = TaskRemoteMgr._get_remote_tidy_targets(
        set(platform_names), set(install_targets))

    with suppress(KeyError):
        install_targets_map.pop('localhost')

    assert (
        expect['targets'] == flatten_install_targets_map(install_targets_map))

    if expect['unreachable']:
        for unreachable in expect["unreachable"]:
            assert (
                unreachable in caplog.records[0].msg)
    else:
        assert not caplog.records


shared_eval_params = [
    pytest.param(
        'localhost', {}, 'localhost', id="localhost"
    ),
    pytest.param(
        '$(some-cmd)', {}, None, id="subshell_1st_eval"
    ),
    pytest.param(
        '$(some-cmd)', {'some-cmd': None}, None,
        id="subshell_awaiting_eval"
    ),
    pytest.param(
        '$(some-cmd)', {'some-cmd': 'isaac clarke'}, 'isaac clarke',
        id="subshell_finished_eval"
    ),
    pytest.param(
        '$SOME_ENV', {}, 'nolan stross', id="env_var"
    ),
    pytest.param(
        '$(env-cmd)', {'env-cmd': '$SOME_ENV'}, 'nolan stross',
        id="subshell_env_var"
    ),
    pytest.param(
        'titan_station', {}, 'titan_station', id="verbatim_name"
    ),
    pytest.param(
        '', {}, 'localhost', id="empty"
    ),
]


@pytest.fixture
def task_remote_mgr_eval(monkeypatch: pytest.MonkeyPatch):
    """Fixture providing a task remote manager for eval_platform() &
    eval_host() tests."""
    def _task_remote_mgr_eval(remote_cmd_map: dict) -> TaskRemoteMgr:
        monkeypatch.setenv('SOME_ENV', 'nolan stross')
        task_remote_mgr = TaskRemoteMgr(
            workflow='usg_ishimura',
            proc_pool=Mock(),
            bad_hosts=[],
            db_mgr=None,
            server=None
        )
        task_remote_mgr.remote_command_map = remote_cmd_map
        return task_remote_mgr

    return _task_remote_mgr_eval


@pytest.mark.parametrize(
    'eval_str, remote_cmd_map, expected',
    [
        *shared_eval_params,
        pytest.param(
            'localhost_tau_volantis', {}, 'localhost_tau_volantis',
            id="name_begin_w_localhost"
        )
    ]
)
def test_eval_platform(
    eval_str: str, remote_cmd_map: dict, expected: Optional[str],
    task_remote_mgr_eval,
):
    task_remote_mgr: TaskRemoteMgr = task_remote_mgr_eval(remote_cmd_map)
    assert task_remote_mgr.eval_platform(eval_str) == expected


def test_eval_platform_bad(task_remote_mgr_eval):
    exc_msg = "foreign contaminant detected"
    task_remote_mgr: TaskRemoteMgr = task_remote_mgr_eval(
        {'cmd': PlatformError(exc_msg, 'aegis_vii')}
    )
    with pytest.raises(PlatformError, match=exc_msg):
        task_remote_mgr.eval_platform('$(cmd)')


@pytest.mark.parametrize(
    'eval_str, remote_cmd_map, expected',
    [
        *shared_eval_params,
        pytest.param(
            'localhost4.localdomain4', {}, 'localhost', id="localhost_variant"
        ),
        pytest.param(
            '`other cmd`', {'other cmd': 'nicole brennan'}, 'nicole brennan',
            id="backticks"
        ),
    ]
)
def test_eval_host(
    eval_str: str, remote_cmd_map: dict, expected: Optional[str],
    task_remote_mgr_eval,
):
    task_remote_mgr: TaskRemoteMgr = task_remote_mgr_eval(remote_cmd_map)
    assert task_remote_mgr.eval_host(eval_str) == expected