File: test_utils.py

package info (click to toggle)
ansible-core 2.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 32,752 kB
  • sloc: python: 181,000; cs: 4,929; sh: 4,611; xml: 34; makefile: 21
file content (192 lines) | stat: -rw-r--r-- 5,000 bytes parent folder | download | duplicates (3)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from __future__ import annotations

import pytest

from ansible._internal._errors import _error_factory

from ansible.errors import AnsibleError
from ansible._internal._datatag._tags import Origin
from ansible._internal._errors._error_utils import format_exception_message
from ansible.utils.display import _format_message
from ansible.module_utils._internal import _messages
from units.mock.error_helper import raise_exceptions


_shared_cause = Exception('shared cause')
_origin_x = Origin(path='/x')
_origin_y = Origin(path='/y')


@pytest.mark.parametrize("exceptions, expected_message_chain, expected_formatted_message", (
    ([AnsibleError('a')], 'a', None),
    ([AnsibleError('a'), AnsibleError('b')], 'a: b', None),
    ([AnsibleError('a: b'), AnsibleError('b')], 'a: b', None),
    ([Exception('a')], 'a', None),
    ([Exception('a'), Exception('b')], 'a: b', None),
    ([Exception('a: b'), Exception('b')], 'a: b', None),
    ([AnsibleError('a'), Exception('b')], 'a: b', None),
    ([AnsibleError('a: b'), Exception('b')], 'a: b', None),
    ([Exception('a'), AnsibleError('b')], 'a: b', None),
    ([Exception('a: b'), AnsibleError('b')], 'a: b', None),
    ([AnsibleError('a'), AnsibleError('b'), Exception('c'), Exception('d')], 'a: b: c: d', None),
    ([AnsibleError('a: b: c: d'), AnsibleError('b'), Exception('c: d'), Exception('d')], 'a: b: c: d', None),

    # collapsing help_text

    (
        [
            AnsibleError('a', help_text='one'),
            Exception('b'),
        ],
        'a: b',
        (
            'a: b one'
        ),
    ),
    (
        [
            AnsibleError('a', help_text='one'),
            AnsibleError('b', help_text='one'),
        ],
        'a: b',
        (
            'a: b one'
        ),
    ),
    (
        [
            AnsibleError('a', help_text='one'),
            Exception('b'),
            AnsibleError('c', help_text='one'),
        ],
        'a: b: c',
        (
            'a: b: c one'
        ),
    ),
    (
        [
            AnsibleError('a', help_text='one'),
            AnsibleError('b', help_text='two'),
        ],
        'a: b',
        (
            'a: b\n\n'
            'a one\n\n'
            '<<< caused by >>>\n\n'
            'b two'
        ),
    ),
    (
        [
            AnsibleError('a'),
            AnsibleError('b', help_text='one'),
        ],
        'a: b',
        (
            'a: b\n\n'
            'a\n\n'
            '<<< caused by >>>\n\n'
            'b one'
        ),
    ),

    # collapsing origin

    (
        [
            AnsibleError('a', obj=_origin_x.tag('x'), orig_exc=Exception('ignored')),
            Exception('b'),
        ],
        'a: b',
        (
            'a: b\n'
            'Origin: /x'
        ),
    ),
    (
        [
            AnsibleError('a', obj=_origin_x.tag('x'), orig_exc=_shared_cause),
            _shared_cause,
        ],
        'a: shared cause',
        (
            'a: shared cause\n'
            'Origin: /x'
        ),
    ),
    (
        [
            # same as above, but exercises the old `orig_exc` path that displays a warning
            AnsibleError('a', obj=_origin_x.tag('x'), orig_exc=Exception('b')),
        ],
        'a: b',
        (
            'a: b\n'
            'Origin: /x'
        ),
    ),
    (
        [
            AnsibleError('a', obj=_origin_x.tag('x')),
            AnsibleError('b', obj=_origin_x.tag('x')),
        ],
        'a: b',
        (
            'a: b\n'
            'Origin: /x'
        ),
    ),
    (
        [
            AnsibleError('a', obj=_origin_x.tag('x')),
            Exception('b'),
            AnsibleError('c', obj=_origin_x.tag('x')),
        ],
        'a: b: c',
        (
            'a: b: c\n'
            'Origin: /x'
        ),
    ),
    (
        [
            AnsibleError('a', obj=_origin_x.tag('x')),
            AnsibleError('b', obj=_origin_y.tag('x')),
        ],
        'a: b',
        (
            'a: b\n\n'
            'a\n'
            'Origin: /x\n\n'
            '<<< caused by >>>\n\n'
            'b\n'
            'Origin: /y'
        ),
    ),
    (
        [
            AnsibleError('a'),
            AnsibleError('b', obj=_origin_y.tag('x')),
        ],
        'a: b',
        (
            'a: b\n\n'
            'a\n\n'
            '<<< caused by >>>\n\n'
            'b\n'
            'Origin: /y'
        ),
    ),
), ids=str)
def test_error_messages(exceptions: list[BaseException], expected_message_chain: str, expected_formatted_message: str | None) -> None:
    with pytest.raises(Exception) as error:
        raise_exceptions(exceptions)

    event = _error_factory.ControllerEventFactory.from_exception(error.value, False)

    message_chain = format_exception_message(error.value)
    formatted_message = _format_message(_messages.ErrorSummary(event=event), False)

    assert message_chain == expected_message_chain
    assert formatted_message.strip() == (expected_formatted_message or expected_message_chain)