Revision 00f87624
b/snf-tools/burnin.py | ||
---|---|---|
1 |
#!/usr/bin/env python |
|
2 |
|
|
3 |
# Copyright 2011 GRNET S.A. All rights reserved. |
|
4 |
# |
|
5 |
# Redistribution and use in source and binary forms, with or |
|
6 |
# without modification, are permitted provided that the following |
|
7 |
# conditions are met: |
|
8 |
# |
|
9 |
# 1. Redistributions of source code must retain the above |
|
10 |
# copyright notice, this list of conditions and the following |
|
11 |
# disclaimer. |
|
12 |
# |
|
13 |
# 2. Redistributions in binary form must reproduce the above |
|
14 |
# copyright notice, this list of conditions and the following |
|
15 |
# disclaimer in the documentation and/or other materials |
|
16 |
# provided with the distribution. |
|
17 |
# |
|
18 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
|
19 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
20 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
21 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
|
22 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
|
25 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
|
26 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
27 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
28 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
29 |
# POSSIBILITY OF SUCH DAMAGE. |
|
30 |
# |
|
31 |
# The views and conclusions contained in the software and |
|
32 |
# documentation are those of the authors and should not be |
|
33 |
# interpreted as representing official policies, either expressed |
|
34 |
# or implied, of GRNET S.A. |
|
35 |
|
|
36 |
"""Perform integration testing on a running Synnefo deployment""" |
|
37 |
|
|
38 |
import __main__ |
|
39 |
import datetime |
|
40 |
import inspect |
|
41 |
import logging |
|
42 |
import os |
|
43 |
import paramiko |
|
44 |
import prctl |
|
45 |
import subprocess |
|
46 |
import signal |
|
47 |
import socket |
|
48 |
import struct |
|
49 |
import sys |
|
50 |
import time |
|
51 |
|
|
52 |
from IPy import IP |
|
53 |
from multiprocessing import Process, Queue |
|
54 |
from random import choice |
|
55 |
|
|
56 |
from kamaki.client import Client, ClientError |
|
57 |
from vncauthproxy.d3des import generate_response as d3des_generate_response |
|
58 |
|
|
59 |
# Use backported unittest functionality if Python < 2.7 |
|
60 |
try: |
|
61 |
import unittest2 as unittest |
|
62 |
except ImportError: |
|
63 |
if sys.version_info < (2, 7): |
|
64 |
raise Exception("The unittest2 package is required for Python < 2.7") |
|
65 |
import unittest |
|
66 |
|
|
67 |
|
|
68 |
API = None |
|
69 |
TOKEN = None |
|
70 |
DEFAULT_API = "http://dev67.dev.grnet.gr:8000/api/v1.1" |
|
71 |
DEFAULT_TOKEN = "46e427d657b20defe352804f0eb6f8a2" |
|
72 |
# A unique id identifying this test run |
|
73 |
TEST_RUN_ID = datetime.datetime.strftime(datetime.datetime.now(), |
|
74 |
"%Y%m%d%H%M%S") |
|
75 |
SNF_TEST_PREFIX = "snf-test-" |
|
76 |
|
|
77 |
# Setup logging (FIXME - verigak) |
|
78 |
logging.basicConfig(format="%(message)s") |
|
79 |
log = logging.getLogger("burnin") |
|
80 |
log.setLevel(logging.INFO) |
|
81 |
|
|
82 |
|
|
83 |
class UnauthorizedTestCase(unittest.TestCase): |
|
84 |
def test_unauthorized_access(self): |
|
85 |
"""Test access without a valid token fails""" |
|
86 |
c = Client(API, "123") |
|
87 |
with self.assertRaises(ClientError) as cm: |
|
88 |
c.list_servers() |
|
89 |
self.assertEqual(cm.exception.status, 401) |
|
90 |
|
|
91 |
|
|
92 |
class ImagesTestCase(unittest.TestCase): |
|
93 |
"""Test image lists for consistency""" |
|
94 |
@classmethod |
|
95 |
def setUpClass(cls): |
|
96 |
"""Initialize kamaki, get (detailed) list of images""" |
|
97 |
log.info("Getting simple and detailed list of images") |
|
98 |
cls.client = Client(API, TOKEN) |
|
99 |
cls.images = cls.client.list_images() |
|
100 |
cls.dimages = cls.client.list_images(detail=True) |
|
101 |
|
|
102 |
def test_001_list_images(self): |
|
103 |
"""Test image list actually returns images""" |
|
104 |
self.assertGreater(len(self.images), 0) |
|
105 |
|
|
106 |
def test_002_list_images_detailed(self): |
|
107 |
"""Test detailed image list is the same length as list""" |
|
108 |
self.assertEqual(len(self.dimages), len(self.images)) |
|
109 |
|
|
110 |
def test_003_same_image_names(self): |
|
111 |
"""Test detailed and simple image list contain same names""" |
|
112 |
names = sorted(map(lambda x: x["name"], self.images)) |
|
113 |
dnames = sorted(map(lambda x: x["name"], self.dimages)) |
|
114 |
self.assertEqual(names, dnames) |
|
115 |
|
|
116 |
def test_004_unique_image_names(self): |
|
117 |
"""Test images have unique names""" |
|
118 |
names = sorted(map(lambda x: x["name"], self.images)) |
|
119 |
self.assertEqual(sorted(list(set(names))), names) |
|
120 |
|
|
121 |
def test_005_image_metadata(self): |
|
122 |
"""Test every image has specific metadata defined""" |
|
123 |
keys = frozenset(["OS", "description", "size"]) |
|
124 |
for i in self.dimages: |
|
125 |
self.assertTrue(keys.issubset(i["metadata"]["values"].keys())) |
|
126 |
|
|
127 |
|
|
128 |
class FlavorsTestCase(unittest.TestCase): |
|
129 |
"""Test flavor lists for consistency""" |
|
130 |
@classmethod |
|
131 |
def setUpClass(cls): |
|
132 |
"""Initialize kamaki, get (detailed) list of flavors""" |
|
133 |
log.info("Getting simple and detailed list of flavors") |
|
134 |
cls.client = Client(API, TOKEN) |
|
135 |
cls.flavors = cls.client.list_flavors() |
|
136 |
cls.dflavors = cls.client.list_flavors(detail=True) |
|
137 |
|
|
138 |
def test_001_list_flavors(self): |
|
139 |
"""Test flavor list actually returns flavors""" |
|
140 |
self.assertGreater(len(self.flavors), 0) |
|
141 |
|
|
142 |
def test_002_list_flavors_detailed(self): |
|
143 |
"""Test detailed flavor list is the same length as list""" |
|
144 |
self.assertEquals(len(self.dflavors), len(self.flavors)) |
|
145 |
|
|
146 |
def test_003_same_flavor_names(self): |
|
147 |
"""Test detailed and simple flavor list contain same names""" |
|
148 |
names = sorted(map(lambda x: x["name"], self.flavors)) |
|
149 |
dnames = sorted(map(lambda x: x["name"], self.dflavors)) |
|
150 |
self.assertEqual(names, dnames) |
|
151 |
|
|
152 |
def test_004_unique_flavor_names(self): |
|
153 |
"""Test flavors have unique names""" |
|
154 |
names = sorted(map(lambda x: x["name"], self.flavors)) |
|
155 |
self.assertEqual(sorted(list(set(names))), names) |
|
156 |
|
|
157 |
def test_005_well_formed_flavor_names(self): |
|
158 |
"""Test flavors have names of the form CxxRyyDzz |
|
159 |
|
|
160 |
Where xx is vCPU count, yy is RAM in MiB, zz is Disk in GiB |
|
161 |
|
|
162 |
""" |
|
163 |
for f in self.dflavors: |
|
164 |
self.assertEqual("C%dR%dD%d" % (f["cpu"], f["ram"], f["disk"]), |
|
165 |
f["name"], |
|
166 |
"Flavor %s does not match its specs." % f["name"]) |
|
167 |
|
|
168 |
|
|
169 |
class ServersTestCase(unittest.TestCase): |
|
170 |
"""Test server lists for consistency""" |
|
171 |
@classmethod |
|
172 |
def setUpClass(cls): |
|
173 |
"""Initialize kamaki, get (detailed) list of servers""" |
|
174 |
log.info("Getting simple and detailed list of servers") |
|
175 |
cls.client = Client(API, TOKEN) |
|
176 |
cls.servers = cls.client.list_servers() |
|
177 |
cls.dservers = cls.client.list_servers(detail=True) |
|
178 |
|
|
179 |
def test_001_list_servers(self): |
|
180 |
"""Test server list actually returns servers""" |
|
181 |
self.assertGreater(len(self.servers), 0) |
|
182 |
|
|
183 |
def test_002_list_servers_detailed(self): |
|
184 |
"""Test detailed server list is the same length as list""" |
|
185 |
self.assertEqual(len(self.dservers), len(self.servers)) |
|
186 |
|
|
187 |
def test_003_same_server_names(self): |
|
188 |
"""Test detailed and simple flavor list contain same names""" |
|
189 |
names = sorted(map(lambda x: x["name"], self.servers)) |
|
190 |
dnames = sorted(map(lambda x: x["name"], self.dservers)) |
|
191 |
self.assertEqual(names, dnames) |
|
192 |
|
|
193 |
|
|
194 |
# This class gets replicated into actual TestCases dynamically |
|
195 |
class SpawnServerTestCase(unittest.TestCase): |
|
196 |
"""Test scenario for server of the specified image""" |
|
197 |
|
|
198 |
@classmethod |
|
199 |
def setUpClass(cls): |
|
200 |
"""Initialize a kamaki instance""" |
|
201 |
log.info("Spawning server for image `%s'", cls.imagename) |
|
202 |
cls.client = Client(API, TOKEN) |
|
203 |
|
|
204 |
def _get_ipv4(self, server): |
|
205 |
"""Get the public IPv4 of a server from the detailed server info""" |
|
206 |
public_addrs = filter(lambda x: x["id"] == "public", |
|
207 |
server["addresses"]["values"]) |
|
208 |
self.assertEqual(len(public_addrs), 1) |
|
209 |
ipv4_addrs = filter(lambda x: x["version"] == 4, |
|
210 |
public_addrs[0]["values"]) |
|
211 |
self.assertEqual(len(ipv4_addrs), 1) |
|
212 |
return ipv4_addrs[0]["addr"] |
|
213 |
|
|
214 |
def _get_ipv6(self, server): |
|
215 |
"""Get the public IPv6 of a server from the detailed server info""" |
|
216 |
public_addrs = filter(lambda x: x["id"] == "public", |
|
217 |
server["addresses"]["values"]) |
|
218 |
self.assertEqual(len(public_addrs), 1) |
|
219 |
ipv6_addrs = filter(lambda x: x["version"] == 6, |
|
220 |
public_addrs[0]["values"]) |
|
221 |
self.assertEqual(len(ipv6_addrs), 1) |
|
222 |
return ipv6_addrs[0]["addr"] |
|
223 |
|
|
224 |
def _connect_loginname(self, os): |
|
225 |
"""Return the login name for connections based on the server OS""" |
|
226 |
if os in ("ubuntu", "kubuntu", "fedora"): |
|
227 |
return "user" |
|
228 |
elif os == "windows": |
|
229 |
return "Administrator" |
|
230 |
else: |
|
231 |
return "root" |
|
232 |
|
|
233 |
def _verify_server_status(self, current_status, new_status): |
|
234 |
"""Verify a server has switched to a specified status""" |
|
235 |
server = self.client.get_server_details(self.serverid) |
|
236 |
if server["status"] not in (current_status, new_status): |
|
237 |
return None # Do not raise exception, return so the test fails |
|
238 |
self.assertEquals(server["status"], new_status) |
|
239 |
|
|
240 |
def _get_connected_tcp_socket(self, family, host, port): |
|
241 |
"""Get a connected socket from the specified family to host:port""" |
|
242 |
sock = None |
|
243 |
for res in \ |
|
244 |
socket.getaddrinfo(host, port, family, socket.SOCK_STREAM, 0, |
|
245 |
socket.AI_PASSIVE): |
|
246 |
af, socktype, proto, canonname, sa = res |
|
247 |
try: |
|
248 |
sock = socket.socket(af, socktype, proto) |
|
249 |
except socket.error as msg: |
|
250 |
sock = None |
|
251 |
continue |
|
252 |
try: |
|
253 |
sock.connect(sa) |
|
254 |
except socket.error as msg: |
|
255 |
sock.close() |
|
256 |
sock = None |
|
257 |
continue |
|
258 |
self.assertIsNotNone(sock) |
|
259 |
return sock |
|
260 |
|
|
261 |
def _ping_once(self, ipv6, ip): |
|
262 |
"""Test server responds to a single IPv4 or IPv6 ping""" |
|
263 |
cmd = "ping%s -c 2 -w 3 %s" % ("6" if ipv6 else "", ip) |
|
264 |
ping = subprocess.Popen(cmd, shell=True, |
|
265 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
266 |
(stdout, stderr) = ping.communicate() |
|
267 |
ret = ping.wait() |
|
268 |
self.assertEquals(ret, 0) |
|
269 |
|
|
270 |
def _get_hostname_over_ssh(self, hostip, username, password): |
|
271 |
ssh = paramiko.SSHClient() |
|
272 |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
|
273 |
try: |
|
274 |
ssh.connect(hostip, username=username, password=password) |
|
275 |
except socket.error: |
|
276 |
raise AssertionError |
|
277 |
stdin, stdout, stderr = ssh.exec_command("hostname") |
|
278 |
lines = stdout.readlines() |
|
279 |
self.assertEqual(len(lines), 1) |
|
280 |
return lines[0] |
|
281 |
|
|
282 |
def _try_until_timeout_expires(self, warn_timeout, fail_timeout, |
|
283 |
opmsg, callable, *args, **kwargs): |
|
284 |
if warn_timeout == fail_timeout: |
|
285 |
warn_timeout = fail_timeout + 1 |
|
286 |
warn_tmout = time.time() + warn_timeout |
|
287 |
fail_tmout = time.time() + fail_timeout |
|
288 |
while True: |
|
289 |
self.assertLess(time.time(), fail_tmout, |
|
290 |
"operation `%s' timed out" % opmsg) |
|
291 |
if time.time() > warn_tmout: |
|
292 |
log.warning("Server %d: `%s' operation `%s' not done yet", |
|
293 |
self.serverid, self.servername, opmsg) |
|
294 |
try: |
|
295 |
log.info("%s... " % opmsg) |
|
296 |
return callable(*args, **kwargs) |
|
297 |
except AssertionError: |
|
298 |
pass |
|
299 |
time.sleep(self.query_interval) |
|
300 |
|
|
301 |
def _insist_on_tcp_connection(self, family, host, port): |
|
302 |
familystr = {socket.AF_INET: "IPv4", socket.AF_INET6: "IPv6", |
|
303 |
socket.AF_UNSPEC: "Unspecified-IPv4/6"} |
|
304 |
msg = "connect over %s to %s:%s" % \ |
|
305 |
(familystr.get(family, "Unknown"), host, port) |
|
306 |
sock = self._try_until_timeout_expires( |
|
307 |
self.action_timeout, self.action_timeout, |
|
308 |
msg, self._get_connected_tcp_socket, |
|
309 |
family, host, port) |
|
310 |
return sock |
|
311 |
|
|
312 |
def _insist_on_status_transition(self, current_status, new_status, |
|
313 |
fail_timeout, warn_timeout=None): |
|
314 |
msg = "Server %d: `%s', waiting for %s -> %s" % \ |
|
315 |
(self.serverid, self.servername, current_status, new_status) |
|
316 |
if warn_timeout is None: |
|
317 |
warn_timeout = fail_timeout |
|
318 |
self._try_until_timeout_expires(warn_timeout, fail_timeout, |
|
319 |
msg, self._verify_server_status, |
|
320 |
current_status, new_status) |
|
321 |
# Ensure the status is actually the expected one |
|
322 |
server = self.client.get_server_details(self.serverid) |
|
323 |
self.assertEquals(server["status"], new_status) |
|
324 |
|
|
325 |
def _insist_on_ssh_hostname(self, hostip, username, password): |
|
326 |
msg = "SSH to %s, as %s/%s" % (hostip, username, password) |
|
327 |
hostname = self._try_until_timeout_expires( |
|
328 |
self.action_timeout, self.action_timeout, |
|
329 |
msg, self._get_hostname_over_ssh, |
|
330 |
hostip, username, password) |
|
331 |
|
|
332 |
# The hostname must be of the form 'prefix-id' |
|
333 |
self.assertTrue(hostname.endswith("-%d\n" % self.serverid)) |
|
334 |
|
|
335 |
def _skipIf(self, condition, msg): |
|
336 |
if condition: |
|
337 |
self.skipTest(msg) |
|
338 |
|
|
339 |
def test_001_submit_create_server(self): |
|
340 |
"""Test submit create server request""" |
|
341 |
server = self.client.create_server(self.servername, self.flavorid, |
|
342 |
self.imageid, self.personality) |
|
343 |
self.assertEqual(server["name"], self.servername) |
|
344 |
self.assertEqual(server["flavorRef"], self.flavorid) |
|
345 |
self.assertEqual(server["imageRef"], self.imageid) |
|
346 |
self.assertEqual(server["status"], "BUILD") |
|
347 |
|
|
348 |
# Update class attributes to reflect data on building server |
|
349 |
cls = type(self) |
|
350 |
cls.serverid = server["id"] |
|
351 |
cls.username = None |
|
352 |
cls.passwd = server["adminPass"] |
|
353 |
|
|
354 |
def test_002a_server_is_building_in_list(self): |
|
355 |
"""Test server is in BUILD state, in server list""" |
|
356 |
servers = self.client.list_servers(detail=True) |
|
357 |
servers = filter(lambda x: x["name"] == self.servername, servers) |
|
358 |
self.assertEqual(len(servers), 1) |
|
359 |
server = servers[0] |
|
360 |
self.assertEqual(server["name"], self.servername) |
|
361 |
self.assertEqual(server["flavorRef"], self.flavorid) |
|
362 |
self.assertEqual(server["imageRef"], self.imageid) |
|
363 |
self.assertEqual(server["status"], "BUILD") |
|
364 |
|
|
365 |
def test_002b_server_is_building_in_details(self): |
|
366 |
"""Test server is in BUILD state, in details""" |
|
367 |
server = self.client.get_server_details(self.serverid) |
|
368 |
self.assertEqual(server["name"], self.servername) |
|
369 |
self.assertEqual(server["flavorRef"], self.flavorid) |
|
370 |
self.assertEqual(server["imageRef"], self.imageid) |
|
371 |
self.assertEqual(server["status"], "BUILD") |
|
372 |
|
|
373 |
def test_002c_set_server_metadata(self): |
|
374 |
image = self.client.get_image_details(self.imageid) |
|
375 |
os = image["metadata"]["values"]["OS"] |
|
376 |
loginname = image["metadata"]["values"].get("loginname", None) |
|
377 |
self.client.update_server_metadata(self.serverid, OS=os) |
|
378 |
|
|
379 |
# Determine the username to use for future connections |
|
380 |
# to this host |
|
381 |
cls = type(self) |
|
382 |
cls.username = loginname |
|
383 |
if not cls.username: |
|
384 |
cls.username = self._connect_loginname(os) |
|
385 |
self.assertIsNotNone(cls.username) |
|
386 |
|
|
387 |
def test_002d_verify_server_metadata(self): |
|
388 |
"""Test server metadata keys are set based on image metadata""" |
|
389 |
servermeta = self.client.get_server_metadata(self.serverid) |
|
390 |
imagemeta = self.client.get_image_metadata(self.imageid) |
|
391 |
self.assertEqual(servermeta["OS"], imagemeta["OS"]) |
|
392 |
|
|
393 |
def test_003_server_becomes_active(self): |
|
394 |
"""Test server becomes ACTIVE""" |
|
395 |
self._insist_on_status_transition("BUILD", "ACTIVE", |
|
396 |
self.build_fail, self.build_warning) |
|
397 |
|
|
398 |
def test_003a_get_server_oob_console(self): |
|
399 |
"""Test getting OOB server console over VNC |
|
400 |
|
|
401 |
Implementation of RFB protocol follows |
|
402 |
http://www.realvnc.com/docs/rfbproto.pdf. |
|
403 |
|
|
404 |
""" |
|
405 |
console = self.client.get_server_console(self.serverid) |
|
406 |
self.assertEquals(console['type'], "vnc") |
|
407 |
sock = self._insist_on_tcp_connection(socket.AF_UNSPEC, |
|
408 |
console["host"], console["port"]) |
|
409 |
|
|
410 |
# Step 1. ProtocolVersion message (par. 6.1.1) |
|
411 |
version = sock.recv(1024) |
|
412 |
self.assertEquals(version, 'RFB 003.008\n') |
|
413 |
sock.send(version) |
|
414 |
|
|
415 |
# Step 2. Security (par 6.1.2): Only VNC Authentication supported |
|
416 |
sec = sock.recv(1024) |
|
417 |
self.assertEquals(list(sec), ['\x01', '\x02']) |
|
418 |
|
|
419 |
# Step 3. Request VNC Authentication (par 6.1.2) |
|
420 |
sock.send('\x02') |
|
421 |
|
|
422 |
# Step 4. Receive Challenge (par 6.2.2) |
|
423 |
challenge = sock.recv(1024) |
|
424 |
self.assertEquals(len(challenge), 16) |
|
425 |
|
|
426 |
# Step 5. DES-Encrypt challenge, use password as key (par 6.2.2) |
|
427 |
response = d3des_generate_response( |
|
428 |
(console["password"] + '\0' * 8)[:8], challenge) |
|
429 |
sock.send(response) |
|
430 |
|
|
431 |
# Step 6. SecurityResult (par 6.1.3) |
|
432 |
result = sock.recv(4) |
|
433 |
self.assertEquals(list(result), ['\x00', '\x00', '\x00', '\x00']) |
|
434 |
sock.close() |
|
435 |
|
|
436 |
def test_004_server_has_ipv4(self): |
|
437 |
"""Test active server has a valid IPv4 address""" |
|
438 |
server = self.client.get_server_details(self.serverid) |
|
439 |
ipv4 = self._get_ipv4(server) |
|
440 |
self.assertEquals(IP(ipv4).version(), 4) |
|
441 |
|
|
442 |
def test_005_server_has_ipv6(self): |
|
443 |
"""Test active server has a valid IPv6 address""" |
|
444 |
server = self.client.get_server_details(self.serverid) |
|
445 |
ipv6 = self._get_ipv6(server) |
|
446 |
self.assertEquals(IP(ipv6).version(), 6) |
|
447 |
|
|
448 |
def test_006_server_responds_to_ping_IPv4(self): |
|
449 |
"""Test server responds to ping on IPv4 address""" |
|
450 |
server = self.client.get_server_details(self.serverid) |
|
451 |
ip = self._get_ipv4(server) |
|
452 |
self._try_until_timeout_expires(self.action_timeout, |
|
453 |
self.action_timeout, |
|
454 |
"PING IPv4 to %s" % ip, |
|
455 |
self._ping_once, |
|
456 |
False, ip) |
|
457 |
|
|
458 |
def test_007_server_responds_to_ping_IPv6(self): |
|
459 |
"""Test server responds to ping on IPv6 address""" |
|
460 |
server = self.client.get_server_details(self.serverid) |
|
461 |
ip = self._get_ipv6(server) |
|
462 |
self._try_until_timeout_expires(self.action_timeout, |
|
463 |
self.action_timeout, |
|
464 |
"PING IPv6 to %s" % ip, |
|
465 |
self._ping_once, |
|
466 |
True, ip) |
|
467 |
|
|
468 |
def test_008_submit_shutdown_request(self): |
|
469 |
"""Test submit request to shutdown server""" |
|
470 |
self.client.shutdown_server(self.serverid) |
|
471 |
|
|
472 |
def test_009_server_becomes_stopped(self): |
|
473 |
"""Test server becomes STOPPED""" |
|
474 |
self._insist_on_status_transition("ACTIVE", "STOPPED", |
|
475 |
self.action_timeout, |
|
476 |
self.action_timeout) |
|
477 |
|
|
478 |
def test_010_submit_start_request(self): |
|
479 |
"""Test submit start server request""" |
|
480 |
self.client.start_server(self.serverid) |
|
481 |
|
|
482 |
def test_011_server_becomes_active(self): |
|
483 |
"""Test server becomes ACTIVE again""" |
|
484 |
self._insist_on_status_transition("STOPPED", "ACTIVE", |
|
485 |
self.action_timeout, |
|
486 |
self.action_timeout) |
|
487 |
|
|
488 |
def test_011a_server_responds_to_ping_IPv4(self): |
|
489 |
"""Test server OS is actually up and running again""" |
|
490 |
self.test_006_server_responds_to_ping_IPv4() |
|
491 |
|
|
492 |
def test_012_ssh_to_server_IPv4(self): |
|
493 |
"""Test SSH to server public IPv4 works, verify hostname""" |
|
494 |
self._skipIf(self.is_windows, "only valid for Linux servers") |
|
495 |
server = self.client.get_server_details(self.serverid) |
|
496 |
self._insist_on_ssh_hostname(self._get_ipv4(server), |
|
497 |
self.username, self.passwd) |
|
498 |
|
|
499 |
def test_013_ssh_to_server_IPv6(self): |
|
500 |
"""Test SSH to server public IPv6 works, verify hostname""" |
|
501 |
self._skipIf(self.is_windows, "only valid for Linux servers") |
|
502 |
server = self.client.get_server_details(self.serverid) |
|
503 |
self._insist_on_ssh_hostname(self._get_ipv6(server), |
|
504 |
self.username, self.passwd) |
|
505 |
|
|
506 |
def test_014_rdp_to_server_IPv4(self): |
|
507 |
"Test RDP connection to server public IPv4 works""" |
|
508 |
self._skipIf(not self.is_windows, "only valid for Windows servers") |
|
509 |
server = self.client.get_server_details(self.serverid) |
|
510 |
ipv4 = self._get_ipv4(server) |
|
511 |
sock = _insist_on_tcp_connection(socket.AF_INET, ipv4, 3389) |
|
512 |
|
|
513 |
# No actual RDP processing done. We assume the RDP server is there |
|
514 |
# if the connection to the RDP port is successful. |
|
515 |
sock.close() |
|
516 |
|
|
517 |
def test_015_rdp_to_server_IPv6(self): |
|
518 |
"Test RDP connection to server public IPv6 works""" |
|
519 |
self._skipIf(not self.is_windows, "only valid for Windows servers") |
|
520 |
server = self.client.get_server_details(self.serverid) |
|
521 |
ipv6 = self._get_ipv6(server) |
|
522 |
sock = _get_tcp_connection(socket.AF_INET6, ipv6, 3389) |
|
523 |
|
|
524 |
# No actual RDP processing done. We assume the RDP server is there |
|
525 |
# if the connection to the RDP port is successful. |
|
526 |
sock.close() |
|
527 |
|
|
528 |
def test_016_personality_is_enforced(self): |
|
529 |
"""Test file injection for personality enforcement""" |
|
530 |
self._skipIf(self.is_windows, "only implemented for Linux servers") |
|
531 |
self.assertTrue(False, "test not implemented, will fail") |
|
532 |
|
|
533 |
def test_017_submit_delete_request(self): |
|
534 |
"""Test submit request to delete server""" |
|
535 |
self.client.delete_server(self.serverid) |
|
536 |
|
|
537 |
def test_018_server_becomes_deleted(self): |
|
538 |
"""Test server becomes DELETED""" |
|
539 |
self._insist_on_status_transition("ACTIVE", "DELETED", |
|
540 |
self.action_timeout, |
|
541 |
self.action_timeout) |
|
542 |
|
|
543 |
def test_019_server_no_longer_in_server_list(self): |
|
544 |
"""Test server is no longer in server list""" |
|
545 |
servers = self.client.list_servers() |
|
546 |
self.assertNotIn(self.serverid, [s["id"] for s in servers]) |
|
547 |
|
|
548 |
|
|
549 |
class TestRunnerProcess(Process): |
|
550 |
"""A distinct process used to execute part of the tests in parallel""" |
|
551 |
def __init__(self, **kw): |
|
552 |
Process.__init__(self, **kw) |
|
553 |
kwargs = kw["kwargs"] |
|
554 |
self.testq = kwargs["testq"] |
|
555 |
self.runner = kwargs["runner"] |
|
556 |
|
|
557 |
def run(self): |
|
558 |
# Make sure this test runner process dies with the parent |
|
559 |
# and is not left behind. |
|
560 |
# |
|
561 |
# WARNING: This uses the prctl(2) call and is |
|
562 |
# Linux-specific. |
|
563 |
prctl.set_pdeathsig(signal.SIGHUP) |
|
564 |
|
|
565 |
while True: |
|
566 |
log.debug("I am process %d, GETting from queue is %s", |
|
567 |
os.getpid(), self.testq) |
|
568 |
msg = self.testq.get() |
|
569 |
log.debug("Dequeued msg: %s", msg) |
|
570 |
|
|
571 |
if msg == "TEST_RUNNER_TERMINATE": |
|
572 |
raise SystemExit |
|
573 |
elif issubclass(msg, unittest.TestCase): |
|
574 |
# Assemble a TestSuite, and run it |
|
575 |
suite = unittest.TestLoader().loadTestsFromTestCase(msg) |
|
576 |
self.runner.run(suite) |
|
577 |
else: |
|
578 |
raise Exception("Cannot handle msg: %s" % msg) |
|
579 |
|
|
580 |
|
|
581 |
def _run_cases_in_parallel(cases, fanout=1, runner=None): |
|
582 |
"""Run instances of TestCase in parallel, in a number of distinct processes |
|
583 |
|
|
584 |
The cases iterable specifies the TestCases to be executed in parallel, |
|
585 |
by test runners running in distinct processes. |
|
586 |
The fanout parameter specifies the number of processes to spawn, |
|
587 |
and defaults to 1. |
|
588 |
The runner argument specifies the test runner class to use inside each |
|
589 |
runner process. |
|
590 |
|
|
591 |
""" |
|
592 |
if runner is None: |
|
593 |
runner = unittest.TextTestRunner(verbosity=2, failfast=True) |
|
594 |
|
|
595 |
# testq: The master process enqueues TestCase objects into this queue, |
|
596 |
# test runner processes pick them up for execution, in parallel. |
|
597 |
testq = Queue() |
|
598 |
runners = [] |
|
599 |
for i in xrange(0, fanout): |
|
600 |
kwargs = dict(testq=testq, runner=runner) |
|
601 |
runners.append(TestRunnerProcess(kwargs=kwargs)) |
|
602 |
|
|
603 |
log.info("Spawning %d test runner processes", len(runners)) |
|
604 |
for p in runners: |
|
605 |
p.start() |
|
606 |
log.debug("Spawned %d test runners, PIDs are %s", |
|
607 |
len(runners), [p.pid for p in runners]) |
|
608 |
|
|
609 |
# Enqueue test cases |
|
610 |
map(testq.put, cases) |
|
611 |
map(testq.put, ["TEST_RUNNER_TERMINATE"] * len(runners)) |
|
612 |
|
|
613 |
log.debug("Joining %d processes", len(runners)) |
|
614 |
for p in runners: |
|
615 |
p.join() |
|
616 |
log.debug("Done joining %d processes", len(runners)) |
|
617 |
|
|
618 |
|
|
619 |
def _spawn_server_test_case(**kwargs): |
|
620 |
"""Construct a new unit test case class from SpawnServerTestCase""" |
|
621 |
|
|
622 |
name = "SpawnServerTestCase_%d" % kwargs["imageid"] |
|
623 |
cls = type(name, (SpawnServerTestCase,), kwargs) |
|
624 |
|
|
625 |
# Patch extra parameters into test names by manipulating method docstrings |
|
626 |
for (mname, m) in \ |
|
627 |
inspect.getmembers(cls, lambda x: inspect.ismethod(x)): |
|
628 |
if hasattr(m, __doc__): |
|
629 |
m.__func__.__doc__ = "[%s] %s" % (imagename, m.__doc__) |
|
630 |
|
|
631 |
# Make sure the class can be pickled, by listing it among |
|
632 |
# the attributes of __main__. A PicklingError is raised otherwise. |
|
633 |
setattr(__main__, name, cls) |
|
634 |
return cls |
|
635 |
|
|
636 |
|
|
637 |
def cleanup_servers(delete_stale=False): |
|
638 |
c = Client(API, TOKEN) |
|
639 |
servers = c.list_servers() |
|
640 |
stale = [s for s in servers if s["name"].startswith(SNF_TEST_PREFIX)] |
|
641 |
|
|
642 |
if len(stale) == 0: |
|
643 |
return |
|
644 |
|
|
645 |
print >> sys.stderr, "Found these stale servers from previous runs:" |
|
646 |
print " " + \ |
|
647 |
"\n ".join(["%d: %s" % (s["id"], s["name"]) for s in stale]) |
|
648 |
|
|
649 |
if delete_stale: |
|
650 |
print >> sys.stderr, "Deleting %d stale servers:" % len(stale) |
|
651 |
for server in stale: |
|
652 |
c.delete_server(server["id"]) |
|
653 |
print >> sys.stderr, " ...done" |
|
654 |
else: |
|
655 |
print >> sys.stderr, "Use --delete-stale to delete them." |
|
656 |
|
|
657 |
|
|
658 |
def parse_arguments(args): |
|
659 |
from optparse import OptionParser |
|
660 |
|
|
661 |
kw = {} |
|
662 |
kw["usage"] = "%prog [options]" |
|
663 |
kw["description"] = \ |
|
664 |
"%prog runs a number of test scenarios on a " \ |
|
665 |
"Synnefo deployment." |
|
666 |
|
|
667 |
parser = OptionParser(**kw) |
|
668 |
parser.disable_interspersed_args() |
|
669 |
parser.add_option("--api", |
|
670 |
action="store", type="string", dest="api", |
|
671 |
help="The API URI to use to reach the Synnefo API", |
|
672 |
default=DEFAULT_API) |
|
673 |
parser.add_option("--token", |
|
674 |
action="store", type="string", dest="token", |
|
675 |
help="The token to use for authentication to the API", |
|
676 |
default=DEFAULT_TOKEN) |
|
677 |
parser.add_option("--nofailfast", |
|
678 |
action="store_true", dest="nofailfast", |
|
679 |
help="Do not fail immediately if one of the tests " \ |
|
680 |
"fails (EXPERIMENTAL)", |
|
681 |
default=False) |
|
682 |
parser.add_option("--action-timeout", |
|
683 |
action="store", type="int", dest="action_timeout", |
|
684 |
metavar="TIMEOUT", |
|
685 |
help="Wait SECONDS seconds for a server action to " \ |
|
686 |
"complete, then the test is considered failed", |
|
687 |
default=20) |
|
688 |
parser.add_option("--build-warning", |
|
689 |
action="store", type="int", dest="build_warning", |
|
690 |
metavar="TIMEOUT", |
|
691 |
help="Warn if TIMEOUT seconds have passed and a " \ |
|
692 |
"build operation is still pending", |
|
693 |
default=600) |
|
694 |
parser.add_option("--build-fail", |
|
695 |
action="store", type="int", dest="build_fail", |
|
696 |
metavar="BUILD_TIMEOUT", |
|
697 |
help="Fail the test if TIMEOUT seconds have passed " \ |
|
698 |
"and a build operation is still incomplete", |
|
699 |
default=900) |
|
700 |
parser.add_option("--query-interval", |
|
701 |
action="store", type="int", dest="query_interval", |
|
702 |
metavar="INTERVAL", |
|
703 |
help="Query server status when requests are pending " \ |
|
704 |
"every INTERVAL seconds", |
|
705 |
default=3) |
|
706 |
parser.add_option("--fanout", |
|
707 |
action="store", type="int", dest="fanout", |
|
708 |
metavar="COUNT", |
|
709 |
help="Spawn up to COUNT child processes to execute " \ |
|
710 |
"in parallel, essentially have up to COUNT " \ |
|
711 |
"server build requests outstanding (EXPERIMENTAL)", |
|
712 |
default=1) |
|
713 |
parser.add_option("--force-flavor", |
|
714 |
action="store", type="int", dest="force_flavorid", |
|
715 |
metavar="FLAVOR ID", |
|
716 |
help="Force all server creations to use the specified "\ |
|
717 |
"FLAVOR ID instead of a randomly chosen one, " \ |
|
718 |
"useful if disk space is scarce", |
|
719 |
default=None) |
|
720 |
parser.add_option("--force-image", |
|
721 |
action="store", type="int", dest="force_imageid", |
|
722 |
metavar="IMAGE ID", |
|
723 |
help="Instead of testing all available images, test " \ |
|
724 |
"only the specified IMAGE ID", |
|
725 |
default=None) |
|
726 |
parser.add_option("--show-stale", |
|
727 |
action="store_true", dest="show_stale", |
|
728 |
help="Show stale servers from previous runs, whose "\ |
|
729 |
"name starts with `%s'" % SNF_TEST_PREFIX, |
|
730 |
default=False) |
|
731 |
parser.add_option("--delete-stale", |
|
732 |
action="store_true", dest="delete_stale", |
|
733 |
help="Delete stale servers from previous runs, whose "\ |
|
734 |
"name starts with `%s'" % SNF_TEST_PREFIX, |
|
735 |
default=False) |
|
736 |
|
|
737 |
# FIXME: Change the default for build-fanout to 10 |
|
738 |
# FIXME: Allow the user to specify a specific set of Images to test |
|
739 |
|
|
740 |
(opts, args) = parser.parse_args(args) |
|
741 |
|
|
742 |
# Verify arguments |
|
743 |
if opts.delete_stale: |
|
744 |
opts.show_stale = True |
|
745 |
|
|
746 |
return (opts, args) |
|
747 |
|
|
748 |
|
|
749 |
def main(): |
|
750 |
"""Assemble test cases into a test suite, and run it |
|
751 |
|
|
752 |
IMPORTANT: Tests have dependencies and have to be run in the specified |
|
753 |
order inside a single test case. They communicate through attributes of the |
|
754 |
corresponding TestCase class (shared fixtures). Distinct subclasses of |
|
755 |
TestCase MAY SHARE NO DATA, since they are run in parallel, in distinct |
|
756 |
test runner processes. |
|
757 |
|
|
758 |
""" |
|
759 |
(opts, args) = parse_arguments(sys.argv[1:]) |
|
760 |
|
|
761 |
global API, TOKEN |
|
762 |
API = opts.api |
|
763 |
TOKEN = opts.token |
|
764 |
|
|
765 |
# Cleanup stale servers from previous runs |
|
766 |
if opts.show_stale: |
|
767 |
cleanup_servers(delete_stale=opts.delete_stale) |
|
768 |
return 0 |
|
769 |
|
|
770 |
# Initialize a kamaki instance, get flavors, images |
|
771 |
c = Client(API, TOKEN) |
|
772 |
DIMAGES = c.list_images(detail=True) |
|
773 |
DFLAVORS = c.list_flavors(detail=True) |
|
774 |
|
|
775 |
# FIXME: logging, log, LOG PID, TEST_RUN_ID, arguments |
|
776 |
# FIXME: Network testing? Create, destroy, connect, ping, disconnect VMs? |
|
777 |
# Run them: FIXME: In parallel, FAILEARLY, catchbreak? |
|
778 |
#unittest.main(verbosity=2, catchbreak=True) |
|
779 |
|
|
780 |
runner = unittest.TextTestRunner(verbosity=2, failfast=not opts.nofailfast) |
|
781 |
# The following cases run sequentially |
|
782 |
seq_cases = [UnauthorizedTestCase, FlavorsTestCase, ImagesTestCase] |
|
783 |
_run_cases_in_parallel(seq_cases, fanout=3, runner=runner) |
|
784 |
|
|
785 |
# The following cases run in parallel |
|
786 |
par_cases = [] |
|
787 |
|
|
788 |
if opts.force_imageid: |
|
789 |
test_images = filter(lambda x: x["id"] == opts.force_imageid, DIMAGES) |
|
790 |
else: |
|
791 |
test_images = DIMAGES |
|
792 |
|
|
793 |
for image in test_images: |
|
794 |
imageid = image["id"] |
|
795 |
imagename = image["name"] |
|
796 |
if opts.force_flavorid: |
|
797 |
flavorid = opts.force_flavorid |
|
798 |
else: |
|
799 |
flavorid = choice([f["id"] for f in DFLAVORS if f["disk"] >= 20]) |
|
800 |
personality = None # FIXME |
|
801 |
servername = "%s%s for %s" % (SNF_TEST_PREFIX, TEST_RUN_ID, imagename) |
|
802 |
is_windows = imagename.lower().find("windows") >= 0 |
|
803 |
case = _spawn_server_test_case(imageid=imageid, flavorid=flavorid, |
|
804 |
imagename=imagename, |
|
805 |
personality=personality, |
|
806 |
servername=servername, |
|
807 |
is_windows=is_windows, |
|
808 |
action_timeout=opts.action_timeout, |
|
809 |
build_warning=opts.build_warning, |
|
810 |
build_fail=opts.build_fail, |
|
811 |
query_interval=opts.query_interval) |
|
812 |
par_cases.append(case) |
|
813 |
|
|
814 |
_run_cases_in_parallel(par_cases, fanout=opts.fanout, runner=runner) |
|
815 |
|
|
816 |
if __name__ == "__main__": |
|
817 |
sys.exit(main()) |
/dev/null | ||
---|---|---|
1 |
#!/usr/bin/env python |
|
2 |
|
|
3 |
# Copyright 2011 GRNET S.A. All rights reserved. |
|
4 |
# |
|
5 |
# Redistribution and use in source and binary forms, with or |
|
6 |
# without modification, are permitted provided that the following |
|
7 |
# conditions are met: |
|
8 |
# |
|
9 |
# 1. Redistributions of source code must retain the above |
|
10 |
# copyright notice, this list of conditions and the following |
|
11 |
# disclaimer. |
|
12 |
# |
|
13 |
# 2. Redistributions in binary form must reproduce the above |
|
14 |
# copyright notice, this list of conditions and the following |
|
15 |
# disclaimer in the documentation and/or other materials |
|
16 |
# provided with the distribution. |
|
17 |
# |
|
18 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
|
19 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
20 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
21 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
|
22 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
|
25 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
|
26 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
27 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
28 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
29 |
# POSSIBILITY OF SUCH DAMAGE. |
|
30 |
# |
|
31 |
# The views and conclusions contained in the software and |
|
32 |
# documentation are those of the authors and should not be |
|
33 |
# interpreted as representing official policies, either expressed |
|
34 |
# or implied, of GRNET S.A. |
|
35 |
|
|
36 |
"""Perform integration testing on a running Synnefo deployment""" |
|
37 |
|
|
38 |
import __main__ |
|
39 |
import datetime |
|
40 |
import inspect |
|
41 |
import logging |
|
42 |
import os |
|
43 |
import paramiko |
|
44 |
import prctl |
|
45 |
import subprocess |
|
46 |
import signal |
|
47 |
import socket |
|
48 |
import struct |
|
49 |
import sys |
|
50 |
import time |
|
51 |
|
|
52 |
from IPy import IP |
|
53 |
from multiprocessing import Process, Queue |
|
54 |
from random import choice |
|
55 |
|
|
56 |
from kamaki.client import Client, ClientError |
|
57 |
from vncauthproxy.d3des import generate_response as d3des_generate_response |
|
58 |
|
|
59 |
# Use backported unittest functionality if Python < 2.7 |
|
60 |
try: |
|
61 |
import unittest2 as unittest |
|
62 |
except ImportError: |
|
63 |
if sys.version_info < (2, 7): |
|
64 |
raise Exception("The unittest2 package is required for Python < 2.7") |
|
65 |
import unittest |
|
66 |
|
|
67 |
|
|
68 |
API = None |
|
69 |
TOKEN = None |
|
70 |
DEFAULT_API = "http://dev67.dev.grnet.gr:8000/api/v1.1" |
|
71 |
DEFAULT_TOKEN = "46e427d657b20defe352804f0eb6f8a2" |
|
72 |
# A unique id identifying this test run |
|
73 |
TEST_RUN_ID = datetime.datetime.strftime(datetime.datetime.now(), |
|
74 |
"%Y%m%d%H%M%S") |
|
75 |
SNF_TEST_PREFIX = "snf-test-" |
|
76 |
|
|
77 |
# Setup logging (FIXME - verigak) |
|
78 |
logging.basicConfig(format="%(message)s") |
|
79 |
log = logging.getLogger("snf-test") |
|
80 |
log.setLevel(logging.INFO) |
|
81 |
|
|
82 |
|
|
83 |
class UnauthorizedTestCase(unittest.TestCase): |
|
84 |
def test_unauthorized_access(self): |
|
85 |
"""Test access without a valid token fails""" |
|
86 |
c = Client(API, "123") |
|
87 |
with self.assertRaises(ClientError) as cm: |
|
88 |
c.list_servers() |
|
89 |
self.assertEqual(cm.exception.status, 401) |
|
90 |
|
|
91 |
|
|
92 |
class ImagesTestCase(unittest.TestCase): |
|
93 |
"""Test image lists for consistency""" |
|
94 |
@classmethod |
|
95 |
def setUpClass(cls): |
|
96 |
"""Initialize kamaki, get (detailed) list of images""" |
|
97 |
log.info("Getting simple and detailed list of images") |
|
98 |
cls.client = Client(API, TOKEN) |
|
99 |
cls.images = cls.client.list_images() |
|
100 |
cls.dimages = cls.client.list_images(detail=True) |
|
101 |
|
|
102 |
def test_001_list_images(self): |
|
103 |
"""Test image list actually returns images""" |
|
104 |
self.assertGreater(len(self.images), 0) |
|
105 |
|
|
106 |
def test_002_list_images_detailed(self): |
|
107 |
"""Test detailed image list is the same length as list""" |
|
108 |
self.assertEqual(len(self.dimages), len(self.images)) |
|
109 |
|
|
110 |
def test_003_same_image_names(self): |
|
111 |
"""Test detailed and simple image list contain same names""" |
|
112 |
names = sorted(map(lambda x: x["name"], self.images)) |
|
113 |
dnames = sorted(map(lambda x: x["name"], self.dimages)) |
|
114 |
self.assertEqual(names, dnames) |
|
115 |
|
|
116 |
def test_004_unique_image_names(self): |
|
117 |
"""Test images have unique names""" |
|
118 |
names = sorted(map(lambda x: x["name"], self.images)) |
|
119 |
self.assertEqual(sorted(list(set(names))), names) |
|
120 |
|
|
121 |
def test_005_image_metadata(self): |
|
122 |
"""Test every image has specific metadata defined""" |
|
123 |
keys = frozenset(["OS", "description", "size"]) |
|
124 |
for i in self.dimages: |
|
125 |
self.assertTrue(keys.issubset(i["metadata"]["values"].keys())) |
|
126 |
|
|
127 |
|
|
128 |
class FlavorsTestCase(unittest.TestCase): |
|
129 |
"""Test flavor lists for consistency""" |
|
130 |
@classmethod |
|
131 |
def setUpClass(cls): |
|
132 |
"""Initialize kamaki, get (detailed) list of flavors""" |
|
133 |
log.info("Getting simple and detailed list of flavors") |
|
134 |
cls.client = Client(API, TOKEN) |
|
135 |
cls.flavors = cls.client.list_flavors() |
|
136 |
cls.dflavors = cls.client.list_flavors(detail=True) |
|
137 |
|
|
138 |
def test_001_list_flavors(self): |
|
139 |
"""Test flavor list actually returns flavors""" |
|
140 |
self.assertGreater(len(self.flavors), 0) |
|
141 |
|
|
142 |
def test_002_list_flavors_detailed(self): |
|
143 |
"""Test detailed flavor list is the same length as list""" |
|
144 |
self.assertEquals(len(self.dflavors), len(self.flavors)) |
|
145 |
|
|
146 |
def test_003_same_flavor_names(self): |
|
147 |
"""Test detailed and simple flavor list contain same names""" |
|
148 |
names = sorted(map(lambda x: x["name"], self.flavors)) |
|
149 |
dnames = sorted(map(lambda x: x["name"], self.dflavors)) |
|
150 |
self.assertEqual(names, dnames) |
|
151 |
|
|
152 |
def test_004_unique_flavor_names(self): |
|
153 |
"""Test flavors have unique names""" |
|
154 |
names = sorted(map(lambda x: x["name"], self.flavors)) |
|
155 |
self.assertEqual(sorted(list(set(names))), names) |
|
156 |
|
|
157 |
def test_005_well_formed_flavor_names(self): |
|
158 |
"""Test flavors have names of the form CxxRyyDzz |
|
159 |
|
|
160 |
Where xx is vCPU count, yy is RAM in MiB, zz is Disk in GiB |
|
161 |
|
|
162 |
""" |
|
163 |
for f in self.dflavors: |
|
164 |
self.assertEqual("C%dR%dD%d" % (f["cpu"], f["ram"], f["disk"]), |
|
165 |
f["name"], |
|
166 |
"Flavor %s does not match its specs." % f["name"]) |
|
167 |
|
|
168 |
|
|
169 |
class ServersTestCase(unittest.TestCase): |
|
170 |
"""Test server lists for consistency""" |
|
171 |
@classmethod |
|
172 |
def setUpClass(cls): |
|
173 |
"""Initialize kamaki, get (detailed) list of servers""" |
|
174 |
log.info("Getting simple and detailed list of servers") |
|
175 |
cls.client = Client(API, TOKEN) |
|
176 |
cls.servers = cls.client.list_servers() |
|
177 |
cls.dservers = cls.client.list_servers(detail=True) |
|
178 |
|
|
179 |
def test_001_list_servers(self): |
|
180 |
"""Test server list actually returns servers""" |
|
181 |
self.assertGreater(len(self.servers), 0) |
|
182 |
|
|
183 |
def test_002_list_servers_detailed(self): |
|
184 |
"""Test detailed server list is the same length as list""" |
|
185 |
self.assertEqual(len(self.dservers), len(self.servers)) |
|
186 |
|
|
187 |
def test_003_same_server_names(self): |
|
188 |
"""Test detailed and simple flavor list contain same names""" |
|
189 |
names = sorted(map(lambda x: x["name"], self.servers)) |
|
190 |
dnames = sorted(map(lambda x: x["name"], self.dservers)) |
|
191 |
self.assertEqual(names, dnames) |
|
192 |
|
|
193 |
|
|
194 |
# This class gets replicated into actual TestCases dynamically |
|
195 |
class SpawnServerTestCase(unittest.TestCase): |
|
196 |
"""Test scenario for server of the specified image""" |
|
197 |
|
|
198 |
@classmethod |
|
199 |
def setUpClass(cls): |
|
200 |
"""Initialize a kamaki instance""" |
|
201 |
log.info("Spawning server for image `%s'", cls.imagename) |
|
202 |
cls.client = Client(API, TOKEN) |
|
203 |
|
|
204 |
def _get_ipv4(self, server): |
|
205 |
"""Get the public IPv4 of a server from the detailed server info""" |
|
206 |
public_addrs = filter(lambda x: x["id"] == "public", |
|
207 |
server["addresses"]["values"]) |
|
208 |
self.assertEqual(len(public_addrs), 1) |
|
209 |
ipv4_addrs = filter(lambda x: x["version"] == 4, |
|
210 |
public_addrs[0]["values"]) |
|
211 |
self.assertEqual(len(ipv4_addrs), 1) |
|
212 |
return ipv4_addrs[0]["addr"] |
|
213 |
|
|
214 |
def _get_ipv6(self, server): |
|
215 |
"""Get the public IPv6 of a server from the detailed server info""" |
|
216 |
public_addrs = filter(lambda x: x["id"] == "public", |
|
217 |
server["addresses"]["values"]) |
|
218 |
self.assertEqual(len(public_addrs), 1) |
|
219 |
ipv6_addrs = filter(lambda x: x["version"] == 6, |
|
220 |
public_addrs[0]["values"]) |
|
221 |
self.assertEqual(len(ipv6_addrs), 1) |
|
222 |
return ipv6_addrs[0]["addr"] |
|
223 |
|
|
224 |
def _connect_loginname(self, os): |
|
225 |
"""Return the login name for connections based on the server OS""" |
|
226 |
if os in ("ubuntu", "kubuntu", "fedora"): |
|
227 |
return "user" |
|
228 |
elif os == "windows": |
|
229 |
return "Administrator" |
|
230 |
else: |
|
231 |
return "root" |
|
232 |
|
|
233 |
def _verify_server_status(self, current_status, new_status): |
|
234 |
"""Verify a server has switched to a specified status""" |
|
235 |
server = self.client.get_server_details(self.serverid) |
|
236 |
self.assertIn(server["status"], (current_status, new_status)) |
|
237 |
if server["status"] not in (current_status, new_status): |
|
238 |
return None # Do not raise exception, return so the test fails |
|
239 |
self.assertEquals(server["status"], new_status) |
|
240 |
|
|
241 |
def _get_connected_tcp_socket(self, family, host, port): |
|
242 |
"""Get a connected socket from the specified family to host:port""" |
|
243 |
sock = None |
|
244 |
for res in \ |
|
245 |
socket.getaddrinfo(host, port, family, socket.SOCK_STREAM, 0, |
|
246 |
socket.AI_PASSIVE): |
|
247 |
af, socktype, proto, canonname, sa = res |
|
248 |
try: |
|
249 |
sock = socket.socket(af, socktype, proto) |
|
250 |
except socket.error as msg: |
|
251 |
sock = None |
|
252 |
continue |
|
253 |
try: |
|
254 |
sock.connect(sa) |
|
255 |
except socket.error as msg: |
|
256 |
sock.close() |
|
257 |
sock = None |
|
258 |
continue |
|
259 |
self.assertIsNotNone(sock) |
|
260 |
return sock |
|
261 |
|
|
262 |
def _ping_once(self, ipv6, ip): |
|
263 |
"""Test server responds to a single IPv4 or IPv6 ping""" |
|
264 |
cmd = "ping%s -c 2 -w 3 %s" % ("6" if ipv6 else "", ip) |
|
265 |
ping = subprocess.Popen(cmd, shell=True, |
|
266 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
267 |
(stdout, stderr) = ping.communicate() |
|
268 |
ret = ping.wait() |
|
269 |
self.assertEquals(ret, 0) |
|
270 |
|
|
271 |
def _get_hostname_over_ssh(self, hostip, username, password): |
|
272 |
ssh = paramiko.SSHClient() |
|
273 |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
|
274 |
try: |
|
275 |
ssh.connect(hostip, username=username, password=password) |
|
276 |
except socket.error: |
|
277 |
raise AssertionError |
|
278 |
stdin, stdout, stderr = ssh.exec_command("hostname") |
|
279 |
lines = stdout.readlines() |
|
280 |
self.assertEqual(len(lines), 1) |
|
281 |
return lines[0] |
|
282 |
|
|
283 |
def _try_until_timeout_expires(self, warn_timeout, fail_timeout, |
|
284 |
opmsg, callable, *args, **kwargs): |
|
285 |
if warn_timeout == fail_timeout: |
|
286 |
warn_timeout = fail_timeout + 1 |
|
287 |
warn_tmout = time.time() + warn_timeout |
|
288 |
fail_tmout = time.time() + fail_timeout |
|
289 |
while True: |
|
290 |
self.assertLess(time.time(), fail_tmout, |
|
291 |
"operation `%s' timed out" % opmsg) |
|
292 |
if time.time() > warn_tmout: |
|
293 |
log.warning("Server %d: `%s' operation `%s' not done yet", |
|
294 |
self.serverid, self.servername, opmsg) |
|
295 |
try: |
|
296 |
log.info("%s... " % opmsg) |
|
297 |
return callable(*args, **kwargs) |
|
298 |
except AssertionError: |
|
299 |
pass |
|
300 |
time.sleep(self.query_interval) |
|
301 |
|
|
302 |
def _insist_on_tcp_connection(self, family, host, port): |
|
303 |
familystr = {socket.AF_INET: "IPv4", socket.AF_INET6: "IPv6", |
|
304 |
socket.AF_UNSPEC: "Unspecified-IPv4/6"} |
|
305 |
msg = "connect over %s to %s:%s" % \ |
|
306 |
(familystr.get(family, "Unknown"), host, port) |
|
307 |
sock = self._try_until_timeout_expires( |
|
308 |
self.action_timeout, self.action_timeout, |
|
309 |
msg, self._get_connected_tcp_socket, |
|
310 |
family, host, port) |
|
311 |
return sock |
|
312 |
|
|
313 |
def _insist_on_status_transition(self, current_status, new_status, |
|
314 |
fail_timeout, warn_timeout=None): |
|
315 |
msg = "Server %d: `%s', waiting for %s -> %s" % \ |
|
316 |
(self.serverid, self.servername, current_status, new_status) |
|
317 |
if warn_timeout is None: |
|
318 |
warn_timeout = fail_timeout |
|
319 |
self._try_until_timeout_expires(warn_timeout, fail_timeout, |
|
320 |
msg, self._verify_server_status, |
|
321 |
current_status, new_status) |
|
322 |
# Ensure the status is actually the expected one |
|
323 |
server = self.client.get_server_details(self.serverid) |
|
324 |
self.assertEquals(server["status"], new_status) |
|
325 |
|
|
326 |
def _insist_on_ssh_hostname(self, hostip, username, password): |
|
327 |
msg = "SSH to %s, as %s/%s" % (hostip, username, password) |
|
328 |
hostname = self._try_until_timeout_expires( |
|
329 |
self.action_timeout, self.action_timeout, |
|
330 |
msg, self._get_hostname_over_ssh, |
|
331 |
hostip, username, password) |
|
332 |
|
|
333 |
# The hostname must be of the form 'prefix-id' |
|
334 |
self.assertTrue(hostname.endswith("-%d\n" % self.serverid)) |
|
335 |
|
|
336 |
def _skipIf(self, condition, msg): |
|
337 |
if condition: |
|
338 |
self.skipTest(msg) |
|
339 |
|
|
340 |
def test_001_submit_create_server(self): |
|
341 |
"""Test submit create server request""" |
|
342 |
server = self.client.create_server(self.servername, self.flavorid, |
|
343 |
self.imageid, self.personality) |
|
344 |
self.assertEqual(server["name"], self.servername) |
|
345 |
self.assertEqual(server["flavorRef"], self.flavorid) |
|
346 |
self.assertEqual(server["imageRef"], self.imageid) |
|
347 |
self.assertEqual(server["status"], "BUILD") |
|
348 |
|
|
349 |
# Update class attributes to reflect data on building server |
|
350 |
cls = type(self) |
|
351 |
cls.serverid = server["id"] |
|
352 |
cls.username = None |
|
353 |
cls.passwd = server["adminPass"] |
|
354 |
|
|
355 |
def test_002a_server_is_building_in_list(self): |
|
356 |
"""Test server is in BUILD state, in server list""" |
|
357 |
servers = self.client.list_servers(detail=True) |
|
358 |
servers = filter(lambda x: x["name"] == self.servername, servers) |
|
359 |
self.assertEqual(len(servers), 1) |
|
360 |
server = servers[0] |
|
361 |
self.assertEqual(server["name"], self.servername) |
|
362 |
self.assertEqual(server["flavorRef"], self.flavorid) |
|
363 |
self.assertEqual(server["imageRef"], self.imageid) |
|
364 |
self.assertEqual(server["status"], "BUILD") |
|
365 |
|
|
366 |
def test_002b_server_is_building_in_details(self): |
|
367 |
"""Test server is in BUILD state, in details""" |
|
368 |
server = self.client.get_server_details(self.serverid) |
|
369 |
self.assertEqual(server["name"], self.servername) |
|
370 |
self.assertEqual(server["flavorRef"], self.flavorid) |
|
371 |
self.assertEqual(server["imageRef"], self.imageid) |
|
372 |
self.assertEqual(server["status"], "BUILD") |
|
373 |
|
|
374 |
def test_002c_set_server_metadata(self): |
|
375 |
image = self.client.get_image_details(self.imageid) |
|
376 |
os = image["metadata"]["values"]["OS"] |
|
377 |
loginname = image["metadata"]["values"].get("loginname", None) |
|
378 |
self.client.update_server_metadata(self.serverid, OS=os) |
|
379 |
|
|
380 |
# Determine the username to use for future connections |
|
381 |
# to this host |
|
382 |
cls = type(self) |
|
383 |
cls.username = loginname |
|
384 |
if not cls.username: |
|
385 |
cls.username = self._connect_loginname(os) |
|
386 |
self.assertIsNotNone(cls.username) |
|
387 |
|
|
388 |
def test_002d_verify_server_metadata(self): |
|
389 |
"""Test server metadata keys are set based on image metadata""" |
|
390 |
servermeta = self.client.get_server_metadata(self.serverid) |
|
391 |
imagemeta = self.client.get_image_metadata(self.imageid) |
|
392 |
self.assertEqual(servermeta["OS"], imagemeta["OS"]) |
|
393 |
|
|
394 |
def test_003_server_becomes_active(self): |
|
395 |
"""Test server becomes ACTIVE""" |
|
396 |
self._insist_on_status_transition("BUILD", "ACTIVE", |
|
397 |
self.build_fail, self.build_warning) |
|
398 |
|
|
399 |
def test_003a_get_server_oob_console(self): |
|
400 |
"""Test getting OOB server console over VNC |
|
401 |
|
|
402 |
Implementation of RFB protocol follows |
|
403 |
http://www.realvnc.com/docs/rfbproto.pdf. |
|
404 |
|
|
405 |
""" |
|
406 |
console = self.client.get_server_console(self.serverid) |
|
407 |
self.assertEquals(console['type'], "vnc") |
|
408 |
sock = self._insist_on_tcp_connection(socket.AF_UNSPEC, |
|
409 |
console["host"], console["port"]) |
|
410 |
|
|
411 |
# Step 1. ProtocolVersion message (par. 6.1.1) |
|
412 |
version = sock.recv(1024) |
|
413 |
self.assertEquals(version, 'RFB 003.008\n') |
|
414 |
sock.send(version) |
|
415 |
|
|
416 |
# Step 2. Security (par 6.1.2): Only VNC Authentication supported |
|
417 |
sec = sock.recv(1024) |
|
418 |
self.assertEquals(list(sec), ['\x01', '\x02']) |
|
419 |
|
|
420 |
# Step 3. Request VNC Authentication (par 6.1.2) |
|
421 |
sock.send('\x02') |
|
422 |
|
|
423 |
# Step 4. Receive Challenge (par 6.2.2) |
|
424 |
challenge = sock.recv(1024) |
|
425 |
self.assertEquals(len(challenge), 16) |
|
426 |
|
|
427 |
# Step 5. DES-Encrypt challenge, use password as key (par 6.2.2) |
|
428 |
response = d3des_generate_response( |
|
429 |
(console["password"] + '\0' * 8)[:8], challenge) |
|
430 |
sock.send(response) |
|
431 |
|
|
432 |
# Step 6. SecurityResult (par 6.1.3) |
|
433 |
result = sock.recv(4) |
|
434 |
self.assertEquals(list(result), ['\x00', '\x00', '\x00', '\x00']) |
|
435 |
sock.close() |
|
436 |
|
|
437 |
def test_004_server_has_ipv4(self): |
|
438 |
"""Test active server has a valid IPv4 address""" |
|
439 |
server = self.client.get_server_details(self.serverid) |
|
440 |
ipv4 = self._get_ipv4(server) |
|
441 |
self.assertEquals(IP(ipv4).version(), 4) |
|
442 |
|
|
443 |
def test_005_server_has_ipv6(self): |
|
444 |
"""Test active server has a valid IPv6 address""" |
|
445 |
server = self.client.get_server_details(self.serverid) |
|
446 |
ipv6 = self._get_ipv6(server) |
|
447 |
self.assertEquals(IP(ipv6).version(), 6) |
|
448 |
|
|
449 |
def test_006_server_responds_to_ping_IPv4(self): |
|
450 |
"""Test server responds to ping on IPv4 address""" |
|
451 |
server = self.client.get_server_details(self.serverid) |
|
452 |
ip = self._get_ipv4(server) |
|
453 |
self._try_until_timeout_expires(self.action_timeout, |
|
454 |
self.action_timeout, |
|
455 |
"PING IPv4 to %s" % ip, |
|
456 |
self._ping_once, |
|
457 |
False, ip) |
|
458 |
|
|
459 |
def test_007_server_responds_to_ping_IPv6(self): |
|
460 |
"""Test server responds to ping on IPv6 address""" |
|
461 |
server = self.client.get_server_details(self.serverid) |
|
462 |
ip = self._get_ipv6(server) |
|
463 |
self._try_until_timeout_expires(self.action_timeout, |
|
464 |
self.action_timeout, |
|
465 |
"PING IPv6 to %s" % ip, |
|
466 |
self._ping_once, |
|
467 |
True, ip) |
|
468 |
|
|
469 |
def test_008_submit_shutdown_request(self): |
|
470 |
"""Test submit request to shutdown server""" |
|
471 |
self.client.shutdown_server(self.serverid) |
|
472 |
|
|
473 |
def test_009_server_becomes_stopped(self): |
|
474 |
"""Test server becomes STOPPED""" |
|
475 |
self._insist_on_status_transition("ACTIVE", "STOPPED", |
|
476 |
self.action_timeout, |
|
477 |
self.action_timeout) |
|
478 |
|
|
479 |
def test_010_submit_start_request(self): |
|
480 |
"""Test submit start server request""" |
|
481 |
self.client.start_server(self.serverid) |
|
482 |
|
|
483 |
def test_011_server_becomes_active(self): |
|
484 |
"""Test server becomes ACTIVE again""" |
|
485 |
self._insist_on_status_transition("STOPPED", "ACTIVE", |
|
486 |
self.action_timeout, |
|
487 |
self.action_timeout) |
|
488 |
|
|
489 |
def test_011a_server_responds_to_ping_IPv4(self): |
|
490 |
"""Test server OS is actually up and running again""" |
|
491 |
self.test_006_server_responds_to_ping_IPv4() |
|
492 |
|
|
493 |
def test_012_ssh_to_server_IPv4(self): |
|
494 |
"""Test SSH to server public IPv4 works, verify hostname""" |
|
495 |
self._skipIf(self.is_windows, "only valid for Linux servers") |
|
496 |
server = self.client.get_server_details(self.serverid) |
|
497 |
self._insist_on_ssh_hostname(self._get_ipv4(server), |
|
498 |
self.username, self.passwd) |
|
499 |
|
|
500 |
def test_013_ssh_to_server_IPv6(self): |
|
501 |
"""Test SSH to server public IPv6 works, verify hostname""" |
|
502 |
self._skipIf(self.is_windows, "only valid for Linux servers") |
|
503 |
server = self.client.get_server_details(self.serverid) |
|
504 |
self._insist_on_ssh_hostname(self._get_ipv6(server), |
|
505 |
self.username, self.passwd) |
|
506 |
|
|
507 |
def test_014_rdp_to_server_IPv4(self): |
|
508 |
"Test RDP connection to server public IPv4 works""" |
|
509 |
self._skipIf(not self.is_windows, "only valid for Windows servers") |
|
510 |
server = self.client.get_server_details(self.serverid) |
|
511 |
ipv4 = self._get_ipv4(server) |
|
512 |
sock = _insist_on_tcp_connection(socket.AF_INET, ipv4, 3389) |
|
513 |
|
|
514 |
# No actual RDP processing done. We assume the RDP server is there |
|
515 |
# if the connection to the RDP port is successful. |
|
516 |
sock.close() |
|
517 |
|
|
518 |
def test_015_rdp_to_server_IPv6(self): |
|
519 |
"Test RDP connection to server public IPv6 works""" |
|
520 |
self._skipIf(not self.is_windows, "only valid for Windows servers") |
|
521 |
server = self.client.get_server_details(self.serverid) |
|
522 |
ipv6 = self._get_ipv6(server) |
|
523 |
sock = _get_tcp_connection(socket.AF_INET6, ipv6, 3389) |
|
524 |
|
|
525 |
# No actual RDP processing done. We assume the RDP server is there |
|
526 |
# if the connection to the RDP port is successful. |
|
527 |
sock.close() |
|
528 |
|
|
529 |
def test_016_personality_is_enforced(self): |
|
530 |
"""Test file injection for personality enforcement""" |
|
531 |
self._skipIf(self.is_windows, "only implemented for Linux servers") |
|
532 |
self.assertTrue(False, "test not implemented, will fail") |
|
533 |
|
|
534 |
def test_017_submit_delete_request(self): |
|
535 |
"""Test submit request to delete server""" |
|
536 |
self.client.delete_server(self.serverid) |
|
537 |
|
|
538 |
def test_018_server_becomes_deleted(self): |
|
539 |
"""Test server becomes DELETED""" |
|
540 |
self._insist_on_status_transition("ACTIVE", "DELETED", |
|
541 |
self.action_timeout, |
|
542 |
self.action_timeout) |
|
543 |
|
|
544 |
def test_019_server_no_longer_in_server_list(self): |
|
545 |
"""Test server is no longer in server list""" |
|
546 |
servers = self.client.list_servers() |
|
547 |
self.assertNotIn(self.serverid, [s["id"] for s in servers]) |
|
548 |
|
|
549 |
|
|
550 |
class TestRunnerProcess(Process): |
|
551 |
"""A distinct process used to execute part of the tests in parallel""" |
|
552 |
def __init__(self, **kw): |
|
553 |
Process.__init__(self, **kw) |
|
554 |
kwargs = kw["kwargs"] |
|
555 |
self.testq = kwargs["testq"] |
|
556 |
self.runner = kwargs["runner"] |
|
557 |
|
|
558 |
def run(self): |
|
559 |
# Make sure this test runner process dies with the parent |
|
560 |
# and is not left behind. |
|
561 |
# |
|
562 |
# WARNING: This uses the prctl(2) call and is |
|
563 |
# Linux-specific. |
|
564 |
prctl.set_pdeathsig(signal.SIGHUP) |
|
565 |
|
|
566 |
while True: |
|
567 |
log.debug("I am process %d, GETting from queue is %s", |
|
568 |
os.getpid(), self.testq) |
|
569 |
msg = self.testq.get() |
|
570 |
log.debug("Dequeued msg: %s", msg) |
|
571 |
|
|
572 |
if msg == "TEST_RUNNER_TERMINATE": |
|
573 |
raise SystemExit |
|
574 |
elif issubclass(msg, unittest.TestCase): |
|
575 |
# Assemble a TestSuite, and run it |
|
576 |
suite = unittest.TestLoader().loadTestsFromTestCase(msg) |
|
577 |
self.runner.run(suite) |
|
578 |
else: |
|
579 |
raise Exception("Cannot handle msg: %s" % msg) |
|
580 |
|
|
581 |
|
|
582 |
def _run_cases_in_parallel(cases, fanout=1, runner=None): |
|
583 |
"""Run instances of TestCase in parallel, in a number of distinct processes |
|
584 |
|
|
585 |
The cases iterable specifies the TestCases to be executed in parallel, |
|
586 |
by test runners running in distinct processes. |
|
587 |
|
|
588 |
The fanout parameter specifies the number of processes to spawn, |
|
589 |
and defaults to 1. |
|
590 |
|
|
591 |
The runner argument specifies the test runner class to use inside each |
|
592 |
runner process. |
|
593 |
|
|
594 |
""" |
|
595 |
if runner is None: |
|
596 |
runner = unittest.TextTestRunner() |
|
597 |
|
|
598 |
# testq: The master process enqueues TestCase objects into this queue, |
|
599 |
# test runner processes pick them up for execution, in parallel. |
|
600 |
testq = Queue() |
|
601 |
runners = [] |
|
602 |
for i in xrange(0, fanout): |
|
603 |
kwargs = dict(testq=testq, runner=runner) |
|
604 |
runners.append(TestRunnerProcess(kwargs=kwargs)) |
|
605 |
|
|
606 |
log.info("Spawning %d test runner processes", len(runners)) |
|
607 |
for p in runners: |
|
608 |
p.start() |
|
609 |
log.debug("Spawned %d test runners, PIDs are %s", |
|
610 |
len(runners), [p.pid for p in runners]) |
|
611 |
|
|
612 |
# Enqueue test cases |
|
613 |
map(testq.put, cases) |
|
614 |
map(testq.put, ["TEST_RUNNER_TERMINATE"] * len(runners)) |
|
615 |
|
|
616 |
log.debug("Joining %d processes", len(runners)) |
|
617 |
for p in runners: |
|
618 |
p.join() |
|
619 |
log.debug("Done joining %d processes", len(runners)) |
|
620 |
|
|
621 |
|
|
622 |
def _spawn_server_test_case(**kwargs): |
|
623 |
"""Construct a new unit test case class from SpawnServerTestCase""" |
|
624 |
|
|
625 |
name = "SpawnServerTestCase_%d" % kwargs["imageid"] |
|
626 |
cls = type(name, (SpawnServerTestCase,), kwargs) |
|
627 |
|
|
628 |
# Patch extra parameters into test names by manipulating method docstrings |
|
629 |
for (mname, m) in \ |
|
630 |
inspect.getmembers(cls, lambda x: inspect.ismethod(x)): |
|
631 |
if hasattr(m, __doc__): |
|
632 |
m.__func__.__doc__ = "[%s] %s" % (imagename, m.__doc__) |
|
633 |
|
|
634 |
# Make sure the class can be pickled, by listing it among |
|
635 |
# the attributes of __main__. A PicklingError is raised otherwise. |
|
636 |
setattr(__main__, name, cls) |
|
637 |
return cls |
|
638 |
|
|
639 |
|
|
640 |
def cleanup_servers(delete_stale=False): |
|
641 |
c = Client(API, TOKEN) |
|
642 |
servers = c.list_servers() |
|
643 |
stale = [s for s in servers if s["name"].startswith(SNF_TEST_PREFIX)] |
|
644 |
|
|
645 |
if len(stale) == 0: |
|
646 |
return |
|
647 |
|
|
648 |
print >> sys.stderr, "Found these stale servers from previous runs:" |
|
649 |
print " " + \ |
|
650 |
"\n ".join(["%d: %s" % (s["id"], s["name"]) for s in stale]) |
|
651 |
|
|
652 |
if delete_stale: |
|
653 |
print >> sys.stderr, "Deleting %d stale servers:" % len(stale) |
|
654 |
for server in stale: |
|
655 |
c.delete_server(server["id"]) |
|
656 |
print >> sys.stderr, " ...done" |
|
657 |
else: |
|
658 |
print >> sys.stderr, "Use --delete-stale to delete them." |
|
659 |
|
|
660 |
|
Also available in: Unified diff