Skip to content

Commit c669cfd

Browse files
committed
test: lambda error responses
1 parent 2664b99 commit c669cfd

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

samcli/local/lambda_service/local_lambda_http_service.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ def _invoke_request_handler(self, function_name):
259259
if validation_error := self._validate_function_for_invocation(function_name):
260260
return validation_error
261261

262+
# LocalLambdaService is blocking, so threads used by ThreadPoolExecutor
263+
# will be cleaned up when Python exits by the ThreadPoolExecutor implementation
262264
self.executor.submit(self._invoke_async_lambda, **arguments)
263265
return self.service_response("", headers, 202)
264266

tests/unit/local/lambda_service/test_lambda_error_responses.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ def test_generic_method_not_allowed(self, service_response_mock):
9797
405,
9898
)
9999

100+
@patch("samcli.local.services.base_local_service.BaseLocalService.service_response")
101+
def test_validation_exception(self, service_response_mock):
102+
service_response_mock.return_value = "ValidationException"
103+
104+
response = LambdaErrorResponses.validation_exception("ValidationException")
105+
106+
self.assertEqual(response, "ValidationException")
107+
service_response_mock.assert_called_once_with(
108+
'{"Type": "User", "Message": "ValidationException"}',
109+
{"x-amzn-errortype": "ValidationException", "Content-Type": "application/json"},
110+
400,
111+
)
112+
100113
@patch("samcli.local.services.base_local_service.BaseLocalService.service_response")
101114
def test_durable_execution_not_found(self, service_response_mock):
102115
service_response_mock.return_value = "DurableExecutionNotFound"
@@ -109,3 +122,16 @@ def test_durable_execution_not_found(self, service_response_mock):
109122
{"x-amzn-errortype": "ResourceNotFound", "Content-Type": "application/json"},
110123
404,
111124
)
125+
126+
@patch("samcli.local.services.base_local_service.BaseLocalService.service_response")
127+
def test_container_creation_failed(self, service_response_mock):
128+
service_response_mock.return_value = "ContainerCreationFailed"
129+
130+
response = LambdaErrorResponses.container_creation_failed("Container creation failed: test message")
131+
132+
self.assertEqual(response, "ContainerCreationFailed")
133+
service_response_mock.assert_called_once_with(
134+
'{"Type": "LocalService", "Message": "Container creation failed: test message"}',
135+
{"x-amzn-errortype": "ContainerCreationFailed", "Content-Type": "application/json"},
136+
501,
137+
)

tests/unit/local/lambda_service/test_local_lambda_http_service.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,14 @@ def fake_invoke(*args, **kwargs):
505505
response = service._invoke_request_handler(function_name="HelloWorld")
506506

507507
# Assert that first a 202 response is returned
508-
assert response.status_code == 202
508+
self.assertEqual(response.status_code, 202)
509509

510510
# Then assert that invoke has not finished
511-
assert not finished.is_set()
511+
self.assertFalse(finished.is_set())
512512
handler_returned.set()
513513

514514
# Finally assert that invoke has finished
515-
assert finished.wait(timeout=5), "Task never finished"
515+
self.assertTrue(finished.wait(timeout=5), "Task never finished")
516516

517517
@patch("samcli.local.lambda_service.local_lambda_http_service.LocalLambdaHttpService.service_response")
518518
@patch("samcli.local.lambda_service.local_lambda_http_service.ThreadPoolExecutor")

0 commit comments

Comments
 (0)