@@ -11,6 +11,7 @@ def handler_function(event, context):
1111
1212
1313init_handler_has_been_called = False
14+ python36_extractor_has_been_called = False
1415
1516
1617def init_handler ():
@@ -27,10 +28,18 @@ def _get_handler(self, handler):
2728class BootstrapPython36ModuleMock :
2829 # for python3.6 version of lambda runtime bootstrap
2930 def _get_handlers (self , handler , mode , invokeid ):
31+ global python36_extractor_has_been_called
32+ python36_extractor_has_been_called = True
3033 if handler == "handler_module.handler_function" and mode == "event" :
3134 return init_handler , handler_function
3235
3336
37+ class RicBootstrapModuleMock :
38+ def _get_handler (self , handler ):
39+ if handler == "handler_module.handler_function" :
40+ return handler_function
41+
42+
3443class TestLambdaHandler :
3544 class TestWhenLambdaHandlerModuleIsLoaded :
3645 @pytest .fixture (autouse = True )
@@ -53,48 +62,86 @@ def test_call_handler_calls_the_inner_handler(self):
5362 assert lambda_handler_module .call_handler (event = "expected_event" ,
5463 context = "expected_context" ) == "expected result"
5564
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
8396
8497 class TestWhenPython36LambdaBootstrapCalls :
8598 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 ()
90103 global init_handler_has_been_called
91104 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' ]
92109
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
96115
97116 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" )
100120 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