def test_invoke_can_call_api_handler(self, stubbed_session): arn = 'arn:aws:lambda:region:id:function:name-dev' stubbed_session.stub('lambda').invoke( FunctionName=arn, InvocationType='RequestResponse').returns({}) stubbed_session.activate_stubs() client = TypedAWSClient(stubbed_session) invoker = LambdaInvoker(arn, client) invoker.invoke() stubbed_session.verify_stubs()
def test_invoke_can_call_api_handler(self, stubbed_session): arn = 'arn:aws:lambda:region:id:function:name-dev' stubbed_session.stub('lambda').invoke( FunctionName=arn, InvocationType='RequestResponse' ).returns({}) stubbed_session.activate_stubs() client = TypedAWSClient(stubbed_session) invoker = LambdaInvoker(arn, client) invoker.invoke() stubbed_session.verify_stubs()
def test_invoke_does_forward_payload(self, stubbed_session): arn = 'arn:aws:lambda:region:id:function:name-dev' stubbed_session.stub('lambda').invoke( FunctionName=arn, InvocationType='RequestResponse', Payload=b'foobar', ).returns({}) stubbed_session.activate_stubs() client = TypedAWSClient(stubbed_session) invoker = LambdaInvoker(arn, client) invoker.invoke(b'foobar') stubbed_session.verify_stubs()
def create_lambda_invoke_handler(self, name, stage): # type: (str, str) -> LambdaInvokeHandler config = self.create_config_obj(stage) deployed = config.deployed_resources(stage) try: resource = deployed.resource_values(name) arn = resource['lambda_arn'] except (KeyError, ValueError): raise NoSuchFunctionError(name) function_scoped_config = config.scope(stage, name) # The session for max retries needs to be set to 0 for invoking a # lambda function because in the case of a timeout or other retriable # error the underlying client will call the function again. session = self.create_botocore_session( read_timeout=function_scoped_config.lambda_timeout, max_retries=0, ) client = TypedAWSClient(session) invoker = LambdaInvoker(arn, client) handler = LambdaInvokeHandler( invoker, LambdaResponseFormatter(), UI(), ) return handler
def test_invoke_can_format_and_write_small_error_case( self, stubbed_session): # Some error response payloads do not have the errorType or # stackTrace key. arn = 'arn:aws:lambda:region:id:function:name-dev' error = { "errorMessage": "Something bad happened", } serialized_error = json.dumps(error).encode('utf-8') stubbed_session.stub('lambda').invoke( FunctionName=arn, InvocationType='RequestResponse').returns({ 'StatusCode': 200, 'FunctionError': 'Unhandled', 'ExecutedVersion': '$LATEST', 'Payload': FakeStreamingBody(serialized_error) }) ui = FakeUI() stubbed_session.activate_stubs() client = TypedAWSClient(stubbed_session) invoker = LambdaInvoker(arn, client) formatter = LambdaResponseFormatter() invoke_handler = LambdaInvokeHandler(invoker, formatter, ui) with pytest.raises(UnhandledLambdaError): invoke_handler.invoke() stubbed_session.verify_stubs() assert [('Something bad happened\n')] == ui.errors
def test_invoke_can_format_and_write_success_case(self, stubbed_session): arn = 'arn:aws:lambda:region:id:function:name-dev' stubbed_session.stub('lambda').invoke( FunctionName=arn, InvocationType='RequestResponse').returns({ 'StatusCode': 200, 'ExecutedVersion': '$LATEST', 'Payload': FakeStreamingBody(b'foobarbaz') }) ui = FakeUI() stubbed_session.activate_stubs() client = TypedAWSClient(stubbed_session) invoker = LambdaInvoker(arn, client) formatter = LambdaResponseFormatter() invoke_handler = LambdaInvokeHandler(invoker, formatter, ui) invoke_handler.invoke() stubbed_session.verify_stubs() assert ['foobarbaz\n'] == ui.writes
def test_invoke_can_format_and_write_error_case(self, stubbed_session): arn = 'arn:aws:lambda:region:id:function:name-dev' error = { "errorMessage": "Something bad happened", "errorType": "Error", "stackTrace": [["/path/file.py", 123, "main", "foo(bar)"], ["/path/other_file.py", 456, "function", "bar = baz"]] } serialized_error = json.dumps(error).encode('utf-8') stubbed_session.stub('lambda').invoke( FunctionName=arn, InvocationType='RequestResponse').returns({ 'StatusCode': 200, 'FunctionError': 'Unhandled', 'ExecutedVersion': '$LATEST', 'Payload': FakeStreamingBody(serialized_error) }) ui = FakeUI() stubbed_session.activate_stubs() client = TypedAWSClient(stubbed_session) invoker = LambdaInvoker(arn, client) formatter = LambdaResponseFormatter() invoke_handler = LambdaInvokeHandler(invoker, formatter, ui) with pytest.raises(UnhandledLambdaError): invoke_handler.invoke() stubbed_session.verify_stubs() assert [('Traceback (most recent call last):\n' ' File "/path/file.py", line 123, in main\n' ' foo(bar)\n' ' File "/path/other_file.py", line 456, in function\n' ' bar = baz\n' 'Error: Something bad happened\n')] == ui.errors