13
13
BIND 9 container and use them to send gRPC commands to the Kea container.
14
14
"""
15
15
16
- import pytest
16
+ from typing import Tuple
17
+
17
18
from grpclib .exceptions import StreamTerminatedError
18
19
19
20
from core .wrappers import Kea , Bind9
20
21
from core .grpc_client import StorkAgentGRPCClient , GetStateRspAppAccessPoint
21
22
22
23
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
+
23
59
def test_grpc_ping (kea_service : Kea , bind9_service : Bind9 ):
24
60
"""
25
61
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):
28
64
client = StorkAgentGRPCClient .for_service (kea_service )
29
65
client .fetch_certs_from (bind9_service )
30
66
31
- pytest . raises ((ConnectionResetError , StreamTerminatedError ), client .ping )
67
+ assert_raises ((ConnectionResetError , StreamTerminatedError ), client .ping )
32
68
33
69
34
70
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):
39
75
client = StorkAgentGRPCClient .for_service (kea_service )
40
76
client .fetch_certs_from (bind9_service )
41
77
42
- pytest . raises ( StreamTerminatedError , client .get_state )
78
+ assert_raises (( ConnectionResetError , StreamTerminatedError ) , client .get_state )
43
79
44
80
45
81
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):
50
86
client = StorkAgentGRPCClient .for_service (kea_service )
51
87
client .fetch_certs_from (bind9_service )
52
88
53
- pytest . raises (
54
- StreamTerminatedError ,
89
+ assert_raises (
90
+ ( ConnectionResetError , StreamTerminatedError ) ,
55
91
client .forward_to_kea_over_http ,
56
92
GetStateRspAppAccessPoint ("control" , "foo" , 42 , False ),
57
93
{
0 commit comments