Repository navigation
Expand file tree
/
Copy pathlaunch_test.py
More file actions
293 lines (239 loc) · 12.1 KB
/
Copy pathlaunch_test.py
File metadata and controls
293 lines (239 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# Copyright 2013 GridCentric Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import uuid
import random
from novaclient.exceptions import ClientException, BadRequest
from . import harness
from . logger import log
from . util import assert_raises
from . import requirements
from . import host
class TestLaunch(harness.TestCase):
def test_bless(self, image_finder):
with self.harness.booted(image_finder) as master:
assert [] == master.list_blessed()
blessed = master.bless()
assert blessed.get_status() == 'BLESSED'
assert [blessed.id] == master.list_blessed()
blessed.discard()
def test_launch_one(self, image_finder):
with self.harness.security_group() as sg,\
self.harness.security_group() as unassigned_sg,\
self.harness.booted(image_finder) as master:
master.add_security_group(sg.name)
# We need the master around to extract addresses.
blessed = master.bless()
assert [] == blessed.list_launched()
launched = blessed.launch()
assert [launched.id] == blessed.list_launched()
if self.harness.satisfies((requirements.SECURITY_GROUPS,)):
# TODO (tkeith): We are removing security groups rather than
# querying for them because Essex doesn't support querying.
# Switch to querying once Essex is no longer supported.
# Check that security group got passed through from master to
# launched by removing it
launched.remove_security_group(sg.name)
# Try removing a non-assigned security group
assert_raises(ClientException, launched.remove_security_group, (unassigned_sg.name,))
# Ensure that the addresses are disjoint.
launched_addrs = launched.get_addrs()
master_addrs = master.get_addrs()
assert set(launched_addrs).isdisjoint(master_addrs)
# Verify that there's no user_data
launched.assert_userdata('')
# Cleanup.
launched.delete()
blessed.discard()
def test_master_gone(self, image_finder):
# We do a manual boot to ensure the ordering.
master = self.harness.boot(image_finder)
blessed = master.bless()
# Prior to discard, delete the master.
master.delete()
# Launch a VM.
launched = blessed.launch()
launched.delete()
blessed.discard()
def test_delete_blessed(self, image_finder):
with self.harness.blessed(image_finder) as blessed:
e = assert_raises(ClientException, blessed.delete)
assert e.code / 100 == 4
def test_multiple_bless(self, image_finder):
with self.harness.booted(image_finder) as master:
assert [] == master.list_blessed()
blessed_a = master.bless()
assert [blessed_a.id] == master.list_blessed()
blessed_b = master.bless()
blessed_ids = master.list_blessed()
assert sorted([blessed_a.id, blessed_b.id]) == sorted(blessed_ids)
blessed_a.discard()
assert [blessed_b.id] == master.list_blessed()
blessed_b.discard()
assert [] == master.list_blessed()
def test_multiple_launch(self, image_finder):
with self.harness.blessed(image_finder) as blessed:
assert [] == blessed.list_launched()
launched_a = blessed.launch()
assert [launched_a.id] == blessed.list_launched()
launched_b = blessed.launch()
launched_ids = blessed.list_launched()
assert sorted([launched_a.id, launched_b.id]) == sorted(launched_ids)
launched_a.delete()
assert [launched_b.id] == blessed.list_launched()
launched_b.delete()
assert [] == blessed.list_launched()
def test_failed_discard(self, image_finder):
with self.harness.blessed(image_finder) as blessed:
# Launch a clone.
launched_a = blessed.launch()
# Cannot discard blessed with launched.
e = assert_raises(ClientException, blessed.discard)
assert e.code / 100 == 4 or e.code / 100 == 5
# Make sure that we can still launch after a failed discard.
launched_b = blessed.launch()
launched_a.delete()
launched_b.delete()
def test_launch_iptables_rules(self, image_finder):
with self.harness.booted(image_finder) as master:
master_iptables_rules = master.get_iptables_rules()
assert master_iptables_rules[0]
assert [] != master_iptables_rules[1]
blessed = master.bless()
# The iptables rules for the master should also be for launched instances.
launched = blessed.launch()
master_iptables_rules == launched.get_iptables_rules()
# Remember the host otherwise we won't know where to look after delete.
host = launched.get_host()
# Remember the instance's ID before we delete the instance.
server_id = launched.get_raw_id()
# Ensure that iptables rules exist before deleting the instance.
assert launched.get_iptables_rules()[0]
assert [] != launched.get_iptables_rules()[1]
launched.delete()
# Ensure that the rules are cleaned up after deleting the instance.
assert (False, []) == host.get_nova_compute_instance_filter_rules(server_id)
# Cleanup the blessed instance.
blessed.discard()
def test_launch_master(self, image_finder):
with self.harness.booted(image_finder) as master:
# Can't launch master.
e = assert_raises(ClientException, master.launch)
assert e.code / 100 == 4 or e.code / 100 == 5
def test_discard_master(self, image_finder):
with self.harness.booted(image_finder) as master:
# Can't discard master.
e = assert_raises(ClientException, master.discard)
assert e.code / 100 == 4 or e.code / 100 == 5
def test_list_blessed_bad_id(self):
# We should not be able to list blessed instances.
e = assert_raises(ClientException,
self.harness.gcapi.list_blessed_instances,
self.harness.fake_id())
assert e.code / 100 == 4 or e.code / 100 == 5
def test_list_launched_bad_id(self):
# Nor launched instances.
e = assert_raises(ClientException,
self.harness.gcapi.list_launched_instances,
self.harness.fake_id())
assert e.code / 100 == 4 or e.code / 100 == 5
def test_launch_with_params(self, image_finder):
with self.harness.booted(image_finder) as master:
blessed = master.bless()
def assert_guest_params_success(params):
""" These parameters should successfully be added to the instance. """
launched = blessed.launch(guest_params=params)
inguest_params = launched.read_params()
for param in params:
assert param in inguest_params
assert inguest_params[param] == "verified"
launched.delete()
def assert_guest_params_failure(params):
""" These parameters should cause the launching of the instance to fail. """
launched = blessed.launch(guest_params=params, status="ERROR")
launched.delete()
# Ensure that the guest parameters behave as expected.
assert_guest_params_success({})
assert_guest_params_success({"test_parameter":"verified"})
assert_guest_params_success({"test_parameter":"verified", "test_parameter2":"verified"})
assert_guest_params_failure({"sometext": "somelargetext" * 1000})
blessed.discard()
@harness.requires(requirements.LAUNCH_NAME)
def test_launch_with_name(self, image_finder):
test_name = 'launch-name-{}'.format(str(uuid.uuid4()))
with self.harness.blessed(image_finder) as blessed:
launched = blessed.launch(name=test_name)
# blessed.launch will take care of assertions
launched.delete()
@harness.requires(requirements.USER_DATA)
@harness.platformtest(exclude=["windows"])
def test_launch_with_user_data(self, image_finder):
test_data = 'some user data'
with self.harness.blessed(image_finder) as blessed:
launched = blessed.launch(user_data=test_data)
# Verify user_data
launched.assert_userdata(test_data)
# Cleanup.
launched.delete()
@harness.requires(requirements.SECURITY_GROUPS)
def test_launch_with_security_group(self, image_finder):
with self.harness.security_group() as master_sg,\
self.harness.security_group() as launched_sg,\
self.harness.booted(image_finder) as master:
master.server.add_security_group(master_sg.name)
blessed = master.bless()
launched = blessed.launch(\
security_groups=[self.config.security_group, launched_sg.name])
# TODO (tkeith): We are removing security groups rather than
# querying for them because Essex doesn't support querying.
# Switch to querying once Essex is no longer supported.
# Verify that master_sg didn't get passed from master to launched
assert_raises(ClientException, launched.remove_security_group, (master_sg.name,))
# Verify that launched_sg was added to launched
launched.remove_security_group(launched_sg.name)
def test_repeat_launch_delete(self, image_finder):
""" This test was added because repeated launching & discarding caused an issue
setting the IP with DHCP. This test used to fail between the 7th and 16th
iteration. """
with self.harness.blessed(image_finder) as blessed:
for i in range(20):
launched = blessed.launch()
launched.delete()
@harness.requires(requirements.AVAILABILITY_ZONE)
def test_launch_host_targeted(self, image_finder):
hosts = self.harness.config.hosts
def assert_launched_host(blessed, host_name):
host_az = host.Host(host_name, self.harness.config).host_az()
launched = blessed.launch(availability_zone=host_az)
launched.delete()
assert_raises(BadRequest, blessed.launch,
availability_zone='nonexistent:%s' % host_name)
assert_raises(BadRequest, blessed.launch,
availability_zone='%s:nonexistent' %\
self.config.default_az)
with self.harness.blessed(image_finder) as blessed:
assert_launched_host(blessed, hosts[0])
if len(hosts) > 1:
assert_launched_host(blessed, hosts[1])
if len(hosts) > 2:
assert_launched_host(blessed, random.choice(hosts[2:]))
@harness.requires(requirements.AVAILABILITY_ZONE)
def test_launch_with_invalid_az(self, image_finder):
with self.harness.blessed(image_finder) as blessed:
assert_raises(BadRequest, blessed.launch, availability_zone='nonexistent-az')
@harness.requires(requirements.AVAILABILITY_ZONE)
def test_launch_with_az(self, image_finder):
with self.harness.blessed(image_finder) as blessed:
launched = blessed.launch(availability_zone=self.config.default_az)
launched.delete()