3
from tests.unit import unittest
4
from tests.unit import AWSMockServiceTestCase
8
from boto.ec2.connection import EC2Connection
10
DESCRIBE_INSTANCE_VPC = r"""<?xml version="1.0" encoding="UTF-8"?>
11
<DescribeInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2012-10-01/">
12
<requestId>c6132c74-b524-4884-87f5-0f4bde4a9760</requestId>
15
<reservationId>r-72ef4a0a</reservationId>
16
<ownerId>184906166255</ownerId>
20
<instanceId>i-instance</instanceId>
21
<imageId>ami-1624987f</imageId>
29
<keyName>mykeypair</keyName>
30
<amiLaunchIndex>0</amiLaunchIndex>
32
<instanceType>m1.small</instanceType>
33
<launchTime>2012-12-14T23:48:37.000Z</launchTime>
35
<availabilityZone>us-east-1d</availabilityZone>
37
<tenancy>default</tenancy>
39
<kernelId>aki-88aa75e1</kernelId>
41
<state>disabled</state>
43
<subnetId>subnet-0dc60667</subnetId>
45
<privateIpAddress>10.0.0.67</privateIpAddress>
46
<sourceDestCheck>true</sourceDestCheck>
49
<groupId>sg-id</groupId>
50
<groupName>WebServerSG</groupName>
53
<architecture>x86_64</architecture>
54
<rootDeviceType>ebs</rootDeviceType>
55
<rootDeviceName>/dev/sda1</rootDeviceName>
58
<deviceName>/dev/sda1</deviceName>
60
<volumeId>vol-id</volumeId>
61
<status>attached</status>
62
<attachTime>2012-12-14T23:48:43.000Z</attachTime>
63
<deleteOnTermination>true</deleteOnTermination>
67
<virtualizationType>paravirtual</virtualizationType>
68
<clientToken>foo</clientToken>
75
<hypervisor>xen</hypervisor>
78
<networkInterfaceId>eni-id</networkInterfaceId>
79
<subnetId>subnet-id</subnetId>
81
<description>Primary network interface</description>
82
<ownerId>ownerid</ownerId>
83
<status>in-use</status>
84
<privateIpAddress>10.0.0.67</privateIpAddress>
85
<sourceDestCheck>true</sourceDestCheck>
88
<groupId>sg-id</groupId>
89
<groupName>WebServerSG</groupName>
93
<attachmentId>eni-attach-id</attachmentId>
94
<deviceIndex>0</deviceIndex>
95
<status>attached</status>
96
<attachTime>2012-12-14T23:48:37.000Z</attachTime>
97
<deleteOnTermination>true</deleteOnTermination>
99
<privateIpAddressesSet>
101
<privateIpAddress>10.0.0.67</privateIpAddress>
102
<primary>true</primary>
105
<privateIpAddress>10.0.0.54</privateIpAddress>
106
<primary>false</primary>
109
<privateIpAddress>10.0.0.55</privateIpAddress>
110
<primary>false</primary>
112
</privateIpAddressesSet>
114
</networkInterfaceSet>
115
<ebsOptimized>false</ebsOptimized>
120
</DescribeInstancesResponse>
123
RUN_INSTANCE_RESPONSE = r"""
124
<RunInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2012-06-01/">
125
<requestId>ad4b83c2-f606-4c39-90c6-5dcc5be823e1</requestId>
126
<reservationId>r-c5cef7a7</reservationId>
127
<ownerId>ownerid</ownerId>
130
<groupId>sg-id</groupId>
131
<groupName>SSH</groupName>
136
<instanceId>i-ff0f1299</instanceId>
137
<imageId>ami-ed65ba84</imageId>
145
<keyName>awskeypair</keyName>
146
<amiLaunchIndex>0</amiLaunchIndex>
148
<instanceType>t1.micro</instanceType>
149
<launchTime>2012-05-30T19:21:18.000Z</launchTime>
151
<availabilityZone>us-east-1a</availabilityZone>
153
<tenancy>default</tenancy>
155
<kernelId>aki-b6aa75df</kernelId>
157
<state>disabled</state>
161
<groupId>sg-99a710f1</groupId>
162
<groupName>SSH</groupName>
167
<message>pending</message>
169
<architecture>i386</architecture>
170
<rootDeviceType>ebs</rootDeviceType>
171
<rootDeviceName>/dev/sda1</rootDeviceName>
172
<blockDeviceMapping/>
173
<virtualizationType>paravirtual</virtualizationType>
175
<hypervisor>xen</hypervisor>
176
<networkInterfaceSet/>
178
<arn>arn:aws:iam::ownerid:instance-profile/myinstanceprofile</arn>
180
</iamInstanceProfile>
183
</RunInstancesResponse>
187
class TestRunInstanceResponseParsing(unittest.TestCase):
188
def testIAMInstanceProfileParsedCorrectly(self):
189
ec2 = EC2Connection(aws_access_key_id='aws_access_key_id',
190
aws_secret_access_key='aws_secret_access_key')
191
mock_response = mock.Mock()
192
mock_response.read.return_value = RUN_INSTANCE_RESPONSE
193
mock_response.status = 200
194
ec2.make_request = mock.Mock(return_value=mock_response)
195
reservation = ec2.run_instances(image_id='ami-12345')
196
self.assertEqual(len(reservation.instances), 1)
197
instance = reservation.instances[0]
198
self.assertEqual(instance.image_id, 'ami-ed65ba84')
199
# iamInstanceProfile has an ID element, so we want to make sure
200
# that this does not map to instance.id (which should be the
201
# id of the ec2 instance).
202
self.assertEqual(instance.id, 'i-ff0f1299')
203
self.assertDictEqual(
204
instance.instance_profile,
205
{'arn': ('arn:aws:iam::ownerid:'
206
'instance-profile/myinstanceprofile'),
210
class TestDescribeInstances(AWSMockServiceTestCase):
211
connection_class = EC2Connection
213
def default_body(self):
214
return DESCRIBE_INSTANCE_VPC
216
def test_multiple_private_ip_addresses(self):
217
self.set_http_response(status_code=200)
219
api_response = self.service_connection.get_all_instances()
220
self.assertEqual(len(api_response), 1)
222
instances = api_response[0].instances
223
self.assertEqual(len(instances), 1)
225
instance = instances[0]
226
self.assertEqual(len(instance.interfaces), 1)
228
interface = instance.interfaces[0]
229
self.assertEqual(len(interface.private_ip_addresses), 3)
231
addresses = interface.private_ip_addresses
232
self.assertEqual(addresses[0].private_ip_address, '10.0.0.67')
233
self.assertTrue(addresses[0].primary)
235
self.assertEqual(addresses[1].private_ip_address, '10.0.0.54')
236
self.assertFalse(addresses[1].primary)
238
self.assertEqual(addresses[2].private_ip_address, '10.0.0.55')
239
self.assertFalse(addresses[2].primary)
242
if __name__ == '__main__':