@@ -11,6 +11,7 @@ def handler_function(event, context):
11
11
12
12
13
13
init_handler_has_been_called = False
14
+ python36_extractor_has_been_called = False
14
15
15
16
16
17
def init_handler ():
@@ -27,10 +28,18 @@ def _get_handler(self, handler):
27
28
class BootstrapPython36ModuleMock :
28
29
# for python3.6 version of lambda runtime bootstrap
29
30
def _get_handlers (self , handler , mode , invokeid ):
31
+ global python36_extractor_has_been_called
32
+ python36_extractor_has_been_called = True
30
33
if handler == "handler_module.handler_function" and mode == "event" :
31
34
return init_handler , handler_function
32
35
33
36
37
+ class RicBootstrapModuleMock :
38
+ def _get_handler (self , handler ):
39
+ if handler == "handler_module.handler_function" :
40
+ return handler_function
41
+
42
+
34
43
class TestLambdaHandler :
35
44
class TestWhenLambdaHandlerModuleIsLoaded :
36
45
@pytest .fixture (autouse = True )
@@ -53,48 +62,86 @@ def test_call_handler_calls_the_inner_handler(self):
53
62
assert lambda_handler_module .call_handler (event = "expected_event" ,
54
63
context = "expected_context" ) == "expected result"
55
64
56
- class TestLoadModuleFunction :
57
- class TestWhenPython38LambdaBootstrapCalls :
58
- class TestWhenHandlerEnvIsSetProperly :
59
- @before
60
- def before (self ):
61
- self .bootstrap = BootstrapModuleMock ()
62
- self .env = {"HANDLER_ENV_NAME_FOR_CODEGURU" : "handler_module.handler_function" }
63
-
64
- def test_it_returns_the_handler_function (self ):
65
- from codeguru_profiler_agent .aws_lambda .lambda_handler import load_handler
66
- assert load_handler (self .bootstrap , self .env ) == handler_function
67
-
68
- def test_it_resets_handler_env_variable (self ):
69
- from codeguru_profiler_agent .aws_lambda .lambda_handler import load_handler
70
- load_handler (self .bootstrap , self .env )
71
- assert self .env ['_HANDLER' ] == "handler_module.handler_function"
72
-
73
- class TestWhenHandlerEnvIsMissing :
74
- @before
75
- def before (self ):
76
- self .bootstrap = BootstrapModuleMock ()
77
- self .env = {}
78
-
79
- def test_it_throws_value_error (self ):
80
- with pytest .raises (ValueError ):
81
- from codeguru_profiler_agent .aws_lambda .lambda_handler import load_handler
82
- load_handler (self .bootstrap , self .env )
65
+ class TestGetHandlerExtractor :
66
+ class TestWhenRicIsAvailable :
67
+ @pytest .fixture (autouse = True )
68
+ def around (self ):
69
+ # simulate that we are in a lambda environment where the awslambdaric.bootstrap module is available
70
+ self .module_available = RicBootstrapModuleMock ()
71
+ sys .modules ['awslambdaric.bootstrap' ] = self .module_available
72
+ yield
73
+ del sys .modules ['awslambdaric.bootstrap' ]
74
+
75
+ def test_it_loads_the_ric_module_code (self ):
76
+ from codeguru_profiler_agent .aws_lambda .lambda_handler import get_lambda_handler_extractor
77
+ result = get_lambda_handler_extractor ()
78
+ assert result == self .module_available ._get_handler
79
+
80
+ class TestWhenLambdaBootstrapIsAvailable :
81
+ @pytest .fixture (autouse = True )
82
+ def around (self ):
83
+ # simulate that we are in a lambda environment where the awslambdaric.bootstrap module is not available
84
+ # but bootstrap from lambda is available.
85
+ self .module_available = BootstrapModuleMock ()
86
+ if 'awslambdaric.bootstrap' in sys .modules :
87
+ del sys .modules ['awslambdaric.bootstrap' ]
88
+ sys .modules ['bootstrap' ] = self .module_available
89
+ yield
90
+ del sys .modules ['bootstrap' ]
91
+
92
+ def test_it_loads_the_lambda_module_code (self ):
93
+ from codeguru_profiler_agent .aws_lambda .lambda_handler import get_lambda_handler_extractor
94
+ result = get_lambda_handler_extractor ()
95
+ assert result == self .module_available ._get_handler
83
96
84
97
class TestWhenPython36LambdaBootstrapCalls :
85
98
class TestWhenHandlerEnvIsSetProperly :
86
- @before
87
- def before (self ):
88
- self .bootstrap = BootstrapPython36ModuleMock ()
89
- self . env = { "HANDLER_ENV_NAME_FOR_CODEGURU" : "handler_module.handler_function" }
99
+ @pytest . fixture ( autouse = True )
100
+ def around (self ):
101
+ # simulate that we are in a lambda environment where the awslambdaric .bootstrap module is available
102
+ sys . modules [ 'bootstrap' ] = BootstrapPython36ModuleMock ()
90
103
global init_handler_has_been_called
91
104
init_handler_has_been_called = False
105
+ global python36_extractor_has_been_called
106
+ python36_extractor_has_been_called = False
107
+ yield
108
+ del sys .modules ['bootstrap' ]
92
109
93
- def test_it_returns_the_handler_function (self ):
94
- from codeguru_profiler_agent .aws_lambda .lambda_handler import load_handler
95
- assert load_handler (self .bootstrap , self .env ) == handler_function
110
+ def test_it_uses_the_old_bootstrap_code (self ):
111
+ from codeguru_profiler_agent .aws_lambda .lambda_handler import get_lambda_handler_extractor
112
+ # call extractor
113
+ get_lambda_handler_extractor ()("handler_module.handler_function" )
114
+ assert python36_extractor_has_been_called
96
115
97
116
def test_it_calls_the_init_handler (self ):
98
- from codeguru_profiler_agent .aws_lambda .lambda_handler import load_handler
99
- load_handler (self .bootstrap , self .env )
117
+ from codeguru_profiler_agent .aws_lambda .lambda_handler import get_lambda_handler_extractor
118
+ # call extractor
119
+ get_lambda_handler_extractor ()("handler_module.handler_function" )
100
120
assert init_handler_has_been_called
121
+
122
+ class TestLoadHandlerFunction :
123
+ class TestWhenHandlerEnvIsSetProperly :
124
+ @before
125
+ def before (self ):
126
+ self .extractor = BootstrapModuleMock ()._get_handler
127
+ self .env = {"HANDLER_ENV_NAME_FOR_CODEGURU" : "handler_module.handler_function" }
128
+
129
+ def test_it_returns_the_handler_function (self ):
130
+ from codeguru_profiler_agent .aws_lambda .lambda_handler import load_handler
131
+ assert load_handler (self .extractor , self .env ) == handler_function
132
+
133
+ def test_it_resets_handler_env_variable (self ):
134
+ from codeguru_profiler_agent .aws_lambda .lambda_handler import load_handler
135
+ load_handler (self .extractor , self .env )
136
+ assert self .env ['_HANDLER' ] == "handler_module.handler_function"
137
+
138
+ class TestWhenHandlerEnvIsMissing :
139
+ @before
140
+ def before (self ):
141
+ self .extractor = BootstrapModuleMock ()._get_handler
142
+ self .env = {}
143
+
144
+ def test_it_throws_value_error (self ):
145
+ with pytest .raises (ValueError ):
146
+ from codeguru_profiler_agent .aws_lambda .lambda_handler import load_handler
147
+ load_handler (self .extractor , self .env )
0 commit comments