Exemplo n.º 1
0
    def test_can_generate_layer_package(self):
        function = create_function_resource('myfunction')
        function.managed_layer = models.LambdaLayer(
            resource_name='managed-layer',
            layer_name='appname-dev-managed-layer',
            runtime='python2.7',
            deployment_package=models.DeploymentPackage(
                models.Placeholder.BUILD_STAGE))
        lambda_packager = mock.Mock(spec=packager.BaseLambdaDeploymentPackager)
        layer_packager = mock.Mock(spec=packager.BaseLambdaDeploymentPackager)
        lambda_packager.create_deployment_package.return_value = 'package.zip'
        layer_packager.create_deployment_package.return_value = (
            'package-layer.zip')

        config = Config.create(project_dir='.')

        p = ManagedLayerDeploymentPackager(lambda_packager, layer_packager)
        p.handle(config, function.managed_layer)
        p.handle(config, function)
        assert function.deployment_package.filename == 'package.zip'
        lambda_packager.create_deployment_package.assert_called_with(
            '.', config.lambda_python_version)
        assert function.managed_layer.deployment_package.filename == (
            'package-layer.zip')
        layer_packager.create_deployment_package.assert_called_with(
            '.', config.lambda_python_version)
Exemplo n.º 2
0
 def test_can_build_single_lambda_function_app_with_managed_layer(
         self, sample_app_lambda_only):
     # This is the simplest configuration we can get.
     builder = ApplicationGraphBuilder()
     config = self.create_config(sample_app_lambda_only,
                                 iam_role_arn='role:arn',
                                 automatic_layer=True)
     application = builder.build(config, stage_name='dev')
     # The top level resource is always an Application.
     assert isinstance(application, models.Application)
     assert len(application.resources) == 1
     assert application.resources[0] == models.LambdaFunction(
         resource_name='myfunction',
         function_name='lambda-only-dev-myfunction',
         environment_variables={},
         runtime=config.lambda_python_version,
         handler='app.myfunction',
         tags=config.tags,
         timeout=None,
         memory_size=None,
         deployment_package=models.DeploymentPackage(
             models.Placeholder.BUILD_STAGE),
         role=models.PreCreatedIAMRole('role:arn'),
         security_group_ids=[],
         subnet_ids=[],
         layers=[],
         managed_layer=models.LambdaLayer(
             resource_name='managed-layer',
             layer_name='lambda-only-dev-managed-layer',
             runtime=config.lambda_python_version,
             deployment_package=models.DeploymentPackage(
                 models.Placeholder.BUILD_STAGE, )),
         reserved_concurrency=None,
         xray=None,
     )
Exemplo n.º 3
0
    def test_managed_layer_removed_if_no_deps(self):
        function = create_function_resource('myfunction')
        function.managed_layer = models.LambdaLayer(
            resource_name='managed-layer',
            layer_name='appname-dev-managed-layer',
            runtime='python2.7',
            deployment_package=models.DeploymentPackage(
                models.Placeholder.BUILD_STAGE
            )
        )
        lambda_packager = mock.Mock(spec=packager.BaseLambdaDeploymentPackager)
        layer_packager = mock.Mock(spec=packager.BaseLambdaDeploymentPackager)
        lambda_packager.create_deployment_package.return_value = 'package.zip'
        layer_packager.create_deployment_package.side_effect = \
            packager.EmptyPackageError()

        config = Config.create(project_dir='.')

        p = ManagedLayerDeploymentPackager(lambda_packager, layer_packager)
        p.handle(config, function.managed_layer)
        p.handle(config, function)
        # If the deployment package for layers would result in an empty
        # deployment package, we expect that resource to be removed, it can't
        # be created on the service.
        assert function.managed_layer is None
Exemplo n.º 4
0
 def _get_managed_lambda_layer(self, config):
     # type: (Config) -> Optional[models.LambdaLayer]
     if not config.automatic_layer:
         return None
     if self._managed_layer is None:
         self._managed_layer = models.LambdaLayer(
             resource_name='managed-layer',
             layer_name='%s-%s-%s' %
             (config.app_name, config.chalice_stage, 'managed-layer'),
             runtime=config.lambda_python_version,
             deployment_package=models.DeploymentPackage(
                 models.Placeholder.BUILD_STAGE))
     return self._managed_layer
Exemplo n.º 5
0
    def test_layer_package_not_generated_if_filename_populated(self):
        generator = mock.Mock(spec=packager.BaseLambdaDeploymentPackager)

        function = create_function_resource('myfunction')
        layer = models.LambdaLayer(resource_name='layer',
                                   layer_name='name',
                                   runtime='python2.7',
                                   deployment_package=models.DeploymentPackage(
                                       filename='original.zip'))
        function.managed_layer = layer
        config = Config.create(project_dir='.')

        p = ManagedLayerDeploymentPackager(None, generator)
        p.handle(config, layer)

        assert layer.deployment_package.filename == 'original.zip'
        assert not generator.create_deployment_package.called