~ubuntu-branches/ubuntu/trusty/python-boto/trusty

« back to all changes in this revision

Viewing changes to tests/unit/ec2/test_instance.py

  • Committer: Package Import Robot
  • Author(s): Eric Evans
  • Date: 2013-05-10 23:38:14 UTC
  • mfrom: (1.1.10) (14.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20130510233814-701dvlop7xfh88i7
Tags: 2.9.2-1
New upstream release (Closes: #700743).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
from tests.unit import unittest
 
4
from tests.unit import AWSMockServiceTestCase
 
5
 
 
6
import mock
 
7
 
 
8
from boto.ec2.connection import EC2Connection
 
9
 
 
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>
 
13
    <reservationSet>
 
14
        <item>
 
15
            <reservationId>r-72ef4a0a</reservationId>
 
16
            <ownerId>184906166255</ownerId>
 
17
            <groupSet/>
 
18
            <instancesSet>
 
19
                <item>
 
20
                    <instanceId>i-instance</instanceId>
 
21
                    <imageId>ami-1624987f</imageId>
 
22
                    <instanceState>
 
23
                        <code>16</code>
 
24
                        <name>running</name>
 
25
                    </instanceState>
 
26
                    <privateDnsName/>
 
27
                    <dnsName/>
 
28
                    <reason/>
 
29
                    <keyName>mykeypair</keyName>
 
30
                    <amiLaunchIndex>0</amiLaunchIndex>
 
31
                    <productCodes/>
 
32
                    <instanceType>m1.small</instanceType>
 
33
                    <launchTime>2012-12-14T23:48:37.000Z</launchTime>
 
34
                    <placement>
 
35
                        <availabilityZone>us-east-1d</availabilityZone>
 
36
                        <groupName/>
 
37
                        <tenancy>default</tenancy>
 
38
                    </placement>
 
39
                    <kernelId>aki-88aa75e1</kernelId>
 
40
                    <monitoring>
 
41
                        <state>disabled</state>
 
42
                    </monitoring>
 
43
                    <subnetId>subnet-0dc60667</subnetId>
 
44
                    <vpcId>vpc-id</vpcId>
 
45
                    <privateIpAddress>10.0.0.67</privateIpAddress>
 
46
                    <sourceDestCheck>true</sourceDestCheck>
 
47
                    <groupSet>
 
48
                        <item>
 
49
                            <groupId>sg-id</groupId>
 
50
                            <groupName>WebServerSG</groupName>
 
51
                        </item>
 
52
                    </groupSet>
 
53
                    <architecture>x86_64</architecture>
 
54
                    <rootDeviceType>ebs</rootDeviceType>
 
55
                    <rootDeviceName>/dev/sda1</rootDeviceName>
 
56
                    <blockDeviceMapping>
 
57
                        <item>
 
58
                            <deviceName>/dev/sda1</deviceName>
 
59
                            <ebs>
 
60
                                <volumeId>vol-id</volumeId>
 
61
                                <status>attached</status>
 
62
                                <attachTime>2012-12-14T23:48:43.000Z</attachTime>
 
63
                                <deleteOnTermination>true</deleteOnTermination>
 
64
                            </ebs>
 
65
                        </item>
 
66
                    </blockDeviceMapping>
 
67
                    <virtualizationType>paravirtual</virtualizationType>
 
68
                    <clientToken>foo</clientToken>
 
69
                    <tagSet>
 
70
                        <item>
 
71
                            <key>Name</key>
 
72
                            <value/>
 
73
                        </item>
 
74
                    </tagSet>
 
75
                    <hypervisor>xen</hypervisor>
 
76
                    <networkInterfaceSet>
 
77
                        <item>
 
78
                            <networkInterfaceId>eni-id</networkInterfaceId>
 
79
                            <subnetId>subnet-id</subnetId>
 
80
                            <vpcId>vpc-id</vpcId>
 
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>
 
86
                            <groupSet>
 
87
                                <item>
 
88
                                    <groupId>sg-id</groupId>
 
89
                                    <groupName>WebServerSG</groupName>
 
90
                                </item>
 
91
                            </groupSet>
 
92
                            <attachment>
 
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>
 
98
                            </attachment>
 
99
                            <privateIpAddressesSet>
 
100
                                <item>
 
101
                                    <privateIpAddress>10.0.0.67</privateIpAddress>
 
102
                                    <primary>true</primary>
 
103
                                </item>
 
104
                                <item>
 
105
                                    <privateIpAddress>10.0.0.54</privateIpAddress>
 
106
                                    <primary>false</primary>
 
107
                                </item>
 
108
                                <item>
 
109
                                    <privateIpAddress>10.0.0.55</privateIpAddress>
 
110
                                    <primary>false</primary>
 
111
                                </item>
 
112
                            </privateIpAddressesSet>
 
113
                        </item>
 
114
                    </networkInterfaceSet>
 
115
                    <ebsOptimized>false</ebsOptimized>
 
116
                </item>
 
117
            </instancesSet>
 
118
        </item>
 
119
    </reservationSet>
 
120
</DescribeInstancesResponse>
 
121
"""
 
122
 
 
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>
 
128
    <groupSet>
 
129
        <item>
 
130
            <groupId>sg-id</groupId>
 
131
            <groupName>SSH</groupName>
 
132
        </item>
 
133
    </groupSet>
 
134
    <instancesSet>
 
135
        <item>
 
136
            <instanceId>i-ff0f1299</instanceId>
 
137
            <imageId>ami-ed65ba84</imageId>
 
138
            <instanceState>
 
139
                <code>0</code>
 
140
                <name>pending</name>
 
141
            </instanceState>
 
142
            <privateDnsName/>
 
143
            <dnsName/>
 
144
            <reason/>
 
145
            <keyName>awskeypair</keyName>
 
146
            <amiLaunchIndex>0</amiLaunchIndex>
 
147
            <productCodes/>
 
148
            <instanceType>t1.micro</instanceType>
 
149
            <launchTime>2012-05-30T19:21:18.000Z</launchTime>
 
150
            <placement>
 
151
                <availabilityZone>us-east-1a</availabilityZone>
 
152
                <groupName/>
 
153
                <tenancy>default</tenancy>
 
154
            </placement>
 
155
            <kernelId>aki-b6aa75df</kernelId>
 
156
            <monitoring>
 
157
                <state>disabled</state>
 
158
            </monitoring>
 
159
            <groupSet>
 
160
                <item>
 
161
                    <groupId>sg-99a710f1</groupId>
 
162
                    <groupName>SSH</groupName>
 
163
                </item>
 
164
            </groupSet>
 
165
            <stateReason>
 
166
                <code>pending</code>
 
167
                <message>pending</message>
 
168
            </stateReason>
 
169
            <architecture>i386</architecture>
 
170
            <rootDeviceType>ebs</rootDeviceType>
 
171
            <rootDeviceName>/dev/sda1</rootDeviceName>
 
172
            <blockDeviceMapping/>
 
173
            <virtualizationType>paravirtual</virtualizationType>
 
174
            <clientToken/>
 
175
            <hypervisor>xen</hypervisor>
 
176
            <networkInterfaceSet/>
 
177
            <iamInstanceProfile>
 
178
                <arn>arn:aws:iam::ownerid:instance-profile/myinstanceprofile</arn>
 
179
                <id>iamid</id>
 
180
            </iamInstanceProfile>
 
181
        </item>
 
182
    </instancesSet>
 
183
</RunInstancesResponse>
 
184
"""
 
185
 
 
186
 
 
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'),
 
207
             'id': 'iamid'})
 
208
 
 
209
 
 
210
class TestDescribeInstances(AWSMockServiceTestCase):
 
211
    connection_class = EC2Connection
 
212
 
 
213
    def default_body(self):
 
214
        return DESCRIBE_INSTANCE_VPC
 
215
 
 
216
    def test_multiple_private_ip_addresses(self):
 
217
        self.set_http_response(status_code=200)
 
218
 
 
219
        api_response = self.service_connection.get_all_instances()
 
220
        self.assertEqual(len(api_response), 1)
 
221
 
 
222
        instances = api_response[0].instances
 
223
        self.assertEqual(len(instances), 1)
 
224
 
 
225
        instance = instances[0]
 
226
        self.assertEqual(len(instance.interfaces), 1)
 
227
 
 
228
        interface = instance.interfaces[0]
 
229
        self.assertEqual(len(interface.private_ip_addresses), 3)
 
230
 
 
231
        addresses = interface.private_ip_addresses
 
232
        self.assertEqual(addresses[0].private_ip_address, '10.0.0.67')
 
233
        self.assertTrue(addresses[0].primary)
 
234
 
 
235
        self.assertEqual(addresses[1].private_ip_address, '10.0.0.54')
 
236
        self.assertFalse(addresses[1].primary)
 
237
 
 
238
        self.assertEqual(addresses[2].private_ip_address, '10.0.0.55')
 
239
        self.assertFalse(addresses[2].primary)
 
240
 
 
241
 
 
242
if __name__ == '__main__':
 
243
    unittest.main()