Debian Codegraph @

tests/unit/test_host_select_remote.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 """Test the cylc.flow.host_select module with hosts.
18
19 NOTE: These tests require a remote host to work with and are skipped
20 unless one is provided.
21
22 NOTE: These are functional tests, for unit tests see the docstrings in
23 the host_select module.
24
25 """
26 from shlex import quote
27 import socket
28 from subprocess import call, DEVNULL
29
30 import pytest
31
32 from cylc.flow.cfgspec.glbl_cfg import glbl_cfg
33 from cylc.flow.exceptions import HostSelectException
34 from cylc.flow.host_select import (
35 select_host,
36 select_workflow_host
37 )
38 from cylc.flow.hostuserutil import get_fqdn_by_host
39
40
41 local_host, local_host_alises, _ = socket.gethostbyname_ex('localhost')
42 local_host_fqdn = get_fqdn_by_host(local_host)
43
44
45 try:
46 # get a suitable remote host for running tests on
47 # NOTE: do NOT copy this testing approach in other python tests
48 remote_platform = glbl_cfg().get(
49 ['platforms', '_remote_background_shared_tcp', 'hosts'],
50 []
51 )[0]
52 # don't run tests unless host is contactable
53 if call(
54 ['ssh', quote(remote_platform), 'hostname'],
55 stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL
56 ):
57 raise KeyError('remote platform')
58 # get the fqdn for this host
59 remote_platform_fqdn = get_fqdn_by_host(remote_platform)
60 except (KeyError, IndexError):
61 pytest.skip('Remote test host not available', allow_module_level=True)
62 remote_platform = None
63
64
65 def test_remote_select():
66 """Test host selection works with remote host names."""
67 assert select_host([remote_platform]) == (
68 remote_platform, remote_platform_fqdn
69 )
70
71
72 def test_remote_blacklict():
73 """Test that blacklisting works with remote host names."""
74 # blacklist by fqdn
75 with pytest.raises(HostSelectException):
76 select_host(
77 [remote_platform],
78 blacklist=[remote_platform]
79 )
80 # blacklist by short name
81 with pytest.raises(HostSelectException):
82 select_host(
83 [remote_platform],
84 blacklist=[remote_platform_fqdn]
85 )
86 # make extra sure filters are really being applied
87 for _ in range(10):
88 assert select_host(
89 [remote_platform, local_host],
90 blacklist=[remote_platform]
91 ) == (local_host, local_host_fqdn)
92
93
94 def test_remote_rankings():
95 """Test that ranking evaluation works on hosts (via SSH)."""
96 assert select_host(
97 [remote_platform],
98 ranking_string='''
99 # if this test fails due to race conditions
100 # then you have bigger issues than a test failure
101 virtual_memory().available > 1
102 getloadavg()[0] < 500
103 cpu_count() > 1
104 disk_usage('/').free > 1
105 '''
106 ) == (remote_platform, remote_platform_fqdn)
107
108
109 def test_remote_exclude(monkeypatch):
110 """Ensure that hosts get excluded if they don't meet the rankings.
111
112 Already tested elsewhere but this double-checks that it works if more
113 than one host is provided to choose from."""
114 def mocked_get_metrics(hosts, metrics, _=None):
115 # pretend that ssh to remote_platform failed
116 return {f'{local_host_fqdn}': {('cpu_count',): 123}}
117 monkeypatch.setattr(
118 'cylc.flow.host_select._get_metrics',
119 mocked_get_metrics
120 )
121 assert select_host(
122 [local_host, remote_platform],
123 ranking_string='''
124 cpu_count()
125 '''
126 ) == (local_host, local_host_fqdn)
127
128
129 def test_remote_workflow_host_select(mock_glbl_cfg):
130 """test [scheduler][run hosts]available"""
131 mock_glbl_cfg(
132 'cylc.flow.host_select.glbl_cfg',
133 f'''
134 [scheduler]
135 [[run hosts]]
136 available = {remote_platform}
137 '''
138 )
139 assert select_workflow_host() == (remote_platform, remote_platform_fqdn)
140
141
142 def test_remote_workflow_host_condemned(mock_glbl_cfg):
143 """test [scheduler][run hosts]condemned hosts"""
144 mock_glbl_cfg(
145 'cylc.flow.host_select.glbl_cfg',
146 f'''
147 [scheduler]
148 [[run hosts]]
149 available = {remote_platform}, {local_host}
150 condemned = {remote_platform}
151 '''
152 )
153 for _ in range(10):
154 assert select_workflow_host() == (local_host, local_host_fqdn)
155
156
157 def test_remote_workflow_host_rankings(mock_glbl_cfg):
158 """test [scheduler][run hosts]rankings"""
159 mock_glbl_cfg(
160 'cylc.flow.host_select.glbl_cfg',
161 f'''
162 [scheduler]
163 [[run hosts]]
164 available = {remote_platform}
165 ranking = """
166 # if this test fails due to race conditions
167 # then you are very lucky
168 virtual_memory().available > 123456789123456789
169 cpu_count() > 512
170 disk_usage('/').free > 123456789123456789
171 """
172 '''
173 )
174 with pytest.raises(HostSelectException) as excinfo:
175 select_workflow_host()
176 # ensure that host selection actually evaluated rankings
177 assert set(excinfo.value.data[remote_platform_fqdn]) - {'returncode'} == {
178 'virtual_memory().available > 123456789123456789',
179 'cpu_count() > 512',
180 "disk_usage('/').free > 123456789123456789"
181 }
182 # ensure that none of the rankings passed
183 assert not any(excinfo.value.data[remote_platform_fqdn].values())