Skip to content

Commit cbc17b4

Browse files
committed
[#1345] Add specialized assert function
1 parent 45442c9 commit cbc17b4

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

tests/system/tests/test_grpc.py

+41-5
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,49 @@
1313
BIND 9 container and use them to send gRPC commands to the Kea container.
1414
"""
1515

16-
import pytest
16+
from typing import Tuple
17+
1718
from grpclib.exceptions import StreamTerminatedError
1819

1920
from core.wrappers import Kea, Bind9
2021
from core.grpc_client import StorkAgentGRPCClient, GetStateRspAppAccessPoint
2122

2223

24+
def assert_raises(exceptions: Tuple[Exception], func, *args, **kwargs):
25+
"""
26+
Assert that the function raises the expected exception.
27+
It handles the case when the original exception is covered by another one
28+
thrown in its except block.
29+
30+
It is needed because the GRPC client raises various exceptions in various
31+
environments:
32+
33+
- StreamTerminatedError - on my local machine
34+
- ConnectionResetError - in Docker-in-Docker running on my local machine
35+
- StreamTerminatedError but the AttributeError is raised when the exception
36+
is internally processed by the GRPC library - on CI
37+
38+
I spent a lot of time trying to figure out why the tests failed differently
39+
but I didn't find the reason. I decided to write this helper function as
40+
a workaround.
41+
"""
42+
43+
raised = False
44+
try:
45+
func(*args, **kwargs)
46+
except exceptions:
47+
raised = True
48+
except Exception as ex: # pylint: disable=broad-except
49+
original_exception = ex.__context__
50+
if original_exception is not None and isinstance(
51+
original_exception, exceptions
52+
):
53+
raised = True
54+
55+
if not raised:
56+
raise AssertionError(f"Function did not raise any of {exceptions}")
57+
58+
2359
def test_grpc_ping(kea_service: Kea, bind9_service: Bind9):
2460
"""
2561
Check if the gRPC Ping command doesn't work if the request was not signed
@@ -28,7 +64,7 @@ def test_grpc_ping(kea_service: Kea, bind9_service: Bind9):
2864
client = StorkAgentGRPCClient.for_service(kea_service)
2965
client.fetch_certs_from(bind9_service)
3066

31-
pytest.raises((ConnectionResetError, StreamTerminatedError), client.ping)
67+
assert_raises((ConnectionResetError, StreamTerminatedError), client.ping)
3268

3369

3470
def test_grpc_get_state(kea_service: Kea, bind9_service: Bind9):
@@ -39,7 +75,7 @@ def test_grpc_get_state(kea_service: Kea, bind9_service: Bind9):
3975
client = StorkAgentGRPCClient.for_service(kea_service)
4076
client.fetch_certs_from(bind9_service)
4177

42-
pytest.raises(StreamTerminatedError, client.get_state)
78+
assert_raises((ConnectionResetError, StreamTerminatedError), client.get_state)
4379

4480

4581
def test_grpc_forward_to_kea_over_http(kea_service: Kea, bind9_service: Bind9):
@@ -50,8 +86,8 @@ def test_grpc_forward_to_kea_over_http(kea_service: Kea, bind9_service: Bind9):
5086
client = StorkAgentGRPCClient.for_service(kea_service)
5187
client.fetch_certs_from(bind9_service)
5288

53-
pytest.raises(
54-
StreamTerminatedError,
89+
assert_raises(
90+
(ConnectionResetError, StreamTerminatedError),
5591
client.forward_to_kea_over_http,
5692
GetStateRspAppAccessPoint("control", "foo", 42, False),
5793
{

0 commit comments

Comments
 (0)