| 1 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 2 |
# |
| 3 |
# This program is free software: you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation, either version 3 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
# |
| 16 |
# Tests for the get_host_from_platform lookup. |
| 17 |
|
| 18 |
import pytest |
| 19 |
|
| 20 |
from cylc.flow.exceptions import NoHostsError, NoPlatformsError |
| 21 |
from cylc.flow.platforms import get_host_from_platform, get_platform_from_group |
| 22 |
from cylc.flow.exceptions import CylcError |
| 23 |
|
| 24 |
|
| 25 |
TEST_PLATFORM = { |
| 26 |
'name': 'Elephant', |
| 27 |
'hosts': ['nellie', 'dumbo', 'jumbo'], |
| 28 |
'selection': {'method': 'definition order'} |
| 29 |
} |
| 30 |
|
| 31 |
TEST_GROUP = { |
| 32 |
'platforms': ['one', 'two', 'three'], |
| 33 |
'selection': {'method': 'definition order'} |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
@pytest.mark.parametrize( |
| 38 |
'badhosts, expect', |
| 39 |
[ |
| 40 |
pytest.param(None, 'nellie'), |
| 41 |
pytest.param({'nellie', 'dumbo'}, 'jumbo') |
| 42 |
] |
| 43 |
) |
| 44 |
def test_get_host_from_platform(badhosts, expect): |
| 45 |
platform = TEST_PLATFORM |
| 46 |
assert get_host_from_platform(platform, badhosts) == expect |
| 47 |
|
| 48 |
|
| 49 |
def test_get_host_from_platform_fails_no_goodhosts(): |
| 50 |
platform = TEST_PLATFORM |
| 51 |
with pytest.raises(NoHostsError) as err: |
| 52 |
get_host_from_platform(platform, {'nellie', 'dumbo', 'jumbo'}) |
| 53 |
assert err.exconly() == ( |
| 54 |
'cylc.flow.exceptions.NoHostsError: ' |
| 55 |
'Unable to find valid host for Elephant' |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
def test_get_host_from_platform_fails_bad_method(): |
| 60 |
platform = TEST_PLATFORM.copy() |
| 61 |
platform['selection']['method'] = 'roulette' |
| 62 |
with pytest.raises(CylcError) as err: |
| 63 |
get_host_from_platform(platform, {'Elephant'}) |
| 64 |
assert err.exconly() == ( |
| 65 |
'cylc.flow.exceptions.CylcError: method "roulette" is not a ' |
| 66 |
'supported host selection method.' |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
@pytest.mark.parametrize( |
| 71 |
'expect, bad_hosts', |
| 72 |
[ |
| 73 |
pytest.param( |
| 74 |
'one', None, id='No bad_hosts get platform one'), |
| 75 |
pytest.param( |
| 76 |
'three', {'foo', 'bar'}, |
| 77 |
id='All platforms bad except platform three') |
| 78 |
] |
| 79 |
) |
| 80 |
def test_get_platform_from_group(monkeypatch, expect, bad_hosts): |
| 81 |
def get_plat(name): |
| 82 |
if name == 'three': |
| 83 |
return {'hosts': {'foo', 'bar', 'baz'}} |
| 84 |
else: |
| 85 |
return {'hosts': {'foo', 'bar'}} |
| 86 |
|
| 87 |
monkeypatch.setattr('cylc.flow.platforms.platform_from_name', get_plat) |
| 88 |
platform = get_platform_from_group(TEST_GROUP, 'mygroup_name', bad_hosts) |
| 89 |
assert platform == expect |
| 90 |
|
| 91 |
|
| 92 |
def test_get_platform_from_group_fails_no_goodhosts(monkeypatch): |
| 93 |
monkeypatch.setattr( |
| 94 |
'cylc.flow.platforms.platform_from_name', |
| 95 |
lambda _: {'hosts': {'foo', 'bar', 'baz'}}, |
| 96 |
) |
| 97 |
with pytest.raises(NoPlatformsError) as err: |
| 98 |
get_platform_from_group( |
| 99 |
TEST_GROUP, 'mygroup_name', {'foo', 'bar', 'baz'} |
| 100 |
) |
| 101 |
assert err.exconly() == ( |
| 102 |
'cylc.flow.exceptions.NoPlatformsError: ' |
| 103 |
'Unable to find a platform from group mygroup_name.' |
| 104 |
) |
| 105 |
|
| 106 |
|
| 107 |
def test_get_platform_from_group_fails_bad_method(monkeypatch): |
| 108 |
monkeypatch.setattr( |
| 109 |
'cylc.flow.platforms.platform_from_name', |
| 110 |
lambda _: {'hosts': {'foo', 'bar', 'baz'}}, |
| 111 |
) |
| 112 |
group = TEST_GROUP.copy() |
| 113 |
group['selection']['method'] = 'roulette' |
| 114 |
with pytest.raises(CylcError) as err: |
| 115 |
get_platform_from_group(group, {'foo'}) |
| 116 |
assert err.exconly() == ( |
| 117 |
'cylc.flow.exceptions.CylcError: "roulette" is not a ' |
| 118 |
'supported platform selection method.' |
| 119 |
) |