Debian Codegraph @

tests/u/test_exceptions.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 from textwrap import dedent
18
19 import pytest
20
21 from cylc.flow.exceptions import (
22 CylcError,
23 HostSelectException,
24 )
25
26
27 def test_cylc_error_str():
28 error = CylcError("abcd")
29 assert str(error) == "abcd"
30
31
32 def test_host_select_exception():
33 """Test exception used for host selection failures.
34
35 * Could not connect to hosts (e.g. SSH failure).
36 * Commands failed (e.g. configuration error).
37 * Could not obtain metrics (e.g. host ranking expression error).
38 * No available hosts (e.g. no hosts met ranking thresholds).
39
40 """
41 # it should format the selection results nicely
42 exc = HostSelectException(
43 {
44 'host-1': {'returncode': 1},
45 'host-2': {'returncode': 1},
46 },
47 ranking='virtual_memory().available > 1',
48 )
49 assert str(exc) == dedent('''
50 Could not select host from:
51 host-1:
52 returncode: 1
53 host-2:
54 returncode: 1
55 ''').strip()
56
57
58 @pytest.mark.parametrize(
59 'ret_code, expect',
60 [
61 # it should give a useful hint for exit code "2"
62 # (error in the selection ranking expression)
63 (2, 'This is likely an error in the ranking expression'),
64 # it should give a useful hint for the exit code "255"
65 # (ssh error)
66 (255, 'Cylc could not establish SSH connection to the run hosts.')
67 ]
68 )
69 def test_host_select_exception_returncodes(ret_code, expect):
70 assert expect in str(
71 HostSelectException(
72 {
73 'host-1': {'returncode': ret_code},
74 'host-2': {'returncode': ret_code},
75 },
76 ranking='virtual_memory().available > 1',
77 )
78 )