Debian Codegraph @

tests/unit/network/test__init__.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 """Test __init__.py for network interfaces to Cylc scheduler objects."""
17
18 import pytest
19
20 import cylc.flow
21 from cylc.flow.exceptions import CylcVersionError
22 from cylc.flow.network import get_location
23 from cylc.flow.workflow_files import ContactFileFields
24
25
26 BASE_CONTACT_DATA = {
27 ContactFileFields.HOST: 'foo',
28 ContactFileFields.PORT: '42',
29 }
30
31
32 @pytest.fixture()
33 def mpatch_get_fqdn_by_host(monkeypatch):
34 """Monkeypatch function used the same by all tests."""
35 monkeypatch.setattr(
36 cylc.flow.network, 'get_fqdn_by_host', lambda _: 'myhost.x.y.z'
37 )
38
39
40 def test_get_location_ok(monkeypatch, mpatch_get_fqdn_by_host):
41 """It passes when information is available."""
42 contact_data = BASE_CONTACT_DATA.copy()
43 contact_data[ContactFileFields.PUBLISH_PORT] = '8042'
44 contact_data[ContactFileFields.VERSION] = cylc.flow.__version__
45 monkeypatch.setattr(
46 cylc.flow.network, 'load_contact_file', lambda _: contact_data
47 )
48 assert get_location('_') == (
49 'myhost.x.y.z', 42, 8042, cylc.flow.__version__
50 )
51
52
53 def test_get_location_old_contact_file(monkeypatch, mpatch_get_fqdn_by_host):
54 """It Fails because it's not a Cylc 8 workflow."""
55 contact_data = BASE_CONTACT_DATA.copy()
56 contact_data['CYLC_SUITE_PUBLISH_PORT'] = '8042'
57 contact_data['CYLC_VERSION'] = '5.1.2'
58 monkeypatch.setattr(
59 cylc.flow.network, 'load_contact_file', lambda _: contact_data
60 )
61 with pytest.raises(CylcVersionError, match=r'.*5.1.2.*'):
62 get_location('_')