attmpt 2 to resolve StringIO not being within Python 3 anymore

This commit is contained in:
rocky4570fft 2016-10-07 00:14:47 +10:00
parent 6c577091da
commit 7c3005e582
2 changed files with 15 additions and 10 deletions

View File

@ -1,14 +1,19 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import StringIO
import base64 import base64
import datetime import datetime
import hashlib import hashlib
import io import io
import json import json
import StringIO
import sys import sys
import zipfile import zipfile
try:
from StringIO import StringIO
except:
from io import StringIO
import boto.awslambda import boto.awslambda
from moto.core import BaseBackend from moto.core import BaseBackend
from moto.s3.models import s3_backend from moto.s3.models import s3_backend
@ -104,18 +109,18 @@ class LambdaFunction(object):
def _invoke_lambda(self, code, event={}, context={}): def _invoke_lambda(self, code, event={}, context={}):
# TO DO: context not yet implemented # TO DO: context not yet implemented
try: try:
codeOut = StringIO.StringIO() codeOut = StringIO()
codeErr = StringIO.StringIO() codeErr = StringIO()
mycode = "\n".join([self.code, 'print lambda_handler(%s, %s)' % (event, context)]) mycode = "\n".join([self.code, 'print lambda_handler(%s, %s)' % (event, context)])
#print "moto_lambda_debug: ", mycode #print "moto_lambda_debug: ", mycode
sys.stdout = codeOut sys.stdout = codeOut
sys.stderr = codeErr sys.stderr = codeErr
exec(mycode, {'event': event, 'context': context}) exec mycode
exec_err = codeErr.getvalue() exec_err = codeErr.getvalue()
exec_out = codeOut.getvalue() exec_out = codeOut.getvalue()
result = "\n".join([exec_out, exec_err]) result = "\n".join([exec_out, exec_err])
except Exception as ex: except Exception as ex:
result = 'Exception %s, %s' % (ex, ex.message) result = '%s\n\n\nException %s, %s' % (mycode, ex, ex.message)
finally: finally:
codeErr.close() codeErr.close()
codeOut.close() codeOut.close()

View File

@ -5,6 +5,7 @@ import botocore.client
import boto3 import boto3
import hashlib import hashlib
import io import io
import json
import zipfile import zipfile
import sure # noqa import sure # noqa
@ -67,7 +68,7 @@ def test_invoke_function():
Publish=True, 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) success_result["StatusCode"].should.equal(202)
conn.invoke.when.called_with( conn.invoke.when.called_with(
@ -76,10 +77,9 @@ def test_invoke_function():
Payload='{}' Payload='{}'
).should.throw(botocore.client.ClientError) ).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) 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") 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 @mock_ec2