-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_error.py
More file actions
108 lines (87 loc) · 3.57 KB
/
Copy pathtest_error.py
File metadata and controls
108 lines (87 loc) · 3.57 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
"""Tests for agentrun_cli._utils.error — the handle_errors decorator."""
import json
import sys
from unittest.mock import patch
import click
import pytest
from agentrun_cli._utils.error import (
EXIT_AUTH_ERROR,
EXIT_BAD_INPUT,
EXIT_NOT_FOUND,
EXIT_SERVER_ERROR,
PREREQUISITES_HINT,
handle_errors,
)
def _make_raising_func(exc):
"""Return a decorated function that raises *exc* when called."""
@handle_errors
def fn():
raise exc
return fn
class TestHandleErrors:
"""Cover all exception branches in the handle_errors decorator."""
def test_success_passthrough(self):
@handle_errors
def fn():
return "ok"
assert fn() == "ok"
def test_click_usage_error_reraises(self):
with pytest.raises(click.UsageError):
_make_raising_func(click.UsageError("bad param"))()
def test_resource_not_exist(self):
# Simulate SDK exception with matching type name
exc = type("ResourceNotExistError", (Exception,), {})("resource missing")
with pytest.raises(SystemExit) as exc_info:
_make_raising_func(exc)()
assert exc_info.value.code == EXIT_NOT_FOUND
def test_not_exist_variant(self):
exc = type("SomethingNotExistException", (Exception,), {})("not found")
with pytest.raises(SystemExit) as exc_info:
_make_raising_func(exc)()
assert exc_info.value.code == EXIT_NOT_FOUND
def test_resource_already_exist(self):
exc = type("ResourceAlreadyExistError", (Exception,), {})("already exists")
with pytest.raises(SystemExit) as exc_info:
_make_raising_func(exc)()
assert exc_info.value.code == EXIT_BAD_INPUT
def test_already_exist_variant(self):
exc = type("AlreadyExistException", (Exception,), {})("dup")
with pytest.raises(SystemExit) as exc_info:
_make_raising_func(exc)()
assert exc_info.value.code == EXIT_BAD_INPUT
def test_forbidden_auth_error(self):
exc = Exception("Forbidden: access denied")
with pytest.raises(SystemExit) as exc_info:
_make_raising_func(exc)()
assert exc_info.value.code == EXIT_AUTH_ERROR
def test_invalid_access_key_auth_error(self):
exc = Exception("InvalidAccessKeyId.NotFound")
with pytest.raises(SystemExit) as exc_info:
_make_raising_func(exc)()
assert exc_info.value.code == EXIT_AUTH_ERROR
def test_signature_mismatch_auth_error(self):
exc = Exception("SignatureDoesNotMatch")
with pytest.raises(SystemExit) as exc_info:
_make_raising_func(exc)()
assert exc_info.value.code == EXIT_AUTH_ERROR
@pytest.mark.parametrize(
"message",
[
"AccessDenied: user has no permission",
"NoPermission to call CreateSuperAgent",
"Role AliyunAgentRunSuperAgentRole not authorized",
"EntityNotExist.Role: role not found",
],
)
def test_prerequisite_patterns_emit_hint(self, message, capsys):
with pytest.raises(SystemExit) as exc_info:
_make_raising_func(Exception(message))()
assert exc_info.value.code == EXIT_AUTH_ERROR
payload = json.loads(capsys.readouterr().err)
assert payload["error"] == "AuthenticationFailed"
assert payload["hint"] == PREREQUISITES_HINT
def test_generic_exception(self):
exc = Exception("something unexpected")
with pytest.raises(SystemExit) as exc_info:
_make_raising_func(exc)()
assert exc_info.value.code == EXIT_SERVER_ERROR