Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/functional/ec2/test_run_instances.py
1567 views
1
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
from awscli.compat import compat_open
14
15
from awscli.testutils import temporary_file
16
from awscli.testutils import BaseAWSCommandParamsTest
17
18
19
class TestRunInstances(BaseAWSCommandParamsTest):
20
21
prefix = 'ec2 run-instances'
22
23
def assert_run_instances_call(self, args, result):
24
if not isinstance(args, list):
25
args_list = (self.prefix + args).split()
26
else:
27
args_list = self.prefix.split() + args
28
self.assert_params_for_cmd(
29
args_list, result, ignore_params=['ClientToken'])
30
31
def test_no_count(self):
32
args = ' --image-id ami-foobar'
33
result = {
34
'ImageId': 'ami-foobar',
35
'MaxCount': 1,
36
'MinCount': 1
37
}
38
self.assert_run_instances_call(args, result)
39
40
def test_count_scalar(self):
41
args = ' --image-id ami-foobar --count 2'
42
result = {
43
'ImageId': 'ami-foobar',
44
'MaxCount': 2,
45
'MinCount': 2
46
}
47
self.assert_run_instances_call(args, result)
48
49
def test_user_data(self):
50
data = u'\u0039'
51
with temporary_file('r+') as tmp:
52
with compat_open(tmp.name, 'w') as f:
53
f.write(data)
54
f.flush()
55
args = ' --image-id foo --user-data file://%s' % f.name
56
result = {'ImageId': 'foo',
57
'MaxCount': 1,
58
'MinCount': 1,
59
# base64 encoded content of utf-8 encoding of data.
60
'UserData': 'OQ=='}
61
self.assert_run_instances_call(args, result)
62
63
def test_count_range(self):
64
args = ' --image-id ami-foobar --count 5:10'
65
result = {
66
'ImageId': 'ami-foobar',
67
'MaxCount': 10,
68
'MinCount': 5
69
}
70
self.assert_run_instances_call(args, result)
71
72
def test_count_in_json_only(self):
73
input_json = '{"ImageId":"ami-xxxx","MaxCount":9,"MinCount":5}'
74
args = ' --cli-input-json ' + input_json
75
result = {'ImageId': 'ami-xxxx', 'MaxCount': 9, 'MinCount': 5}
76
self.assert_run_instances_call(args, result)
77
78
def test_count_in_cli_and_in_json(self):
79
input_json = '{"ImageId":"ami-xxxx","MaxCount":9,"MinCount":5}'
80
args = ' --count 3 --cli-input-json ' + input_json
81
result = {'ImageId': 'ami-xxxx', 'MaxCount': 3, 'MinCount': 3}
82
self.assert_run_instances_call(args, result)
83
84
def test_block_device_mapping(self):
85
args_list = ' --image-id ami-foobar --count 1'.split()
86
# We're switching to list form because we need to test
87
# when there's leading spaces. This is the CLI equivalent
88
# of --block-dev-mapping ' [{"device_name" ...'
89
# (note the space between ``'`` and ``[``)
90
args_list.append('--block-device-mapping')
91
args_list.append(
92
' [{"DeviceName":"/dev/sda1","Ebs":{"VolumeSize":20}}]')
93
result = {
94
'BlockDeviceMappings': [
95
{'DeviceName': '/dev/sda1',
96
'Ebs': {'VolumeSize': 20}},
97
],
98
'ImageId': 'ami-foobar',
99
'MaxCount': 1,
100
'MinCount': 1
101
}
102
self.assert_run_instances_call(args_list, result)
103
104
def test_secondary_ip_address(self):
105
args = ' --image-id ami-foobar --count 1 '
106
args += '--secondary-private-ip-addresses 10.0.2.106'
107
args_list = (self.prefix + args).split()
108
result = {
109
'ImageId': 'ami-foobar',
110
'NetworkInterfaces': [
111
{'DeviceIndex': 0,
112
'PrivateIpAddresses': [
113
{'Primary': False, 'PrivateIpAddress': '10.0.2.106'}]}],
114
'MaxCount': 1,
115
'MinCount': 1}
116
self.assert_run_instances_call(args, result)
117
118
def test_secondary_ip_address_with_subnet(self):
119
args = ' --image-id ami-foobar --count 1 --subnet subnet-12345678 '
120
args += '--secondary-private-ip-addresses 10.0.2.106'
121
result = {
122
'ImageId': 'ami-foobar',
123
'NetworkInterfaces': [
124
{'DeviceIndex': 0,
125
'SubnetId': 'subnet-12345678',
126
'PrivateIpAddresses': [
127
{'Primary': False, 'PrivateIpAddress': '10.0.2.106'}]}],
128
'MaxCount': 1,
129
'MinCount': 1}
130
self.assert_run_instances_call(args, result)
131
132
def test_secondary_ip_addresses(self):
133
args = ' --image-id ami-foobar --count 1 '
134
args += '--secondary-private-ip-addresses 10.0.2.106 10.0.2.107'
135
result = {
136
'ImageId': 'ami-foobar',
137
'NetworkInterfaces': [
138
{'DeviceIndex': 0,
139
'PrivateIpAddresses': [
140
{'Primary': False, 'PrivateIpAddress': u'10.0.2.106'},
141
{'Primary': False, 'PrivateIpAddress': u'10.0.2.107'}]}],
142
'MaxCount': 1,
143
'MinCount': 1}
144
self.assert_run_instances_call(args, result)
145
146
def test_secondary_ip_address_count(self):
147
args = ' --image-id ami-foobar --count 1 '
148
args += '--secondary-private-ip-address-count 4'
149
result = {
150
'NetworkInterfaces': [{'DeviceIndex': 0,
151
'SecondaryPrivateIpAddressCount': 4}],
152
'ImageId': 'ami-foobar',
153
'MaxCount': 1,
154
'MinCount': 1
155
}
156
self.assert_run_instances_call(args, result)
157
158
def test_secondary_ip_address_count_with_subnet(self):
159
args = ' --image-id ami-foobar --count 1 --subnet subnet-12345678 '
160
args += '--secondary-private-ip-address-count 4'
161
result = {
162
'NetworkInterfaces': [{'DeviceIndex': 0,
163
'SubnetId': 'subnet-12345678',
164
'SecondaryPrivateIpAddressCount': 4}],
165
'ImageId': 'ami-foobar',
166
'MaxCount': 1,
167
'MinCount': 1
168
}
169
self.assert_run_instances_call(args, result)
170
171
def test_associate_public_ip_address(self):
172
args = ' --image-id ami-foobar --count 1 --subnet-id subnet-12345678 '
173
args += '--associate-public-ip-address'
174
result = {
175
'NetworkInterfaces': [
176
{'DeviceIndex': 0,
177
'AssociatePublicIpAddress': True,
178
'SubnetId': 'subnet-12345678'},
179
],
180
'ImageId': 'ami-foobar',
181
'MaxCount': 1,
182
'MinCount': 1
183
}
184
self.assert_run_instances_call(args, result)
185
186
def test_associate_public_ip_address_switch_order(self):
187
args = ' --image-id ami-foobar --count 1 '
188
args += '--associate-public-ip-address --subnet-id subnet-12345678'
189
result = {
190
'NetworkInterfaces': [
191
{'DeviceIndex': 0,
192
'AssociatePublicIpAddress': True,
193
'SubnetId': 'subnet-12345678'}
194
],
195
'ImageId': 'ami-foobar',
196
'MaxCount': 1,
197
'MinCount': 1
198
}
199
self.assert_run_instances_call(args, result)
200
201
def test_no_associate_public_ip_address(self):
202
args = ' --image-id ami-foobar --count 1 --subnet-id subnet-12345678 '
203
args += '--no-associate-public-ip-address'
204
result = {
205
'ImageId': 'ami-foobar',
206
'NetworkInterfaces': [{'AssociatePublicIpAddress': False,
207
'DeviceIndex': 0,
208
'SubnetId': 'subnet-12345678'}],
209
'MaxCount': 1,
210
'MinCount': 1}
211
self.assert_run_instances_call(args, result)
212
213
def test_subnet_alone(self):
214
args = ' --image-id ami-foobar --count 1 --subnet-id subnet-12345678'
215
result = {
216
'SubnetId': 'subnet-12345678',
217
'ImageId': 'ami-foobar',
218
'MaxCount': 1,
219
'MinCount': 1
220
}
221
self.assert_run_instances_call(args, result)
222
223
def test_associate_public_ip_address_and_group_id(self):
224
args = ' --image-id ami-foobar --count 1 '
225
args += '--security-group-id sg-12345678 '
226
args += '--associate-public-ip-address --subnet-id subnet-12345678'
227
result = {
228
'NetworkInterfaces': [
229
{'DeviceIndex': 0,
230
'AssociatePublicIpAddress': True,
231
'SubnetId': 'subnet-12345678',
232
'Groups': ['sg-12345678']}
233
],
234
'ImageId': 'ami-foobar',
235
'MaxCount': 1,
236
'MinCount': 1
237
}
238
self.assert_run_instances_call(args, result)
239
240
def test_group_id_alone(self):
241
args = ' --image-id ami-foobar --count 1 '
242
args += '--security-group-id sg-12345678'
243
result = {
244
'SecurityGroupIds': ['sg-12345678'],
245
'ImageId': 'ami-foobar',
246
'MaxCount': 1,
247
'MinCount': 1
248
}
249
self.assert_run_instances_call(args, result)
250
251
def test_associate_public_ip_address_and_private_ip_address(self):
252
args = ' --image-id ami-foobar --count 1 '
253
args += '--private-ip-address 10.0.0.200 '
254
args += '--associate-public-ip-address --subnet-id subnet-12345678'
255
result = {
256
'NetworkInterfaces': [{
257
'DeviceIndex': 0,
258
'AssociatePublicIpAddress': True,
259
'SubnetId': 'subnet-12345678',
260
'PrivateIpAddresses': [
261
{'PrivateIpAddress': '10.0.0.200',
262
'Primary': True}],
263
}],
264
'ImageId': 'ami-foobar',
265
'MaxCount': 1,
266
'MinCount': 1
267
}
268
self.assert_run_instances_call(args, result)
269
270
def test_private_ip_address_alone(self):
271
args = ' --image-id ami-foobar --count 1 '
272
args += '--private-ip-address 10.0.0.200'
273
result = {
274
'PrivateIpAddress': '10.0.0.200',
275
'ImageId': 'ami-foobar',
276
'MaxCount': 1,
277
'MinCount': 1
278
}
279
self.assert_run_instances_call(args, result)
280
281
def test_ipv6_address_count_and_associate_public_ip_address(self):
282
args = ' --associate-public-ip-address'
283
args += ' --ipv6-address-count 5 --image-id ami-foobar --count 1'
284
expected = {
285
'NetworkInterfaces': [{
286
'DeviceIndex': 0,
287
'AssociatePublicIpAddress': True,
288
'Ipv6AddressCount': 5
289
}],
290
'ImageId': 'ami-foobar',
291
'MaxCount': 1,
292
'MinCount': 1
293
}
294
self.assert_run_instances_call(args, expected)
295
296
def test_ipv6_addresses_and_associate_public_ip_address(self):
297
args = ' --associate-public-ip-address --count 1'
298
args += ' --ipv6-addresses Ipv6Address=::1 --image-id ami-foobar '
299
expected = {
300
'NetworkInterfaces': [{
301
'DeviceIndex': 0,
302
'AssociatePublicIpAddress': True,
303
'Ipv6Addresses': [{'Ipv6Address': '::1'}]
304
}],
305
'ImageId': 'ami-foobar',
306
'MaxCount': 1,
307
'MinCount': 1
308
}
309
self.assert_run_instances_call(args, expected)
310
311
def test_enable_primary_ipv6_and_associate_public_ip_address(self):
312
args = ' --associate-public-ip-address'
313
args += ' --enable-primary-ipv6 --image-id ami-foobar --count 1'
314
expected = {
315
'NetworkInterfaces': [{
316
'DeviceIndex': 0,
317
'AssociatePublicIpAddress': True,
318
'PrimaryIpv6': True
319
}],
320
'ImageId': 'ami-foobar',
321
'MaxCount': 1,
322
'MinCount': 1
323
}
324
self.assert_run_instances_call(args, expected)
325