From 7c3005e58258a0bbc80379c46212e0cc2a5445f6 Mon Sep 17 00:00:00 2001 From: rocky4570fft Date: Fri, 7 Oct 2016 00:14:47 +1000 Subject: [PATCH] attmpt 2 to resolve StringIO not being within Python 3 anymore --- moto/awslambda/models.py | 17 +++++++++++------ tests/test_awslambda/test_lambda.py | 8 ++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/moto/awslambda/models.py b/moto/awslambda/models.py index ff21805d3..824bc13a2 100644 --- a/moto/awslambda/models.py +++ b/moto/awslambda/models.py @@ -1,14 +1,19 @@ from __future__ import unicode_literals +import StringIO import base64 import datetime import hashlib import io import json -import StringIO import sys import zipfile +try: + from StringIO import StringIO +except: + from io import StringIO + import boto.awslambda from moto.core import BaseBackend from moto.s3.models import s3_backend @@ -104,18 +109,18 @@ class LambdaFunction(object): def _invoke_lambda(self, code, event={}, context={}): # TO DO: context not yet implemented try: - codeOut = StringIO.StringIO() - codeErr = StringIO.StringIO() - mycode = "\n".join([self.code, 'print lambda_handler(%s, %s)' % (event, context)]) + codeOut = StringIO() + codeErr = StringIO() + mycode = "\n".join([self.code, 'print lambda_handler(%s, %s)' % (event, context)]) #print "moto_lambda_debug: ", mycode sys.stdout = codeOut sys.stderr = codeErr - exec(mycode, {'event': event, 'context': context}) + exec mycode exec_err = codeErr.getvalue() exec_out = codeOut.getvalue() result = "\n".join([exec_out, exec_err]) except Exception as ex: - result = 'Exception %s, %s' % (ex, ex.message) + result = '%s\n\n\nException %s, %s' % (mycode, ex, ex.message) finally: codeErr.close() codeOut.close() diff --git a/tests/test_awslambda/test_lambda.py b/tests/test_awslambda/test_lambda.py index ee17312a6..a82100bc4 100644 --- a/tests/test_awslambda/test_lambda.py +++ b/tests/test_awslambda/test_lambda.py @@ -5,6 +5,7 @@ import botocore.client import boto3 import hashlib import io +import json import zipfile import sure # noqa @@ -67,7 +68,7 @@ def test_invoke_function(): Publish=True, ) - success_result = conn.invoke(FunctionName='testFunction', InvocationType='Event', Payload="Mostly Harmless") + success_result = conn.invoke(FunctionName='testFunction', InvocationType='Event', Payload='Mostly Harmless') success_result["StatusCode"].should.equal(202) conn.invoke.when.called_with( @@ -76,10 +77,9 @@ def test_invoke_function(): Payload='{}' ).should.throw(botocore.client.ClientError) - success_result = conn.invoke(FunctionName='testFunction', InvocationType='RequestResponse', Payload='{"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) - - import base64 base64.b64decode(success_result["LogResult"]).decode('utf-8').should.equal("({u'msg': u'So long and thanks for all the fish'}, {})\n\n") @mock_ec2