2022-03-17 11:32:31 -01:00
import base64
2016-02-16 15:38:59 -05:00
import botocore . client
2016-02-12 14:39:20 -05:00
import boto3
2016-02-16 15:15:34 -05:00
import hashlib
2021-10-18 19:44:29 +00:00
import sure # noqa # pylint: disable=unused-import
2021-09-21 15:19:49 +00:00
import pytest
2016-02-12 14:39:20 -05:00
2022-02-09 18:31:53 -01:00
from botocore . exceptions import ClientError
2016-02-12 14:39:20 -05:00
from freezegun import freeze_time
2021-09-21 15:19:49 +00:00
from moto import mock_lambda , mock_s3
2022-08-13 09:49:43 +00:00
from moto . core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
2021-09-21 15:19:49 +00:00
from uuid import uuid4
from . utilities import (
get_role_name ,
get_test_zip_file1 ,
get_test_zip_file2 ,
create_invalid_lambda ,
2019-10-31 08:44:26 -07:00
)
2016-02-16 16:43:33 -05:00
2017-11-26 21:28:28 +00:00
_lambda_region = " us-west-2 "
2019-08-20 21:54:57 -04:00
boto3 . setup_default_session ( region_name = _lambda_region )
2016-02-16 16:43:33 -05:00
2017-09-27 16:04:58 -07:00
2021-01-08 06:22:12 -08:00
@pytest.mark.parametrize ( " region " , [ " us-west-2 " , " cn-northwest-1 " ] )
@mock_lambda
def test_lambda_regions ( region ) :
client = boto3 . client ( " lambda " , region_name = region )
resp = client . list_functions ( )
resp [ " ResponseMetadata " ] [ " HTTPStatusCode " ] . should . equal ( 200 )
2016-02-12 14:39:20 -05:00
@mock_lambda
def test_list_functions ( ) :
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
initial_list = conn . list_functions ( ) [ " Functions " ]
initial_names = [ f [ " FunctionName " ] for f in initial_list ]
initial_names . shouldnt . contain ( function_name )
2021-08-28 15:26:44 +01:00
conn . create_function (
FunctionName = function_name ,
Runtime = " python3.7 " ,
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " ZipFile " : get_test_zip_file1 ( ) } ,
)
2021-09-21 15:19:49 +00:00
names = [ f [ " FunctionName " ] for f in conn . list_functions ( ) [ " Functions " ] ]
names . should . contain ( function_name )
2021-08-28 15:26:44 +01:00
conn . publish_version ( FunctionName = function_name , Description = " v2 " )
2021-09-21 15:19:49 +00:00
func_list = conn . list_functions ( ) [ " Functions " ]
our_functions = [ f for f in func_list if f [ " FunctionName " ] == function_name ]
our_functions . should . have . length_of ( 1 )
2021-08-28 15:26:44 +01:00
# FunctionVersion=ALL means we should get a list of all versions
2021-09-21 15:19:49 +00:00
full_list = conn . list_functions ( FunctionVersion = " ALL " ) [ " Functions " ]
our_functions = [ f for f in full_list if f [ " FunctionName " ] == function_name ]
our_functions . should . have . length_of ( 2 )
2021-08-28 15:26:44 +01:00
2021-09-21 15:19:49 +00:00
v1 = [ f for f in our_functions if f [ " Version " ] == " 1 " ] [ 0 ]
2021-08-28 15:26:44 +01:00
v1 [ " Description " ] . should . equal ( " v2 " )
v1 [ " FunctionArn " ] . should . equal (
2022-11-17 21:41:08 -01:00
f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } :1 "
2021-08-28 15:26:44 +01:00
)
2021-09-21 15:19:49 +00:00
latest = [ f for f in our_functions if f [ " Version " ] == " $LATEST " ] [ 0 ]
2021-08-28 15:26:44 +01:00
latest [ " Description " ] . should . equal ( " " )
latest [ " FunctionArn " ] . should . equal (
2022-11-17 21:41:08 -01:00
f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } :$LATEST "
2021-08-28 15:26:44 +01:00
)
2016-02-12 14:39:20 -05:00
2016-12-04 09:17:15 +10:00
2016-02-12 14:39:20 -05:00
@mock_lambda
2016-02-16 16:43:33 -05:00
def test_create_based_on_s3_with_missing_bucket ( ) :
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2016-02-16 16:43:33 -05:00
conn . create_function . when . called_with (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2016-02-16 16:43:33 -05:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-09-27 16:04:58 -07:00
Handler = " lambda_function.lambda_handler " ,
2016-02-16 16:43:33 -05:00
Code = { " S3Bucket " : " this-bucket-does-not-exist " , " S3Key " : " test.zip " } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
VpcConfig = { " SecurityGroupIds " : [ " sg-123abc " ] , " SubnetIds " : [ " subnet-123abc " ] } ,
) . should . throw ( botocore . client . ClientError )
@mock_lambda
@mock_s3
@freeze_time ( " 2015-01-01 00:00:00 " )
2016-02-16 14:17:10 -05:00
def test_create_function_from_aws_bucket ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2016-10-06 19:52:23 +10:00
zip_content = get_test_zip_file2 ( )
2016-10-10 01:13:52 +10:00
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2016-02-12 14:39:20 -05:00
result = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2016-02-12 14:39:20 -05:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-09-27 16:04:58 -07:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2016-02-12 14:39:20 -05:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
2022-02-08 20:12:51 -01:00
PackageType = " ZIP " ,
2016-02-12 14:39:20 -05:00
Publish = True ,
2016-02-16 15:35:29 -05:00
VpcConfig = { " SecurityGroupIds " : [ " sg-123abc " ] , " SubnetIds " : [ " subnet-123abc " ] } ,
2016-02-12 14:39:20 -05:00
)
2022-03-17 11:32:31 -01:00
result . should . have . key ( " FunctionName " ) . equals ( function_name )
result . should . have . key ( " FunctionArn " ) . equals (
2022-11-17 21:41:08 -01:00
f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } "
2022-03-17 11:32:31 -01:00
)
result . should . have . key ( " Runtime " ) . equals ( " python2.7 " )
result . should . have . key ( " Handler " ) . equals ( " lambda_function.lambda_handler " )
result . should . have . key ( " CodeSha256 " ) . equals (
base64 . b64encode ( hashlib . sha256 ( zip_content ) . digest ( ) ) . decode ( " utf-8 " )
2016-02-16 15:15:34 -05:00
)
2022-03-17 11:32:31 -01:00
result . should . have . key ( " State " ) . equals ( " Active " )
2016-02-16 15:15:34 -05:00
@mock_lambda
@freeze_time ( " 2015-01-01 00:00:00 " )
def test_create_function_from_zipfile ( ) :
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2016-10-06 19:52:23 +10:00
zip_content = get_test_zip_file1 ( )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2016-02-16 15:15:34 -05:00
result = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2016-02-16 15:15:34 -05:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-09-27 16:04:58 -07:00
Handler = " lambda_function.lambda_handler " ,
2016-02-16 15:15:34 -05:00
Code = { " ZipFile " : zip_content } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
2017-02-23 21:37:43 -05:00
# this is hard to match against, so remove it
result [ " ResponseMetadata " ] . pop ( " HTTPHeaders " , None )
# Botocore inserts retry attempts not seen in Python27
result [ " ResponseMetadata " ] . pop ( " RetryAttempts " , None )
2016-02-16 15:15:34 -05:00
result . pop ( " LastModified " )
2016-02-12 14:39:20 -05:00
2016-02-16 15:15:34 -05:00
result . should . equal (
2019-10-31 08:44:26 -07:00
{
2021-09-21 15:19:49 +00:00
" FunctionName " : function_name ,
2022-11-17 21:41:08 -01:00
" FunctionArn " : f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } " ,
2016-02-12 14:39:20 -05:00
" Runtime " : " python2.7 " ,
2019-11-07 17:11:13 +00:00
" Role " : result [ " Role " ] ,
2017-09-27 16:04:58 -07:00
" Handler " : " lambda_function.lambda_handler " ,
2016-02-17 16:24:17 -05:00
" CodeSize " : len ( zip_content ) ,
2016-02-12 14:39:20 -05:00
" Description " : " test lambda function " ,
" Timeout " : 3 ,
" MemorySize " : 128 ,
2022-03-17 11:32:31 -01:00
" CodeSha256 " : base64 . b64encode ( hashlib . sha256 ( zip_content ) . digest ( ) ) . decode (
" utf-8 "
) ,
2016-02-12 14:39:20 -05:00
" Version " : " 1 " ,
2016-02-16 15:35:29 -05:00
" VpcConfig " : { " SecurityGroupIds " : [ ] , " SubnetIds " : [ ] } ,
2016-02-16 15:24:41 -05:00
" ResponseMetadata " : { " HTTPStatusCode " : 201 } ,
2020-01-23 12:46:24 -06:00
" State " : " Active " ,
2021-01-17 17:28:49 +02:00
" Layers " : [ ] ,
2022-03-17 11:32:31 -01:00
" TracingConfig " : { " Mode " : " PassThrough " } ,
2016-02-12 14:39:20 -05:00
}
)
2022-03-17 11:32:31 -01:00
@mock_lambda
@pytest.mark.parametrize (
" tracing_mode " ,
[ ( None , " PassThrough " ) , ( " PassThrough " , " PassThrough " ) , ( " Active " , " Active " ) ] ,
)
def test_create_function__with_tracingmode ( tracing_mode ) :
conn = boto3 . client ( " lambda " , _lambda_region )
source , output = tracing_mode
zip_content = get_test_zip_file1 ( )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
kwargs = dict (
FunctionName = function_name ,
Runtime = " python2.7 " ,
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " ZipFile " : zip_content } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
if source :
kwargs [ " TracingConfig " ] = { " Mode " : source }
result = conn . create_function ( * * kwargs )
result . should . have . key ( " TracingConfig " ) . should . equal ( { " Mode " : output } )
2022-06-10 14:09:13 +00:00
@mock_lambda
def test_create_function_from_image ( ) :
lambda_client = boto3 . client ( " lambda " , " us-east-1 " )
fn_name = str ( uuid4 ( ) ) [ 0 : 6 ]
image_uri = " 111122223333.dkr.ecr.us-east-1.amazonaws.com/testlambda:latest "
dic = {
" FunctionName " : fn_name ,
" Role " : get_role_name ( ) ,
" Code " : { " ImageUri " : image_uri } ,
" PackageType " : " Image " ,
" Timeout " : 100 ,
}
resp = lambda_client . create_function ( * * dic )
resp . should . have . key ( " FunctionName " ) . equals ( fn_name )
resp . should . have . key ( " CodeSize " ) . equals ( 0 )
resp . should . have . key ( " CodeSha256 " )
resp . should . have . key ( " PackageType " ) . equals ( " Image " )
result = lambda_client . get_function ( FunctionName = fn_name )
result . should . have . key ( " Configuration " )
config = result [ " Configuration " ]
result . should . have . key ( " Code " )
code = result [ " Code " ]
code . should . have . key ( " RepositoryType " ) . equals ( " ECR " )
code . should . have . key ( " ImageUri " ) . equals ( image_uri )
image_uri_without_tag = image_uri . split ( " : " ) [ 0 ]
resolved_image_uri = f " { image_uri_without_tag } @sha256: { config [ ' CodeSha256 ' ] } "
code . should . have . key ( " ResolvedImageUri " ) . equals ( resolved_image_uri )
2016-02-12 14:39:20 -05:00
@mock_lambda
2016-02-16 16:43:33 -05:00
@mock_s3
2016-02-12 14:39:20 -05:00
@freeze_time ( " 2015-01-01 00:00:00 " )
def test_get_function ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2016-02-17 16:24:17 -05:00
2016-10-06 19:52:23 +10:00
zip_content = get_test_zip_file1 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2016-02-12 14:39:20 -05:00
conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2016-02-12 14:39:20 -05:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-09-27 16:04:58 -07:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2016-02-12 14:39:20 -05:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
2019-11-04 12:22:03 -03:00
Environment = { " Variables " : { " test_variable " : " test_value " } } ,
2016-02-12 14:39:20 -05:00
)
2021-09-21 15:19:49 +00:00
result = conn . get_function ( FunctionName = function_name )
2017-02-23 21:37:43 -05:00
# this is hard to match against, so remove it
result [ " ResponseMetadata " ] . pop ( " HTTPHeaders " , None )
# Botocore inserts retry attempts not seen in Python27
result [ " ResponseMetadata " ] . pop ( " RetryAttempts " , None )
2017-11-26 21:28:28 +00:00
result [ " Configuration " ] . pop ( " LastModified " )
2016-02-12 14:39:20 -05:00
2017-11-26 21:28:28 +00:00
result [ " Code " ] [ " Location " ] . should . equal (
2022-11-17 21:41:08 -01:00
f " s3://awslambda- { _lambda_region } -tasks.s3- { _lambda_region } .amazonaws.com/test.zip "
2019-10-31 08:44:26 -07:00
)
2017-11-26 21:28:28 +00:00
result [ " Code " ] [ " RepositoryType " ] . should . equal ( " S3 " )
result [ " Configuration " ] [ " CodeSha256 " ] . should . equal (
2022-03-17 11:32:31 -01:00
base64 . b64encode ( hashlib . sha256 ( zip_content ) . digest ( ) ) . decode ( " utf-8 " )
2019-10-31 08:44:26 -07:00
)
2017-11-26 21:28:28 +00:00
result [ " Configuration " ] [ " CodeSize " ] . should . equal ( len ( zip_content ) )
2019-05-22 01:49:56 +09:00
result [ " Configuration " ] [ " Description " ] . should . equal ( " test lambda function " )
result [ " Configuration " ] . should . contain ( " FunctionArn " )
2021-09-21 15:19:49 +00:00
result [ " Configuration " ] [ " FunctionName " ] . should . equal ( function_name )
2017-11-26 21:28:28 +00:00
result [ " Configuration " ] [ " Handler " ] . should . equal ( " lambda_function.lambda_handler " )
2019-05-22 01:49:56 +09:00
result [ " Configuration " ] [ " MemorySize " ] . should . equal ( 128 )
2019-11-07 17:11:13 +00:00
result [ " Configuration " ] [ " Role " ] . should . equal ( get_role_name ( ) )
2017-11-26 21:28:28 +00:00
result [ " Configuration " ] [ " Runtime " ] . should . equal ( " python2.7 " )
result [ " Configuration " ] [ " Timeout " ] . should . equal ( 3 )
2019-05-22 01:49:56 +09:00
result [ " Configuration " ] [ " Version " ] . should . equal ( " $LATEST " )
2017-11-26 21:28:28 +00:00
result [ " Configuration " ] . should . contain ( " VpcConfig " )
2019-11-04 12:22:03 -03:00
result [ " Configuration " ] . should . contain ( " Environment " )
result [ " Configuration " ] [ " Environment " ] . should . contain ( " Variables " )
result [ " Configuration " ] [ " Environment " ] [ " Variables " ] . should . equal (
{ " test_variable " : " test_value " }
)
2017-11-26 21:28:28 +00:00
2020-08-26 07:06:53 -03:00
# Test get function with qualifier
2021-09-21 15:19:49 +00:00
result = conn . get_function ( FunctionName = function_name , Qualifier = " $LATEST " )
2017-11-26 21:28:28 +00:00
result [ " Configuration " ] [ " Version " ] . should . equal ( " $LATEST " )
result [ " Configuration " ] [ " FunctionArn " ] . should . equal (
2022-11-17 21:41:08 -01:00
f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function: { function_name } :$LATEST "
2019-10-31 08:44:26 -07:00
)
2016-02-12 14:39:20 -05:00
2019-02-17 22:32:39 -06:00
# Test get function when can't find function name
2020-10-06 07:54:49 +02:00
with pytest . raises ( conn . exceptions . ResourceNotFoundException ) :
2019-02-17 22:32:39 -06:00
conn . get_function ( FunctionName = " junk " , Qualifier = " $LATEST " )
2019-11-04 12:22:03 -03:00
2021-08-28 07:23:44 +01:00
@pytest.mark.parametrize ( " key " , [ " FunctionName " , " FunctionArn " ] )
2021-01-10 17:24:04 +02:00
@mock_lambda
@mock_s3
@freeze_time ( " 2015-01-01 00:00:00 " )
2021-08-28 07:23:44 +01:00
def test_get_function_configuration ( key ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2021-01-10 17:24:04 +02:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2021-01-10 17:24:04 +02:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
zip_content = get_test_zip_file1 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2021-01-10 17:24:04 +02:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2021-01-10 17:24:04 +02:00
2021-08-28 07:23:44 +01:00
fxn = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2021-01-10 17:24:04 +02:00
Runtime = " python2.7 " ,
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2021-01-10 17:24:04 +02:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
Environment = { " Variables " : { " test_variable " : " test_value " } } ,
)
2021-08-28 07:23:44 +01:00
name_or_arn = fxn [ key ]
2021-01-10 17:24:04 +02:00
2021-08-28 07:23:44 +01:00
result = conn . get_function_configuration ( FunctionName = name_or_arn )
2021-01-10 17:24:04 +02:00
2022-03-17 11:32:31 -01:00
result [ " CodeSha256 " ] . should . equal (
base64 . b64encode ( hashlib . sha256 ( zip_content ) . digest ( ) ) . decode ( " utf-8 " )
)
2021-01-10 17:24:04 +02:00
result [ " CodeSize " ] . should . equal ( len ( zip_content ) )
result [ " Description " ] . should . equal ( " test lambda function " )
result . should . contain ( " FunctionArn " )
2021-09-21 15:19:49 +00:00
result [ " FunctionName " ] . should . equal ( function_name )
2021-01-10 17:24:04 +02:00
result [ " Handler " ] . should . equal ( " lambda_function.lambda_handler " )
result [ " MemorySize " ] . should . equal ( 128 )
result [ " Role " ] . should . equal ( get_role_name ( ) )
result [ " Runtime " ] . should . equal ( " python2.7 " )
result [ " Timeout " ] . should . equal ( 3 )
result [ " Version " ] . should . equal ( " $LATEST " )
result . should . contain ( " VpcConfig " )
result . should . contain ( " Environment " )
result [ " Environment " ] . should . contain ( " Variables " )
result [ " Environment " ] [ " Variables " ] . should . equal ( { " test_variable " : " test_value " } )
# Test get function with qualifier
result = conn . get_function_configuration (
2021-08-28 07:23:44 +01:00
FunctionName = name_or_arn , Qualifier = " $LATEST "
2021-01-10 17:24:04 +02:00
)
result [ " Version " ] . should . equal ( " $LATEST " )
result [ " FunctionArn " ] . should . equal (
2022-11-17 21:41:08 -01:00
f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } :$LATEST "
2021-01-10 17:24:04 +02:00
)
# Test get function when can't find function name
with pytest . raises ( conn . exceptions . ResourceNotFoundException ) :
conn . get_function_configuration ( FunctionName = " junk " , Qualifier = " $LATEST " )
2022-02-08 20:12:51 -01:00
@pytest.mark.parametrize ( " key " , [ " FunctionName " , " FunctionArn " ] )
@mock_lambda
@mock_s3
def test_get_function_code_signing_config ( key ) :
bucket_name = str ( uuid4 ( ) )
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
Bucket = bucket_name ,
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
zip_content = get_test_zip_file1 ( )
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
conn = boto3 . client ( " lambda " , _lambda_region )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
fxn = conn . create_function (
FunctionName = function_name ,
Runtime = " python3.7 " ,
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
CodeSigningConfigArn = " csc:arn " ,
)
name_or_arn = fxn [ key ]
result = conn . get_function_code_signing_config ( FunctionName = name_or_arn )
result [ " FunctionName " ] . should . equal ( function_name )
result [ " CodeSigningConfigArn " ] . should . equal ( " csc:arn " )
2019-11-04 10:44:01 -03:00
@mock_lambda
@mock_s3
def test_get_function_by_arn ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2019-11-04 12:22:03 -03:00
s3_conn = boto3 . client ( " s3 " , " us-east-1 " )
2020-02-02 11:48:32 +00:00
s3_conn . create_bucket (
Bucket = bucket_name ,
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2019-11-04 10:44:01 -03:00
zip_content = get_test_zip_file2 ( )
2019-11-04 12:22:03 -03:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
conn = boto3 . client ( " lambda " , " us-east-1 " )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2019-11-04 10:44:01 -03:00
2019-11-04 12:22:03 -03:00
fnc = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2019-11-04 12:22:03 -03:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-11-04 12:22:03 -03:00
Handler = " lambda_function.lambda_handler " ,
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
2019-11-04 10:44:01 -03:00
2019-11-04 12:22:03 -03:00
result = conn . get_function ( FunctionName = fnc [ " FunctionArn " ] )
2021-09-21 15:19:49 +00:00
result [ " Configuration " ] [ " FunctionName " ] . should . equal ( function_name )
2019-11-04 10:44:01 -03:00
2019-02-17 22:32:39 -06:00
2016-02-12 14:39:20 -05:00
@mock_lambda
2016-02-16 16:43:33 -05:00
@mock_s3
2016-02-12 14:39:20 -05:00
def test_delete_function ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2016-02-17 16:24:17 -05:00
2016-10-06 19:52:23 +10:00
zip_content = get_test_zip_file2 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2016-02-12 14:39:20 -05:00
conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2016-02-12 14:39:20 -05:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-09-27 16:04:58 -07:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2016-02-12 14:39:20 -05:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
2021-09-21 15:19:49 +00:00
success_result = conn . delete_function ( FunctionName = function_name )
2017-02-23 21:37:43 -05:00
# this is hard to match against, so remove it
success_result [ " ResponseMetadata " ] . pop ( " HTTPHeaders " , None )
# Botocore inserts retry attempts not seen in Python27
success_result [ " ResponseMetadata " ] . pop ( " RetryAttempts " , None )
2016-10-10 01:13:52 +10:00
2016-02-12 14:39:20 -05:00
success_result . should . equal ( { " ResponseMetadata " : { " HTTPStatusCode " : 204 } } )
2021-09-21 15:19:49 +00:00
func_list = conn . list_functions ( ) [ " Functions " ]
our_functions = [ f for f in func_list if f [ " FunctionName " ] == function_name ]
our_functions . should . have . length_of ( 0 )
2019-10-22 09:31:37 +01:00
@mock_lambda
@mock_s3
def test_delete_function_by_arn ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2019-10-22 09:31:37 +01:00
s3_conn = boto3 . client ( " s3 " , " us-east-1 " )
2020-02-02 11:48:32 +00:00
s3_conn . create_bucket (
Bucket = bucket_name ,
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2019-10-22 09:31:37 +01:00
zip_content = get_test_zip_file2 ( )
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
conn = boto3 . client ( " lambda " , " us-east-1 " )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2019-10-31 08:44:26 -07:00
2019-10-22 09:31:37 +01:00
fnc = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2019-10-22 09:31:37 +01:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-10-22 09:31:37 +01:00
Handler = " lambda_function.lambda_handler " ,
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
conn . delete_function ( FunctionName = fnc [ " FunctionArn " ] )
2021-09-21 15:19:49 +00:00
func_list = conn . list_functions ( ) [ " Functions " ]
our_functions = [ f for f in func_list if f [ " FunctionName " ] == function_name ]
our_functions . should . have . length_of ( 0 )
2019-10-22 09:31:37 +01:00
@mock_lambda
def test_delete_unknown_function ( ) :
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2017-02-23 21:37:43 -05:00
conn . delete_function . when . called_with (
FunctionName = " testFunctionThatDoesntExist "
) . should . throw ( botocore . client . ClientError )
2016-02-12 14:39:20 -05:00
2022-03-15 14:07:01 -01:00
@mock_lambda
@pytest.mark.parametrize (
" name " ,
[
" bad_function_name " ,
f " arn:aws:lambda:eu-west-1: { ACCOUNT_ID } :function:bad_function_name " ,
] ,
)
def test_publish_version_unknown_function ( name ) :
client = boto3 . client ( " lambda " , " eu-west-1 " )
with pytest . raises ( ClientError ) as exc :
client . publish_version ( FunctionName = name , Description = " v2 " )
err = exc . value . response [ " Error " ]
err [ " Code " ] . should . equal ( " ResourceNotFoundException " )
err [ " Message " ] . should . equal (
f " Function not found: arn:aws:lambda:eu-west-1: { ACCOUNT_ID } :function:bad_function_name "
)
2017-11-26 21:28:28 +00:00
@mock_lambda
@mock_s3
def test_publish ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2017-11-26 21:28:28 +00:00
zip_content = get_test_zip_file2 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2017-11-26 21:28:28 +00:00
conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2017-11-26 21:28:28 +00:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-11-26 21:28:28 +00:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2017-11-26 21:28:28 +00:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
2019-05-22 01:49:56 +09:00
Publish = False ,
2017-11-26 21:28:28 +00:00
)
2021-09-21 15:19:49 +00:00
function_list = conn . list_functions ( FunctionVersion = " ALL " ) [ " Functions " ]
our_functions = [ f for f in function_list if f [ " FunctionName " ] == function_name ]
our_functions . should . have . length_of ( 1 )
latest_arn = our_functions [ 0 ] [ " FunctionArn " ]
2017-11-26 21:28:28 +00:00
2021-09-21 15:19:49 +00:00
res = conn . publish_version ( FunctionName = function_name )
2019-03-29 15:52:47 +09:00
assert res [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 201
2017-11-26 21:28:28 +00:00
2021-09-21 15:19:49 +00:00
function_list = conn . list_functions ( FunctionVersion = " ALL " ) [ " Functions " ]
our_functions = [ f for f in function_list if f [ " FunctionName " ] == function_name ]
our_functions . should . have . length_of ( 2 )
2017-11-26 21:28:28 +00:00
# #SetComprehension ;-)
2021-09-21 15:19:49 +00:00
published_arn = list ( { f [ " FunctionArn " ] for f in our_functions } - { latest_arn } ) [ 0 ]
2022-11-17 21:41:08 -01:00
published_arn . should . contain ( f " { function_name } :1 " )
2017-11-26 21:28:28 +00:00
2021-09-21 15:19:49 +00:00
conn . delete_function ( FunctionName = function_name , Qualifier = " 1 " )
2017-11-26 21:28:28 +00:00
2021-09-21 15:19:49 +00:00
function_list = conn . list_functions ( ) [ " Functions " ]
our_functions = [ f for f in function_list if f [ " FunctionName " ] == function_name ]
our_functions . should . have . length_of ( 1 )
our_functions [ 0 ] [ " FunctionArn " ] . should . contain ( function_name )
2017-11-26 21:28:28 +00:00
2016-02-12 14:39:20 -05:00
@mock_lambda
2016-02-16 16:43:33 -05:00
@mock_s3
2016-02-12 14:39:20 -05:00
@freeze_time ( " 2015-01-01 00:00:00 " )
def test_list_create_list_get_delete_list ( ) :
"""
test ` list - > create - > list - > get - > delete - > list ` integration
"""
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2016-02-17 16:24:17 -05:00
2016-10-06 19:52:23 +10:00
zip_content = get_test_zip_file2 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2016-02-12 14:39:20 -05:00
2021-09-21 15:19:49 +00:00
initial_list = conn . list_functions ( ) [ " Functions " ]
initial_names = [ f [ " FunctionName " ] for f in initial_list ]
initial_names . shouldnt . contain ( function_name )
2016-02-12 14:39:20 -05:00
2021-09-21 15:19:49 +00:00
function_name = function_name
2016-02-12 14:39:20 -05:00
conn . create_function (
2021-08-28 15:26:44 +01:00
FunctionName = function_name ,
2016-02-12 14:39:20 -05:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-09-27 16:04:58 -07:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2016-02-12 14:39:20 -05:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
expected_function_result = {
" Code " : {
2022-11-17 21:41:08 -01:00
" Location " : f " s3://awslambda- { _lambda_region } -tasks.s3- { _lambda_region } .amazonaws.com/test.zip " ,
2016-02-12 14:39:20 -05:00
" RepositoryType " : " S3 " ,
} ,
" Configuration " : {
2022-03-17 11:32:31 -01:00
" CodeSha256 " : base64 . b64encode ( hashlib . sha256 ( zip_content ) . digest ( ) ) . decode (
" utf-8 "
) ,
2016-02-17 16:24:17 -05:00
" CodeSize " : len ( zip_content ) ,
2016-02-12 14:39:20 -05:00
" Description " : " test lambda function " ,
2021-08-28 15:26:44 +01:00
" FunctionName " : function_name ,
2017-09-27 16:04:58 -07:00
" Handler " : " lambda_function.lambda_handler " ,
2016-02-12 14:39:20 -05:00
" MemorySize " : 128 ,
2019-11-07 17:11:13 +00:00
" Role " : get_role_name ( ) ,
2016-02-12 14:39:20 -05:00
" Runtime " : " python2.7 " ,
" Timeout " : 3 ,
" Version " : " $LATEST " ,
2016-02-16 15:35:29 -05:00
" VpcConfig " : { " SecurityGroupIds " : [ ] , " SubnetIds " : [ ] } ,
2020-01-23 12:46:24 -06:00
" State " : " Active " ,
2021-01-17 17:28:49 +02:00
" Layers " : [ ] ,
2022-03-17 11:32:31 -01:00
" LastUpdateStatus " : " Successful " ,
" TracingConfig " : { " Mode " : " PassThrough " } ,
2016-02-12 14:39:20 -05:00
} ,
" ResponseMetadata " : { " HTTPStatusCode " : 200 } ,
}
2021-08-28 15:26:44 +01:00
functions = conn . list_functions ( ) [ " Functions " ]
2021-09-21 15:19:49 +00:00
func_names = [ f [ " FunctionName " ] for f in functions ]
func_names . should . contain ( function_name )
func_arn = [
f [ " FunctionArn " ] for f in functions if f [ " FunctionName " ] == function_name
] [ 0 ]
func_arn . should . equal (
2022-11-17 21:41:08 -01:00
f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } "
2021-08-28 15:26:44 +01:00
)
functions = conn . list_functions ( FunctionVersion = " ALL " ) [ " Functions " ]
2021-09-21 15:19:49 +00:00
our_functions = [ f for f in functions if f [ " FunctionName " ] == function_name ]
our_functions . should . have . length_of ( 2 )
2021-08-28 15:26:44 +01:00
2021-09-21 15:19:49 +00:00
latest = [ f for f in our_functions if f [ " Version " ] == " $LATEST " ] [ 0 ]
2021-08-28 15:26:44 +01:00
latest [ " FunctionArn " ] . should . equal (
2022-11-17 21:41:08 -01:00
f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } :$LATEST "
2021-08-28 15:26:44 +01:00
)
latest . pop ( " FunctionArn " )
latest . pop ( " LastModified " )
latest . should . equal ( expected_function_result [ " Configuration " ] )
2021-09-21 15:19:49 +00:00
published = [ f for f in our_functions if f [ " Version " ] != " $LATEST " ] [ 0 ]
2021-08-28 15:26:44 +01:00
published [ " Version " ] . should . equal ( " 1 " )
published [ " FunctionArn " ] . should . equal (
2022-11-17 21:41:08 -01:00
f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } :1 "
2021-08-28 15:26:44 +01:00
)
func = conn . get_function ( FunctionName = function_name )
func [ " Configuration " ] [ " FunctionArn " ] . should . equal (
2022-11-17 21:41:08 -01:00
f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } "
2021-08-28 15:26:44 +01:00
)
2016-02-12 14:39:20 -05:00
2017-02-23 21:37:43 -05:00
# this is hard to match against, so remove it
func [ " ResponseMetadata " ] . pop ( " HTTPHeaders " , None )
# Botocore inserts retry attempts not seen in Python27
func [ " ResponseMetadata " ] . pop ( " RetryAttempts " , None )
2017-02-23 19:43:48 -05:00
func [ " Configuration " ] . pop ( " LastModified " )
2021-08-28 15:26:44 +01:00
func [ " Configuration " ] . pop ( " FunctionArn " )
2016-10-10 01:13:52 +10:00
2016-06-29 23:45:21 +00:00
func . should . equal ( expected_function_result )
2021-09-21 15:19:49 +00:00
conn . delete_function ( FunctionName = function_name )
2017-03-16 22:00:57 -04:00
2021-09-21 15:19:49 +00:00
functions = conn . list_functions ( ) [ " Functions " ]
func_names = [ f [ " FunctionName " ] for f in functions ]
func_names . shouldnt . contain ( function_name )
2017-09-13 16:44:22 +12:00
2017-10-25 20:04:00 +01:00
2017-09-15 14:49:32 +12:00
@mock_lambda
@freeze_time ( " 2015-01-01 00:00:00 " )
def test_get_function_created_with_zipfile ( ) :
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2017-09-15 14:49:32 +12:00
zip_content = get_test_zip_file1 ( )
2021-10-18 19:44:29 +00:00
conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2017-09-15 14:49:32 +12:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-09-15 14:49:32 +12:00
Handler = " lambda_function.handler " ,
Code = { " ZipFile " : zip_content } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
2021-09-21 15:19:49 +00:00
response = conn . get_function ( FunctionName = function_name )
2017-09-15 14:49:32 +12:00
response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] . should . equal ( 200 )
2017-09-27 16:04:58 -07:00
assert len ( response [ " Code " ] ) == 2
assert response [ " Code " ] [ " RepositoryType " ] == " S3 "
assert response [ " Code " ] [ " Location " ] . startswith (
2022-11-17 21:41:08 -01:00
f " s3://awslambda- { _lambda_region } -tasks.s3- { _lambda_region } .amazonaws.com "
2019-10-31 08:44:26 -07:00
)
2022-03-17 11:32:31 -01:00
response . should . have . key ( " Configuration " )
config = response [ " Configuration " ]
config . should . have . key ( " CodeSha256 " ) . equals (
base64 . b64encode ( hashlib . sha256 ( zip_content ) . digest ( ) ) . decode ( " utf-8 " )
2017-10-03 13:33:50 +13:00
)
2022-03-17 11:32:31 -01:00
config . should . have . key ( " CodeSize " ) . equals ( len ( zip_content ) )
config . should . have . key ( " Description " ) . equals ( " test lambda function " )
config . should . have . key ( " FunctionArn " ) . equals (
f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } "
2017-10-03 15:23:00 +13:00
)
2022-03-17 11:32:31 -01:00
config . should . have . key ( " FunctionName " ) . equals ( function_name )
config . should . have . key ( " Handler " ) . equals ( " lambda_function.handler " )
config . should . have . key ( " MemorySize " ) . equals ( 128 )
config . should . have . key ( " Role " ) . equals ( get_role_name ( ) )
config . should . have . key ( " Runtime " ) . equals ( " python2.7 " )
config . should . have . key ( " Timeout " ) . equals ( 3 )
config . should . have . key ( " Version " ) . equals ( " $LATEST " )
config . should . have . key ( " State " ) . equals ( " Active " )
config . should . have . key ( " Layers " ) . equals ( [ ] )
config . should . have . key ( " LastUpdateStatus " ) . equals ( " Successful " )
2019-02-16 09:27:23 -06:00
@mock_lambda
@mock_s3
def test_list_versions_by_function ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2019-02-16 09:27:23 -06:00
zip_content = get_test_zip_file2 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2019-02-16 09:27:23 -06:00
conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2019-02-16 09:27:23 -06:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-02-16 09:27:23 -06:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2019-02-16 09:27:23 -06:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
2021-09-21 15:19:49 +00:00
res = conn . publish_version ( FunctionName = function_name )
2019-03-29 15:52:47 +09:00
assert res [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 201
2021-09-21 15:19:49 +00:00
versions = conn . list_versions_by_function ( FunctionName = function_name )
2019-05-22 01:49:56 +09:00
assert len ( versions [ " Versions " ] ) == 3
2022-11-17 21:41:08 -01:00
assert (
versions [ " Versions " ] [ 0 ] [ " FunctionArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function: { function_name } :$LATEST "
)
assert (
versions [ " Versions " ] [ 1 ] [ " FunctionArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function: { function_name } :1 "
)
assert (
versions [ " Versions " ] [ 2 ] [ " FunctionArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function: { function_name } :2 "
)
2019-05-22 01:49:56 +09:00
conn . create_function (
FunctionName = " testFunction_2 " ,
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-05-22 01:49:56 +09:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2019-05-22 01:49:56 +09:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = False ,
)
versions = conn . list_versions_by_function ( FunctionName = " testFunction_2 " )
assert len ( versions [ " Versions " ] ) == 1
2022-11-17 21:41:08 -01:00
assert (
versions [ " Versions " ] [ 0 ] [ " FunctionArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function:testFunction_2:$LATEST "
2019-10-31 08:44:26 -07:00
)
2019-02-16 09:27:23 -06:00
2019-02-16 12:37:46 -06:00
@mock_lambda
@mock_s3
def test_create_function_with_already_exists ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2019-02-16 12:37:46 -06:00
zip_content = get_test_zip_file2 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2019-02-16 12:37:46 -06:00
conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2019-02-16 12:37:46 -06:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-02-16 12:37:46 -06:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2019-02-16 12:37:46 -06:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
response = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2019-02-16 12:37:46 -06:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-02-16 12:37:46 -06:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2019-02-16 12:37:46 -06:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
2021-09-21 15:19:49 +00:00
assert response [ " FunctionName " ] == function_name
2019-02-16 12:37:46 -06:00
@mock_lambda
@mock_s3
def test_list_versions_by_function_for_nonexistent_function ( ) :
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
versions = conn . list_versions_by_function ( FunctionName = function_name )
2019-02-16 12:37:46 -06:00
2019-02-18 08:52:37 -06:00
assert len ( versions [ " Versions " ] ) == 0
2019-08-20 21:54:57 -04:00
2021-08-28 07:23:44 +01:00
@pytest.mark.parametrize ( " key " , [ " FunctionName " , " FunctionArn " ] )
2019-10-08 15:59:03 -05:00
@mock_lambda
@mock_s3
2021-08-28 07:23:44 +01:00
def test_update_configuration ( key ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2019-10-08 15:59:03 -05:00
zip_content = get_test_zip_file2 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2019-10-08 15:59:03 -05:00
fxn = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2019-10-08 15:59:03 -05:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-10-08 15:59:03 -05:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2019-10-08 15:59:03 -05:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
2019-11-04 12:22:03 -03:00
Environment = { " Variables " : { " test_old_environment " : " test_old_value " } } ,
2019-10-08 15:59:03 -05:00
)
2021-08-28 07:23:44 +01:00
name_or_arn = fxn [ key ]
2019-10-08 15:59:03 -05:00
assert fxn [ " Description " ] == " test lambda function "
assert fxn [ " Handler " ] == " lambda_function.lambda_handler "
assert fxn [ " MemorySize " ] == 128
assert fxn [ " Runtime " ] == " python2.7 "
assert fxn [ " Timeout " ] == 3
updated_config = conn . update_function_configuration (
2021-08-28 07:23:44 +01:00
FunctionName = name_or_arn ,
2019-10-08 15:59:03 -05:00
Description = " updated test lambda function " ,
Handler = " lambda_function.new_lambda_handler " ,
Runtime = " python3.6 " ,
2019-11-04 10:44:01 -03:00
Timeout = 7 ,
2020-11-18 08:45:31 +00:00
VpcConfig = { " SecurityGroupIds " : [ " sg-123abc " ] , " SubnetIds " : [ " subnet-123abc " ] } ,
2019-11-04 12:22:03 -03:00
Environment = { " Variables " : { " test_environment " : " test_value " } } ,
2019-10-08 15:59:03 -05:00
)
assert updated_config [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 200
assert updated_config [ " Description " ] == " updated test lambda function "
assert updated_config [ " Handler " ] == " lambda_function.new_lambda_handler "
assert updated_config [ " MemorySize " ] == 128
assert updated_config [ " Runtime " ] == " python3.6 "
assert updated_config [ " Timeout " ] == 7
2019-11-04 12:22:03 -03:00
assert updated_config [ " Environment " ] [ " Variables " ] == {
" test_environment " : " test_value "
}
2020-11-18 08:45:31 +00:00
assert updated_config [ " VpcConfig " ] == {
" SecurityGroupIds " : [ " sg-123abc " ] ,
" SubnetIds " : [ " subnet-123abc " ] ,
" VpcId " : " vpc-123abc " ,
}
2019-10-08 15:59:03 -05:00
2021-08-28 07:23:44 +01:00
@pytest.mark.parametrize ( " key " , [ " FunctionName " , " FunctionArn " ] )
2019-10-08 15:59:03 -05:00
@mock_lambda
2021-08-28 07:23:44 +01:00
def test_update_function_zip ( key ) :
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2019-10-08 15:59:03 -05:00
zip_content_one = get_test_zip_file1 ( )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2019-10-08 15:59:03 -05:00
fxn = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2019-10-08 15:59:03 -05:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-10-08 15:59:03 -05:00
Handler = " lambda_function.lambda_handler " ,
Code = { " ZipFile " : zip_content_one } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
2021-08-28 07:23:44 +01:00
name_or_arn = fxn [ key ]
2019-10-08 15:59:03 -05:00
zip_content_two = get_test_zip_file2 ( )
2021-08-28 07:23:44 +01:00
conn . update_function_code (
FunctionName = name_or_arn , ZipFile = zip_content_two , Publish = True
2019-10-08 15:59:03 -05:00
)
2021-09-21 15:19:49 +00:00
response = conn . get_function ( FunctionName = function_name , Qualifier = " 2 " )
2019-10-08 15:59:03 -05:00
response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] . should . equal ( 200 )
assert len ( response [ " Code " ] ) == 2
assert response [ " Code " ] [ " RepositoryType " ] == " S3 "
assert response [ " Code " ] [ " Location " ] . startswith (
2022-11-17 21:41:08 -01:00
f " s3://awslambda- { _lambda_region } -tasks.s3- { _lambda_region } .amazonaws.com "
2019-10-31 08:44:26 -07:00
)
2022-03-17 11:32:31 -01:00
config = response [ " Configuration " ]
config . should . have . key ( " CodeSize " ) . equals ( len ( zip_content_two ) )
config . should . have . key ( " Description " ) . equals ( " test lambda function " )
config . should . have . key ( " FunctionArn " ) . equals (
f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } :2 "
2019-10-09 16:20:49 -05:00
)
2022-03-17 11:32:31 -01:00
config . should . have . key ( " FunctionName " ) . equals ( function_name )
config . should . have . key ( " Version " ) . equals ( " 2 " )
config . should . have . key ( " LastUpdateStatus " ) . equals ( " Successful " )
2019-10-09 16:20:49 -05:00
2019-10-31 08:44:26 -07:00
2019-10-09 16:20:49 -05:00
@mock_lambda
@mock_s3
def test_update_function_s3 ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2019-10-09 16:20:49 -05:00
zip_content = get_test_zip_file1 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2019-10-09 16:20:49 -05:00
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2019-10-09 16:20:49 -05:00
2022-03-17 11:32:31 -01:00
conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2019-10-09 16:20:49 -05:00
Runtime = " python2.7 " ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-10-09 16:20:49 -05:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2019-10-09 16:20:49 -05:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
zip_content_two = get_test_zip_file2 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test2.zip " , Body = zip_content_two )
2019-10-09 16:20:49 -05:00
2021-10-18 19:44:29 +00:00
conn . update_function_code (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
S3Bucket = bucket_name ,
2019-10-09 16:20:49 -05:00
S3Key = " test2.zip " ,
Publish = True ,
)
2021-09-21 15:19:49 +00:00
response = conn . get_function ( FunctionName = function_name , Qualifier = " 2 " )
2019-10-09 16:20:49 -05:00
response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] . should . equal ( 200 )
assert len ( response [ " Code " ] ) == 2
assert response [ " Code " ] [ " RepositoryType " ] == " S3 "
assert response [ " Code " ] [ " Location " ] . startswith (
2022-11-17 21:41:08 -01:00
f " s3://awslambda- { _lambda_region } -tasks.s3- { _lambda_region } .amazonaws.com "
2019-10-31 08:44:26 -07:00
)
2022-03-17 11:32:31 -01:00
config = response [ " Configuration " ]
config . should . have . key ( " CodeSha256 " ) . equals (
base64 . b64encode ( hashlib . sha256 ( zip_content_two ) . digest ( ) ) . decode ( " utf-8 " )
)
config . should . have . key ( " CodeSize " ) . equals ( len ( zip_content_two ) )
config . should . have . key ( " Description " ) . equals ( " test lambda function " )
config . should . have . key ( " FunctionArn " ) . equals (
f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } :2 "
2019-10-08 15:59:03 -05:00
)
2022-03-17 11:32:31 -01:00
config . should . have . key ( " FunctionName " ) . equals ( function_name )
config . should . have . key ( " Version " ) . equals ( " 2 " )
config . should . have . key ( " LastUpdateStatus " ) . equals ( " Successful " )
2019-11-07 17:11:13 +00:00
@mock_lambda
def test_create_function_with_invalid_arn ( ) :
err = create_invalid_lambda ( " test-iam-role " )
2020-10-06 08:04:09 +02:00
err . value . response [ " Error " ] [ " Message " ] . should . equal (
2020-09-10 13:50:26 +05:30
r " 1 validation error detected: Value ' test-iam-role ' at ' role ' failed to satisfy constraint: Member must satisfy regular expression pattern: arn:(aws[a-zA-Z-]*)?:iam::( \ d {12} ):role/?[a-zA-Z_0-9+=,.@ \ -_/]+ "
2019-11-07 17:11:13 +00:00
)
@mock_lambda
def test_create_function_with_arn_from_different_account ( ) :
err = create_invalid_lambda ( " arn:aws:iam::000000000000:role/example_role " )
2020-10-06 08:04:09 +02:00
err . value . response [ " Error " ] [ " Message " ] . should . equal (
2019-11-07 17:11:13 +00:00
" Cross-account pass role is not allowed. "
)
@mock_lambda
def test_create_function_with_unknown_arn ( ) :
err = create_invalid_lambda (
" arn:aws:iam:: " + str ( ACCOUNT_ID ) + " :role/service-role/unknown_role "
)
2020-10-06 08:04:09 +02:00
err . value . response [ " Error " ] [ " Message " ] . should . equal (
2019-11-07 17:11:13 +00:00
" The role defined for the function cannot be assumed by Lambda. "
)
2022-02-09 18:31:53 -01:00
@mock_lambda
def test_remove_unknown_permission_throws_error ( ) :
conn = boto3 . client ( " lambda " , _lambda_region )
zip_content = get_test_zip_file1 ( )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
f = conn . create_function (
FunctionName = function_name ,
Runtime = " python3.7 " ,
Role = ( get_role_name ( ) ) ,
Handler = " lambda_function.handler " ,
Code = { " ZipFile " : zip_content } ,
)
arn = f [ " FunctionArn " ]
with pytest . raises ( ClientError ) as exc :
conn . remove_permission ( FunctionName = arn , StatementId = " 1 " )
err = exc . value . response [ " Error " ]
err [ " Code " ] . should . equal ( " ResourceNotFoundException " )
err [ " Message " ] . should . equal ( " No policy is associated with the given resource. " )
2022-10-18 09:35:17 +00:00
@mock_lambda
def test_multiple_qualifiers ( ) :
client = boto3 . client ( " lambda " , " us-east-1 " )
zip_content = get_test_zip_file1 ( )
fn_name = str ( uuid4 ( ) ) [ 0 : 6 ]
client . create_function (
FunctionName = fn_name ,
Runtime = " python3.7 " ,
Role = ( get_role_name ( ) ) ,
Handler = " lambda_function.handler " ,
Code = { " ZipFile " : zip_content } ,
)
for _ in range ( 10 ) :
client . publish_version ( FunctionName = fn_name )
resp = client . list_versions_by_function ( FunctionName = fn_name ) [ " Versions " ]
qualis = [ fn [ " FunctionArn " ] . split ( " : " ) [ - 1 ] for fn in resp ]
qualis . should . equal ( [ " $LATEST " , " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 6 " , " 7 " , " 8 " , " 9 " , " 10 " ] )
client . delete_function ( FunctionName = fn_name , Qualifier = " 4 " )
client . delete_function ( FunctionName = fn_name , Qualifier = " 5 " )
resp = client . list_versions_by_function ( FunctionName = fn_name ) [ " Versions " ]
qualis = [ fn [ " FunctionArn " ] . split ( " : " ) [ - 1 ] for fn in resp ]
qualis . should . equal ( [ " $LATEST " , " 1 " , " 2 " , " 3 " , " 6 " , " 7 " , " 8 " , " 9 " , " 10 " ] )
fn = client . get_function ( FunctionName = fn_name , Qualifier = " 6 " ) [ " Configuration " ]
fn [ " FunctionArn " ] . should . equal (
f " arn:aws:lambda:us-east-1: { ACCOUNT_ID } :function: { fn_name } :6 "
)
2022-11-07 12:03:07 -08:00
def test_get_role_name_utility_race_condition ( ) :
# Play with these variables as needed to reproduce the error.
max_workers , num_threads = 3 , 15
errors = [ ]
roles = [ ]
def thread_function ( _ ) :
while True :
# noinspection PyBroadException
try :
role = get_role_name ( )
except ClientError as e :
errors . append ( str ( e ) )
break
except Exception :
# boto3 and our own IAMBackend are not thread-safe,
# and occasionally throw weird errors, so we just
# pass and retry.
# https://github.com/boto/boto3/issues/1592
pass
else :
roles . append ( role )
break
import concurrent . futures
with concurrent . futures . ThreadPoolExecutor ( max_workers = max_workers ) as executor :
executor . map ( thread_function , range ( num_threads ) )
# Check all threads are accounted for, all roles are the same entity,
# and there are no client errors.
assert len ( errors ) + len ( roles ) == num_threads
assert roles . count ( roles [ 0 ] ) == len ( roles )
assert len ( errors ) == 0