Skip to content

Commit 3ec0f50

Browse files
author
AntoniaSzecsi
committed
Format files according to ruff standards
1 parent 37697b1 commit 3ec0f50

File tree

2 files changed

+51
-34
lines changed

2 files changed

+51
-34
lines changed

awslambdaric/bootstrap.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ def handle_event_request(
201201
)
202202

203203
if error_result is not None:
204-
205204
log_error(error_result, log_sink)
206205
lambda_runtime_client.post_invocation_error(
207206
invoke_id, to_json(error_result), to_json(xray_fault)

tests/test_bootstrap.py

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def test_handle_event_request_happy_case(self):
8888
{},
8989
"invoked_function_arn",
9090
0,
91+
"tenant_id",
9192
bootstrap.StandardLogSink(),
9293
)
9394
self.lambda_runtime.post_invocation_result.assert_called_once_with(
@@ -111,6 +112,7 @@ def test_handle_event_request_invalid_client_context(self):
111112
{},
112113
"invoked_function_arn",
113114
0,
115+
"tenant_id",
114116
bootstrap.StandardLogSink(),
115117
)
116118
args, _ = self.lambda_runtime.post_invocation_error.call_args
@@ -152,6 +154,7 @@ def test_handle_event_request_invalid_cognito_idenity(self):
152154
"invalid_cognito_identity",
153155
"invoked_function_arn",
154156
0,
157+
"tenant_id",
155158
bootstrap.StandardLogSink(),
156159
)
157160
args, _ = self.lambda_runtime.post_invocation_error.call_args
@@ -194,6 +197,7 @@ def test_handle_event_request_invalid_event_body(self):
194197
{},
195198
"invoked_function_arn",
196199
0,
200+
"tenant_id",
197201
bootstrap.StandardLogSink(),
198202
)
199203
args, _ = self.lambda_runtime.post_invocation_error.call_args
@@ -238,6 +242,7 @@ def invalid_json_response(json_input, lambda_context):
238242
{},
239243
"invoked_function_arn",
240244
0,
245+
"tenant_id",
241246
bootstrap.StandardLogSink(),
242247
)
243248
args, _ = self.lambda_runtime.post_invocation_error.call_args
@@ -283,6 +288,7 @@ def __init__(self, message):
283288
{},
284289
"invoked_function_arn",
285290
0,
291+
"tenant_id",
286292
bootstrap.StandardLogSink(),
287293
)
288294
args, _ = self.lambda_runtime.post_invocation_error.call_args
@@ -335,6 +341,7 @@ def __init__(self, message):
335341
{},
336342
"invoked_function_arn",
337343
0,
344+
"tenant_id",
338345
bootstrap.StandardLogSink(),
339346
)
340347
args, _ = self.lambda_runtime.post_invocation_error.call_args
@@ -386,6 +393,7 @@ def unable_to_import_module(json_input, lambda_context):
386393
{},
387394
"invoked_function_arn",
388395
0,
396+
"tenant_id",
389397
bootstrap.StandardLogSink(),
390398
)
391399
args, _ = self.lambda_runtime.post_invocation_error.call_args
@@ -425,6 +433,7 @@ def raise_exception_handler(json_input, lambda_context):
425433
{},
426434
"invoked_function_arn",
427435
0,
436+
"tenant_id",
428437
bootstrap.StandardLogSink(),
429438
)
430439
args, _ = self.lambda_runtime.post_invocation_error.call_args
@@ -475,15 +484,12 @@ def raise_exception_handler(json_input, lambda_context):
475484
{},
476485
"invoked_function_arn",
477486
0,
487+
"tenant_id",
478488
bootstrap.StandardLogSink(),
479489
)
480490

481491
# NOTE: Indentation characters are NO-BREAK SPACE (U+00A0) not SPACE (U+0020)
482-
error_logs = (
483-
lambda_unhandled_exception_warning_message
484-
+ "\n"
485-
+ "[ERROR] FaultExceptionType: Fault exception msg\r"
486-
)
492+
error_logs = "[ERROR] FaultExceptionType: Fault exception msg\r"
487493
error_logs += "Traceback (most recent call last):\r"
488494
error_logs += '  File "spam.py", line 3, in <module>\r'
489495
error_logs += "    spam.eggs()\r"
@@ -514,13 +520,10 @@ def raise_exception_handler(json_input, lambda_context):
514520
{},
515521
"invoked_function_arn",
516522
0,
523+
"tenant_id",
517524
bootstrap.StandardLogSink(),
518525
)
519-
error_logs = (
520-
lambda_unhandled_exception_warning_message
521-
+ "\n"
522-
+ "[ERROR] FaultExceptionType: Fault exception msg\rTraceback (most recent call last):\n"
523-
)
526+
error_logs = "[ERROR] FaultExceptionType: Fault exception msg\rTraceback (most recent call last):\n"
524527

525528
self.assertEqual(mock_stdout.getvalue(), error_logs)
526529

@@ -546,13 +549,10 @@ def raise_exception_handler(json_input, lambda_context):
546549
{},
547550
"invoked_function_arn",
548551
0,
552+
"tenant_id",
549553
bootstrap.StandardLogSink(),
550554
)
551-
error_logs = (
552-
lambda_unhandled_exception_warning_message
553-
+ "\n"
554-
+ "[ERROR] FaultExceptionType\rTraceback (most recent call last):\n"
555-
)
555+
error_logs = "[ERROR] FaultExceptionType\rTraceback (most recent call last):\n"
556556

557557
self.assertEqual(mock_stdout.getvalue(), error_logs)
558558

@@ -578,13 +578,10 @@ def raise_exception_handler(json_input, lambda_context):
578578
{},
579579
"invoked_function_arn",
580580
0,
581+
"tenant_id",
581582
bootstrap.StandardLogSink(),
582583
)
583-
error_logs = (
584-
lambda_unhandled_exception_warning_message
585-
+ "\n"
586-
+ "[ERROR] Fault exception msg\rTraceback (most recent call last):\n"
587-
)
584+
error_logs = "[ERROR] Fault exception msg\rTraceback (most recent call last):\n"
588585

589586
self.assertEqual(mock_stdout.getvalue(), error_logs)
590587

@@ -619,9 +616,10 @@ def raise_exception_handler(json_input, lambda_context):
619616
{},
620617
"invoked_function_arn",
621618
0,
619+
"tenant_id",
622620
bootstrap.StandardLogSink(),
623621
)
624-
error_logs = lambda_unhandled_exception_warning_message + "\n[ERROR]\r"
622+
error_logs = "[ERROR]\r"
625623
error_logs += "Traceback (most recent call last):\r"
626624
error_logs += '  File "spam.py", line 3, in <module>\r'
627625
error_logs += "    spam.eggs()\r"
@@ -652,25 +650,16 @@ def raise_exception_handler(json_input, lambda_context):
652650
{},
653651
"invoked_function_arn",
654652
0,
653+
"tenant_id",
655654
bootstrap.StandardLogSink(),
656655
)
657656

658657
stdout_value = mock_stdout.getvalue()
659-
received_warning = stdout_value.split("\n")[0]
660-
received_rest = stdout_value[len(received_warning) + 1 :]
661-
662-
warning = json.loads(received_warning)
663-
self.assertEqual(warning["level"], "WARNING")
664-
self.assertEqual(warning["message"], lambda_unhandled_exception_warning_message)
665-
self.assertEqual(warning["logger"], "root")
666-
self.assertIn("timestamp", warning)
667658

668659
# this line is not in json because of the way the test runtime is bootstrapped
669-
error_logs = (
670-
"\n[ERROR] FaultExceptionType\rTraceback (most recent call last):\n"
671-
)
660+
error_logs = "[ERROR] FaultExceptionType\rTraceback (most recent call last):\n"
672661

673-
self.assertEqual(received_rest, error_logs)
662+
self.assertEqual(stdout_value, error_logs)
674663

675664

676665
class TestXrayFault(unittest.TestCase):
@@ -868,6 +857,7 @@ def test_application_json(self):
868857
cognito_identity_json=None,
869858
invoked_function_arn="invocation-arn",
870859
epoch_deadline_time_in_ms=1415836801003,
860+
tenant_id=None,
871861
log_sink=bootstrap.StandardLogSink(),
872862
)
873863

@@ -887,6 +877,7 @@ def test_binary_request_binary_response(self):
887877
cognito_identity_json=None,
888878
invoked_function_arn="invocation-arn",
889879
epoch_deadline_time_in_ms=1415836801003,
880+
tenant_id=None,
890881
log_sink=bootstrap.StandardLogSink(),
891882
)
892883

@@ -906,6 +897,7 @@ def test_json_request_binary_response(self):
906897
cognito_identity_json=None,
907898
invoked_function_arn="invocation-arn",
908899
epoch_deadline_time_in_ms=1415836801003,
900+
tenant_id=None,
909901
log_sink=bootstrap.StandardLogSink(),
910902
)
911903

@@ -924,6 +916,7 @@ def test_binary_with_application_json(self):
924916
cognito_identity_json=None,
925917
invoked_function_arn="invocation-arn",
926918
epoch_deadline_time_in_ms=1415836801003,
919+
tenant_id=None,
927920
log_sink=bootstrap.StandardLogSink(),
928921
)
929922

@@ -1360,6 +1353,31 @@ def test_json_formatter(self, mock_stderr):
13601353
)
13611354
self.assertEqual(mock_stderr.getvalue(), "")
13621355

1356+
@patch("awslambdaric.bootstrap._GLOBAL_TENANT_ID", "test-tenant-id")
1357+
@patch("sys.stderr", new_callable=StringIO)
1358+
def test_json_formatter_with_tenant_id(self, mock_stderr):
1359+
logger = logging.getLogger("a.b")
1360+
level = logging.INFO
1361+
message = "Test json formatting with tenant id"
1362+
expected = {
1363+
"level": "INFO",
1364+
"logger": "a.b",
1365+
"message": message,
1366+
"requestId": "",
1367+
"tenantId": "test-tenant-id",
1368+
}
1369+
1370+
with patch("sys.stdout", new_callable=StringIO) as mock_stdout:
1371+
logger.log(level, message)
1372+
1373+
data = json.loads(mock_stdout.getvalue())
1374+
data.pop("timestamp")
1375+
self.assertEqual(
1376+
data,
1377+
expected,
1378+
)
1379+
self.assertEqual(mock_stderr.getvalue(), "")
1380+
13631381
@patch("sys.stdout", new_callable=StringIO)
13641382
@patch("sys.stderr", new_callable=StringIO)
13651383
def test_exception(self, mock_stderr, mock_stdout):

0 commit comments

Comments
 (0)