2016-02-12 19:39:20 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-02-16 20:38:59 +00:00
|
|
|
import botocore.client
|
2016-02-12 19:39:20 +00:00
|
|
|
import boto3
|
2016-02-16 20:15:34 +00:00
|
|
|
import hashlib
|
|
|
|
import io
|
|
|
|
import zipfile
|
2016-02-12 19:39:20 +00:00
|
|
|
import sure # noqa
|
|
|
|
|
|
|
|
from freezegun import freeze_time
|
2016-02-16 21:43:33 +00:00
|
|
|
from moto import mock_lambda, mock_s3
|
|
|
|
|
|
|
|
|
|
|
|
def get_test_zip_file():
|
|
|
|
zip_output = io.BytesIO()
|
2016-02-16 21:56:20 +00:00
|
|
|
zip_file = zipfile.ZipFile(zip_output, 'w')
|
|
|
|
zip_file.writestr('lambda_function.py', b'''\
|
2016-02-16 21:43:33 +00:00
|
|
|
def handler(event, context):
|
|
|
|
return "hello world"
|
|
|
|
''')
|
2016-02-16 21:56:20 +00:00
|
|
|
zip_file.close()
|
2016-02-16 21:43:33 +00:00
|
|
|
zip_output.seek(0)
|
|
|
|
return zip_output.read()
|
2016-02-12 19:39:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_lambda
|
|
|
|
def test_list_functions():
|
|
|
|
conn = boto3.client('lambda', 'us-west-2')
|
|
|
|
|
|
|
|
result = conn.list_functions()
|
|
|
|
|
|
|
|
result['Functions'].should.have.length_of(0)
|
|
|
|
|
|
|
|
|
|
|
|
@mock_lambda
|
|
|
|
@freeze_time('2015-01-01 00:00:00')
|
2016-02-16 21:43:33 +00:00
|
|
|
def test_create_based_on_s3_with_missing_bucket():
|
|
|
|
conn = boto3.client('lambda', 'us-west-2')
|
|
|
|
|
|
|
|
conn.create_function.when.called_with(
|
|
|
|
FunctionName='testFunction',
|
|
|
|
Runtime='python2.7',
|
|
|
|
Role='test-iam-role',
|
|
|
|
Handler='lambda_function.handler',
|
|
|
|
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 19:17:10 +00:00
|
|
|
def test_create_function_from_aws_bucket():
|
2016-02-16 21:43:33 +00:00
|
|
|
s3_conn = boto3.client('s3', 'us-west-2')
|
|
|
|
s3_conn.create_bucket(Bucket='test-bucket')
|
|
|
|
s3_conn.put_object(Bucket='test-bucket', Key='test.zip', Body=get_test_zip_file())
|
2016-02-12 19:39:20 +00:00
|
|
|
conn = boto3.client('lambda', 'us-west-2')
|
|
|
|
|
|
|
|
result = conn.create_function(
|
|
|
|
FunctionName='testFunction',
|
|
|
|
Runtime='python2.7',
|
|
|
|
Role='test-iam-role',
|
2016-02-16 19:17:10 +00:00
|
|
|
Handler='lambda_function.handler',
|
2016-02-12 19:39:20 +00:00
|
|
|
Code={
|
|
|
|
'S3Bucket': 'test-bucket',
|
|
|
|
'S3Key': 'test.zip',
|
|
|
|
},
|
|
|
|
Description='test lambda function',
|
|
|
|
Timeout=3,
|
|
|
|
MemorySize=128,
|
|
|
|
Publish=True,
|
2016-02-16 20:35:29 +00:00
|
|
|
VpcConfig={
|
|
|
|
"SecurityGroupIds": ["sg-123abc"],
|
|
|
|
"SubnetIds": ["subnet-123abc"],
|
|
|
|
},
|
2016-02-12 19:39:20 +00:00
|
|
|
)
|
|
|
|
result.should.equal({
|
|
|
|
'FunctionName': 'testFunction',
|
|
|
|
'FunctionArn': 'arn:aws:lambda:123456789012:function:testFunction',
|
|
|
|
'Runtime': 'python2.7',
|
|
|
|
'Role': 'test-iam-role',
|
2016-02-16 20:15:34 +00:00
|
|
|
'Handler': 'lambda_function.handler',
|
|
|
|
'CodeSize': 123,
|
2016-02-12 19:39:20 +00:00
|
|
|
'Description': 'test lambda function',
|
|
|
|
'Timeout': 3,
|
|
|
|
'MemorySize': 128,
|
|
|
|
'LastModified': '2015-01-01 00:00:00',
|
2016-02-16 20:15:34 +00:00
|
|
|
'CodeSha256': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
|
|
|
|
'Version': '$LATEST',
|
2016-02-16 20:35:29 +00:00
|
|
|
'VpcConfig': {
|
|
|
|
"SecurityGroupIds": ["sg-123abc"],
|
|
|
|
"SubnetIds": ["subnet-123abc"],
|
|
|
|
"VpcId": "vpc-123abc"
|
|
|
|
},
|
2016-02-16 20:15:34 +00:00
|
|
|
|
2016-02-16 20:24:41 +00:00
|
|
|
'ResponseMetadata': {'HTTPStatusCode': 201},
|
2016-02-16 20:15:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@mock_lambda
|
|
|
|
@freeze_time('2015-01-01 00:00:00')
|
|
|
|
def test_create_function_from_zipfile():
|
|
|
|
conn = boto3.client('lambda', 'us-west-2')
|
|
|
|
|
2016-02-16 21:43:33 +00:00
|
|
|
zip_content = get_test_zip_file()
|
2016-02-16 20:15:34 +00:00
|
|
|
result = conn.create_function(
|
|
|
|
FunctionName='testFunction',
|
|
|
|
Runtime='python2.7',
|
|
|
|
Role='test-iam-role',
|
|
|
|
Handler='lambda_function.handler',
|
|
|
|
Code={
|
|
|
|
'ZipFile': zip_content,
|
|
|
|
},
|
|
|
|
Description='test lambda function',
|
|
|
|
Timeout=3,
|
|
|
|
MemorySize=128,
|
|
|
|
Publish=True,
|
|
|
|
)
|
|
|
|
result.should.equal({
|
|
|
|
'FunctionName': 'testFunction',
|
|
|
|
'FunctionArn': 'arn:aws:lambda:123456789012:function:testFunction',
|
|
|
|
'Runtime': 'python2.7',
|
|
|
|
'Role': 'test-iam-role',
|
|
|
|
'Handler': 'lambda_function.handler',
|
|
|
|
'CodeSize': len(zip_content),
|
|
|
|
'Description': 'test lambda function',
|
|
|
|
'Timeout': 3,
|
|
|
|
'MemorySize': 128,
|
|
|
|
'LastModified': '2015-01-01 00:00:00',
|
|
|
|
'CodeSha256': hashlib.sha256(zip_content).hexdigest(),
|
2016-02-12 19:39:20 +00:00
|
|
|
'Version': '$LATEST',
|
2016-02-16 20:35:29 +00:00
|
|
|
'VpcConfig': {
|
|
|
|
"SecurityGroupIds": [],
|
|
|
|
"SubnetIds": [],
|
|
|
|
},
|
2016-02-12 19:39:20 +00:00
|
|
|
|
2016-02-16 20:24:41 +00:00
|
|
|
'ResponseMetadata': {'HTTPStatusCode': 201},
|
2016-02-12 19:39:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@mock_lambda
|
2016-02-16 21:43:33 +00:00
|
|
|
@mock_s3
|
2016-02-12 19:39:20 +00:00
|
|
|
@freeze_time('2015-01-01 00:00:00')
|
|
|
|
def test_get_function():
|
2016-02-16 21:43:33 +00:00
|
|
|
s3_conn = boto3.client('s3', 'us-west-2')
|
|
|
|
s3_conn.create_bucket(Bucket='test-bucket')
|
|
|
|
s3_conn.put_object(Bucket='test-bucket', Key='test.zip', Body=get_test_zip_file())
|
2016-02-12 19:39:20 +00:00
|
|
|
conn = boto3.client('lambda', 'us-west-2')
|
|
|
|
|
|
|
|
conn.create_function(
|
|
|
|
FunctionName='testFunction',
|
|
|
|
Runtime='python2.7',
|
|
|
|
Role='test-iam-role',
|
2016-02-16 19:17:10 +00:00
|
|
|
Handler='lambda_function.handler',
|
2016-02-12 19:39:20 +00:00
|
|
|
Code={
|
|
|
|
'S3Bucket': 'test-bucket',
|
|
|
|
'S3Key': 'test.zip',
|
|
|
|
},
|
|
|
|
Description='test lambda function',
|
|
|
|
Timeout=3,
|
|
|
|
MemorySize=128,
|
|
|
|
Publish=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
result = conn.get_function(FunctionName='testFunction')
|
|
|
|
|
|
|
|
result.should.equal({
|
|
|
|
"Code": {
|
|
|
|
"Location": "s3://lambda-functions.aws.amazon.com/test.zip",
|
|
|
|
"RepositoryType": "S3"
|
|
|
|
},
|
|
|
|
"Configuration": {
|
2016-02-16 20:15:34 +00:00
|
|
|
"CodeSha256": 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
|
|
|
|
"CodeSize": 123,
|
2016-02-12 19:39:20 +00:00
|
|
|
"Description": "test lambda function",
|
|
|
|
"FunctionArn": "arn:aws:lambda:123456789012:function:testFunction",
|
|
|
|
"FunctionName": "testFunction",
|
2016-02-16 19:17:10 +00:00
|
|
|
"Handler": "lambda_function.handler",
|
2016-02-12 19:39:20 +00:00
|
|
|
"LastModified": "2015-01-01 00:00:00",
|
|
|
|
"MemorySize": 128,
|
|
|
|
"Role": "test-iam-role",
|
|
|
|
"Runtime": "python2.7",
|
|
|
|
"Timeout": 3,
|
|
|
|
"Version": '$LATEST',
|
2016-02-16 20:35:29 +00:00
|
|
|
"VpcConfig": {
|
|
|
|
"SecurityGroupIds": [],
|
|
|
|
"SubnetIds": [],
|
|
|
|
}
|
2016-02-12 19:39:20 +00:00
|
|
|
},
|
|
|
|
'ResponseMetadata': {'HTTPStatusCode': 200},
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@mock_lambda
|
2016-02-16 21:43:33 +00:00
|
|
|
@mock_s3
|
2016-02-12 19:39:20 +00:00
|
|
|
def test_delete_function():
|
2016-02-16 21:43:33 +00:00
|
|
|
s3_conn = boto3.client('s3', 'us-west-2')
|
|
|
|
s3_conn.create_bucket(Bucket='test-bucket')
|
|
|
|
s3_conn.put_object(Bucket='test-bucket', Key='test.zip', Body=get_test_zip_file())
|
2016-02-12 19:39:20 +00:00
|
|
|
conn = boto3.client('lambda', 'us-west-2')
|
|
|
|
|
|
|
|
conn.create_function(
|
|
|
|
FunctionName='testFunction',
|
|
|
|
Runtime='python2.7',
|
|
|
|
Role='test-iam-role',
|
2016-02-16 19:17:10 +00:00
|
|
|
Handler='lambda_function.handler',
|
2016-02-12 19:39:20 +00:00
|
|
|
Code={
|
|
|
|
'S3Bucket': 'test-bucket',
|
|
|
|
'S3Key': 'test.zip',
|
|
|
|
},
|
|
|
|
Description='test lambda function',
|
|
|
|
Timeout=3,
|
|
|
|
MemorySize=128,
|
|
|
|
Publish=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
success_result = conn.delete_function(FunctionName='testFunction')
|
|
|
|
success_result.should.equal({'ResponseMetadata': {'HTTPStatusCode': 204}})
|
|
|
|
|
2016-02-16 20:38:59 +00:00
|
|
|
conn.delete_function.when.called_with(FunctionName='testFunctionThatDoesntExist').should.throw(botocore.client.ClientError)
|
2016-02-12 19:39:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_lambda
|
2016-02-16 21:43:33 +00:00
|
|
|
@mock_s3
|
2016-02-12 19:39:20 +00: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
|
|
|
|
|
|
|
|
"""
|
2016-02-16 21:43:33 +00:00
|
|
|
s3_conn = boto3.client('s3', 'us-west-2')
|
|
|
|
s3_conn.create_bucket(Bucket='test-bucket')
|
|
|
|
s3_conn.put_object(Bucket='test-bucket', Key='test.zip', Body=get_test_zip_file())
|
2016-02-12 19:39:20 +00:00
|
|
|
conn = boto3.client('lambda', 'us-west-2')
|
|
|
|
|
|
|
|
conn.list_functions()['Functions'].should.have.length_of(0)
|
|
|
|
|
|
|
|
conn.create_function(
|
|
|
|
FunctionName='testFunction',
|
|
|
|
Runtime='python2.7',
|
|
|
|
Role='test-iam-role',
|
2016-02-16 19:17:10 +00:00
|
|
|
Handler='lambda_function.handler',
|
2016-02-12 19:39:20 +00:00
|
|
|
Code={
|
|
|
|
'S3Bucket': 'test-bucket',
|
|
|
|
'S3Key': 'test.zip',
|
|
|
|
},
|
|
|
|
Description='test lambda function',
|
|
|
|
Timeout=3,
|
|
|
|
MemorySize=128,
|
|
|
|
Publish=True,
|
|
|
|
)
|
|
|
|
expected_function_result = {
|
|
|
|
"Code": {
|
|
|
|
"Location": "s3://lambda-functions.aws.amazon.com/test.zip",
|
|
|
|
"RepositoryType": "S3"
|
|
|
|
},
|
|
|
|
"Configuration": {
|
2016-02-16 20:15:34 +00:00
|
|
|
"CodeSha256": 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
|
|
|
|
"CodeSize": 123,
|
2016-02-12 19:39:20 +00:00
|
|
|
"Description": "test lambda function",
|
|
|
|
"FunctionArn": "arn:aws:lambda:123456789012:function:testFunction",
|
|
|
|
"FunctionName": "testFunction",
|
2016-02-16 19:17:10 +00:00
|
|
|
"Handler": "lambda_function.handler",
|
2016-02-12 19:39:20 +00:00
|
|
|
"LastModified": "2015-01-01 00:00:00",
|
|
|
|
"MemorySize": 128,
|
|
|
|
"Role": "test-iam-role",
|
|
|
|
"Runtime": "python2.7",
|
|
|
|
"Timeout": 3,
|
|
|
|
"Version": '$LATEST',
|
2016-02-16 20:35:29 +00:00
|
|
|
"VpcConfig": {
|
|
|
|
"SecurityGroupIds": [],
|
|
|
|
"SubnetIds": [],
|
|
|
|
}
|
2016-02-12 19:39:20 +00:00
|
|
|
},
|
|
|
|
'ResponseMetadata': {'HTTPStatusCode': 200},
|
|
|
|
}
|
|
|
|
conn.list_functions()['Functions'].should.equal([expected_function_result['Configuration']])
|
|
|
|
|
|
|
|
conn.get_function(FunctionName='testFunction').should.equal(expected_function_result)
|
|
|
|
conn.delete_function(FunctionName='testFunction')
|
|
|
|
|
|
|
|
conn.list_functions()['Functions'].should.have.length_of(0)
|