-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathtest_exceptions.py
More file actions
188 lines (152 loc) · 5.86 KB
/
test_exceptions.py
File metadata and controls
188 lines (152 loc) · 5.86 KB
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
# Copyright 2016-2024 Swiss National Supercomputing Centre (CSCS/ETH Zurich)
# ReFrame Project Developers. See the top-level LICENSE file for details.
#
# SPDX-License-Identifier: BSD-3-Clause
import pytest
import reframe.core.exceptions as exc
def assert_args(exc_type, *args):
e = exc_type(*args)
assert args == e.args
def test_soft_error():
with pytest.raises(exc.ReframeError, match=r'random error'):
raise exc.ReframeError('random error')
assert_args(exc.ReframeError, 'error msg')
assert_args(exc.ReframeError, 'error msg', 'another arg')
def test_reraise_soft_error():
try:
try:
raise ValueError('random value error')
except ValueError as e:
# reraise as ReframeError
raise exc.ReframeError('soft error') from e
except exc.ReframeError as e:
assert 'soft error: random value error' == str(e)
def test_fatal_error():
try:
raise exc.ReframeFatalError('fatal error')
except Exception:
pytest.fail('fatal error should not derive from Exception')
except BaseException:
pass
def test_reraise_fatal_error():
try:
try:
raise ValueError('random value error')
except ValueError as e:
# reraise as ReframeError
raise exc.ReframeFatalError('fatal error') from e
except exc.ReframeFatalError as e:
assert 'fatal error: random value error' == str(e)
def test_spawned_process_error():
exc_args = ('foo bar', 'partial output', 'error message', 1)
e = exc.SpawnedProcessError(*exc_args)
with pytest.raises(
exc.ReframeError,
match=(r"command 'foo bar' failed with exit code 1:\n"
r"--- stdout ---\n"
r'partial output\n'
r"--- stdout ---\n"
r"--- stderr ---\n"
r"error message\n"
r"--- stderr ---")
):
raise e
assert exc_args == e.args
def test_spawned_process_error_list_args():
exc_args = (['foo', 'bar'], 'partial output', 'error message', 1)
e = exc.SpawnedProcessError(*exc_args)
with pytest.raises(
exc.ReframeError,
match=(r"command 'foo bar' failed with exit code 1:\n"
r"--- stdout ---\n"
r'partial output\n'
r"--- stdout ---\n"
r"--- stderr ---\n"
r"error message\n"
r"--- stderr ---")
):
raise e
assert exc_args == e.args
def test_spawned_process_error_nostdout():
exc_args = ('foo bar', '', 'error message', 1)
e = exc.SpawnedProcessError(*exc_args)
with pytest.raises(
exc.ReframeError,
match=(r"command 'foo bar' failed with exit code 1:\n"
r"--- stdout ---\n"
r"--- stdout ---\n"
r"--- stderr ---\n"
r"error message\n"
r"--- stderr ---")
):
raise e
def test_spawned_process_error_nostderr():
exc_args = ('foo bar', 'partial output', '', 1)
e = exc.SpawnedProcessError(*exc_args)
with pytest.raises(
exc.ReframeError,
match=(r"command 'foo bar' failed with exit code 1:\n"
r"--- stdout ---\n"
r'partial output\n'
r"--- stdout ---\n"
r"--- stderr ---\n"
r"--- stderr ---")
):
raise e
def test_spawned_process_timeout():
exc_args = ('foo bar', 'partial output', 'partial error', 10)
e = exc.SpawnedProcessTimeout(*exc_args)
with pytest.raises(exc.ReframeError,
match=(r"command 'foo bar' timed out after 10s:\n"
r"--- stdout ---\n"
r'partial output\n'
r"--- stdout ---\n"
r"--- stderr ---\n"
r"partial error\n"
r"--- stderr ---")):
raise e
def test_spawned_process_timeout_nostdout():
exc_args = ('foo bar', '', 'partial error', 10)
e = exc.SpawnedProcessTimeout(*exc_args)
with pytest.raises(exc.ReframeError,
match=(r"command 'foo bar' timed out after 10s:\n"
r"--- stdout ---\n"
r"--- stdout ---\n"
r"--- stderr ---\n"
r"partial error\n"
r"--- stderr ---")):
raise e
def test_spawned_process_timeout_nostderr():
exc_args = ('foo bar', 'partial output', '', 10)
e = exc.SpawnedProcessTimeout(*exc_args)
with pytest.raises(exc.ReframeError,
match=(r"command 'foo bar' timed out after 10s:\n"
r"--- stdout ---\n"
r'partial output\n'
r"--- stdout ---\n"
r"--- stderr ---\n"
r"--- stderr ---")):
raise e
def test_job_error():
exc_args = ('some error',)
e = exc.JobError(*exc_args, jobid=1234)
assert 1234 == e.jobid
with pytest.raises(exc.JobError, match=r'\[jobid=1234\] some error'):
raise e
assert exc_args == e.args
def test_reraise_job_error():
try:
try:
raise ValueError('random value error')
except ValueError as e:
raise exc.JobError('some error', jobid=1234) from e
except exc.JobError as e:
assert '[jobid=1234] some error: random value error' == str(e)
def test_reraise_job_error_no_message():
try:
try:
raise ValueError('random value error')
except ValueError as e:
raise exc.JobError(jobid=1234) from e
except exc.JobError as e:
assert '[jobid=1234]: random value error' == str(e)