2016-02-12 19:39:20 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-10-06 09:52:23 +00:00
|
|
|
import base64
|
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
|
2016-10-06 14:14:47 +00:00
|
|
|
import json
|
2016-02-16 20:15:34 +00:00
|
|
|
import zipfile
|
2016-02-12 19:39:20 +00:00
|
|
|
import sure # noqa
|
|
|
|
|
|
|
|
from freezegun import freeze_time
|
2017-02-24 00:43:48 +00:00
|
|
|
from moto import mock_lambda, mock_s3, mock_ec2, settings
|
2016-02-16 21:43:33 +00:00
|
|
|
|
|
|
|
|
2016-10-06 09:52:23 +00:00
|
|
|
def _process_lamda(pfunc):
|
2016-02-16 21:43:33 +00:00
|
|
|
zip_output = io.BytesIO()
|
2016-10-06 09:52:23 +00:00
|
|
|
zip_file = zipfile.ZipFile(zip_output, 'w', zipfile.ZIP_DEFLATED)
|
|
|
|
zip_file.writestr('lambda_function.zip', pfunc)
|
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
|
|
|
|
|
|
|
|
2016-10-06 09:52:23 +00:00
|
|
|
def get_test_zip_file1():
|
2016-10-09 15:13:52 +00:00
|
|
|
pfunc = """
|
2016-10-06 09:52:23 +00:00
|
|
|
def lambda_handler(event, context):
|
2016-12-03 23:17:15 +00:00
|
|
|
return event
|
2016-10-06 09:52:23 +00:00
|
|
|
"""
|
|
|
|
return _process_lamda(pfunc)
|
|
|
|
|
|
|
|
|
|
|
|
def get_test_zip_file2():
|
2016-10-09 15:13:52 +00:00
|
|
|
pfunc = """
|
2016-10-06 09:52:23 +00:00
|
|
|
def lambda_handler(event, context):
|
|
|
|
volume_id = event.get('volume_id')
|
2016-10-09 15:13:52 +00:00
|
|
|
print('get volume details for %s' % volume_id)
|
2016-10-06 09:52:23 +00:00
|
|
|
import boto3
|
2017-02-24 00:43:48 +00:00
|
|
|
ec2 = boto3.resource('ec2', region_name='us-west-2', endpoint_url="http://{base_url}")
|
2016-10-06 09:52:23 +00:00
|
|
|
vol = ec2.Volume(volume_id)
|
2016-10-09 15:13:52 +00:00
|
|
|
print('Volume - %s state=%s, size=%s' % (volume_id, vol.state, vol.size))
|
2016-12-03 23:17:15 +00:00
|
|
|
return event
|
2017-03-13 01:04:19 +00:00
|
|
|
""".format(base_url="localhost:5000" if settings.TEST_SERVER_MODE else "ec2.us-west-2.amazonaws.com")
|
2016-10-06 09:52:23 +00:00
|
|
|
return _process_lamda(pfunc)
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-12-03 23:17:15 +00:00
|
|
|
|
2016-06-22 19:24:46 +00:00
|
|
|
@mock_lambda
|
2016-12-03 23:17:15 +00:00
|
|
|
def test_invoke_requestresponse_function():
|
2016-06-22 19:24:46 +00:00
|
|
|
conn = boto3.client('lambda', 'us-west-2')
|
|
|
|
conn.create_function(
|
|
|
|
FunctionName='testFunction',
|
|
|
|
Runtime='python2.7',
|
|
|
|
Role='test-iam-role',
|
|
|
|
Handler='lambda_function.handler',
|
|
|
|
Code={
|
2016-10-06 09:52:23 +00:00
|
|
|
'ZipFile': get_test_zip_file1(),
|
2016-06-22 19:24:46 +00:00
|
|
|
},
|
|
|
|
Description='test lambda function',
|
|
|
|
Timeout=3,
|
|
|
|
MemorySize=128,
|
|
|
|
Publish=True,
|
|
|
|
)
|
|
|
|
|
2016-12-03 23:17:15 +00:00
|
|
|
in_data = {'msg': 'So long and thanks for all the fish'}
|
|
|
|
success_result = conn.invoke(FunctionName='testFunction', InvocationType='RequestResponse',
|
|
|
|
Payload=json.dumps(in_data))
|
2016-06-22 19:24:46 +00:00
|
|
|
|
2016-12-03 23:17:15 +00:00
|
|
|
success_result["StatusCode"].should.equal(202)
|
2017-02-24 02:37:43 +00:00
|
|
|
base64.b64decode(success_result["LogResult"]).decode(
|
|
|
|
'utf-8').should.equal(json.dumps(in_data))
|
|
|
|
json.loads(success_result["Payload"].read().decode(
|
|
|
|
'utf-8')).should.equal(in_data)
|
2016-06-22 19:24:46 +00:00
|
|
|
|
2016-10-09 15:13:52 +00:00
|
|
|
|
|
|
|
@mock_lambda
|
2016-12-03 23:17:15 +00:00
|
|
|
def test_invoke_event_function():
|
2016-10-09 15:13:52 +00:00
|
|
|
conn = boto3.client('lambda', 'us-west-2')
|
|
|
|
conn.create_function(
|
|
|
|
FunctionName='testFunction',
|
|
|
|
Runtime='python2.7',
|
|
|
|
Role='test-iam-role',
|
|
|
|
Handler='lambda_function.handler',
|
|
|
|
Code={
|
|
|
|
'ZipFile': get_test_zip_file1(),
|
|
|
|
},
|
|
|
|
Description='test lambda function',
|
|
|
|
Timeout=3,
|
|
|
|
MemorySize=128,
|
|
|
|
Publish=True,
|
|
|
|
)
|
|
|
|
|
2016-12-03 23:17:15 +00:00
|
|
|
conn.invoke.when.called_with(
|
|
|
|
FunctionName='notAFunction',
|
|
|
|
InvocationType='Event',
|
|
|
|
Payload='{}'
|
|
|
|
).should.throw(botocore.client.ClientError)
|
|
|
|
|
|
|
|
in_data = {'msg': 'So long and thanks for all the fish'}
|
2017-02-24 02:37:43 +00:00
|
|
|
success_result = conn.invoke(
|
|
|
|
FunctionName='testFunction', InvocationType='Event', Payload=json.dumps(in_data))
|
2016-10-06 09:52:23 +00:00
|
|
|
success_result["StatusCode"].should.equal(202)
|
2017-02-24 02:37:43 +00:00
|
|
|
json.loads(success_result['Payload'].read().decode(
|
|
|
|
'utf-8')).should.equal({})
|
2016-10-09 15:13:52 +00:00
|
|
|
|
2016-10-06 09:52:23 +00:00
|
|
|
|
|
|
|
@mock_ec2
|
|
|
|
@mock_lambda
|
|
|
|
def test_invoke_function_get_ec2_volume():
|
|
|
|
conn = boto3.resource("ec2", "us-west-2")
|
|
|
|
vol = conn.create_volume(Size=99, AvailabilityZone='us-west-2')
|
|
|
|
vol = conn.Volume(vol.id)
|
|
|
|
|
|
|
|
conn = boto3.client('lambda', 'us-west-2')
|
|
|
|
conn.create_function(
|
|
|
|
FunctionName='testFunction',
|
|
|
|
Runtime='python2.7',
|
|
|
|
Role='test-iam-role',
|
|
|
|
Handler='lambda_function.handler',
|
|
|
|
Code={
|
|
|
|
'ZipFile': get_test_zip_file2(),
|
|
|
|
},
|
|
|
|
Description='test lambda function',
|
|
|
|
Timeout=3,
|
|
|
|
MemorySize=128,
|
|
|
|
Publish=True,
|
|
|
|
)
|
|
|
|
|
2016-12-03 23:17:15 +00:00
|
|
|
in_data = {'volume_id': vol.id}
|
2017-02-24 02:37:43 +00:00
|
|
|
result = conn.invoke(FunctionName='testFunction',
|
|
|
|
InvocationType='RequestResponse', Payload=json.dumps(in_data))
|
2016-12-03 23:17:15 +00:00
|
|
|
result["StatusCode"].should.equal(202)
|
2017-02-24 02:37:43 +00:00
|
|
|
msg = 'get volume details for %s\nVolume - %s state=%s, size=%s\n%s' % (
|
|
|
|
vol.id, vol.id, vol.state, vol.size, json.dumps(in_data))
|
2016-12-03 23:17:15 +00:00
|
|
|
base64.b64decode(result["LogResult"]).decode('utf-8').should.equal(msg)
|
|
|
|
result['Payload'].read().decode('utf-8').should.equal(msg)
|
2016-06-22 19:24:46 +00:00
|
|
|
|
|
|
|
|
2016-02-12 19:39:20 +00:00
|
|
|
@mock_lambda
|
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')
|
2016-10-06 09:52:23 +00:00
|
|
|
zip_content = get_test_zip_file2()
|
2016-10-09 15:13:52 +00:00
|
|
|
|
2016-02-17 21:24:17 +00:00
|
|
|
s3_conn.put_object(Bucket='test-bucket', Key='test.zip', Body=zip_content)
|
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
|
|
|
)
|
2017-02-24 02:37:43 +00: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-02-24 00:43:48 +00:00
|
|
|
result.pop('LastModified')
|
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',
|
2016-02-17 21:24:17 +00:00
|
|
|
"CodeSha256": hashlib.sha256(zip_content).hexdigest(),
|
|
|
|
"CodeSize": len(zip_content),
|
2016-02-12 19:39:20 +00:00
|
|
|
'Description': 'test lambda function',
|
|
|
|
'Timeout': 3,
|
|
|
|
'MemorySize': 128,
|
2016-02-16 20:15:34 +00:00
|
|
|
'Version': '$LATEST',
|
2016-02-16 20:35:29 +00:00
|
|
|
'VpcConfig': {
|
|
|
|
"SecurityGroupIds": ["sg-123abc"],
|
|
|
|
"SubnetIds": ["subnet-123abc"],
|
|
|
|
"VpcId": "vpc-123abc"
|
|
|
|
},
|
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-10-06 09:52:23 +00:00
|
|
|
zip_content = get_test_zip_file1()
|
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,
|
|
|
|
)
|
2017-02-24 02:37:43 +00: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-02-24 00:43:48 +00:00
|
|
|
result.pop('LastModified')
|
2016-10-09 15:13:52 +00:00
|
|
|
|
2016-02-16 20:15:34 +00:00
|
|
|
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,
|
|
|
|
'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')
|
2016-02-17 21:24:17 +00:00
|
|
|
|
2016-10-06 09:52:23 +00:00
|
|
|
zip_content = get_test_zip_file1()
|
2016-02-17 21:24:17 +00:00
|
|
|
s3_conn.put_object(Bucket='test-bucket', Key='test.zip', Body=zip_content)
|
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')
|
2017-02-24 02:37:43 +00: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-02-24 00:43:48 +00:00
|
|
|
result['Configuration'].pop('LastModified')
|
2016-02-12 19:39:20 +00:00
|
|
|
|
|
|
|
result.should.equal({
|
|
|
|
"Code": {
|
|
|
|
"Location": "s3://lambda-functions.aws.amazon.com/test.zip",
|
|
|
|
"RepositoryType": "S3"
|
|
|
|
},
|
|
|
|
"Configuration": {
|
2016-02-17 21:24:17 +00:00
|
|
|
"CodeSha256": hashlib.sha256(zip_content).hexdigest(),
|
|
|
|
"CodeSize": len(zip_content),
|
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
|
|
|
"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')
|
2016-02-17 21:24:17 +00:00
|
|
|
|
2016-10-06 09:52:23 +00:00
|
|
|
zip_content = get_test_zip_file2()
|
2016-02-17 21:24:17 +00:00
|
|
|
s3_conn.put_object(Bucket='test-bucket', Key='test.zip', Body=zip_content)
|
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')
|
2017-02-24 02:37:43 +00: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-09 15:13:52 +00:00
|
|
|
|
2016-02-12 19:39:20 +00:00
|
|
|
success_result.should.equal({'ResponseMetadata': {'HTTPStatusCode': 204}})
|
|
|
|
|
2017-02-24 02:37:43 +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')
|
2016-02-17 21:24:17 +00:00
|
|
|
|
2016-10-06 09:52:23 +00:00
|
|
|
zip_content = get_test_zip_file2()
|
2016-02-17 21:24:17 +00:00
|
|
|
s3_conn.put_object(Bucket='test-bucket', Key='test.zip', Body=zip_content)
|
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-17 21:24:17 +00:00
|
|
|
"CodeSha256": hashlib.sha256(zip_content).hexdigest(),
|
|
|
|
"CodeSize": len(zip_content),
|
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
|
|
|
"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},
|
|
|
|
}
|
2017-02-24 00:43:48 +00:00
|
|
|
func = conn.list_functions()['Functions'][0]
|
|
|
|
func.pop('LastModified')
|
|
|
|
func.should.equal(expected_function_result['Configuration'])
|
2016-02-12 19:39:20 +00:00
|
|
|
|
2016-06-29 23:45:21 +00:00
|
|
|
func = conn.get_function(FunctionName='testFunction')
|
2017-02-24 02:37:43 +00: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-24 00:43:48 +00:00
|
|
|
func['Configuration'].pop('LastModified')
|
2016-10-09 15:13:52 +00:00
|
|
|
|
2016-06-29 23:45:21 +00:00
|
|
|
func.should.equal(expected_function_result)
|
2016-02-12 19:39:20 +00:00
|
|
|
conn.delete_function(FunctionName='testFunction')
|
|
|
|
|
|
|
|
conn.list_functions()['Functions'].should.have.length_of(0)
|
2017-03-17 02:00:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_lambda
|
|
|
|
def test_invoke_lambda_error():
|
|
|
|
lambda_fx = """
|
|
|
|
def lambda_handler(event, context):
|
|
|
|
raise Exception('failsauce')
|
|
|
|
"""
|
|
|
|
zip_output = io.BytesIO()
|
|
|
|
zip_file = zipfile.ZipFile(zip_output, 'w', zipfile.ZIP_DEFLATED)
|
|
|
|
zip_file.writestr('lambda_function.zip', lambda_fx)
|
|
|
|
zip_file.close()
|
|
|
|
zip_output.seek(0)
|
|
|
|
|
|
|
|
client = boto3.client('lambda', region_name='us-east-1')
|
|
|
|
client.create_function(
|
|
|
|
FunctionName='test-lambda-fx',
|
|
|
|
Runtime='python2.7',
|
|
|
|
Role='test-iam-role',
|
|
|
|
Handler='lambda_function.lambda_handler',
|
|
|
|
Description='test lambda function',
|
|
|
|
Timeout=3,
|
|
|
|
MemorySize=128,
|
|
|
|
Publish=True,
|
|
|
|
Code={
|
|
|
|
'ZipFile': zip_output.read()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
result = client.invoke(
|
|
|
|
FunctionName='test-lambda-fx',
|
|
|
|
InvocationType='RequestResponse',
|
|
|
|
LogType='Tail'
|
|
|
|
)
|
|
|
|
|
|
|
|
assert 'FunctionError' in result
|
|
|
|
assert result['FunctionError'] == 'Handled'
|