-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathexceptions_test.py
More file actions
200 lines (136 loc) · 5.42 KB
/
exceptions_test.py
File metadata and controls
200 lines (136 loc) · 5.42 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
189
190
191
192
193
194
195
196
197
198
199
200
import ssl
from json import JSONDecodeError
import click
import pytest
from jumpstarter_cli_common.exceptions import (
async_handle_exceptions,
handle_exceptions,
handle_exceptions_with_reauthentication,
)
@pytest.fixture
def anyio_backend():
return "asyncio"
def test_handle_exceptions_maps_timeout_error() -> None:
@handle_exceptions
def timeout_fn():
raise TimeoutError("flash operation exceeded timeout")
with pytest.raises(click.ClickException, match="Operation timed out"):
timeout_fn()
def test_handle_exceptions_maps_tls_certificate_error() -> None:
@handle_exceptions
def cert_fn():
raise ssl.SSLCertVerificationError("certificate verify failed")
with pytest.raises(click.ClickException, match="TLS certificate validation failed"):
cert_fn()
@pytest.mark.anyio
async def test_async_handle_exceptions_maps_timeout_error() -> None:
@async_handle_exceptions
async def timeout_async_fn():
raise TimeoutError("deadline exceeded while waiting for flasher")
with pytest.raises(click.ClickException, match="Operation timed out"):
await timeout_async_fn()
def test_handle_exceptions_with_reauth_still_maps_common_exceptions() -> None:
calls = []
def login_func(_config):
calls.append(True)
@handle_exceptions_with_reauthentication(login_func)
def timeout_fn():
raise TimeoutError("operation timed out")
with pytest.raises(click.ClickException, match="Operation timed out"):
timeout_fn()
assert calls == []
def test_handle_exceptions_with_reauth_maps_keyboard_interrupt() -> None:
calls = []
def login_func(_config):
calls.append(True)
@handle_exceptions_with_reauthentication(login_func)
def interrupt_fn():
raise KeyboardInterrupt()
with pytest.raises(click.ClickException, match="Cancelled by user"):
interrupt_fn()
assert calls == []
def test_handle_exceptions_maps_file_not_found() -> None:
@handle_exceptions
def missing_file_fn():
raise FileNotFoundError("/tmp/missing.img")
with pytest.raises(click.ClickException, match="File not found"):
missing_file_fn()
def test_handle_exceptions_maps_connection_refused() -> None:
@handle_exceptions
def connection_refused_fn():
raise ConnectionRefusedError("connection refused")
with pytest.raises(click.ClickException, match="Connection was refused"):
connection_refused_fn()
def test_handle_exceptions_maps_json_decode_error() -> None:
@handle_exceptions
def bad_json_fn():
raise JSONDecodeError("Expecting value", "x", 0)
with pytest.raises(click.ClickException, match="Received invalid JSON data"):
bad_json_fn()
def test_handle_exceptions_maps_keyboard_interrupt() -> None:
@handle_exceptions
def interrupt_fn():
raise KeyboardInterrupt()
with pytest.raises(click.ClickException, match="Cancelled by user"):
interrupt_fn()
def test_handle_exceptions_maps_click_abort() -> None:
@handle_exceptions
def abort_fn():
raise click.Abort()
with pytest.raises(click.ClickException, match="Aborted by user"):
abort_fn()
def test_handle_exceptions_maps_grpc_unauthenticated() -> None:
class MockGrpcError(Exception):
def code(self):
return type("Code", (), {"name": "UNAUTHENTICATED"})()
def details(self):
return "token expired"
@handle_exceptions
def grpc_auth_fn():
raise MockGrpcError()
with pytest.raises(click.ClickException, match="Authentication or authorization failed"):
grpc_auth_fn()
def test_handle_exceptions_with_reauth_retries_on_expired_token() -> None:
"""After successful re-auth, the command is retried automatically."""
from jumpstarter.common.exceptions import ConnectionError
call_count = [0]
def login_func(config):
config.token = "new_token"
@handle_exceptions_with_reauthentication(login_func)
def command_fn(config=None):
call_count[0] += 1
if call_count[0] == 1:
exc = ConnectionError("token is expired")
exc.set_config(config)
raise exc
return "result"
config = type("Config", (), {"token": "old_token"})()
result = command_fn(config=config)
assert result == "result"
assert call_count[0] == 2
def test_handle_exceptions_with_reauth_does_not_retry_twice() -> None:
"""If retry also fails with expired token, the error propagates."""
from jumpstarter.common.exceptions import ConnectionError
login_calls = [0]
def login_func(config):
login_calls[0] += 1
@handle_exceptions_with_reauthentication(login_func)
def always_expired_fn(config=None):
exc = ConnectionError("token is expired")
exc.set_config(config)
raise exc
config = type("Config", (), {"token": "tok"})()
with pytest.raises(click.ClickException):
always_expired_fn(config=config)
assert login_calls[0] == 1
def test_handle_exceptions_maps_grpc_invalid_argument() -> None:
class MockGrpcError(Exception):
def code(self):
return type("Code", (), {"name": "INVALID_ARGUMENT"})()
def details(self):
return "invalid selector"
@handle_exceptions
def grpc_invalid_arg_fn():
raise MockGrpcError()
with pytest.raises(click.ClickException, match="Invalid request arguments"):
grpc_invalid_arg_fn()