def test_get_event_handler_syntax_error(self): importlib.invalidate_caches() with tempfile.NamedTemporaryFile(suffix=".py", dir=".", delete=False) as tmp_file: tmp_file.write( b"def syntax_error()\n\tprint('syntax error, no colon after function')" ) tmp_file.flush() filename_w_ext = os.path.basename(tmp_file.name) filename, _ = os.path.splitext(filename_w_ext) handler_name = "{}.syntax_error".format(filename) response_handler = bootstrap._get_handler(handler_name) with self.assertRaises(FaultException) as cm: response_handler() returned_exception = cm.exception self.assertEqual( self.FaultExceptionMatcher( "Syntax error in", "Runtime.UserCodeSyntaxError", ".*File.*\\.py.*Line 1.*", ), returned_exception, )
def test_get_event_handler_build_in_conflict(self): response_handler = bootstrap._get_handler("sys.hello") with self.assertRaises(FaultException) as cm: response_handler() returned_exception = cm.exception self.assertEqual( self.FaultExceptionMatcher( "Cannot use built-in module sys as a handler module", "Runtime.BuiltInModuleConflict", ), returned_exception, )
def test_get_event_handler_import_error(self): handler_name = "no_module.handler" response_handler = bootstrap._get_handler(handler_name) with self.assertRaises(FaultException) as cm: response_handler() returned_exception = cm.exception self.assertEqual( self.FaultExceptionMatcher( "Unable to import module 'no_module': No module named 'no_module'", "Runtime.ImportModuleError", ), returned_exception, )
def test_get_event_handler_bad_handler(self): handler_name = "bad_handler" response_handler = bootstrap._get_handler(handler_name) with self.assertRaises(FaultException) as cm: response_handler() returned_exception = cm.exception self.assertEqual( self.FaultExceptionMatcher( "Bad handler 'bad_handler': not enough values to unpack (expected 2, got 1)", "Runtime.MalformedHandlerName", ), returned_exception, )
def test_get_event_handler_build_in_conflict(self, mock_find_module): handler_name = "sys.hello" mock_find_module.return_value = (None, None, ("", "", C_BUILTIN)) response_handler = bootstrap._get_handler(handler_name) with self.assertRaises(FaultException) as cm: response_handler() returned_exception = cm.exception self.assertEqual( self.FaultExceptionMatcher( "Cannot use built-in module sys as a handler module", "Runtime.BuiltInModuleConflict", ), returned_exception, )
def test_get_event_handler_missing_error(self): tmp_file = tempfile.NamedTemporaryFile(suffix=".py", dir=".", delete=False) tmp_file.write(b"def wrong_handler_name():\n\tprint('hello')") tmp_file.close() filename_w_ext = os.path.basename(tmp_file.name) filename, _ = os.path.splitext(filename_w_ext) handler_name = "{}.my_handler".format(filename) response_handler = bootstrap._get_handler(handler_name) with self.assertRaises(FaultException) as cm: response_handler() returned_exception = cm.exception self.assertEqual( self.FaultExceptionMatcher( "Handler 'my_handler' missing on module '{}'".format(filename), "Runtime.HandlerNotFound", ), returned_exception, ) if os.path.exists(tmp_file.name): os.remove(tmp_file.name)
def test_handle_event_request_fault_exception_logging_syntax_error( self, mock_load_module, mock_find_module, mock_stdout ): try: eval("-") except SyntaxError as e: syntax_error = e mock_find_module.return_value = (None, None, ("", "", None)) mock_load_module.side_effect = syntax_error response_handler = bootstrap._get_handler("a.b") bootstrap.handle_event_request( self.lambda_runtime, response_handler, "invoke_id", self.event_body, "application/json", {}, {}, "invoked_function_arn", 0, bootstrap.StandardLogSink(), ) import sys sys.stderr.write(mock_stdout.getvalue()) error_logs = "[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'a': unexpected EOF while parsing (<string>, line 1)\r" error_logs += "Traceback (most recent call last):\r" error_logs += ' File "<string>" Line 1\r' error_logs += " -\n" self.assertEqual(mock_stdout.getvalue(), error_logs)
def test_get_event_handler_doent_throw_build_in_module_name(self): response_handler = bootstrap._get_handler( "tests.test_built_in_module_name.sys.my_handler") response_handler()
def test_get_event_handler_doesnt_throw_build_in_module_name_slash(self): response_handler = bootstrap._get_handler( "tests/test_built_in_module_name/sys.my_handler") response_handler()
def test_get_event_handler_slash(self): importlib.invalidate_caches() handler_name = "tests/test_handler_with_slash/test_handler.my_handler" response_handler = bootstrap._get_handler(handler_name) response_handler()