Debian Codegraph @

tests/i/test_task_remote_mgr.py

raw
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 cylc
18 from cylc.flow.task_remote_mgr import (
19 REMOTE_FILE_INSTALL_DONE,
20 REMOTE_FILE_INSTALL_FAILED
21 )
22
23
24 async def test_remote_tidy(
25 flow,
26 scheduler,
27 start,
28 mock_glbl_cfg,
29 one_conf,
30 monkeypatch
31 ):
32 """Remote tidy gets platforms for install targets.
33
34 In particular, referencing https://github.com/cylc/cylc-flow/issues/5429,
35 ensure that install targets defined implicitly by platform name are found.
36
37 Mock remote init map:
38 - Include an install target (quiz) with
39 message != REMOTE_FILE_INSTALL_DONE to ensure that
40 this is picked out.
41 - Install targets where we can get a platform
42 - foo - Install target is implicitly the platfrom name.
43 - bar9 - The install target is implicitly the plaform name,
44 and the platform name matches a platform regex.
45 - baz - Install target is set explicitly.
46 - An install target (qux) where we cannot get a platform: Ensure
47 that we get the desired error.
48
49 Test that platforms with no good hosts (no host not in bad hosts).
50 """
51 # Monkeypatch away subprocess.Popen calls - prevent any interaction with
52 # remotes actually happening:
53 class MockProc:
54 def __init__(self, *args, **kwargs):
55 self.poll = lambda: True
56 if (
57 'baum' in args[0]
58 or 'bay' in args[0]
59 ):
60 self.returncode = 255
61 else:
62 self.returncode = 0
63 self.communicate = lambda: ('out', 'err')
64
65 monkeypatch.setattr(
66 cylc.flow.task_remote_mgr,
67 'Popen',
68 lambda *args, **kwargs: MockProc(*args, **kwargs)
69 )
70
71 # Monkeypath function to add a sort order which we don't need in the
72 # real code but rely to prevent the test becoming flaky:
73 def mock_get_install_target_platforms_map(*args, **kwargs):
74 """Add sort to original function to ensure test consistency"""
75 from cylc.flow.platforms import get_install_target_to_platforms_map
76 result = get_install_target_to_platforms_map(*args, **kwargs)
77 sorted_result = {}
78 for key in sorted(result):
79 sorted_result[key] = sorted(
80 result[key], key=lambda x: x['name'], reverse=True)
81 return sorted_result
82
83 monkeypatch.setattr(
84 cylc.flow.task_remote_mgr,
85 'get_install_target_to_platforms_map',
86 mock_get_install_target_platforms_map
87 )
88
89 # Set up global config
90 mock_glbl_cfg(
91 'cylc.flow.platforms.glbl_cfg',
92 '''
93 [platforms]
94 [[foo]]
95 # install target = foo (implicit)
96 # hosts = foo (implicit)
97 [[bar.]]
98 # install target = bar1 to bar9 (implicit)
99 # hosts = bar1 to bar9 (implicit)
100 [[baz]]
101 install target = baz
102 # baum and bay should be uncontactable:
103 hosts = baum, bay, baz
104 [[[selection]]]
105 method = definition order
106 [[notthisone]]
107 install target = baz
108 hosts = baum, bay
109 [[bay]]
110 ''',
111 )
112
113 # Get a scheduler:
114 id_ = flow(one_conf)
115 schd = scheduler(id_)
116 async with start(schd) as log:
117 # Write database with 6 tasks using 3 platforms:
118 platforms = ['baz', 'bar9', 'foo', 'notthisone', 'bay']
119 line = r"('', '', {}, 0, 1, '', '', 0,'', '', '', 0, '', '{}', 4, '')"
120 stmt = r"INSERT INTO task_jobs VALUES" + r','.join([
121 line.format(i, platform) for i, platform in enumerate(platforms)
122 ])
123 schd.workflow_db_mgr.pri_dao.connect().execute(stmt)
124 schd.workflow_db_mgr.pri_dao.connect().commit()
125
126 # Mock a remote init map.
127 schd.task_job_mgr.task_remote_mgr.remote_init_map = {
128 'baz': REMOTE_FILE_INSTALL_DONE, # Should match platform baz
129 'bar9': REMOTE_FILE_INSTALL_DONE, # Should match platform bar.
130 'foo': REMOTE_FILE_INSTALL_DONE, # Should match plaform foo
131 'qux': REMOTE_FILE_INSTALL_DONE, # Should not match a plaform
132 'quiz': REMOTE_FILE_INSTALL_FAILED, # Should not be considered
133 'bay': REMOTE_FILE_INSTALL_DONE, # Should return NoPlatforms
134 }
135
136 # Clear the log, run the test:
137 log.clear()
138 schd.task_job_mgr.task_remote_mgr.bad_hosts.update(['baum', 'bay'])
139 schd.task_job_mgr.task_remote_mgr.remote_tidy()
140 pass
141
142 records = [str(r.msg) for r in log.records]
143
144 # We can't get qux, no defined platform has a matching install target:
145 qux_msg = 'No platforms available to remote tidy install targets:\n * qux'
146 assert qux_msg in records
147
148 # We can get foo bar baz, and we try to remote tidy them.
149 # (This will ultimately fail, but past the point we are testing).
150 for target in ['foo', 'bar9', 'baz']:
151 msg = f'platform: {target} - remote tidy (on {target})'
152 assert msg in records
153
154 # We haven't done anything with Quiz because we're only looking
155 # at cases where platform == REMOTE_FILE_INSTALL_DONE
156 assert not [r for r in records if 'quiz' in r]
157
158 notthisone_msg = (
159 'platform: notthisone - clean up did not complete'
160 '\nUnable to find valid host for notthisone'
161 )
162 assert notthisone_msg in records
163
164 bay_msg = (
165 'Unable to find a platform from install target'
166 ' bay during remote tidy.')
167 assert bay_msg in records