Beispiel #1
0
def test_lambda_deployer_repeated_deploy(app_policy, sample_app):
    osutils = InMemoryOSUtils({'packages.zip': b'package contents'})
    aws_client = mock.Mock(spec=TypedAWSClient)
    packager = mock.Mock(spec=LambdaDeploymentPackager)

    packager.deployment_package_filename.return_value = 'packages.zip'
    # Given the lambda function already exists:
    aws_client.lambda_function_exists.return_value = True
    # And given we don't want chalice to manage our iam role for the lambda
    # function:
    cfg = Config({
        'chalice_app': sample_app,
        'manage_iam_role': False,
        'app_name': 'appname',
        'iam_role_arn': True,
        'project_dir': './myproject'
    })

    d = LambdaDeployer(aws_client, packager, None, osutils, app_policy)
    # Doing a lambda deploy:
    d.deploy(cfg)

    # Should result in injecting the latest app code.
    packager.inject_latest_app.assert_called_with('packages.zip',
                                                  './myproject')

    # And should result in the lambda function being updated with the API.
    aws_client.update_function_code.assert_called_with('appname',
                                                       'package contents')
Beispiel #2
0
def test_prompted_on_runtime_change_can_reject_change(app_policy, sample_app):
    osutils = InMemoryOSUtils({'packages.zip': b'package contents'})
    aws_client = mock.Mock(spec=TypedAWSClient)
    packager = mock.Mock(spec=LambdaDeploymentPackager)
    packager.deployment_package_filename.return_value = 'packages.zip'
    aws_client.lambda_function_exists.return_value = True
    aws_client.get_function_configuration.return_value = {
        'Runtime': 'python1.0',
    }
    aws_client.update_function.return_value = {"FunctionArn": "myarn"}
    cfg = Config.create(
        chalice_stage='dev',
        chalice_app=sample_app,
        manage_iam_role=False,
        app_name='appname',
        iam_role_arn=True,
        project_dir='./myproject',
        environment_variables={"FOO": "BAR"},
    )
    prompter = mock.Mock(spec=NoPrompt)
    prompter.confirm.side_effect = RuntimeError("Aborted")

    d = LambdaDeployer(aws_client, packager, prompter, osutils, app_policy)
    # Doing a lambda deploy with a different runtime:
    lambda_function_name = 'lambda_function_name'
    deployed = DeployedResources('api', 'api_handler_arn',
                                 lambda_function_name, None, 'dev', None, None)
    with pytest.raises(RuntimeError):
        d.deploy(cfg, deployed, 'dev')

    assert not packager.inject_latest_app.called
    assert not aws_client.update_function.called
    assert prompter.confirm.called
    message = prompter.confirm.call_args[0][0]
    assert 'runtime will change' in message
Beispiel #3
0
    def test_lambda_deployer_with_tags(self, sample_app):
        cfg = Config.create(chalice_stage='dev',
                            app_name='myapp',
                            chalice_app=sample_app,
                            manage_iam_role=False,
                            iam_role_arn='role-arn',
                            project_dir='.',
                            tags={'mykey': 'myvalue'})
        deployer = LambdaDeployer(self.aws_client, self.packager, None,
                                  self.osutils, self.app_policy)

        deployer.deploy(cfg, None, 'dev')
        self.aws_client.create_function.assert_called_with(
            function_name='myapp-dev',
            role_arn='role-arn',
            zip_contents=b'package contents',
            runtime=cfg.lambda_python_version,
            tags={
                'aws-chalice':
                'version=%s:stage=dev:app=myapp' % (chalice_version),
                'mykey': 'myvalue'
            },
            environment_variables={},
            timeout=60,
            memory_size=128)
Beispiel #4
0
def test_lambda_deployer_repeated_deploy(app_policy, sample_app):
    osutils = InMemoryOSUtils({'packages.zip': b'package contents'})
    aws_client = mock.Mock(spec=TypedAWSClient)
    packager = mock.Mock(spec=LambdaDeploymentPackager)

    packager.deployment_package_filename.return_value = 'packages.zip'
    # Given the lambda function already exists:
    aws_client.lambda_function_exists.return_value = True
    aws_client.update_function.return_value = {"FunctionArn": "myarn"}
    # And given we don't want chalice to manage our iam role for the lambda
    # function:
    cfg = Config.create(chalice_stage='dev',
                        chalice_app=sample_app,
                        manage_iam_role=False,
                        app_name='appname',
                        iam_role_arn=True,
                        project_dir='./myproject',
                        environment_variables={"FOO": "BAR"},
                        lambda_timeout=120,
                        lambda_memory_size=256,
                        tags={'mykey': 'myvalue'})
    aws_client.get_function_configuration.return_value = {
        'Runtime': cfg.lambda_python_version,
    }
    prompter = mock.Mock(spec=NoPrompt)
    prompter.confirm.return_value = True

    d = LambdaDeployer(aws_client, packager, prompter, osutils, app_policy)
    # Doing a lambda deploy:
    lambda_function_name = 'lambda_function_name'
    deployed = DeployedResources('api', 'api_handler_arn',
                                 lambda_function_name, None, 'dev', None, None)
    d.deploy(cfg, deployed, 'dev')

    # Should result in injecting the latest app code.
    packager.inject_latest_app.assert_called_with('packages.zip',
                                                  './myproject')

    # And should result in the lambda function being updated with the API.
    aws_client.update_function.assert_called_with(
        function_name=lambda_function_name,
        zip_contents=b'package contents',
        runtime=cfg.lambda_python_version,
        environment_variables={"FOO": "BAR"},
        tags={
            'aws-chalice':
            'version=%s:stage=%s:app=%s' % (chalice_version, 'dev', 'appname'),
            'mykey':
            'myvalue'
        },
        timeout=120,
        memory_size=256)
Beispiel #5
0
def test_lambda_deployer_initial_deploy(app_policy, sample_app):
    osutils = InMemoryOSUtils({'packages.zip': b'package contents'})
    aws_client = mock.Mock(spec=TypedAWSClient)
    aws_client.create_function.return_value = 'lambda-arn'
    packager = mock.Mock(LambdaDeploymentPackager)
    packager.create_deployment_package.return_value = 'packages.zip'
    cfg = Config.create(
        chalice_stage='dev',
        app_name='myapp',
        chalice_app=sample_app,
        manage_iam_role=False,
        iam_role_arn='role-arn',
        project_dir='.',
        environment_variables={"FOO": "BAR"},
    )

    d = LambdaDeployer(aws_client, packager, None, osutils, app_policy)
    deployed = d.deploy(cfg, None, 'dev')
    assert deployed == {
        'api_handler_arn': 'lambda-arn',
        'api_handler_name': 'myapp-dev',
    }
    aws_client.create_function.assert_called_with('myapp-dev', 'role-arn',
                                                  b'package contents',
                                                  {"FOO": "BAR"})
Beispiel #6
0
def test_lambda_deployer_initial_deploy(app_policy, sample_app):
    osutils = InMemoryOSUtils({'packages.zip': b'package contents'})
    aws_client = mock.Mock(spec=TypedAWSClient)
    aws_client.create_function.return_value = 'lambda-arn'
    packager = mock.Mock(LambdaDeploymentPackager)
    packager.create_deployment_package.return_value = 'packages.zip'
    cfg = Config.create(chalice_stage='dev',
                        app_name='myapp',
                        chalice_app=sample_app,
                        manage_iam_role=False,
                        iam_role_arn='role-arn',
                        project_dir='.',
                        environment_variables={"FOO": "BAR"},
                        lambda_timeout=120,
                        lambda_memory_size=256,
                        tags={'mykey': 'myvalue'})

    d = LambdaDeployer(aws_client, packager, None, osutils, app_policy)
    deployed = d.deploy(cfg, None, 'dev')
    assert deployed == {
        'api_handler_arn': 'lambda-arn',
        'api_handler_name': 'myapp-dev',
    }
    aws_client.create_function.assert_called_with(
        function_name='myapp-dev',
        role_arn='role-arn',
        zip_contents=b'package contents',
        environment_variables={"FOO": "BAR"},
        runtime=cfg.lambda_python_version,
        tags={
            'aws-chalice': 'version=%s:stage=dev:app=myapp' % chalice_version,
            'mykey': 'myvalue'
        },
        timeout=120,
        memory_size=256)
Beispiel #7
0
    def test_lambda_deployer_with_memory_size_configured(self, sample_app):
        cfg = Config.create(chalice_stage='dev',
                            app_name='myapp',
                            chalice_app=sample_app,
                            manage_iam_role=False,
                            iam_role_arn='role-arn',
                            project_dir='.',
                            lambda_memory_size=256)
        deployer = LambdaDeployer(self.aws_client, self.packager,
                                  self.prompter, self.osutils, self.app_policy)

        deployer.deploy(cfg, self.deployed_resources, 'dev')
        self.aws_client.update_function.assert_called_with(
            function_name=self.lambda_function_name,
            zip_contents=self.package_contents,
            runtime=cfg.lambda_python_version,
            tags={
                'aws-chalice':
                'version=%s:stage=dev:app=myapp' % (chalice_version),
            },
            environment_variables={},
            timeout=60,
            memory_size=256)
Beispiel #8
0
def test_lambda_deployer_repeated_deploy(app_policy, sample_app):
    osutils = InMemoryOSUtils({'packages.zip': b'package contents'})
    aws_client = mock.Mock(spec=TypedAWSClient)
    packager = mock.Mock(spec=LambdaDeploymentPackager)

    packager.deployment_package_filename.return_value = 'packages.zip'
    # Given the lambda function already exists:
    aws_client.lambda_function_exists.return_value = True
    aws_client.update_function.return_value = {"FunctionArn": "myarn"}
    # And given we don't want chalice to manage our iam role for the lambda
    # function:
    cfg = Config.create(
        chalice_stage='dev',
        chalice_app=sample_app,
        manage_iam_role=False,
        app_name='appname',
        iam_role_arn=True,
        project_dir='./myproject',
        environment_variables={"FOO": "BAR"},
    )

    d = LambdaDeployer(aws_client, packager, None, osutils, app_policy)
    # Doing a lambda deploy:
    lambda_function_name = 'lambda_function_name'
    deployed = DeployedResources('api', 'api_handler_arn',
                                 lambda_function_name, None, 'dev', None, None)
    d.deploy(cfg, deployed, 'dev')

    # Should result in injecting the latest app code.
    packager.inject_latest_app.assert_called_with('packages.zip',
                                                  './myproject')

    # And should result in the lambda function being updated with the API.
    aws_client.update_function.assert_called_with(lambda_function_name,
                                                  'package contents',
                                                  {"FOO": "BAR"})