Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_no_injection_when_values_are_set(self):
injector = InjectDefaults(
lambda_timeout=100,
lambda_memory_size=512,
)
function = models.LambdaFunction(
# The timeout/memory_size are set to
# None, so the injector should fill them
# in the with the default values above.
timeout=1,
memory_size=1,
resource_name='foo',
function_name='app-stage-foo',
environment_variables={},
runtime='python2.7',
handler='app.app',
tags={},
deployment_package=None,
role=None,
)
config = Config.create()
injector.handle(config, function)
def create_function_resource(name):
return models.LambdaFunction(
resource_name=name,
function_name='appname-dev-%s' % name,
environment_variables={},
runtime='python2.7',
handler='app.app',
tags={},
timeout=60,
memory_size=128,
deployment_package=models.DeploymentPackage(filename='foo'),
role=models.PreCreatedIAMRole(role_arn='role:arn'),
security_group_ids=[],
subnet_ids=[],
layers=[]
)
def test_can_build_lambda_function_with_layers(self, lambda_app):
# This is the simplest configuration we can get.
builder = ApplicationGraphBuilder()
layers = ['arn:aws:lambda:us-east-1:111:layer:test_layer:1']
config = self.create_config(lambda_app,
iam_role_arn='role:arn',
layers=layers)
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='foo',
function_name='lambda-only-dev-foo',
environment_variables={},
runtime=config.lambda_python_version,
handler='app.foo',
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=layers,
reserved_concurrency=None,
)
def lambda_function(self):
return models.LambdaFunction(
resource_name='foo',
function_name='app-dev-foo',
environment_variables={},
runtime='python27',
handler='app.app',
tags={'foo': 'bar'},
timeout=120,
memory_size=128,
deployment_package=models.DeploymentPackage(filename='foo.zip'),
role=models.PreCreatedIAMRole(role_arn='role:arn'),
security_group_ids=[],
subnet_ids=[],
layers=[],
reserved_concurrency=None,
)
@rest_api_app.authorizer()
def my_auth(auth_request):
pass
@rest_api_app.route('/auth', authorizer=my_auth)
def needs_auth():
return {'foo': 'bar'}
config = self.create_config(rest_api_app,
app_name='rest-api-app',
autogen_policy=True)
builder = ApplicationGraphBuilder()
application = builder.build(config, stage_name='dev')
rest_api = application.resources[0]
assert len(rest_api.authorizers) == 1
assert isinstance(rest_api.authorizers[0], models.LambdaFunction)
def test_sam_injects_policy(self, sample_app):
function = models.LambdaFunction(
resource_name='foo',
function_name='app-dev-foo',
environment_variables={},
runtime='python27',
handler='app.app',
tags={'foo': 'bar'},
timeout=120,
memory_size=128,
deployment_package=models.DeploymentPackage(filename='foo.zip'),
role=models.ManagedIAMRole(
resource_name='role',
role_name='app-role',
trust_policy={},
policy=models.AutoGenIAMPolicy(document={'iam': 'policy'}),
),
security_group_ids=[],
def test_can_build_single_lambda_function_app(self, lambda_app):
# This is the simplest configuration we can get.
builder = ApplicationGraphBuilder()
config = self.create_config(lambda_app, iam_role_arn='role:arn')
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='foo',
function_name='lambda-only-dev-foo',
environment_variables={},
runtime=config.lambda_python_version,
handler='app.foo',
tags=config.tags,
timeout=None,
memory_size=None,
deployment_package=models.DeploymentPackage(
models.Placeholder.BUILD_STAGE),
role=models.PreCreatedIAMRole('role:arn'),
)
def test_role_arn_inserted_when_necessary(self):
function = models.LambdaFunction(
resource_name='foo',
function_name='app-dev-foo',
environment_variables={},
runtime='python27',
handler='app.app',
tags={'foo': 'bar'},
timeout=120,
memory_size=128,
deployment_package=models.DeploymentPackage(filename='foo.zip'),
role=models.PreCreatedIAMRole(role_arn='role:arn'),
security_group_ids=[],
subnet_ids=[],
layers=[],
reserved_concurrency=None,
)
template = self.template_gen.generate([function])
def _build_lambda_function(self,
config, # type: Config
name, # type: str
handler_name, # type: str
deployment, # type: models.DeploymentPackage
role, # type: models.IAMRole
):
# type: (...) -> models.LambdaFunction
function_name = '%s-%s-%s' % (
config.app_name, config.chalice_stage, name)
return models.LambdaFunction(
resource_name=name,
function_name=function_name,
environment_variables=config.environment_variables,
runtime=config.lambda_python_version,
handler=handler_name,
tags=config.tags,
timeout=config.lambda_timeout,
memory_size=config.lambda_memory_size,
deployment_package=deployment,
role=role,
)