Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / tools / burnin.py @ fade8dab

History | View | Annotate | Download (48.2 kB)

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
import hashlib
52
from base64 import b64encode
53
from pwd import getpwuid
54
from grp import getgrgid
55
from IPy import IP
56
from multiprocessing import Process, Queue
57
from random import choice
58

    
59
from kamaki.clients.compute import ComputeClient
60
from kamaki.clients.cyclades import CycladesClient
61

    
62

    
63
from vncauthproxy.d3des import generate_response as d3des_generate_response
64

    
65
# Use backported unittest functionality if Python < 2.7
66
try:
67
    import unittest2 as unittest
68
except ImportError:
69
    if sys.version_info < (2, 7):
70
        raise Exception("The unittest2 package is required for Python < 2.7")
71
    import unittest
72

    
73

    
74
API = None
75
TOKEN = None
76
DEFAULT_API = "https://cyclades.okeanos.grnet.gr/api/v1.1"
77

    
78
# A unique id identifying this test run
79
TEST_RUN_ID = datetime.datetime.strftime(datetime.datetime.now(),
80
                                         "%Y%m%d%H%M%S")
81
SNF_TEST_PREFIX = "snf-test-"
82

    
83
# Setup logging (FIXME - verigak)
84
logging.basicConfig(format="%(message)s")
85
log = logging.getLogger("burnin")
86
log.setLevel(logging.INFO)
87

    
88
class UnauthorizedTestCase(unittest.TestCase):
89
    def test_unauthorized_access(self):
90
        """Test access without a valid token fails"""
91
        falseToken = '12345'
92
        c=ComputeClient(API, falseToken)
93

    
94
        with self.assertRaises(ClientError) as cm:
95
            c.list_servers()
96
        self.assertEqual(cm.exception.status, 401)
97

    
98

    
99
class ImagesTestCase(unittest.TestCase):
100
    """Test image lists for consistency"""
101
    @classmethod
102
    def setUpClass(cls):
103
        """Initialize kamaki, get (detailed) list of images"""
104
        log.info("Getting simple and detailed list of images")
105

    
106
        cls.client = ComputeClient(API, TOKEN)
107
        cls.images = cls.client.list_images()
108
        cls.dimages = cls.client.list_images(detail=True)
109

    
110
    def test_001_list_images(self):
111
        """Test image list actually returns images"""
112
        self.assertGreater(len(self.images), 0)
113

    
114
    def test_002_list_images_detailed(self):
115
        """Test detailed image list is the same length as list"""
116
        self.assertEqual(len(self.dimages), len(self.images))
117

    
118
    def test_003_same_image_names(self):
119
        """Test detailed and simple image list contain same names"""
120
        names = sorted(map(lambda x: x["name"], self.images))
121
        dnames = sorted(map(lambda x: x["name"], self.dimages))
122
        self.assertEqual(names, dnames)
123

    
124
    def test_004_unique_image_names(self):
125
        """Test images have unique names"""
126
        names = sorted(map(lambda x: x["name"], self.images))
127
        self.assertEqual(sorted(list(set(names))), names)
128

    
129
    def test_005_image_metadata(self):
130
        """Test every image has specific metadata defined"""
131
        keys = frozenset(["os", "description", "size"])
132
        for i in self.dimages:
133
            self.assertTrue(keys.issubset(i["metadata"]["values"].keys()))
134

    
135

    
136
class FlavorsTestCase(unittest.TestCase):
137
    """Test flavor lists for consistency"""
138
    @classmethod
139
    def setUpClass(cls):
140
        """Initialize kamaki, get (detailed) list of flavors"""
141
        log.info("Getting simple and detailed list of flavors")
142

    
143
        cls.client = ComputeClient(API, TOKEN)
144
        cls.flavors = cls.client.list_flavors()
145
        cls.dflavors = cls.client.list_flavors(detail=True)
146

    
147
    def test_001_list_flavors(self):
148
        """Test flavor list actually returns flavors"""
149
        self.assertGreater(len(self.flavors), 0)
150

    
151
    def test_002_list_flavors_detailed(self):
152
        """Test detailed flavor list is the same length as list"""
153
        self.assertEquals(len(self.dflavors), len(self.flavors))
154

    
155
    def test_003_same_flavor_names(self):
156
        """Test detailed and simple flavor list contain same names"""
157
        names = sorted(map(lambda x: x["name"], self.flavors))
158
        dnames = sorted(map(lambda x: x["name"], self.dflavors))
159
        self.assertEqual(names, dnames)
160

    
161
    def test_004_unique_flavor_names(self):
162
        """Test flavors have unique names"""
163
        names = sorted(map(lambda x: x["name"], self.flavors))
164
        self.assertEqual(sorted(list(set(names))), names)
165

    
166
    def test_005_well_formed_flavor_names(self):
167
        """Test flavors have names of the form CxxRyyDzz
168

169
        Where xx is vCPU count, yy is RAM in MiB, zz is Disk in GiB
170

171
        """
172
        for f in self.dflavors:
173
            self.assertEqual("C%dR%dD%d" % (f["cpu"], f["ram"], f["disk"]),
174
                             f["name"],
175
                             "Flavor %s does not match its specs." % f["name"])
176

    
177

    
178
class ServersTestCase(unittest.TestCase):
179
    """Test server lists for consistency"""
180
    @classmethod
181
    def setUpClass(cls):
182
        """Initialize kamaki, get (detailed) list of servers"""
183
        log.info("Getting simple and detailed list of servers")
184

    
185
        cls.client = ComputeClient(API, TOKEN)
186
        cls.servers = cls.client.list_servers()
187
        cls.dservers = cls.client.list_servers(detail=True)
188

    
189
    def test_001_list_servers(self):
190
        """Test server list actually returns servers"""
191
        self.assertGreater(len(self.servers), 0)
192

    
193
    def test_002_list_servers_detailed(self):
194
        """Test detailed server list is the same length as list"""
195
        self.assertEqual(len(self.dservers), len(self.servers))
196

    
197
    def test_003_same_server_names(self):
198
        """Test detailed and simple flavor list contain same names"""
199
        names = sorted(map(lambda x: x["name"], self.servers))
200
        dnames = sorted(map(lambda x: x["name"], self.dservers))
201
        self.assertEqual(names, dnames)
202

    
203

    
204
# This class gets replicated into actual TestCases dynamically
205
class SpawnServerTestCase(unittest.TestCase):
206
    """Test scenario for server of the specified image"""
207

    
208
    @classmethod
209
    def setUpClass(cls):
210
        """Initialize a kamaki instance"""
211
        log.info("Spawning server for image `%s'", cls.imagename)
212

    
213
        cls.client = ComputeClient(API, TOKEN)
214
        cls.cyclades = CycladesClient(API, TOKEN)
215

    
216
    def _get_ipv4(self, server):
217
        """Get the public IPv4 of a server from the detailed server info"""
218

    
219
        public_addrs = filter(lambda x: x["id"] == "public",
220
                              server["addresses"]["values"])
221
        self.assertEqual(len(public_addrs), 1)
222
        ipv4_addrs = filter(lambda x: x["version"] == 4,
223
                            public_addrs[0]["values"])
224
        self.assertEqual(len(ipv4_addrs), 1)
225
        return ipv4_addrs[0]["addr"]
226

    
227
    def _get_ipv6(self, server):
228
        """Get the public IPv6 of a server from the detailed server info"""
229
        public_addrs = filter(lambda x: x["id"] == "public",
230
                              server["addresses"]["values"])
231
        self.assertEqual(len(public_addrs), 1)
232
        ipv6_addrs = filter(lambda x: x["version"] == 6,
233
                            public_addrs[0]["values"])
234
        self.assertEqual(len(ipv6_addrs), 1)
235
        return ipv6_addrs[0]["addr"]
236

    
237
    def _connect_loginname(self, os):
238
        """Return the login name for connections based on the server OS"""
239
        if os in ("Ubuntu", "Kubuntu", "Fedora"):
240
            return "user"
241
        elif os in ("windows", "windows_alpha1"):
242
            return "Administrator"
243
        else:
244
            return "root"
245

    
246
    def _verify_server_status(self, current_status, new_status):
247
        """Verify a server has switched to a specified status"""
248
        server = self.client.get_server_details(self.serverid)
249
        if server["status"] not in (current_status, new_status):
250
            return None  # Do not raise exception, return so the test fails
251
        self.assertEquals(server["status"], new_status)
252

    
253
    def _get_connected_tcp_socket(self, family, host, port):
254
        """Get a connected socket from the specified family to host:port"""
255
        sock = None
256
        for res in \
257
            socket.getaddrinfo(host, port, family, socket.SOCK_STREAM, 0,
258
                               socket.AI_PASSIVE):
259
            af, socktype, proto, canonname, sa = res
260
            try:
261
                sock = socket.socket(af, socktype, proto)
262
            except socket.error as msg:
263
                sock = None
264
                continue
265
            try:
266
                sock.connect(sa)
267
            except socket.error as msg:
268
                sock.close()
269
                sock = None
270
                continue
271
        self.assertIsNotNone(sock)
272
        return sock
273

    
274
    def _ping_once(self, ipv6, ip):
275
        """Test server responds to a single IPv4 or IPv6 ping"""
276
        cmd = "ping%s -c 2 -w 3 %s" % ("6" if ipv6 else "", ip)
277
        ping = subprocess.Popen(cmd, shell=True,
278
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
279
        (stdout, stderr) = ping.communicate()
280
        ret = ping.wait()
281
        self.assertEquals(ret, 0)
282

    
283
    def _get_hostname_over_ssh(self, hostip, username, password):
284
        ssh = paramiko.SSHClient()
285
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
286
        try:
287
            ssh.connect(hostip, username=username, password=password)
288
        except socket.error:
289
            raise AssertionError
290
        stdin, stdout, stderr = ssh.exec_command("hostname")
291
        lines = stdout.readlines()
292
        self.assertEqual(len(lines), 1)
293
        return lines[0]
294

    
295
    def _try_until_timeout_expires(self, warn_timeout, fail_timeout,
296
                                   opmsg, callable, *args, **kwargs):
297
        if warn_timeout == fail_timeout:
298
            warn_timeout = fail_timeout + 1
299
        warn_tmout = time.time() + warn_timeout
300
        fail_tmout = time.time() + fail_timeout
301
        while True:
302
            self.assertLess(time.time(), fail_tmout,
303
                            "operation `%s' timed out" % opmsg)
304
            if time.time() > warn_tmout:
305
                log.warning("Server %d: `%s' operation `%s' not done yet",
306
                            self.serverid, self.servername, opmsg)
307
            try:
308
                log.info("%s... " % opmsg)
309
                return callable(*args, **kwargs)
310
            except AssertionError:
311
                pass
312
            time.sleep(self.query_interval)
313

    
314
    def _insist_on_tcp_connection(self, family, host, port):
315
        familystr = {socket.AF_INET: "IPv4", socket.AF_INET6: "IPv6",
316
                     socket.AF_UNSPEC: "Unspecified-IPv4/6"}
317
        msg = "connect over %s to %s:%s" % \
318
              (familystr.get(family, "Unknown"), host, port)
319
        sock = self._try_until_timeout_expires(
320
                self.action_timeout, self.action_timeout,
321
                msg, self._get_connected_tcp_socket,
322
                family, host, port)
323
        return sock
324

    
325
    def _insist_on_status_transition(self, current_status, new_status,
326
                                    fail_timeout, warn_timeout=None):
327
        msg = "Server %d: `%s', waiting for %s -> %s" % \
328
              (self.serverid, self.servername, current_status, new_status)
329
        if warn_timeout is None:
330
            warn_timeout = fail_timeout
331
        self._try_until_timeout_expires(warn_timeout, fail_timeout,
332
                                        msg, self._verify_server_status,
333
                                        current_status, new_status)
334
        # Ensure the status is actually the expected one
335
        server = self.client.get_server_details(self.serverid)
336
        self.assertEquals(server["status"], new_status)
337

    
338
    def _insist_on_ssh_hostname(self, hostip, username, password):
339
        msg = "SSH to %s, as %s/%s" % (hostip, username, password)
340
        hostname = self._try_until_timeout_expires(
341
                self.action_timeout, self.action_timeout,
342
                msg, self._get_hostname_over_ssh,
343
                hostip, username, password)
344

    
345
        # The hostname must be of the form 'prefix-id'
346
        self.assertTrue(hostname.endswith("-%d\n" % self.serverid))
347

    
348
    def _check_file_through_ssh(self, hostip, username, password, remotepath, content):
349
        msg = "Trying file injection through SSH to %s, as %s/%s" % (hostip, username, password)
350
        log.info(msg)
351
        try:
352
            ssh = paramiko.SSHClient()
353
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
354
            ssh.connect(hostip, username=username, password=password)
355
        except socket.error:
356
            raise AssertionError
357
        
358
        transport = paramiko.Transport((hostip,22))
359
        transport.connect(username = username, password = password)
360

    
361
        localpath = '/tmp/'+SNF_TEST_PREFIX+'injection'
362
        sftp = paramiko.SFTPClient.from_transport(transport)
363
        sftp.get(remotepath, localpath)
364
        
365
        sftp.close()
366
        transport.close()
367

    
368
        f = open(localpath)
369
        remote_content = b64encode(f.read())
370

    
371
        # Check if files are the same
372
        return (remote_content == content)
373

    
374
    def _skipIf(self, condition, msg):
375
        if condition:
376
            self.skipTest(msg)
377

    
378
    def test_001_submit_create_server(self):
379
        """Test submit create server request"""
380
        server = self.client.create_server(self.servername, self.flavorid,
381
                                           self.imageid, self.personality)
382

    
383
        self.assertEqual(server["name"], self.servername)
384
        self.assertEqual(server["flavorRef"], self.flavorid)
385
        self.assertEqual(server["imageRef"], self.imageid)
386
        self.assertEqual(server["status"], "BUILD")
387

    
388
        # Update class attributes to reflect data on building server
389
        cls = type(self)
390
        cls.serverid = server["id"]
391
        cls.username = None
392
        cls.passwd = server["adminPass"]
393

    
394
    def test_002a_server_is_building_in_list(self):
395
        """Test server is in BUILD state, in server list"""
396
        servers = self.client.list_servers(detail=True)
397
        servers = filter(lambda x: x["name"] == self.servername, servers)
398
        self.assertEqual(len(servers), 1)
399
        server = servers[0]
400
        self.assertEqual(server["name"], self.servername)
401
        self.assertEqual(server["flavorRef"], self.flavorid)
402
        self.assertEqual(server["imageRef"], self.imageid)
403
        self.assertEqual(server["status"], "BUILD")
404

    
405
    def test_002b_server_is_building_in_details(self):
406
        """Test server is in BUILD state, in details"""
407
        server = self.client.get_server_details(self.serverid)
408
        self.assertEqual(server["name"], self.servername)
409
        self.assertEqual(server["flavorRef"], self.flavorid)
410
        self.assertEqual(server["imageRef"], self.imageid)
411
        self.assertEqual(server["status"], "BUILD")
412

    
413
    def test_002c_set_server_metadata(self):
414
        image = self.client.get_image_details(self.imageid)
415
        os = image["metadata"]["values"]["os"]
416
        loginname = image["metadata"]["values"].get("users", None)
417
        self.client.update_server_metadata(self.serverid, OS=os)
418

    
419
        # Determine the username to use for future connections
420
        # to this host
421
        cls = type(self)
422
        cls.username = loginname
423
        if not cls.username:
424
            cls.username = self._connect_loginname(os)
425
        self.assertIsNotNone(cls.username)
426

    
427
    def test_002d_verify_server_metadata(self):
428
        """Test server metadata keys are set based on image metadata"""
429
        servermeta = self.client.get_server_metadata(self.serverid)
430
        imagemeta = self.client.get_image_metadata(self.imageid)
431
        self.assertEqual(servermeta["OS"], imagemeta["os"])
432

    
433
    def test_003_server_becomes_active(self):
434
        """Test server becomes ACTIVE"""
435
        self._insist_on_status_transition("BUILD", "ACTIVE",
436
                                         self.build_fail, self.build_warning)
437

    
438
    def test_003a_get_server_oob_console(self):
439
        """Test getting OOB server console over VNC
440

441
        Implementation of RFB protocol follows
442
        http://www.realvnc.com/docs/rfbproto.pdf.
443

444
        """
445
        
446
        console = self.cyclades.get_server_console(self.serverid)
447
        self.assertEquals(console['type'], "vnc")
448
        sock = self._insist_on_tcp_connection(socket.AF_UNSPEC,
449
                                        console["host"], console["port"])
450

    
451
        # Step 1. ProtocolVersion message (par. 6.1.1)
452
        version = sock.recv(1024)
453
        self.assertEquals(version, 'RFB 003.008\n')
454
        sock.send(version)
455

    
456
        # Step 2. Security (par 6.1.2): Only VNC Authentication supported
457
        sec = sock.recv(1024)
458
        self.assertEquals(list(sec), ['\x01', '\x02'])
459

    
460
        # Step 3. Request VNC Authentication (par 6.1.2)
461
        sock.send('\x02')
462

    
463
        # Step 4. Receive Challenge (par 6.2.2)
464
        challenge = sock.recv(1024)
465
        self.assertEquals(len(challenge), 16)
466

    
467
        # Step 5. DES-Encrypt challenge, use password as key (par 6.2.2)
468
        response = d3des_generate_response(
469
            (console["password"] + '\0' * 8)[:8], challenge)
470
        sock.send(response)
471

    
472
        # Step 6. SecurityResult (par 6.1.3)
473
        result = sock.recv(4)
474
        self.assertEquals(list(result), ['\x00', '\x00', '\x00', '\x00'])
475
        sock.close()
476
        
477
    def test_004_server_has_ipv4(self):
478
        """Test active server has a valid IPv4 address"""
479
        server = self.client.get_server_details(self.serverid)
480
        ipv4 = self._get_ipv4(server)
481
        self.assertEquals(IP(ipv4).version(), 4)
482

    
483
    def test_005_server_has_ipv6(self):
484
        """Test active server has a valid IPv6 address"""
485
        server = self.client.get_server_details(self.serverid)
486
        ipv6 = self._get_ipv6(server)
487
        self.assertEquals(IP(ipv6).version(), 6)
488

    
489
    def test_006_server_responds_to_ping_IPv4(self):
490
        """Test server responds to ping on IPv4 address"""
491
        server = self.client.get_server_details(self.serverid)
492
        ip = self._get_ipv4(server)
493
        self._try_until_timeout_expires(self.action_timeout,
494
                                        self.action_timeout,
495
                                        "PING IPv4 to %s" % ip,
496
                                        self._ping_once,
497
                                        False, ip)
498

    
499
    def test_007_server_responds_to_ping_IPv6(self):
500
        """Test server responds to ping on IPv6 address"""
501
        server = self.client.get_server_details(self.serverid)
502
        ip = self._get_ipv6(server)
503
        self._try_until_timeout_expires(self.action_timeout,
504
                                        self.action_timeout,
505
                                        "PING IPv6 to %s" % ip,
506
                                        self._ping_once,
507
                                        True, ip)
508

    
509
    def test_008_submit_shutdown_request(self):
510
        """Test submit request to shutdown server"""
511
        self.cyclades.shutdown_server(self.serverid)
512

    
513
    def test_009_server_becomes_stopped(self):
514
        """Test server becomes STOPPED"""
515
        self._insist_on_status_transition("ACTIVE", "STOPPED",
516
                                         self.action_timeout,
517
                                         self.action_timeout)
518

    
519
    def test_010_submit_start_request(self):
520
        """Test submit start server request"""
521
        self.cyclades.start_server(self.serverid)
522

    
523
    def test_011_server_becomes_active(self):
524
        """Test server becomes ACTIVE again"""
525
        self._insist_on_status_transition("STOPPED", "ACTIVE",
526
                                         self.action_timeout,
527
                                         self.action_timeout)
528

    
529
    def test_011a_server_responds_to_ping_IPv4(self):
530
        """Test server OS is actually up and running again"""
531
        self.test_006_server_responds_to_ping_IPv4()
532

    
533
    def test_012_ssh_to_server_IPv4(self):
534
        """Test SSH to server public IPv4 works, verify hostname"""
535
        self._skipIf(self.is_windows, "only valid for Linux servers")
536
        server = self.client.get_server_details(self.serverid)
537
        self._insist_on_ssh_hostname(self._get_ipv4(server),
538
                                     self.username, self.passwd)
539

    
540
    def test_013_ssh_to_server_IPv6(self):
541
        """Test SSH to server public IPv6 works, verify hostname"""
542
        self._skipIf(self.is_windows, "only valid for Linux servers")
543
        server = self.client.get_server_details(self.serverid)
544
        self._insist_on_ssh_hostname(self._get_ipv6(server),
545
                                     self.username, self.passwd)
546

    
547
    def test_014_rdp_to_server_IPv4(self):
548
        "Test RDP connection to server public IPv4 works"""
549
        self._skipIf(not self.is_windows, "only valid for Windows servers")
550
        server = self.client.get_server_details(self.serverid)
551
        ipv4 = self._get_ipv4(server)
552
        sock = _insist_on_tcp_connection(socket.AF_INET, ipv4, 3389)
553

    
554
        # No actual RDP processing done. We assume the RDP server is there
555
        # if the connection to the RDP port is successful.
556
        # FIXME: Use rdesktop, analyze exit code? see manpage [costasd]
557
        sock.close()
558

    
559
    def test_015_rdp_to_server_IPv6(self):
560
        "Test RDP connection to server public IPv6 works"""
561
        self._skipIf(not self.is_windows, "only valid for Windows servers")
562
        server = self.client.get_server_details(self.serverid)
563
        ipv6 = self._get_ipv6(server)
564
        sock = _get_tcp_connection(socket.AF_INET6, ipv6, 3389)
565

    
566
        # No actual RDP processing done. We assume the RDP server is there
567
        # if the connection to the RDP port is successful.
568
        sock.close()
569

    
570
    def test_016_personality_is_enforced(self):
571
        """Test file injection for personality enforcement"""
572
        self._skipIf(self.is_windows, "only implemented for Linux servers")
573
        self._skipIf(self.personality == None, "No personality file selected")
574

    
575
        server = self.client.get_server_details(self.serverid)
576

    
577
        for inj_file in self.personality:
578
            equal_files = self._check_file_through_ssh(self._get_ipv4(server), inj_file['owner'], 
579
                                                       self.passwd, inj_file['path'], inj_file['contents'])
580
            self.assertTrue(equal_files)
581
        
582

    
583
    def test_017_submit_delete_request(self):
584
        """Test submit request to delete server"""
585
        self.client.delete_server(self.serverid)
586

    
587
    def test_018_server_becomes_deleted(self):
588
        """Test server becomes DELETED"""
589
        self._insist_on_status_transition("ACTIVE", "DELETED",
590
                                         self.action_timeout,
591
                                         self.action_timeout)
592

    
593
    def test_019_server_no_longer_in_server_list(self):
594
        """Test server is no longer in server list"""
595
        servers = self.client.list_servers()
596
        self.assertNotIn(self.serverid, [s["id"] for s in servers])
597

    
598

    
599
class NetworkTestCase(unittest.TestCase):
600
    """ Testing networking in cyclades """
601
  
602
    @classmethod
603
    def setUpClass(cls):
604
        "Initialize kamaki, get list of current networks"
605

    
606
        cls.client = CycladesClient(API, TOKEN)
607
        cls.compute = ComputeClient(API, TOKEN)
608

    
609
        images = cls.compute.list_images(detail = True)
610
        flavors = cls.compute.list_flavors(detail = True)
611

    
612
        cls.imageid = choice([im['id'] for im in images if not im['name'].lower().find("windows") >= 0])
613
        cls.flavorid = choice([f['id'] for f in flavors if f['disk'] >= 20])
614

    
615
        for image in images:
616
            if image['id'] == cls.imageid:
617
                imagename = image['name']
618

    
619
        cls.servername = "%s%s for %s" % (SNF_TEST_PREFIX, TEST_RUN_ID, imagename)
620

    
621
        #Dictionary initialization for the vms credentials
622

    
623
        cls.serverid = dict()
624
        cls.username = dict()
625
        cls.password = dict()
626

    
627
    def _get_ipv4(self, server):
628
    
629
        """Get the public IPv4 of a server from the detailed server info"""
630

    
631
        public_addrs = filter(lambda x: x["id"] == "public",
632
                              server["addresses"]["values"])
633
        self.assertEqual(len(public_addrs), 1)
634
        ipv4_addrs = filter(lambda x: x["version"] == 4,
635
                            public_addrs[0]["values"])
636
        self.assertEqual(len(ipv4_addrs), 1)
637
        return ipv4_addrs[0]["addr"]
638

    
639

    
640
    def _connect_loginname(self, os):
641
        """Return the login name for connections based on the server OS"""
642
        if os in ("Ubuntu", "Kubuntu", "Fedora"):
643
            return "user"
644
        elif os in ("windows", "windows_alpha1"):
645
            return "Administrator"
646
        else:
647
            return "root"
648

    
649

    
650
    def test_00001_submit_create_server_A(self):
651
        """Test submit create server request"""
652
        serverA = self.client.create_server(self.servername, self.flavorid,
653
                                           self.imageid, personality=None)
654

    
655
        self.assertEqual(serverA["name"], self.servername)
656
        self.assertEqual(serverA["flavorRef"], self.flavorid)
657
        self.assertEqual(serverA["imageRef"], self.imageid)
658
        self.assertEqual(serverA["status"], "BUILD")
659

    
660
        # Update class attributes to reflect data on building server
661
        self.serverid['A'] = serverA["id"]
662
        self.username['A'] = None
663
        self.password['A'] = serverA["adminPass"]
664

    
665

    
666
    def test_00001_serverA_becomes_active(self):
667
        """Test server becomes ACTIVE"""
668

    
669
        fail_tmout = time.time()+self.action_timeout
670
        while True:
671
            d = self.client.get_server_details(self.serverid['A'])
672
            status = d['status']
673
            if status == 'ACTIVE':
674
                active = True
675
                break
676
            elif time.time() > fail_tmout:
677
                self.assertLess(time.time(), fail_tmout)
678
            else:
679
                time.sleep(self.query_interval)
680

    
681
        self.assertTrue(active)
682

    
683
        
684
    
685
    def test_00002_submit_create_server_B(self):
686
        """Test submit create server request"""
687
        serverB = self.client.create_server(self.servername, self.flavorid,
688
                                           self.imageid, personality=None)
689

    
690
        self.assertEqual(serverB["name"], self.servername)
691
        self.assertEqual(serverB["flavorRef"], self.flavorid)
692
        self.assertEqual(serverB["imageRef"], self.imageid)
693
        self.assertEqual(serverB["status"], "BUILD")
694

    
695
        # Update class attributes to reflect data on building server
696
        self.serverid['B'] = serverB["id"]
697
        self.username['B'] = None
698
        self.password['B'] = serverB["adminPass"]
699

    
700

    
701
        def test_00002_serverB_becomes_active(self):
702
        """Test server becomes ACTIVE"""
703

    
704
        fail_tmout = time.time()+self.action_timeout
705
        while True:
706
            d = self.client.get_server_details(self.serverid['B'])
707
            status = d['status']
708
            if status == 'ACTIVE':
709
                active = True
710
                break
711
            elif time.time() > fail_tmout:
712
                self.assertLess(time.time(), fail_tmout)
713
            else:
714
                time.sleep(self.query_interval)
715

    
716
        self.assertTrue(active)
717

    
718

    
719
    def test_001_create_network(self):
720
        """Test submit create network request"""
721
        name = SNF_TEST_PREFIX+TEST_RUN_ID
722
        previous_num = len(self.client.list_networks())
723
        network =  self.client.create_network(name)        
724
       
725
        #Test if right name is assigned
726
        self.assertEqual(network['name'], name)
727
        
728
        # Update class attributes
729
        cls = type(self)
730
        cls.networkid = network['id']
731
        networks = self.client.list_networks()
732

    
733
        #Test if new network is created
734
        self.assertTrue(len(networks) > previous_num)
735
        
736
    
737
    def test_002_connect_to_network(self):
738
        """Test connect VM to network"""
739

    
740
        self.client.connect_server(self.serverid['A'], self.networkid)
741
        self.client.connect_server(self.serverid['B'], self.networkid)
742
                
743
        #Insist on connecting until action timeout
744
        fail_tmout = time.time()+self.action_timeout
745

    
746
        while True:
747
            connected = (self.client.get_network_details(self.networkid))
748
            connections = connected['servers']['values']
749
            if (self.serverid['A'] in connections) and (self.serverid['B'] in connections):
750
                conn_exists = True
751
                break
752
            elif time.time() > fail_tmout:
753
                self.assertLess(time.time(), fail_tmout)
754
            else:
755
                time.sleep(self.query_interval)
756

    
757
        self.assertTrue(conn_exists)
758
            
759

    
760
    def test_002a_setup_interface_A(self):
761

    
762
        server = self.client.get_server_details(self.serverid['A'])
763
        image = self.client.get_image_details(self.imageid)
764
        os = image['metadata']['values']['os']
765
        loginname = image["metadata"]["values"].get("users", None)
766
        hostip = self._get_ipv4(server) 
767
        
768
        if not loginname:
769
            loginname = self._connect_loginname(os)
770

    
771
        try:
772
            ssh = paramiko.SSHClient()
773
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
774
            ssh.connect(hostip, username = loginname, password = self.password['A'])
775
        except socket.error:
776
            raise AssertionError
777

    
778
        stdin, stdout, stderr = ssh.exec_command("ifconfig eth1 %s up"%("192.168.0.42"))
779
        lines = stdout.readlines()
780

    
781
        self.assertEqual(len(lines), 0)
782
        
783

    
784
    def test_002a_setup_interface_B(self):
785

    
786
        server = self.client.get_server_details(self.serverid['B'])
787
        image = self.client.get_image_details(self.imageid)
788
        os = image['metadata']['values']['os']
789
        loginname = image["metadata"]["values"].get("users", None)
790
        hostip = self._get_ipv4(server) 
791
        
792
        if not loginname:
793
            loginname = self._connect_loginname(os)
794

    
795
        try:
796
            ssh = paramiko.SSHClient()
797
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
798
            ssh.connect(hostip, username = loginname, password = self.password['B'])
799
        except socket.error:
800
            raise AssertionError
801

    
802
        stdin, stdout, stderr = ssh.exec_command("ifconfig eth1 %s up"%("192.168.0.43"))
803
        lines = stdout.readlines()
804

    
805
        self.assertEqual(len(lines), 0)
806

    
807

    
808

    
809
    def test_002b_test_connection_exists(self):
810
        """Ping serverB from serverA to test if connection exists"""
811

    
812
        server = self.client.get_server_details(self.serverid['A'])
813
        image = self.client.get_image_details(self.imageid)
814
        os = image['metadata']['values']['os']
815
        loginname = image["metadata"]["values"].get("users", None)
816
        
817
        hostip = self._get_ipv4(server)
818
        
819
        if not loginname:
820
            loginname = self._connect_loginname(os)
821

    
822
        try:
823
            ssh = paramiko.SSHClient()
824
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
825
            ssh.connect(hostip, username = loginname, password = self.password['A'])
826
        except socket.error:
827
            raise AssertionError
828

    
829
        cmd = "if ping -c 2 -w 3 %s >/dev/null; then echo \"True\"; fi;" % ("192.168.0.43")
830
        stdin, stdout, stderr = ssh.exec_command(cmd)
831
        lines = stdout.readlines()
832

    
833
        for i in lines:
834
            if i=='True\n':
835
                exists = True
836

    
837
        self.assertTrue(exists)
838

    
839

    
840

    
841
    def test_003_disconnect_from_network(self):
842
        prev_state = self.client.get_network_details(self.networkid)
843
        prev_conn = len(prev_state['servers']['values'])
844

    
845
        self.client.disconnect_server(self.serverid['A'], self.networkid)
846
        self.client.disconnect_server(self.serverid['B'], self.networkid)
847

    
848
        #Insist on deleting until action timeout
849
        fail_tmout = time.time()+self.action_timeout
850

    
851
        while True:
852
            connected = (self.client.get_network_details(self.networkid))
853
            connections = connected['servers']['values']
854
            if (self.serverid['A'] not in connections) and (self.serverid['B'] not in connections):
855
                conn_exists = False
856
                break
857
            elif time.time() > fail_tmout:
858
                self.assertLess(time.time(), fail_tmout)
859
            else:
860
                time.sleep(self.query_interval)
861

    
862
        self.assertFalse(conn_exists)
863

    
864
    def test_004_destroy_network(self):
865
        """Test submit delete network request"""
866
        self.client.delete_network(self.networkid)        
867
        networks = self.client.list_networks()
868

    
869
        curr_net = []
870
        for net in networks:
871
            curr_net.append(net['id'])
872

    
873
        self.assertTrue(self.networkid not in curr_net)
874
        
875
    def test_005_cleanup_servers(self):
876
        """Cleanup servers created for this test"""
877
        self.compute.delete_server(self.serverid['A'])
878
        self.compute.delete_server(self.serverid['B'])
879

    
880
        fail_tmout = time.time()+self.action_timeout
881

    
882
        #Ensure server gets deleted
883
        status = dict() 
884

    
885
        while True:
886
            details = self.compute.get_server_details(self.serverid['A'])
887
            status['A'] = details['status']
888
            details = self.compute.get_server_details(self.serverid['B'])
889
            status['B'] = details['status']
890
            if (status['A'] == 'DELETED') and (status['B'] == 'DELETED'):
891
                deleted = True
892
                break
893
            elif time.time() > fail_tmout: 
894
                self.assertLess(time.time(), fail_tmout)
895
            else:
896
                time.sleep(self.query_interval)
897

    
898
        self.assertTrue(deleted)
899

    
900
class TestRunnerProcess(Process):
901
    """A distinct process used to execute part of the tests in parallel"""
902
    def __init__(self, **kw):
903
        Process.__init__(self, **kw)
904
        kwargs = kw["kwargs"]
905
        self.testq = kwargs["testq"]
906
        self.runner = kwargs["runner"]
907

    
908
    def run(self):
909
        # Make sure this test runner process dies with the parent
910
        # and is not left behind.
911
        #
912
        # WARNING: This uses the prctl(2) call and is
913
        # Linux-specific.
914
        prctl.set_pdeathsig(signal.SIGHUP)
915

    
916
        while True:
917
            log.debug("I am process %d, GETting from queue is %s",
918
                     os.getpid(), self.testq)
919
            msg = self.testq.get()
920
            log.debug("Dequeued msg: %s", msg)
921

    
922
            if msg == "TEST_RUNNER_TERMINATE":
923
                raise SystemExit
924
            elif issubclass(msg, unittest.TestCase):
925
                # Assemble a TestSuite, and run it
926
                suite = unittest.TestLoader().loadTestsFromTestCase(msg)
927
                self.runner.run(suite)
928
            else:
929
                raise Exception("Cannot handle msg: %s" % msg)
930

    
931

    
932

    
933
def _run_cases_in_parallel(cases, fanout=1, runner=None):
934
    """Run instances of TestCase in parallel, in a number of distinct processes
935

936
    The cases iterable specifies the TestCases to be executed in parallel,
937
    by test runners running in distinct processes.
938
    The fanout parameter specifies the number of processes to spawn,
939
    and defaults to 1.
940
    The runner argument specifies the test runner class to use inside each
941
    runner process.
942

943
    """
944
    if runner is None:
945
        runner = unittest.TextTestRunner(verbosity=2, failfast=True)
946

    
947
    # testq: The master process enqueues TestCase objects into this queue,
948
    #        test runner processes pick them up for execution, in parallel.
949
    testq = Queue()
950
    runners = []
951
    for i in xrange(0, fanout):
952
        kwargs = dict(testq=testq, runner=runner)
953
        runners.append(TestRunnerProcess(kwargs=kwargs))
954

    
955
    log.info("Spawning %d test runner processes", len(runners))
956
    for p in runners:
957
        p.start()
958
    log.debug("Spawned %d test runners, PIDs are %s",
959
              len(runners), [p.pid for p in runners])
960

    
961
    # Enqueue test cases
962
    map(testq.put, cases)
963
    map(testq.put, ["TEST_RUNNER_TERMINATE"] * len(runners))
964

    
965
    log.debug("Joining %d processes", len(runners))
966
    for p in runners:
967
        p.join()
968
    log.debug("Done joining %d processes", len(runners))
969

    
970

    
971
def _spawn_server_test_case(**kwargs):
972
    """Construct a new unit test case class from SpawnServerTestCase"""
973

    
974
    name = "SpawnServerTestCase_%s" % kwargs["imageid"]
975
    cls = type(name, (SpawnServerTestCase,), kwargs)
976

    
977
    # Patch extra parameters into test names by manipulating method docstrings
978
    for (mname, m) in \
979
        inspect.getmembers(cls, lambda x: inspect.ismethod(x)):
980
            if hasattr(m, __doc__):
981
                m.__func__.__doc__ = "[%s] %s" % (imagename, m.__doc__)
982

    
983
    # Make sure the class can be pickled, by listing it among
984
    # the attributes of __main__. A PicklingError is raised otherwise.
985
    setattr(__main__, name, cls)
986
    return cls 
987

    
988
def _spawn_network_test_case(**kwargs):
989
    """Construct a new unit test case class from NetworkTestCase"""
990

    
991
    name = "NetworkTestCase"+TEST_RUN_ID
992
    cls = type(name, (NetworkTestCase,), kwargs)
993

    
994
    # Make sure the class can be pickled, by listing it among
995
    # the attributes of __main__. A PicklingError is raised otherwise.
996
    setattr(__main__, name, cls)
997
    return cls 
998

    
999

    
1000
def cleanup_servers(delete_stale=False):
1001

    
1002
    c = ComputeClient(API, TOKEN)
1003

    
1004
    servers = c.list_servers()
1005
    stale = [s for s in servers if s["name"].startswith(SNF_TEST_PREFIX)]
1006

    
1007
    if len(stale) == 0:
1008
        return
1009

    
1010
    print >> sys.stderr, "Found these stale servers from previous runs:"
1011
    print "    " + \
1012
          "\n    ".join(["%d: %s" % (s["id"], s["name"]) for s in stale])
1013

    
1014
    if delete_stale:
1015
        print >> sys.stderr, "Deleting %d stale servers:" % len(stale)
1016
        for server in stale:
1017
            c.delete_server(server["id"])
1018
        print >> sys.stderr, "    ...done"
1019
    else:
1020
        print >> sys.stderr, "Use --delete-stale to delete them."
1021

    
1022

    
1023
def parse_arguments(args):
1024
    from optparse import OptionParser
1025

    
1026
    kw = {}
1027
    kw["usage"] = "%prog [options]"
1028
    kw["description"] = \
1029
        "%prog runs a number of test scenarios on a " \
1030
        "Synnefo deployment."
1031

    
1032
    parser = OptionParser(**kw)
1033
    parser.disable_interspersed_args()
1034
    parser.add_option("--api",
1035
                      action="store", type="string", dest="api",
1036
                      help="The API URI to use to reach the Synnefo API",
1037
                      default=DEFAULT_API)
1038
    parser.add_option("--token",
1039
                      action="store", type="string", dest="token",
1040
                      help="The token to use for authentication to the API")
1041
    parser.add_option("--nofailfast",
1042
                      action="store_true", dest="nofailfast",
1043
                      help="Do not fail immediately if one of the tests " \
1044
                           "fails (EXPERIMENTAL)",
1045
                      default=False)
1046
    parser.add_option("--action-timeout",
1047
                      action="store", type="int", dest="action_timeout",
1048
                      metavar="TIMEOUT",
1049
                      help="Wait SECONDS seconds for a server action to " \
1050
                           "complete, then the test is considered failed",
1051
                      default=100)
1052
    parser.add_option("--build-warning",
1053
                      action="store", type="int", dest="build_warning",
1054
                      metavar="TIMEOUT",
1055
                      help="Warn if TIMEOUT seconds have passed and a " \
1056
                           "build operation is still pending",
1057
                      default=600)
1058
    parser.add_option("--build-fail",
1059
                      action="store", type="int", dest="build_fail",
1060
                      metavar="BUILD_TIMEOUT",
1061
                      help="Fail the test if TIMEOUT seconds have passed " \
1062
                           "and a build operation is still incomplete",
1063
                      default=900)
1064
    parser.add_option("--query-interval",
1065
                      action="store", type="int", dest="query_interval",
1066
                      metavar="INTERVAL",
1067
                      help="Query server status when requests are pending " \
1068
                           "every INTERVAL seconds",
1069
                      default=3)
1070
    parser.add_option("--fanout",
1071
                      action="store", type="int", dest="fanout",
1072
                      metavar="COUNT",
1073
                      help="Spawn up to COUNT child processes to execute " \
1074
                           "in parallel, essentially have up to COUNT " \
1075
                           "server build requests outstanding (EXPERIMENTAL)",
1076
                      default=1)
1077
    parser.add_option("--force-flavor",
1078
                      action="store", type="int", dest="force_flavorid",
1079
                      metavar="FLAVOR ID",
1080
                      help="Force all server creations to use the specified "\
1081
                           "FLAVOR ID instead of a randomly chosen one, " \
1082
                           "useful if disk space is scarce",
1083
                      default=None)
1084
    parser.add_option("--image-id",
1085
                      action="store", type="string", dest="force_imageid",
1086
                      metavar="IMAGE ID",
1087
                      help="Test the specified image id, use 'all' to test " \
1088
                           "all available images (mandatory argument)",
1089
                      default=None)
1090
    parser.add_option("--show-stale",
1091
                      action="store_true", dest="show_stale",
1092
                      help="Show stale servers from previous runs, whose "\
1093
                           "name starts with `%s'" % SNF_TEST_PREFIX,
1094
                      default=False)
1095
    parser.add_option("--delete-stale",
1096
                      action="store_true", dest="delete_stale",
1097
                      help="Delete stale servers from previous runs, whose "\
1098
                           "name starts with `%s'" % SNF_TEST_PREFIX,
1099
                      default=False)
1100
    parser.add_option("--force-personality",
1101
                      action="store", type="string", dest="personality_path",
1102
                      help="Force a personality file injection. File path required. ",
1103
                      default=None)
1104
    
1105

    
1106
    # FIXME: Change the default for build-fanout to 10
1107
    # FIXME: Allow the user to specify a specific set of Images to test
1108

    
1109
    (opts, args) = parser.parse_args(args)
1110

    
1111
    # Verify arguments
1112
    if opts.delete_stale:
1113
        opts.show_stale = True
1114

    
1115
    if not opts.show_stale:
1116
        if not opts.force_imageid:
1117
            print >>sys.stderr, "The --image-id argument is mandatory."
1118
            parser.print_help()
1119
            sys.exit(1)
1120

    
1121
        if opts.force_imageid != 'all':
1122
            try:
1123
                opts.force_imageid = str(opts.force_imageid)
1124
            except ValueError:
1125
                print >>sys.stderr, "Invalid value specified for --image-id." \
1126
                                    "Use a valid id, or `all'."
1127
                sys.exit(1)
1128

    
1129
    return (opts, args)
1130

    
1131

    
1132
def main():
1133
    """Assemble test cases into a test suite, and run it
1134

1135
    IMPORTANT: Tests have dependencies and have to be run in the specified
1136
    order inside a single test case. They communicate through attributes of the
1137
    corresponding TestCase class (shared fixtures). Distinct subclasses of
1138
    TestCase MAY SHARE NO DATA, since they are run in parallel, in distinct
1139
    test runner processes.
1140

1141
    """
1142
    (opts, args) = parse_arguments(sys.argv[1:])
1143

    
1144
    global API, TOKEN
1145
    API = opts.api
1146
    TOKEN = opts.token
1147

    
1148
    # Cleanup stale servers from previous runs
1149
    if opts.show_stale:
1150
        cleanup_servers(delete_stale=opts.delete_stale)
1151
        return 0
1152

    
1153
    # Initialize a kamaki instance, get flavors, images
1154

    
1155
    c = ComputeClient(API, TOKEN)
1156

    
1157
    DIMAGES = c.list_images(detail=True)
1158
    DFLAVORS = c.list_flavors(detail=True)
1159

    
1160
    # FIXME: logging, log, LOG PID, TEST_RUN_ID, arguments
1161
    # Run them: FIXME: In parallel, FAILEARLY, catchbreak?
1162
    #unittest.main(verbosity=2, catchbreak=True)
1163

    
1164
    if opts.force_imageid == 'all':
1165
        test_images = DIMAGES
1166
    else:
1167
        test_images = filter(lambda x: x["id"] == opts.force_imageid, DIMAGES)
1168

    
1169
    for image in test_images:
1170
        imageid = str(image["id"])
1171
        flavorid = choice([f["id"] for f in DFLAVORS if f["disk"] >= 20])
1172
        imagename = image["name"]
1173
        
1174
        #Personality dictionary for file injection test
1175
        if opts.personality_path != None:
1176
            f = open(opts.personality_path)
1177
            content = b64encode(f.read())
1178
            personality = []
1179
            st = os.stat(opts.personality_path)
1180
            personality.append({
1181
                    'path': '/root/test_inj_file',
1182
                    'owner': 'root',
1183
                    'group': 'root',
1184
                    'mode': 0x7777 & st.st_mode,
1185
                    'contents': content
1186
                    })
1187
        else:
1188
            personality = None
1189

    
1190
        servername = "%s%s for %s" % (SNF_TEST_PREFIX, TEST_RUN_ID, imagename)
1191
        is_windows = imagename.lower().find("windows") >= 0
1192
        
1193
    ServerTestCase = _spawn_server_test_case(imageid=imageid, flavorid=flavorid,
1194
                                             imagename=imagename,
1195
                                             personality=personality,
1196
                                             servername=servername,
1197
                                             is_windows=is_windows,
1198
                                             action_timeout=opts.action_timeout,
1199
                                             build_warning=opts.build_warning,
1200
                                             build_fail=opts.build_fail,
1201
                                             query_interval=opts.query_interval,
1202
                                             )
1203

    
1204

    
1205
    #Running all the testcases sequentially
1206
    
1207
    #To run all cases
1208
    #seq_cases = [UnauthorizedTestCase, FlavorsTestCase, ImagesTestCase, ServerTestCase, NetworkTestCase]
1209
    
1210
    newNetworkTestCase = _spawn_network_test_case(action_timeout = opts.action_timeout,
1211
                                                  query_interval = opts.query_interval)    
1212
    seq_cases = [newNetworkTestCase]
1213

    
1214
    for case in seq_cases:
1215
        suite = unittest.TestLoader().loadTestsFromTestCase(case)
1216
        unittest.TextTestRunner(verbosity=2).run(suite)
1217
        
1218
    
1219

    
1220
    # # The Following cases run sequentially
1221
    # seq_cases = [UnauthorizedTestCase, FlavorsTestCase, ImagesTestCase]
1222
    # _run_cases_in_parallel(seq_cases, fanout=3, runner=runner)
1223

    
1224
    # # The following cases run in parallel
1225
    # par_cases = []
1226

    
1227
    # if opts.force_imageid == 'all':
1228
    #     test_images = DIMAGES
1229
    # else:
1230
    #     test_images = filter(lambda x: x["id"] == opts.force_imageid, DIMAGES)
1231

    
1232
    # for image in test_images:
1233
    #     imageid = image["id"]
1234
    #     imagename = image["name"]
1235
    #     if opts.force_flavorid:
1236
    #         flavorid = opts.force_flavorid
1237
    #     else:
1238
    #         flavorid = choice([f["id"] for f in DFLAVORS if f["disk"] >= 20])
1239
    #     personality = None   # FIXME
1240
    #     servername = "%s%s for %s" % (SNF_TEST_PREFIX, TEST_RUN_ID, imagename)
1241
    #     is_windows = imagename.lower().find("windows") >= 0
1242
    #     case = _spawn_server_test_case(imageid=str(imageid), flavorid=flavorid,
1243
    #                                    imagename=imagename,
1244
    #                                    personality=personality,
1245
    #                                    servername=servername,
1246
    #                                    is_windows=is_windows,
1247
    #                                    action_timeout=opts.action_timeout,
1248
    #                                    build_warning=opts.build_warning,
1249
    #                                    build_fail=opts.build_fail,
1250
    #                                    query_interval=opts.query_interval)
1251
    #     par_cases.append(case)
1252

    
1253
    # _run_cases_in_parallel(par_cases, fanout=opts.fanout, runner=runner)
1254

    
1255
if __name__ == "__main__":
1256
    sys.exit(main())