From 477f16c09bf5ddc5cec383fe07d2fbd3897cc46a Mon Sep 17 00:00:00 2001 From: Keshav Date: Sat, 2 Apr 2022 07:45:23 +0200 Subject: [PATCH 1/3] Update patch version to v2.0.2 --- awslambdaric/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awslambdaric/__init__.py b/awslambdaric/__init__.py index eba1e47..470433c 100644 --- a/awslambdaric/__init__.py +++ b/awslambdaric/__init__.py @@ -2,4 +2,4 @@ Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. """ -__version__ = "2.0.1" +__version__ = "2.0.2" From 27084c3c7695068a6314836f53bf673c65b3441f Mon Sep 17 00:00:00 2001 From: Keshav Date: Sat, 2 Apr 2022 07:55:11 +0200 Subject: [PATCH 2/3] Run linter --- awslambdaric/bootstrap.py | 18 ++--- awslambdaric/lambda_context.py | 12 +-- awslambdaric/lambda_runtime_client.py | 12 +-- tests/test_bootstrap.py | 104 ++++++++++---------------- tests/test_lambda_runtime_client.py | 12 +-- 5 files changed, 52 insertions(+), 106 deletions(-) diff --git a/awslambdaric/bootstrap.py b/awslambdaric/bootstrap.py index a08c233..0e5e303 100644 --- a/awslambdaric/bootstrap.py +++ b/awslambdaric/bootstrap.py @@ -143,13 +143,9 @@ def handle_event_request( invoke_id, invoked_function_arn, ) - event = lambda_runtime_client.marshaller.unmarshal_request( - event_body, content_type - ) + event = lambda_runtime_client.marshaller.unmarshal_request(event_body, content_type) response = request_handler(event, lambda_context) - result, result_content_type = lambda_runtime_client.marshaller.marshal_response( - response - ) + result, result_content_type = lambda_runtime_client.marshaller.marshal_response(response) except FaultException as e: xray_fault = make_xray_fault("LambdaValidationError", e.msg, os.getcwd(), []) error_result = make_error(e.msg, e.exception_type, e.trace, invoke_id) @@ -173,9 +169,7 @@ def handle_event_request( invoke_id, to_json(error_result), to_json(xray_fault) ) else: - lambda_runtime_client.post_invocation_result( - invoke_id, result, result_content_type - ) + lambda_runtime_client.post_invocation_result(invoke_id, result, result_content_type) def parse_json_header(header, name): @@ -220,9 +214,7 @@ def build_fault_result(exc_info, msg): tb_tuples = tb_tuples[i:] break - return make_error( - msg if msg else str(value), etype.__name__, traceback.format_list(tb_tuples) - ) + return make_error(msg if msg else str(value), etype.__name__, traceback.format_list(tb_tuples)) def make_xray_fault(ex_type, ex_msg, working_dir, tb_tuples): @@ -327,7 +319,7 @@ def __init__(self, fd): self.frame_type = 0xA55A0001 .to_bytes(4, "big") def __enter__(self): - self.file = os.fdopen(self.fd, 'wb', 0) + self.file = os.fdopen(self.fd, "wb", 0) return self def __exit__(self, exc_type, exc_value, exc_tb): diff --git a/awslambdaric/lambda_context.py b/awslambdaric/lambda_context.py index 1465827..d96171b 100644 --- a/awslambdaric/lambda_context.py +++ b/awslambdaric/lambda_context.py @@ -27,18 +27,12 @@ def __init__( self.client_context = make_obj_from_dict(ClientContext, client_context) if self.client_context is not None: - self.client_context.client = make_obj_from_dict( - Client, self.client_context.client - ) + self.client_context.client = make_obj_from_dict(Client, self.client_context.client) self.identity = make_obj_from_dict(CognitoIdentity, {}) if cognito_identity is not None: - self.identity.cognito_identity_id = cognito_identity.get( - "cognitoIdentityId" - ) - self.identity.cognito_identity_pool_id = cognito_identity.get( - "cognitoIdentityPoolId" - ) + self.identity.cognito_identity_id = cognito_identity.get("cognitoIdentityId") + self.identity.cognito_identity_pool_id = cognito_identity.get("cognitoIdentityPoolId") self._epoch_deadline_time_in_ms = epoch_deadline_time_in_ms diff --git a/awslambdaric/lambda_runtime_client.py b/awslambdaric/lambda_runtime_client.py index 2066f6c..0ec74de 100644 --- a/awslambdaric/lambda_runtime_client.py +++ b/awslambdaric/lambda_runtime_client.py @@ -9,9 +9,7 @@ def _user_agent(): - py_version = ( - f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}" - ) + py_version = f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}" pkg_version = __version__ return f"aws-lambda-python/{py_version}-{pkg_version}" @@ -77,14 +75,10 @@ def wait_next_invocation(self): event_body=response_body, ) - def post_invocation_result( - self, invoke_id, result_data, content_type="application/json" - ): + def post_invocation_result(self, invoke_id, result_data, content_type="application/json"): runtime_client.post_invocation_result( invoke_id, - result_data - if isinstance(result_data, bytes) - else result_data.encode("utf-8"), + result_data if isinstance(result_data, bytes) else result_data.encode("utf-8"), content_type, ) diff --git a/tests/test_bootstrap.py b/tests/test_bootstrap.py index 83b86ba..75f6bf9 100644 --- a/tests/test_bootstrap.py +++ b/tests/test_bootstrap.py @@ -277,21 +277,15 @@ def __init__(self, message): xray_fault = json.loads(args[2]) self.assertEqual(xray_fault["working_directory"], self.working_directory) self.assertEqual(len(xray_fault["exceptions"]), 1) - self.assertEqual( - xray_fault["exceptions"][0]["message"], expected_response["errorMessage"] - ) - self.assertEqual( - xray_fault["exceptions"][0]["type"], expected_response["errorType"] - ) + self.assertEqual(xray_fault["exceptions"][0]["message"], expected_response["errorMessage"]) + self.assertEqual(xray_fault["exceptions"][0]["type"], expected_response["errorType"]) self.assertEqual(len(xray_fault["exceptions"][0]["stack"]), 1) self.assertEqual( xray_fault["exceptions"][0]["stack"][0]["label"], "raise_exception_handler" ) self.assertIsInstance(xray_fault["exceptions"][0]["stack"][0]["line"], int) self.assertTrue( - xray_fault["exceptions"][0]["stack"][0]["path"].endswith( - os.path.relpath(__file__) - ) + xray_fault["exceptions"][0]["stack"][0]["path"].endswith(os.path.relpath(__file__)) ) self.assertEqual(len(xray_fault["paths"]), 1) self.assertTrue(xray_fault["paths"][0].endswith(os.path.relpath(__file__))) @@ -329,21 +323,15 @@ def __init__(self, message): xray_fault = json.loads(args[2]) self.assertEqual(xray_fault["working_directory"], self.working_directory) self.assertEqual(len(xray_fault["exceptions"]), 1) - self.assertEqual( - xray_fault["exceptions"][0]["message"], expected_response["errorMessage"] - ) - self.assertEqual( - xray_fault["exceptions"][0]["type"], expected_response["errorType"] - ) + self.assertEqual(xray_fault["exceptions"][0]["message"], expected_response["errorMessage"]) + self.assertEqual(xray_fault["exceptions"][0]["type"], expected_response["errorType"]) self.assertEqual(len(xray_fault["exceptions"][0]["stack"]), 1) self.assertEqual( xray_fault["exceptions"][0]["stack"][0]["label"], "raise_exception_handler" ) self.assertIsInstance(xray_fault["exceptions"][0]["stack"][0]["line"], int) self.assertTrue( - xray_fault["exceptions"][0]["stack"][0]["path"].endswith( - os.path.relpath(__file__) - ) + xray_fault["exceptions"][0]["stack"][0]["path"].endswith(os.path.relpath(__file__)) ) self.assertEqual(len(xray_fault["paths"]), 1) self.assertTrue(xray_fault["paths"][0].endswith(os.path.relpath(__file__))) @@ -472,9 +460,7 @@ def raise_exception_handler(json_input, lambda_context): try: import invalid_module # noqa: F401 except ImportError: - raise bootstrap.FaultException( - "FaultExceptionType", "Fault exception msg", None - ) + raise bootstrap.FaultException("FaultExceptionType", "Fault exception msg", None) bootstrap.handle_event_request( self.lambda_runtime, @@ -488,14 +474,14 @@ def raise_exception_handler(json_input, lambda_context): 0, bootstrap.StandardLogSink(), ) - error_logs = "[ERROR] FaultExceptionType: Fault exception msg\rTraceback (most recent call last):\n" + error_logs = ( + "[ERROR] FaultExceptionType: Fault exception msg\rTraceback (most recent call last):\n" + ) self.assertEqual(mock_stdout.getvalue(), error_logs) @patch("sys.stdout", new_callable=StringIO) - def test_handle_event_request_fault_exception_logging_nomessage_notrace( - self, mock_stdout - ): + def test_handle_event_request_fault_exception_logging_nomessage_notrace(self, mock_stdout): def raise_exception_handler(json_input, lambda_context): try: import invalid_module # noqa: F401 @@ -519,9 +505,7 @@ def raise_exception_handler(json_input, lambda_context): self.assertEqual(mock_stdout.getvalue(), error_logs) @patch("sys.stdout", new_callable=StringIO) - def test_handle_event_request_fault_exception_logging_notype_notrace( - self, mock_stdout - ): + def test_handle_event_request_fault_exception_logging_notype_notrace(self, mock_stdout): def raise_exception_handler(json_input, lambda_context): try: import invalid_module # noqa: F401 @@ -545,9 +529,7 @@ def raise_exception_handler(json_input, lambda_context): self.assertEqual(mock_stdout.getvalue(), error_logs) @patch("sys.stdout", new_callable=StringIO) - def test_handle_event_request_fault_exception_logging_notype_nomessage( - self, mock_stdout - ): + def test_handle_event_request_fault_exception_logging_notype_nomessage(self, mock_stdout): def raise_exception_handler(json_input, lambda_context): try: import invalid_module # noqa: F401 @@ -670,9 +652,7 @@ def __init__(self): self.assertEqual(actual["exceptions"][0]["stack"][0]["label"], "test_method") self.assertEqual(actual["exceptions"][0]["stack"][0]["path"], "test.py") self.assertEqual(actual["exceptions"][0]["stack"][0]["line"], 28) - self.assertEqual( - actual["exceptions"][0]["stack"][1]["label"], "another_test_method" - ) + self.assertEqual(actual["exceptions"][0]["stack"][1]["label"], "another_test_method") self.assertEqual(actual["exceptions"][0]["stack"][1]["path"], "another_test.py") self.assertEqual(actual["exceptions"][0]["stack"][1]["line"], 2718) @@ -682,9 +662,7 @@ class FaultExceptionMatcher(BaseException): def __init__(self, msg, exception_type=None, trace_pattern=None): self.msg = msg self.exception_type = exception_type - self.trace = ( - trace_pattern if trace_pattern is None else re.compile(trace_pattern) - ) + self.trace = trace_pattern if trace_pattern is None else re.compile(trace_pattern) def __eq__(self, other): trace_matches = True @@ -732,12 +710,8 @@ def test_get_event_handler_import_error(self): 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')" - ) + 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) @@ -759,9 +733,7 @@ def test_get_event_handler_syntax_error(self): def test_get_event_handler_missing_error(self): importlib.invalidate_caches() - with tempfile.NamedTemporaryFile( - suffix=".py", dir=".", delete=False - ) as tmp_file: + with tempfile.NamedTemporaryFile(suffix=".py", dir=".", delete=False) as tmp_file: tmp_file.write(b"def wrong_handler_name():\n\tprint('hello')") tmp_file.flush() @@ -800,15 +772,11 @@ def test_get_event_handler_build_in_conflict(self): ) 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 = bootstrap._get_handler("tests/test_built_in_module_name/sys.my_handler") response_handler() 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 = bootstrap._get_handler("tests.test_built_in_module_name.sys.my_handler") response_handler() @@ -914,7 +882,9 @@ def test_log_error_standard_log_sink(self, mock_stdout): def test_log_error_framed_log_sink(self): with NamedTemporaryFile() as temp_file: - with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as log_sink: + with bootstrap.FramedTelemetryLogSink( + os.open(temp_file.name, os.O_CREAT | os.O_RDWR) + ) as log_sink: err_to_log = bootstrap.make_error("Error message", "ErrorType", None) bootstrap.log_error(err_to_log, log_sink) @@ -949,7 +919,9 @@ def test_log_error_indentation_standard_log_sink(self, mock_stdout): def test_log_error_indentation_framed_log_sink(self): with NamedTemporaryFile() as temp_file: - with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as log_sink: + with bootstrap.FramedTelemetryLogSink( + os.open(temp_file.name, os.O_CREAT | os.O_RDWR) + ) as log_sink: err_to_log = bootstrap.make_error( "Error message", "ErrorType", [" line1 ", " line2 ", " "] ) @@ -974,9 +946,7 @@ def test_log_error_indentation_framed_log_sink(self): @patch("sys.stdout", new_callable=StringIO) def test_log_error_empty_stacktrace_line_standard_log_sink(self, mock_stdout): - err_to_log = bootstrap.make_error( - "Error message", "ErrorType", ["line1", "", "line2"] - ) + err_to_log = bootstrap.make_error("Error message", "ErrorType", ["line1", "", "line2"]) bootstrap.log_error(err_to_log, bootstrap.StandardLogSink()) expected_logged_error = "[ERROR] ErrorType: Error message\rTraceback (most recent call last):\rline1\r\rline2\n" @@ -984,7 +954,9 @@ def test_log_error_empty_stacktrace_line_standard_log_sink(self, mock_stdout): def test_log_error_empty_stacktrace_line_framed_log_sink(self): with NamedTemporaryFile() as temp_file: - with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as log_sink: + with bootstrap.FramedTelemetryLogSink( + os.open(temp_file.name, os.O_CREAT | os.O_RDWR) + ) as log_sink: err_to_log = bootstrap.make_error( "Error message", "ErrorType", ["line1", "", "line2"] ) @@ -1082,7 +1054,9 @@ def test_create_framed_telemetry_log_sinks(self): def test_single_frame(self): with NamedTemporaryFile() as temp_file: message = "hello world\nsomething on a new line!\n" - with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as ls: + with bootstrap.FramedTelemetryLogSink( + os.open(temp_file.name, os.O_CREAT | os.O_RDWR) + ) as ls: ls.log(message) with open(temp_file.name, "rb") as f: content = f.read() @@ -1101,7 +1075,9 @@ def test_multiple_frame(self): first_message = "hello world\nsomething on a new line!" second_message = "hello again\nhere's another message\n" - with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as ls: + with bootstrap.FramedTelemetryLogSink( + os.open(temp_file.name, os.O_CREAT | os.O_RDWR) + ) as ls: ls.log(first_message) ls.log(second_message) @@ -1141,9 +1117,7 @@ def test_run(self, mock_runtime_client, mock_handle_event_request): ] with self.assertRaises(TypeError): - bootstrap.run( - expected_app_root, expected_handler, expected_lambda_runtime_api_addr - ) + bootstrap.run(expected_app_root, expected_handler, expected_lambda_runtime_api_addr) mock_handle_event_request.assert_called_once() @@ -1166,9 +1140,7 @@ class TestException(Exception): mock_sys.exit.side_effect = TestException("Boom!") with self.assertRaises(TestException): - bootstrap.run( - expected_app_root, expected_handler, expected_lambda_runtime_api_addr - ) + bootstrap.run(expected_app_root, expected_handler, expected_lambda_runtime_api_addr) mock_sys.exit.assert_called_once_with(1) diff --git a/tests/test_lambda_runtime_client.py b/tests/test_lambda_runtime_client.py index 814ca96..53a0051 100644 --- a/tests/test_lambda_runtime_client.py +++ b/tests/test_lambda_runtime_client.py @@ -166,9 +166,7 @@ def test_post_invocation_error(self, mock_runtime_client): runtime_client.post_invocation_error(invoke_id, error_data, xray_fault) - mock_runtime_client.post_error.assert_called_once_with( - invoke_id, error_data, xray_fault - ) + mock_runtime_client.post_error.assert_called_once_with(invoke_id, error_data, xray_fault) @patch("awslambdaric.lambda_runtime_client.runtime_client") def test_post_invocation_error_with_large_xray_cause(self, mock_runtime_client): @@ -190,13 +188,9 @@ def test_post_invocation_error_with_too_large_xray_cause(self, mock_runtime_clie invoke_id = "1234" too_large_xray_fault = "a" * int(1024 * 1024) - runtime_client.post_invocation_error( - invoke_id, error_data, too_large_xray_fault - ) + runtime_client.post_invocation_error(invoke_id, error_data, too_large_xray_fault) - mock_runtime_client.post_error.assert_called_once_with( - invoke_id, error_data, "" - ) + mock_runtime_client.post_error.assert_called_once_with(invoke_id, error_data, "") def test_connection_refused(self): with self.assertRaises(ConnectionRefusedError): From 271cdf6c1eefe5eab6b0cf694d7c9b35c85ae463 Mon Sep 17 00:00:00 2001 From: Keshav Date: Sat, 2 Apr 2022 08:08:46 +0200 Subject: [PATCH 3/3] Refix linting issues --- awslambdaric/bootstrap.py | 16 +++-- awslambdaric/lambda_context.py | 12 +++- awslambdaric/lambda_runtime_client.py | 12 +++- tests/test_bootstrap.py | 84 +++++++++++++++++++-------- tests/test_lambda_runtime_client.py | 12 +++- 5 files changed, 100 insertions(+), 36 deletions(-) diff --git a/awslambdaric/bootstrap.py b/awslambdaric/bootstrap.py index 0e5e303..87bdbb6 100644 --- a/awslambdaric/bootstrap.py +++ b/awslambdaric/bootstrap.py @@ -143,9 +143,13 @@ def handle_event_request( invoke_id, invoked_function_arn, ) - event = lambda_runtime_client.marshaller.unmarshal_request(event_body, content_type) + event = lambda_runtime_client.marshaller.unmarshal_request( + event_body, content_type + ) response = request_handler(event, lambda_context) - result, result_content_type = lambda_runtime_client.marshaller.marshal_response(response) + result, result_content_type = lambda_runtime_client.marshaller.marshal_response( + response + ) except FaultException as e: xray_fault = make_xray_fault("LambdaValidationError", e.msg, os.getcwd(), []) error_result = make_error(e.msg, e.exception_type, e.trace, invoke_id) @@ -169,7 +173,9 @@ def handle_event_request( invoke_id, to_json(error_result), to_json(xray_fault) ) else: - lambda_runtime_client.post_invocation_result(invoke_id, result, result_content_type) + lambda_runtime_client.post_invocation_result( + invoke_id, result, result_content_type + ) def parse_json_header(header, name): @@ -214,7 +220,9 @@ def build_fault_result(exc_info, msg): tb_tuples = tb_tuples[i:] break - return make_error(msg if msg else str(value), etype.__name__, traceback.format_list(tb_tuples)) + return make_error( + msg if msg else str(value), etype.__name__, traceback.format_list(tb_tuples) + ) def make_xray_fault(ex_type, ex_msg, working_dir, tb_tuples): diff --git a/awslambdaric/lambda_context.py b/awslambdaric/lambda_context.py index d96171b..1465827 100644 --- a/awslambdaric/lambda_context.py +++ b/awslambdaric/lambda_context.py @@ -27,12 +27,18 @@ def __init__( self.client_context = make_obj_from_dict(ClientContext, client_context) if self.client_context is not None: - self.client_context.client = make_obj_from_dict(Client, self.client_context.client) + self.client_context.client = make_obj_from_dict( + Client, self.client_context.client + ) self.identity = make_obj_from_dict(CognitoIdentity, {}) if cognito_identity is not None: - self.identity.cognito_identity_id = cognito_identity.get("cognitoIdentityId") - self.identity.cognito_identity_pool_id = cognito_identity.get("cognitoIdentityPoolId") + self.identity.cognito_identity_id = cognito_identity.get( + "cognitoIdentityId" + ) + self.identity.cognito_identity_pool_id = cognito_identity.get( + "cognitoIdentityPoolId" + ) self._epoch_deadline_time_in_ms = epoch_deadline_time_in_ms diff --git a/awslambdaric/lambda_runtime_client.py b/awslambdaric/lambda_runtime_client.py index 0ec74de..2066f6c 100644 --- a/awslambdaric/lambda_runtime_client.py +++ b/awslambdaric/lambda_runtime_client.py @@ -9,7 +9,9 @@ def _user_agent(): - py_version = f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}" + py_version = ( + f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}" + ) pkg_version = __version__ return f"aws-lambda-python/{py_version}-{pkg_version}" @@ -75,10 +77,14 @@ def wait_next_invocation(self): event_body=response_body, ) - def post_invocation_result(self, invoke_id, result_data, content_type="application/json"): + def post_invocation_result( + self, invoke_id, result_data, content_type="application/json" + ): runtime_client.post_invocation_result( invoke_id, - result_data if isinstance(result_data, bytes) else result_data.encode("utf-8"), + result_data + if isinstance(result_data, bytes) + else result_data.encode("utf-8"), content_type, ) diff --git a/tests/test_bootstrap.py b/tests/test_bootstrap.py index 75f6bf9..af88924 100644 --- a/tests/test_bootstrap.py +++ b/tests/test_bootstrap.py @@ -277,15 +277,21 @@ def __init__(self, message): xray_fault = json.loads(args[2]) self.assertEqual(xray_fault["working_directory"], self.working_directory) self.assertEqual(len(xray_fault["exceptions"]), 1) - self.assertEqual(xray_fault["exceptions"][0]["message"], expected_response["errorMessage"]) - self.assertEqual(xray_fault["exceptions"][0]["type"], expected_response["errorType"]) + self.assertEqual( + xray_fault["exceptions"][0]["message"], expected_response["errorMessage"] + ) + self.assertEqual( + xray_fault["exceptions"][0]["type"], expected_response["errorType"] + ) self.assertEqual(len(xray_fault["exceptions"][0]["stack"]), 1) self.assertEqual( xray_fault["exceptions"][0]["stack"][0]["label"], "raise_exception_handler" ) self.assertIsInstance(xray_fault["exceptions"][0]["stack"][0]["line"], int) self.assertTrue( - xray_fault["exceptions"][0]["stack"][0]["path"].endswith(os.path.relpath(__file__)) + xray_fault["exceptions"][0]["stack"][0]["path"].endswith( + os.path.relpath(__file__) + ) ) self.assertEqual(len(xray_fault["paths"]), 1) self.assertTrue(xray_fault["paths"][0].endswith(os.path.relpath(__file__))) @@ -323,15 +329,21 @@ def __init__(self, message): xray_fault = json.loads(args[2]) self.assertEqual(xray_fault["working_directory"], self.working_directory) self.assertEqual(len(xray_fault["exceptions"]), 1) - self.assertEqual(xray_fault["exceptions"][0]["message"], expected_response["errorMessage"]) - self.assertEqual(xray_fault["exceptions"][0]["type"], expected_response["errorType"]) + self.assertEqual( + xray_fault["exceptions"][0]["message"], expected_response["errorMessage"] + ) + self.assertEqual( + xray_fault["exceptions"][0]["type"], expected_response["errorType"] + ) self.assertEqual(len(xray_fault["exceptions"][0]["stack"]), 1) self.assertEqual( xray_fault["exceptions"][0]["stack"][0]["label"], "raise_exception_handler" ) self.assertIsInstance(xray_fault["exceptions"][0]["stack"][0]["line"], int) self.assertTrue( - xray_fault["exceptions"][0]["stack"][0]["path"].endswith(os.path.relpath(__file__)) + xray_fault["exceptions"][0]["stack"][0]["path"].endswith( + os.path.relpath(__file__) + ) ) self.assertEqual(len(xray_fault["paths"]), 1) self.assertTrue(xray_fault["paths"][0].endswith(os.path.relpath(__file__))) @@ -460,7 +472,9 @@ def raise_exception_handler(json_input, lambda_context): try: import invalid_module # noqa: F401 except ImportError: - raise bootstrap.FaultException("FaultExceptionType", "Fault exception msg", None) + raise bootstrap.FaultException( + "FaultExceptionType", "Fault exception msg", None + ) bootstrap.handle_event_request( self.lambda_runtime, @@ -474,14 +488,14 @@ def raise_exception_handler(json_input, lambda_context): 0, bootstrap.StandardLogSink(), ) - error_logs = ( - "[ERROR] FaultExceptionType: Fault exception msg\rTraceback (most recent call last):\n" - ) + error_logs = "[ERROR] FaultExceptionType: Fault exception msg\rTraceback (most recent call last):\n" self.assertEqual(mock_stdout.getvalue(), error_logs) @patch("sys.stdout", new_callable=StringIO) - def test_handle_event_request_fault_exception_logging_nomessage_notrace(self, mock_stdout): + def test_handle_event_request_fault_exception_logging_nomessage_notrace( + self, mock_stdout + ): def raise_exception_handler(json_input, lambda_context): try: import invalid_module # noqa: F401 @@ -505,7 +519,9 @@ def raise_exception_handler(json_input, lambda_context): self.assertEqual(mock_stdout.getvalue(), error_logs) @patch("sys.stdout", new_callable=StringIO) - def test_handle_event_request_fault_exception_logging_notype_notrace(self, mock_stdout): + def test_handle_event_request_fault_exception_logging_notype_notrace( + self, mock_stdout + ): def raise_exception_handler(json_input, lambda_context): try: import invalid_module # noqa: F401 @@ -529,7 +545,9 @@ def raise_exception_handler(json_input, lambda_context): self.assertEqual(mock_stdout.getvalue(), error_logs) @patch("sys.stdout", new_callable=StringIO) - def test_handle_event_request_fault_exception_logging_notype_nomessage(self, mock_stdout): + def test_handle_event_request_fault_exception_logging_notype_nomessage( + self, mock_stdout + ): def raise_exception_handler(json_input, lambda_context): try: import invalid_module # noqa: F401 @@ -652,7 +670,9 @@ def __init__(self): self.assertEqual(actual["exceptions"][0]["stack"][0]["label"], "test_method") self.assertEqual(actual["exceptions"][0]["stack"][0]["path"], "test.py") self.assertEqual(actual["exceptions"][0]["stack"][0]["line"], 28) - self.assertEqual(actual["exceptions"][0]["stack"][1]["label"], "another_test_method") + self.assertEqual( + actual["exceptions"][0]["stack"][1]["label"], "another_test_method" + ) self.assertEqual(actual["exceptions"][0]["stack"][1]["path"], "another_test.py") self.assertEqual(actual["exceptions"][0]["stack"][1]["line"], 2718) @@ -662,7 +682,9 @@ class FaultExceptionMatcher(BaseException): def __init__(self, msg, exception_type=None, trace_pattern=None): self.msg = msg self.exception_type = exception_type - self.trace = trace_pattern if trace_pattern is None else re.compile(trace_pattern) + self.trace = ( + trace_pattern if trace_pattern is None else re.compile(trace_pattern) + ) def __eq__(self, other): trace_matches = True @@ -710,8 +732,12 @@ def test_get_event_handler_import_error(self): 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')") + 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) @@ -733,7 +759,9 @@ def test_get_event_handler_syntax_error(self): def test_get_event_handler_missing_error(self): importlib.invalidate_caches() - with tempfile.NamedTemporaryFile(suffix=".py", dir=".", delete=False) as tmp_file: + with tempfile.NamedTemporaryFile( + suffix=".py", dir=".", delete=False + ) as tmp_file: tmp_file.write(b"def wrong_handler_name():\n\tprint('hello')") tmp_file.flush() @@ -772,11 +800,15 @@ def test_get_event_handler_build_in_conflict(self): ) 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 = bootstrap._get_handler( + "tests/test_built_in_module_name/sys.my_handler" + ) response_handler() 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 = bootstrap._get_handler( + "tests.test_built_in_module_name.sys.my_handler" + ) response_handler() @@ -946,7 +978,9 @@ def test_log_error_indentation_framed_log_sink(self): @patch("sys.stdout", new_callable=StringIO) def test_log_error_empty_stacktrace_line_standard_log_sink(self, mock_stdout): - err_to_log = bootstrap.make_error("Error message", "ErrorType", ["line1", "", "line2"]) + err_to_log = bootstrap.make_error( + "Error message", "ErrorType", ["line1", "", "line2"] + ) bootstrap.log_error(err_to_log, bootstrap.StandardLogSink()) expected_logged_error = "[ERROR] ErrorType: Error message\rTraceback (most recent call last):\rline1\r\rline2\n" @@ -1117,7 +1151,9 @@ def test_run(self, mock_runtime_client, mock_handle_event_request): ] with self.assertRaises(TypeError): - bootstrap.run(expected_app_root, expected_handler, expected_lambda_runtime_api_addr) + bootstrap.run( + expected_app_root, expected_handler, expected_lambda_runtime_api_addr + ) mock_handle_event_request.assert_called_once() @@ -1140,7 +1176,9 @@ class TestException(Exception): mock_sys.exit.side_effect = TestException("Boom!") with self.assertRaises(TestException): - bootstrap.run(expected_app_root, expected_handler, expected_lambda_runtime_api_addr) + bootstrap.run( + expected_app_root, expected_handler, expected_lambda_runtime_api_addr + ) mock_sys.exit.assert_called_once_with(1) diff --git a/tests/test_lambda_runtime_client.py b/tests/test_lambda_runtime_client.py index 53a0051..814ca96 100644 --- a/tests/test_lambda_runtime_client.py +++ b/tests/test_lambda_runtime_client.py @@ -166,7 +166,9 @@ def test_post_invocation_error(self, mock_runtime_client): runtime_client.post_invocation_error(invoke_id, error_data, xray_fault) - mock_runtime_client.post_error.assert_called_once_with(invoke_id, error_data, xray_fault) + mock_runtime_client.post_error.assert_called_once_with( + invoke_id, error_data, xray_fault + ) @patch("awslambdaric.lambda_runtime_client.runtime_client") def test_post_invocation_error_with_large_xray_cause(self, mock_runtime_client): @@ -188,9 +190,13 @@ def test_post_invocation_error_with_too_large_xray_cause(self, mock_runtime_clie invoke_id = "1234" too_large_xray_fault = "a" * int(1024 * 1024) - runtime_client.post_invocation_error(invoke_id, error_data, too_large_xray_fault) + runtime_client.post_invocation_error( + invoke_id, error_data, too_large_xray_fault + ) - mock_runtime_client.post_error.assert_called_once_with(invoke_id, error_data, "") + mock_runtime_client.post_error.assert_called_once_with( + invoke_id, error_data, "" + ) def test_connection_refused(self): with self.assertRaises(ConnectionRefusedError):