Mock out instance metadata. Make basic connection with iam roles work. Closes #3.

This commit is contained in:
Steve Pulec 2013-03-10 16:55:34 -04:00
parent 1cc0e0eac7
commit 9e9e057289
3 changed files with 40 additions and 0 deletions

View File

@ -2,6 +2,7 @@ import functools
import re
from moto.packages.httpretty import HTTPretty
from .responses import metadata_response
from .utils import convert_regex_to_flask_path
@ -30,6 +31,13 @@ class MockAWS(object):
body=value,
)
# Mock out localhost instance metadata
HTTPretty.register_uri(
method=method,
uri=re.compile('http://169.254.169.254/latest/meta-data/.*'),
body=metadata_response
)
def stop(self):
HTTPretty.disable()

View File

@ -1,3 +1,6 @@
import datetime
import json
from urlparse import parse_qs
from moto.core.utils import headers_to_dict, camelcase_to_underscores, method_names_from_class
@ -24,3 +27,27 @@ class BaseResponse(object):
method = getattr(self, action)
return method()
raise NotImplementedError("The {} action has not been implemented".format(action))
def metadata_response(uri, body, headers):
"""
Mock response for localhost metadata
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html
"""
tomorrow = datetime.datetime.now() + datetime.timedelta(days=1)
path = uri.path.lstrip("/latest/meta-data/")
if path == '':
return "iam/"
elif path == 'iam/':
return 'security-credentials/'
elif path == 'iam/security-credentials/':
return 'default-role'
elif path == 'iam/security-credentials/default-role':
return json.dumps(dict(
AccessKeyId="test-key",
SecretAccessKey="test-secret-key",
Token="test-session-token",
Expiration=tomorrow.strftime("%Y-%m-%dT%H:%M:%SZ")
))

View File

@ -9,6 +9,11 @@ Test the different ways that the decorator can be used
'''
@mock_ec2
def test_basic_connect():
conn = boto.connect_ec2()
@mock_ec2
def test_basic_decorator():
conn = boto.connect_ec2('the_key', 'the_secret')