Beispiel #1
0
    def test_formatter_can_format_string_stack_trace(self):
        error = {
            "errorMessage":
            "Something bad happened",
            "errorType":
            "Error",
            "stackTrace": [
                '  File "/path/file.py", line 123, in main\n    foo(bar)\n',
                '  File "/path/more.py", line 456, in func\n    bar = baz\n',
            ]
        }
        serialized_error = json.dumps(error).encode('utf-8')
        formatter = LambdaResponseFormatter()
        formatted = formatter.format_response({
            'StatusCode':
            200,
            'FunctionError':
            'Unhandled',
            'ExecutedVersion':
            '$LATEST',
            'Payload':
            FakeStreamingBody(serialized_error)
        })

        assert ('Traceback (most recent call last):\n'
                '  File "/path/file.py", line 123, in main\n'
                '    foo(bar)\n'
                '  File "/path/more.py", line 456, in func\n'
                '    bar = baz\n'
                'Error: Something bad happened\n') == formatted
Beispiel #2
0
    def test_formatter_can_format_stack_trace(self):
        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')
        formatter = LambdaResponseFormatter()
        formatted = formatter.format_response({
            'StatusCode': 200,
            'FunctionError': 'Unhandled',
            'ExecutedVersion': '$LATEST',
            'Payload': FakeStreamingBody(serialized_error)
        })

        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'
        ) == formatted
Beispiel #3
0
 def test_formatter_can_format_success(self):
     formatter = LambdaResponseFormatter()
     formatted = formatter.format_response({
         'StatusCode': 200,
         'ExecutedVersion': '$LATEST',
         'Payload': FakeStreamingBody(b'foobarbaz')
     })
     assert 'foobarbaz\n' == formatted
Beispiel #4
0
 def test_formatter_can_format_success(self):
     formatter = LambdaResponseFormatter()
     formatted = formatter.format_response({
         'StatusCode':
         200,
         'ExecutedVersion':
         '$LATEST',
         'Payload':
         FakeStreamingBody(b'foobarbaz')
     })
     assert 'foobarbaz\n' == formatted
Beispiel #5
0
    def test_formatter_can_format_simple_error(self):
        error = {
            "errorMessage": "Something bad happened",
        }
        serialized_error = json.dumps(error).encode('utf-8')
        formatter = LambdaResponseFormatter()
        formatted = formatter.format_response({
            'StatusCode': 200,
            'FunctionError': 'Unhandled',
            'ExecutedVersion': '$LATEST',
            'Payload': FakeStreamingBody(serialized_error)
        })

        assert 'Something bad happened\n' == formatted
Beispiel #6
0
    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
Beispiel #7
0
    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
Beispiel #8
0
    def test_formatter_can_format_simple_error(self):
        error = {
            "errorMessage": "Something bad happened",
        }
        serialized_error = json.dumps(error).encode('utf-8')
        formatter = LambdaResponseFormatter()
        formatted = formatter.format_response({
            'StatusCode':
            200,
            'FunctionError':
            'Unhandled',
            'ExecutedVersion':
            '$LATEST',
            'Payload':
            FakeStreamingBody(serialized_error)
        })

        assert 'Something bad happened\n' == formatted
Beispiel #9
0
    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
Beispiel #10
0
    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