6
6
7
7
import pytest
8
8
from lumigo_tracer .auto_instrument_handler import _handler , ORIGINAL_HANDLER_KEY
9
+ from lumigo_tracer .libs .lambda_runtime_exception import FaultException
9
10
10
11
11
12
def abc (* args , ** kwargs ):
12
13
return {"hello" : "world" }
13
14
14
15
15
- def test_happy_flow (monkeypatch ):
16
+ def test_happy_flow (monkeypatch , context ):
16
17
monkeypatch .setenv (ORIGINAL_HANDLER_KEY , "test_auto_instrument_handler.abc" )
17
- assert _handler ({}, {} ) == {"hello" : "world" }
18
+ assert _handler ({}, context ) == {"hello" : "world" }
18
19
19
20
20
- def test_hierarchy_happy_flow (monkeypatch ):
21
+ def test_hierarchy_happy_flow (monkeypatch , context ):
21
22
monkeypatch .setenv (ORIGINAL_HANDLER_KEY , "lumigo_tracer/test_module/test.handler" )
22
- assert _handler ({}, {} ) == {"hello" : "world" }
23
+ assert _handler ({}, context ) == {"hello" : "world" }
23
24
24
25
25
- def test_import_error (monkeypatch ):
26
+ def test_hierarchy_happy_flow_with_dots (monkeypatch , context ):
27
+ monkeypatch .setenv (ORIGINAL_HANDLER_KEY , "lumigo_tracer.test_module.test.handler" )
28
+ assert _handler ({}, context ) == {"hello" : "world" }
29
+
30
+
31
+ def test_import_error (monkeypatch , context ):
26
32
monkeypatch .setenv (ORIGINAL_HANDLER_KEY , "blabla.not.exists" )
27
33
28
34
try :
29
- _handler ({}, {} )
30
- except ImportError as e :
35
+ _handler ({}, context )
36
+ except FaultException as e :
31
37
# Note: We're not using pytest.raises in order to get the exception context
32
38
assert "Runtime.ImportModuleError" in str (e )
33
39
assert "another exception occurred" not in traceback .format_exc ()
34
40
else :
35
41
assert False
36
42
37
43
38
- def test_no_env_handler_error (monkeypatch ):
44
+ def test_no_env_handler_error (monkeypatch , context ):
39
45
monkeypatch .delenv (ORIGINAL_HANDLER_KEY , None )
40
46
41
47
with pytest .raises (Exception ) as e :
42
- _handler ({}, {} )
48
+ _handler ({}, context )
43
49
assert "Could not find the original handler" in str (e .value )
44
50
45
51
46
52
def test_error_in_original_handler_no_extra_exception_log (monkeypatch , context ):
47
53
monkeypatch .setattr (imp , "load_module" , mock .Mock (side_effect = ZeroDivisionError ))
48
- monkeypatch .setenv (ORIGINAL_HANDLER_KEY , "sys.exit " )
54
+ monkeypatch .setenv (ORIGINAL_HANDLER_KEY , "lumigo_tracer.test_module.test.handler " )
49
55
50
56
try :
51
57
_handler ({}, context )
@@ -58,37 +64,37 @@ def test_error_in_original_handler_no_extra_exception_log(monkeypatch, context):
58
64
59
65
def test_error_in_original_handler_syntax_error (monkeypatch , context ):
60
66
monkeypatch .setattr (imp , "load_module" , mock .Mock (side_effect = SyntaxError ))
61
- monkeypatch .setenv (ORIGINAL_HANDLER_KEY , "sys.exit " )
67
+ monkeypatch .setenv (ORIGINAL_HANDLER_KEY , "lumigo_tracer.test_module.test.handler " )
62
68
63
69
try :
64
70
_handler ({}, context )
65
- except SyntaxError as e :
71
+ except FaultException as e :
66
72
# Note: We're not using pytest.raises in order to get the exception context
67
73
assert "Runtime.UserCodeSyntaxError" in str (e )
68
74
assert "another exception occurred" not in traceback .format_exc ()
69
75
else :
70
76
assert False
71
77
72
78
73
- def test_handler_bad_format (monkeypatch ):
79
+ def test_handler_bad_format (monkeypatch , context ):
74
80
monkeypatch .setenv (ORIGINAL_HANDLER_KEY , "no_method" )
75
81
76
82
try :
77
- _handler ({}, {} )
78
- except ValueError as e :
83
+ _handler ({}, context )
84
+ except FaultException as e :
79
85
# Note: We're not using pytest.raises in order to get the exception context
80
86
assert "Runtime.MalformedHandlerName" in str (e )
81
87
assert "another exception occurred" not in traceback .format_exc ()
82
88
else :
83
89
assert False
84
90
85
91
86
- def test_handler_not_found (monkeypatch ):
87
- monkeypatch .setenv (ORIGINAL_HANDLER_KEY , "sys .not_found" )
92
+ def test_handler_not_found (monkeypatch , context ):
93
+ monkeypatch .setenv (ORIGINAL_HANDLER_KEY , "lumigo_tracer .not_found" )
88
94
89
95
try :
90
- _handler ({}, {} )
91
- except Exception as e :
96
+ _handler ({}, context )
97
+ except FaultException as e :
92
98
# Note: We're not using pytest.raises in order to get the exception context
93
99
assert "Runtime.HandlerNotFound" in str (e )
94
100
assert "another exception occurred" not in traceback .format_exc ()
0 commit comments