Fixup lambda for ResponseRequest (#781)
* Fixup lambda for ResponseRequest * one day will get this right
This commit is contained in:
parent
5dc8e59fab
commit
ed0e81fc61
@ -112,7 +112,7 @@ class LambdaFunction(object):
|
||||
|
||||
def convert(self, s):
|
||||
try:
|
||||
return str(s, encoding='utf8')
|
||||
return str(s, encoding='utf-8')
|
||||
except:
|
||||
return s
|
||||
|
||||
@ -128,7 +128,8 @@ class LambdaFunction(object):
|
||||
try:
|
||||
mycode = "\n".join(['import json',
|
||||
self.convert(self.code),
|
||||
self.convert('print(lambda_handler(%s, %s))' % (self.is_json(self.convert(event)), context))])
|
||||
self.convert('print(json.dumps(lambda_handler(%s, %s)))' % (self.is_json(self.convert(event)), context))])
|
||||
|
||||
#print("moto_lambda_debug: ", mycode)
|
||||
except Exception as ex:
|
||||
print("Exception %s", ex)
|
||||
@ -141,7 +142,9 @@ class LambdaFunction(object):
|
||||
exec(mycode)
|
||||
exec_err = codeErr.getvalue()
|
||||
exec_out = codeOut.getvalue()
|
||||
result = "\n".join([exec_out, self.convert(exec_err)])
|
||||
result = self.convert(exec_out.strip())
|
||||
if exec_err:
|
||||
result = "\n".join([exec_out.strip(), self.convert(exec_err)])
|
||||
except Exception as ex:
|
||||
result = '%s\n\n\nException %s' % (mycode, ex)
|
||||
finally:
|
||||
@ -160,8 +163,11 @@ class LambdaFunction(object):
|
||||
encoded = base64.b64encode(r.encode('utf-8'))
|
||||
headers["x-amz-log-result"] = encoded.decode('utf-8')
|
||||
payload['result'] = headers["x-amz-log-result"]
|
||||
result = r.encode('utf-8')
|
||||
else:
|
||||
result = json.dumps(payload)
|
||||
|
||||
return json.dumps(payload, indent=4)
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def create_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name):
|
||||
|
@ -25,7 +25,7 @@ def _process_lamda(pfunc):
|
||||
def get_test_zip_file1():
|
||||
pfunc = """
|
||||
def lambda_handler(event, context):
|
||||
return (event, context)
|
||||
return event
|
||||
"""
|
||||
return _process_lamda(pfunc)
|
||||
|
||||
@ -39,6 +39,7 @@ def lambda_handler(event, context):
|
||||
ec2 = boto3.resource('ec2', region_name='us-west-2')
|
||||
vol = ec2.Volume(volume_id)
|
||||
print('Volume - %s state=%s, size=%s' % (volume_id, vol.state, vol.size))
|
||||
return event
|
||||
"""
|
||||
return _process_lamda(pfunc)
|
||||
|
||||
@ -50,33 +51,6 @@ def test_list_functions():
|
||||
result = conn.list_functions()
|
||||
result['Functions'].should.have.length_of(0)
|
||||
|
||||
@mock_lambda
|
||||
@freeze_time('2015-01-01 00:00:00')
|
||||
def test_invoke_event_function():
|
||||
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,
|
||||
)
|
||||
|
||||
success_result = conn.invoke(FunctionName='testFunction', InvocationType='Event', Payload=json.dumps({'msg': 'Mostly Harmless'}))
|
||||
success_result["StatusCode"].should.equal(202)
|
||||
|
||||
conn.invoke.when.called_with(
|
||||
FunctionName='notAFunction',
|
||||
InvocationType='Event',
|
||||
Payload='{}'
|
||||
).should.throw(botocore.client.ClientError)
|
||||
|
||||
|
||||
@mock_lambda
|
||||
@freeze_time('2015-01-01 00:00:00')
|
||||
@ -96,12 +70,44 @@ def test_invoke_requestresponse_function():
|
||||
Publish=True,
|
||||
)
|
||||
|
||||
in_data = {'msg': 'So long and thanks for all the fish'}
|
||||
success_result = conn.invoke(FunctionName='testFunction', InvocationType='RequestResponse',
|
||||
Payload=json.dumps({'msg': 'So long and thanks for all the fish'}))
|
||||
success_result["StatusCode"].should.equal(202)
|
||||
Payload=json.dumps(in_data))
|
||||
|
||||
success_result["StatusCode"].should.equal(202)
|
||||
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)
|
||||
|
||||
|
||||
@mock_lambda
|
||||
@freeze_time('2015-01-01 00:00:00')
|
||||
def test_invoke_event_function():
|
||||
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,
|
||||
)
|
||||
|
||||
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'}
|
||||
success_result = conn.invoke(FunctionName='testFunction', InvocationType='Event', Payload=json.dumps(in_data))
|
||||
success_result["StatusCode"].should.equal(202)
|
||||
json.loads(success_result['Payload'].read().decode('utf-8')).should.equal({})
|
||||
|
||||
#nasty hack - hope someone has better solution dealing with unicode tests working for Py2 and Py3.
|
||||
base64.b64decode(success_result["LogResult"]).decode('utf-8').replace("u'", "'").should.equal("({'msg': 'So long and thanks for all the fish'}, {})\n\n")
|
||||
|
||||
@mock_ec2
|
||||
@mock_lambda
|
||||
@ -126,14 +132,12 @@ def test_invoke_function_get_ec2_volume():
|
||||
Publish=True,
|
||||
)
|
||||
|
||||
import json
|
||||
success_result = conn.invoke(FunctionName='testFunction', InvocationType='RequestResponse', Payload=json.dumps({'volume_id': vol.id}))
|
||||
success_result["StatusCode"].should.equal(202)
|
||||
|
||||
import base64
|
||||
msg = 'get volume details for %s\nVolume - %s state=%s, size=%s\nNone\n\n' % (vol.id, vol.id, vol.state, vol.size)
|
||||
# yet again hacky solution to allow code to run tests for python2 and python3 - pls someone fix :(
|
||||
base64.b64decode(success_result["LogResult"]).decode('utf-8').replace("u'", "'").should.equal(msg)
|
||||
in_data = {'volume_id': vol.id}
|
||||
result = conn.invoke(FunctionName='testFunction', InvocationType='RequestResponse', Payload=json.dumps(in_data))
|
||||
result["StatusCode"].should.equal(202)
|
||||
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))
|
||||
base64.b64decode(result["LogResult"]).decode('utf-8').should.equal(msg)
|
||||
result['Payload'].read().decode('utf-8').should.equal(msg)
|
||||
|
||||
|
||||
@mock_lambda
|
||||
|
Loading…
Reference in New Issue
Block a user