""" NOTE: This unit test is ONLY good for python 3 """ import mock import unittest class EC2Metadata(unittest.TestCase): class MockResponse(object): def __init__(self, resp_data=None, code=200, msg='OK'): if not resp_data: resp_data = """{ "devpayProductCodes" : null, "availabilityZone" : "us-east-1d", "privateIp" : "10.158.112.84", "version" : "2010-08-31", "region" : "us-east-1", "instanceId" : "i-1234567890abcdef0", "billingProducts" : null, "instanceType" : "t1.micro", "accountId" : "123456789012", "pendingTime" : "2015-11-19T16:32:11Z", "imageId" : "ami-5fb8c835", "kernelId" : "aki-919dcaf8", "ramdiskId" : null, "architecture" : "x86_64" }""" self.resp_data = resp_data self.code = code self.msg = msg self.headers = {} def read(self): return self.resp_data @mock.patch('urllib.request.urlopen') def test_parse_ec2_instance_region(self, urlopen): """ Check parsing of EC2 instance identity to determine region TODO: Make test py2 compatible """ urlopen.return_value = self.MockResponse() from util import EC2InstanceIdentity instance_id = EC2InstanceIdentity() self.assertEqual(instance_id.region, 'us-east-1')