File: test_exceptions.py

package info (click to toggle)
cylc-flow 8.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,368 kB
  • sloc: python: 87,751; sh: 17,109; sql: 233; xml: 171; javascript: 78; lisp: 55; makefile: 11
file content (78 lines) | stat: -rw-r--r-- 2,455 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from textwrap import dedent

import pytest

from cylc.flow.exceptions import (
    CylcError,
    HostSelectException,
)


def test_cylc_error_str():
    error = CylcError("abcd")
    assert str(error) == "abcd"


def test_host_select_exception():
    """Test exception used for host selection failures.

    * Could not connect to hosts (e.g. SSH failure).
    * Commands failed (e.g. configuration error).
    * Could not obtain metrics (e.g. host ranking expression error).
    * No available hosts (e.g. no hosts met ranking thresholds).

    """
    # it should format the selection results nicely
    exc = HostSelectException(
        {
            'host-1': {'returncode': 1},
            'host-2': {'returncode': 1},
        },
        ranking='virtual_memory().available > 1',
    )
    assert str(exc) == dedent('''
        Could not select host from:
            host-1:
                returncode: 1
            host-2:
                returncode: 1
    ''').strip()


@pytest.mark.parametrize(
    'ret_code, expect',
    [
        # it should give a useful hint for exit code "2"
        # (error in the selection ranking expression)
        (2, 'This is likely an error in the ranking expression'),
        # it should give a useful hint for the exit code "255"
        # (ssh error)
        (255, 'Cylc could not establish SSH connection to the run hosts.')
    ]
)
def test_host_select_exception_returncodes(ret_code, expect):
    assert expect in str(
        HostSelectException(
            {
                'host-1': {'returncode': ret_code},
                'host-2': {'returncode': ret_code},
            },
            ranking='virtual_memory().available > 1',
        )
    )