Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / livetest / cyclades.py @ f5f2dc53

History | View | Annotate | Download (27.4 kB)

1
# Copyright 2012-2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import time
35

    
36
from kamaki.clients import livetest, ClientError
37
from kamaki.clients.cyclades import CycladesClient
38

    
39

    
40
class Cyclades(livetest.Generic):
41
    """Set up a Cyclades thorough test"""
42
    def setUp(self):
43
        print
44
        with open(self['image', 'details']) as f:
45
            self.img_details = eval(f.read())
46
        self.img = self.img_details['id']
47
        with open(self['flavor', 'details']) as f:
48
            self._flavor_details = eval(f.read())
49
        self.PROFILES = ('ENABLED', 'DISABLED', 'PROTECTED')
50

    
51
        self.servers = {}
52
        self.now = time.mktime(time.gmtime())
53
        self.servname1 = 'serv' + unicode(self.now)
54
        self.servname2 = self.servname1 + '_v2'
55
        self.servname1 += '_v1'
56
        self.flavorid = 1
57
        #servers have to be created at the begining...
58
        self.networks = {}
59
        self.netname1 = 'net' + unicode(self.now)
60
        self.netname2 = 'net' + unicode(self.now) + '_v2'
61

    
62
        self.client = CycladesClient(self['compute', 'url'], self['token'])
63

    
64
    def tearDown(self):
65
        """Destoy servers used in testing"""
66
        for net in self.networks.keys():
67
            self._delete_network(net)
68
        for server in self.servers.values():
69
            self._delete_server(server['id'])
70
            print('DEL VM %s (%s)' % (server['id'], server['name']))
71

    
72
    def test_000(self):
73
        "Prepare a full Cyclades test scenario"
74
        self.server1 = self._create_server(
75
            self.servname1,
76
            self.flavorid,
77
            self.img)
78
        self.server2 = self._create_server(
79
            self.servname2,
80
            self.flavorid + 2,
81
            self.img)
82
        super(self.__class__, self).test_000()
83

    
84
    def _create_server(self, servername, flavorid, imageid, personality=None):
85
        server = self.client.create_server(
86
            servername,
87
            flavorid,
88
            imageid,
89
            personality)
90
        print('CREATE VM %s (%s)' % (server['id'], server['name']))
91
        self.servers[servername] = server
92
        return server
93

    
94
    def _delete_server(self, servid):
95
        try:
96
            current_state = self.client.get_server_details(servid)
97
            current_state = current_state['status']
98
            if current_state == 'DELETED':
99
                return
100
            self.client.delete_server(servid)
101
            self._wait_for_status(servid, current_state)
102
            self.client.delete_server(servid)
103
        except:
104
            return
105

    
106
    def _create_network(self, netname, **kwargs):
107
        net = self.client.create_network(netname, **kwargs)
108
        self.networks[net['id']] = net
109
        return net
110

    
111
    def _delete_network(self, netid):
112
        if not netid in self.networks:
113
            return None
114
        print('Disconnect nics of network %s' % netid)
115
        self.client.disconnect_network_nics(netid)
116

    
117
        def netwait(wait):
118
            try:
119
                self.client.delete_network(netid)
120
            except ClientError:
121
                time.sleep(wait)
122
        self.do_with_progress_bar(
123
            netwait,
124
            'Delete network %s' % netid,
125
            self._waits[:7])
126
        return self.networks.pop(netid)
127

    
128
    def _wait_for_network(self, netid, status):
129

    
130
        def netwait(wait):
131
            r = self.client.get_network_details(netid)
132
            if r['status'] == status:
133
                return
134
            time.sleep(wait)
135
        self.do_with_progress_bar(
136
            netwait,
137
            'Wait network %s to reach status %s' % (netid, status),
138
            self._waits[:5])
139

    
140
    def _wait_for_nic(self, netid, servid, in_creation=True):
141
        self._wait_for_network(netid, 'ACTIVE')
142

    
143
        def nicwait(wait):
144
            nics = self.client.list_server_nics(servid)
145
            for net in nics:
146
                found_nic = net['network_id'] == netid
147
                if (in_creation and found_nic) or not (
148
                        in_creation or found_nic):
149
                    return
150
            time.sleep(wait)
151
        self.do_with_progress_bar(
152
            nicwait,
153
            'Wait nic-%s-%s to %sconnect' % (
154
                netid,
155
                servid,
156
                '' if in_creation else 'dis'),
157
            self._waits[:5])
158
        for net in self.client.list_server_nics(servid):
159
            if netid == net['network_id']:
160
                return True
161
        return False
162

    
163
    def _has_status(self, servid, status):
164
        r = self.client.get_server_details(servid)
165
        return r['status'] == status
166

    
167
    def _wait_for_status(self, servid, status):
168
        (wait_bar, wait_cb) = self._safe_progress_bar(
169
            'Server %s in %s' % (servid, status))
170
        self.client.wait_server(servid, status, wait_cb=wait_cb)
171
        self._safe_progress_bar_finish(wait_bar)
172

    
173
    def test_parallel_creation(self):
174
        """test create with multiple threads
175
        Do not use this in regular livetest
176
        """
177
        from kamaki.clients import SilentEvent
178
        c1 = SilentEvent(
179
            self._create_server,
180
            self.servname1,
181
            self.flavorid,
182
            self.img)
183
        c2 = SilentEvent(
184
            self._create_server,
185
            self.servname2,
186
            self.flavorid + 2,
187
            self.img)
188
        c3 = SilentEvent(
189
            self._create_server,
190
            self.servname1,
191
            self.flavorid,
192
            self.img)
193
        c4 = SilentEvent(
194
            self._create_server,
195
            self.servname2,
196
            self.flavorid + 2,
197
            self.img)
198
        c5 = SilentEvent(
199
            self._create_server,
200
            self.servname1,
201
            self.flavorid,
202
            self.img)
203
        c6 = SilentEvent(
204
            self._create_server,
205
            self.servname2,
206
            self.flavorid + 2,
207
            self.img)
208
        c7 = SilentEvent(
209
            self._create_server,
210
            self.servname1,
211
            self.flavorid,
212
            self.img)
213
        c8 = SilentEvent(
214
            self._create_server,
215
            self.servname2,
216
            self.flavorid + 2,
217
            self.img)
218
        c1.start()
219
        c2.start()
220
        c3.start()
221
        c4.start()
222
        c5.start()
223
        c6.start()
224
        c7.start()
225
        c8.start()
226

    
227
    def test_create_server(self):
228
        """Test create_server"""
229
        self.server1 = self._create_server(
230
            self.servname1,
231
            self.flavorid,
232
            self.img)
233
        self._wait_for_status(self.server1['id'], 'BUILD')
234
        self._test_0010_create_server()
235

    
236
    def _test_0010_create_server(self):
237
        self.assertEqual(self.server1["name"], self.servname1)
238
        self.assertEqual(self.server1["flavorRef"], self.flavorid)
239
        self.assertEqual(self.server1["imageRef"], self.img)
240
        self.assertEqual(self.server1["status"], "BUILD")
241

    
242
    def test_list_servers(self):
243
        """Test list servers"""
244
        self.server1 = self._create_server(
245
            self.servname1,
246
            self.flavorid,
247
            self.img)
248
        self.server2 = self._create_server(
249
            self.servname2,
250
            self.flavorid + 2,
251
            self.img)
252
        self._test_0020_list_servers()
253

    
254
    def _test_0020_list_servers(self):
255
        servers = self.client.list_servers()
256
        dservers = self.client.list_servers(detail=True)
257

    
258
        """detailed and simple are same size"""
259
        self.assertEqual(len(dservers), len(servers))
260
        for i in range(len(servers)):
261
            for field in (
262
                    'created',
263
                    'flavorRef',
264
                    'hostId',
265
                    'imageRef',
266
                    'progress',
267
                    'status',
268
                    'updated'):
269
                self.assertFalse(field in servers[i])
270
                self.assertTrue(field in dservers[i])
271

    
272
        """detailed and simple contain same names"""
273
        names = sorted(map(lambda x: x["name"], servers))
274
        dnames = sorted(map(lambda x: x["name"], dservers))
275
        self.assertEqual(names, dnames)
276

    
277
    def _test_0030_wait_test_servers_to_build(self):
278
        """Pseudo-test to wait for VMs to load"""
279
        print('')
280
        self._wait_for_status(self.server1['id'], 'BUILD')
281
        self._wait_for_status(self.server2['id'], 'BUILD')
282

    
283
    def test_get_server_details(self):
284
        """Test get_server_details"""
285
        self.server1 = self._create_server(
286
            self.servname1,
287
            self.flavorid,
288
            self.img)
289
        self._wait_for_status(self.server1['id'], 'BUILD')
290
        self._test_0040_get_server_details()
291

    
292
    def _test_0040_get_server_details(self):
293
        r = self.client.get_server_details(self.server1['id'])
294
        self.assertEqual(r["name"], self.servname1)
295
        self.assertEqual(r["flavorRef"], self.flavorid)
296
        self.assertEqual(r["imageRef"], self.img)
297
        self.assertEqual(r["status"], "ACTIVE")
298

    
299
    def test_update_server_name(self):
300
        """Test update_server_name"""
301
        self.server1 = self._create_server(
302
            self.servname1,
303
            self.flavorid,
304
            self.img)
305
        self._test_0050_update_server_name()
306

    
307
    def _test_0050_update_server_name(self):
308
        new_name = self.servname1 + '_new_name'
309
        self.client.update_server_name(self.server1['id'], new_name)
310
        r = self.client.get_server_details(
311
            self.server1['id'],
312
            success=(200, 400))
313
        self.assertEqual(r['name'], new_name)
314
        changed = self.servers.pop(self.servname1)
315
        changed['name'] = new_name
316
        self.servers[new_name] = changed
317

    
318
    def test_reboot_server(self):
319
        """Test reboot server"""
320
        self.server1 = self._create_server(
321
            self.servname1,
322
            self.flavorid,
323
            self.img)
324
        self._wait_for_status(self.server1['id'], 'BUILD')
325
        self.server2 = self._create_server(
326
            self.servname2,
327
            self.flavorid + 1,
328
            self.img)
329
        self._wait_for_status(self.server2['id'], 'BUILD')
330
        self._test_0060_reboot_server()
331
        self._wait_for_status(self.server1['id'], 'REBOOT')
332
        self._wait_for_status(self.server2['id'], 'REBOOT')
333

    
334
    def _test_0060_reboot_server(self):
335
        self.client.reboot_server(self.server1['id'])
336
        self.assertTrue(self._has_status(self.server1['id'], 'REBOOT'))
337
        self.client.reboot_server(self.server2['id'], hard=True)
338
        self.assertTrue(self._has_status(self.server2['id'], 'REBOOT'))
339

    
340
    def _test_0070_wait_test_servers_to_reboot(self):
341
        """Pseudo-test to wait for VMs to load"""
342
        print('')
343
        self._wait_for_status(self.server1['id'], 'REBOOT')
344
        self._wait_for_status(self.server2['id'], 'REBOOT')
345

    
346
    def test_create_server_metadata(self):
347
        """Test create_server_metadata"""
348
        self.server1 = self._create_server(
349
            self.servname1,
350
            self.flavorid,
351
            self.img)
352
        self._test_0080_create_server_metadata()
353

    
354
    def _test_0080_create_server_metadata(self):
355
        r1 = self.client.create_server_metadata(
356
            self.server1['id'],
357
            'mymeta',
358
            'mymeta val')
359
        self.assertTrue('mymeta' in r1)
360
        r2 = self.client.get_server_metadata(self.server1['id'], 'mymeta')
361
        self.assert_dicts_are_deeply_equal(r1, r2)
362

    
363
    def test_get_server_metadata(self):
364
        """Test get server_metadata"""
365
        self.server1 = self._create_server(
366
            self.servname1,
367
            self.flavorid,
368
            self.img)
369
        self._test_0090_get_server_metadata()
370

    
371
    def _test_0090_get_server_metadata(self):
372
        self.client.create_server_metadata(
373
            self.server1['id'],
374
            'mymeta_0',
375
            'val_0')
376
        r = self.client.get_server_metadata(self.server1['id'], 'mymeta_0')
377
        self.assertEqual(r['mymeta_0'], 'val_0')
378

    
379
    def test_update_server_metadata(self):
380
        """Test update_server_metadata"""
381
        self.server1 = self._create_server(
382
            self.servname1,
383
            self.flavorid,
384
            self.img)
385
        self._test_0100_update_server_metadata()
386

    
387
    def _test_0100_update_server_metadata(self):
388
        r1 = self.client.create_server_metadata(
389
            self.server1['id'],
390
            'mymeta3',
391
            'val2')
392
        self.assertTrue('mymeta3'in r1)
393
        r2 = self.client.update_server_metadata(
394
            self.server1['id'],
395
            mymeta3='val3')
396
        self.assertTrue(r2['mymeta3'], 'val3')
397

    
398
    def test_delete_server_metadata(self):
399
        """Test delete_server_metadata"""
400
        self.server1 = self._create_server(
401
            self.servname1,
402
            self.flavorid,
403
            self.img)
404
        self._test_0110_delete_server_metadata()
405

    
406
    def _test_0110_delete_server_metadata(self):
407
        r1 = self.client.create_server_metadata(
408
            self.server1['id'],
409
            'mymeta',
410
            'val')
411
        self.assertTrue('mymeta' in r1)
412
        self.client.delete_server_metadata(self.server1['id'], 'mymeta')
413
        try:
414
            self.client.get_server_metadata(self.server1['id'], 'mymeta')
415
            raise ClientError('Wrong Error', status=100)
416
        except ClientError as err:
417
            self.assertEqual(err.status, 404)
418

    
419
    def test_list_flavors(self):
420
        """Test flavors_get"""
421
        self._test_0120_list_flavors()
422

    
423
    def _test_0120_list_flavors(self):
424
        r = self.client.list_flavors()
425
        self.assertTrue(len(r) > 1)
426
        r = self.client.list_flavors(detail=True)
427
        self.assertTrue('SNF:disk_template' in r[0])
428

    
429
    def test_get_flavor_details(self):
430
        """Test test_get_flavor_details"""
431
        self._test_0130_get_flavor_details()
432

    
433
    def _test_0130_get_flavor_details(self):
434
        r = self.client.get_flavor_details(self.flavorid)
435
        self.assert_dicts_are_deeply_equal(self._flavor_details, r)
436

    
437
    def test_list_images(self):
438
        """Test list_images"""
439
        self._test_0140_list_images()
440

    
441
    def _test_0140_list_images(self):
442
        r = self.client.list_images()
443
        self.assertTrue(len(r) > 1)
444
        r = self.client.list_images(detail=True)
445
        for detailed_img in r:
446
            if detailed_img['id'] == self.img:
447
                break
448
        self.assert_dicts_are_deeply_equal(detailed_img, self.img_details)
449

    
450
    def test_get_image_details(self):
451
        """Test image_details"""
452
        self._test_0150_get_image_details()
453

    
454
    def _test_0150_get_image_details(self):
455
        r = self.client.get_image_details(self.img)
456
        self.assert_dicts_are_deeply_equal(r, self.img_details)
457

    
458
    def test_get_image_metadata(self):
459
        """Test get_image_metadata"""
460
        self._test_0160_get_image_metadata()
461

    
462
    def _test_0160_get_image_metadata(self):
463
        r = self.client.get_image_metadata(self.img)
464
        self.assert_dicts_are_deeply_equal(
465
            self.img_details['metadata']['values'], r)
466
        for key, val in self.img_details['metadata']['values'].items():
467
            r = self.client.get_image_metadata(self.img, key)
468
            self.assertEqual(r[key], val)
469

    
470
    def test_shutdown_server(self):
471
        """Test shutdown_server"""
472
        self.server1 = self._create_server(
473
            self.servname1,
474
            self.flavorid,
475
            self.img)
476
        self._wait_for_status(self.server1['id'], 'BUILD')
477
        self._test_0170_shutdown_server()
478

    
479
    def _test_0170_shutdown_server(self):
480
        self.client.shutdown_server(self.server1['id'])
481
        self._wait_for_status(self.server1['id'], 'ACTIVE')
482
        r = self.client.get_server_details(self.server1['id'])
483
        self.assertEqual(r['status'], 'STOPPED')
484

    
485
    def test_start_server(self):
486
        """Test start_server"""
487
        self.server1 = self._create_server(
488
            self.servname1,
489
            self.flavorid,
490
            self.img)
491
        self._wait_for_status(self.server1['id'], 'BUILD')
492
        self.client.shutdown_server(self.server1['id'])
493
        self._wait_for_status(self.server1['id'], 'ACTIVE')
494
        self._test_0180_start_server()
495

    
496
    def _test_0180_start_server(self):
497
        self.client.start_server(self.server1['id'])
498
        self._wait_for_status(self.server1['id'], 'STOPPED')
499
        r = self.client.get_server_details(self.server1['id'])
500
        self.assertEqual(r['status'], 'ACTIVE')
501

    
502
    def test_get_server_console(self):
503
        """Test get_server_console"""
504
        self.server2 = self._create_server(
505
            self.servname2,
506
            self.flavorid + 2,
507
            self.img)
508
        self._wait_for_status(self.server2['id'], 'BUILD')
509
        self._test_0190_get_server_console()
510

    
511
    def _test_0190_get_server_console(self):
512
        r = self.client.get_server_console(self.server2['id'])
513
        self.assertTrue('host' in r)
514
        self.assertTrue('password' in r)
515
        self.assertTrue('port' in r)
516
        self.assertTrue('type' in r)
517

    
518
    def test_get_firewall_profile(self):
519
        """Test get_firewall_profile"""
520
        self.server1 = self._create_server(
521
            self.servname1,
522
            self.flavorid,
523
            self.img)
524
        self._test_0200_get_firewall_profile()
525

    
526
    def _test_0200_get_firewall_profile(self):
527
        self._wait_for_status(self.server1['id'], 'BUILD')
528
        fprofile = self.client.get_firewall_profile(self.server1['id'])
529
        self.assertTrue(fprofile in self.PROFILES)
530

    
531
    def test_set_firewall_profile(self):
532
        """Test set_firewall_profile"""
533
        self.server1 = self._create_server(
534
            self.servname1,
535
            self.flavorid,
536
            self.img)
537
        self._test_0210_set_firewall_profile()
538

    
539
    def _test_0210_set_firewall_profile(self):
540

    
541
        self._wait_for_status(self.server1['id'], 'BUILD')
542
        PROFILES = ['DISABLED', 'ENABLED', 'DISABLED', 'PROTECTED']
543
        fprofile = self.client.get_firewall_profile(self.server1['id'])
544
        print('')
545
        count_success = 0
546
        for counter, fprofile in enumerate(PROFILES):
547
            npos = counter + 1
548
            try:
549
                nprofile = PROFILES[npos]
550
            except IndexError:
551
                nprofile = PROFILES[0]
552
            print('\tprofile swap %s: %s -> %s' % (npos, fprofile, nprofile))
553
            self.client.set_firewall_profile(self.server1['id'], nprofile)
554
            time.sleep(0.5)
555
            self.client.reboot_server(self.server1['id'], hard=True)
556
            time.sleep(1)
557
            self._wait_for_status(self.server1['id'], 'REBOOT')
558
            time.sleep(0.5)
559
            changed = self.client.get_firewall_profile(self.server1['id'])
560
            try:
561
                self.assertEqual(changed, nprofile)
562
            except AssertionError as err:
563
                if count_success:
564
                    print('\tFAIL in swap #%s' % npos)
565
                    break
566
                else:
567
                    raise err
568
            count_success += 1
569

    
570
    def test_get_server_stats(self):
571
        self.server1 = self._create_server(
572
            self.servname1,
573
            self.flavorid,
574
            self.img)
575
        self._test_0220_get_server_stats()
576

    
577
    def _test_0220_get_server_stats(self):
578
        r = self.client.get_server_stats(self.server1['id'])
579
        it = ('cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries', 'refresh')
580
        for term in it:
581
            self.assertTrue(term in r)
582

    
583
    def test_create_network(self):
584
        """Test create_network"""
585
        self._test_0230_create_network()
586

    
587
    def _test_0230_create_network(self):
588
        print('\twith no params')
589
        self.network1 = self._create_network(self.netname1)
590
        self._wait_for_network(self.network1['id'], 'ACTIVE')
591
        n1id = self.network1['id']
592
        self.network1 = self.client.get_network_details(n1id)
593
        nets = self.client.list_networks(self.network1['id'])
594
        chosen = [net for net in nets if net['id'] == n1id][0]
595
        chosen.pop('updated')
596
        net1 = dict(self.network1)
597
        net1.pop('updated')
598
        self.assert_dicts_are_deeply_equal(chosen, net1)
599
        for param, val in dict(
600
                cidr='192.168.0.0/24',
601
                gateway='192.168.0.1',
602
                type='MAC_FILTERED',
603
                dhcp=True).items():
604
            print('\tdelete %s to avoid max net limit' % n1id)
605
            self._delete_network(n1id)
606
            kwargs = {param: val}
607
            print('\twith %s=%s' % (param, val))
608
            self.network1 = self._create_network(self.netname1, **kwargs)
609
            n1id = self.network1['id']
610
            self._wait_for_network(n1id, 'ACTIVE')
611
            self.network1 = self.client.get_network_details(n1id)
612
            self.assertEqual(self.network1[param], val)
613

    
614
    def test_connect_server(self):
615
        """Test connect_server"""
616
        self.server1 = self._create_server(
617
            self.servname1,
618
            self.flavorid,
619
            self.img)
620
        self.network1 = self._create_network(self.netname1)
621
        self._wait_for_status(self.server1['id'], 'BUILD')
622
        self._wait_for_network(self.network1['id'], 'ACTIVE')
623
        self._test_0240_connect_server()
624

    
625
    def _test_0250_connect_server(self):
626
        self.client.connect_server(self.server1['id'], self.network1['id'])
627
        self.assertTrue(self._wait_for_nic(
628
            self.network1['id'],
629
            self.server1['id']))
630

    
631
    def test_disconnect_server(self):
632
        """Test disconnect_server"""
633
        self.test_connect_server()
634
        self._test_0250_disconnect_server()
635

    
636
    def _test_0250_disconnect_server(self):
637
        self.client.disconnect_server(self.server1['id'], self.network1['id'])
638
        self.assertTrue(self._wait_for_nic(
639
            self.network1['id'],
640
            self.server1['id'],
641
            in_creation=False))
642

    
643
    def _test_0260_wait_for_second_network(self):
644
        self.network2 = self._create_network(self.netname2)
645
        self._wait_for_network(self.network2['id'], 'ACTIVE')
646

    
647
    def test_list_server_nics(self):
648
        """Test list_server_nics"""
649
        self.server1 = self._create_server(
650
            self.servname1,
651
            self.flavorid,
652
            self.img)
653
        self.network2 = self._create_network(self.netname2)
654
        self._wait_for_status(self.server1['id'], 'BUILD')
655
        self._wait_for_network(self.network2['id'], 'ACTIVE')
656
        self._test_0280_list_server_nics()
657

    
658
    def _test_0280_list_server_nics(self):
659
        r = self.client.list_server_nics(self.server1['id'])
660
        len0 = len(r)
661
        self.client.connect_server(self.server1['id'], self.network2['id'])
662
        self.assertTrue(self._wait_for_nic(
663
            self.network2['id'],
664
            self.server1['id']))
665
        r = self.client.list_server_nics(self.server1['id'])
666
        self.assertTrue(len(r) > len0)
667

    
668
    def test_list_networks(self):
669
        """Test list_network"""
670
        self.network1 = self._create_network(self.netname1)
671
        self._wait_for_network(self.network1['id'], 'ACTIVE')
672
        self._test_0290_list_networks()
673

    
674
    def _test_0290_list_networks(self):
675
        r = self.client.list_networks()
676
        self.assertTrue(len(r) > 1)
677
        ids = [net['id'] for net in r]
678
        names = [net['name'] for net in r]
679
        self.assertTrue('1' in ids)
680
        #self.assertTrue('public' in names)
681
        self.assertTrue(self.network1['id'] in ids)
682
        self.assertTrue(self.network1['name'] in names)
683

    
684
        r = self.client.list_networks(detail=True)
685
        ids = [net['id'] for net in r]
686
        names = [net['name'] for net in r]
687
        for net in r:
688
            self.assertTrue(net['id'] in ids)
689
            self.assertTrue(net['name'] in names)
690
            for term in ('status', 'updated', 'created'):
691
                self.assertTrue(term in net.keys())
692

    
693
    def test_get_network_details(self):
694
        """Test get_network_details"""
695
        self.network1 = self._create_network(self.netname1)
696
        self._test_0300_get_network_details()
697

    
698
    def _test_0300_get_network_details(self):
699
        r = self.client.get_network_details(self.network1['id'])
700
        net1 = dict(self.network1)
701
        net1.pop('status')
702
        net1.pop('updated', None)
703
        net1.pop('attachments')
704
        r.pop('status')
705
        r.pop('updated', None)
706
        r.pop('attachments')
707
        self.assert_dicts_are_deeply_equal(net1, r)
708

    
709
    def test_update_network_name(self):
710
        self.network2 = self._create_network(self.netname2)
711
        self._test_0310_update_network_name()
712

    
713
    def _test_0310_update_network_name(self):
714
        updated_name = self.netname2 + '_upd'
715
        self.client.update_network_name(self.network2['id'], updated_name)
716

    
717
        def netwait(wait):
718
            r = self.client.get_network_details(self.network2['id'])
719
            if r['name'] == updated_name:
720
                return
721
            time.sleep(wait)
722
        self.do_with_progress_bar(
723
            netwait,
724
            'Network %s name is changing:' % self.network2['id'],
725
            self._waits[:5])
726

    
727
        r = self.client.get_network_details(self.network2['id'])
728
        self.assertEqual(r['name'], updated_name)
729

    
730
    """ Don't have auth to test this
731
    def test_delete_image(self):
732
        ""Test delete_image""
733
        self._test_0330_delete_image()
734
    def _test_0330_delete_image(self):
735
        images = self.client.list_images()
736
        self.client.delete_image(images[2]['id'])
737
        try:
738
            r = self.client.get_image_details(images[2]['id'], success=(400))
739
        except ClientError as err:
740
            self.assertEqual(err.status, 404)
741

742
    def test_create_image_metadata(self):
743
        ""Test create_image_metadata""
744
        self._test_0340_create_image_metadata()
745
    def _test_0340_create_image_metadata(self):
746
        r = self.client.create_image_metadata(self.img, 'mykey', 'myval')
747
        self.assertEqual(r['mykey'], 'myval')
748

749
    def test_update_image_metadata(self):
750
        ""Test update_image_metadata""
751
        self._test_0350_update_image_metadata()
752
    def _test_0350_update_image_metadata(self):
753
        r = self.client.create_image_metadata(self.img, 'mykey0', 'myval')
754
        r = self.client.update_image_metadata(self.img, 'mykey0', 'myval0')
755
        self.assertEqual(r['mykey0'], 'myval0')
756

757
    def test_delete_image_metadata(self):
758
        ""Test delete_image_metadata""
759
        self._test_0360_delete_image_metadata()
760
    def _test_0360_delete_image_metadata(self):
761
        self.client.create_image_metadata(self.img, 'mykey1', 'myval1')
762
        self.client.delete_image_metadata(self.img, 'mykey1')
763
        r = self.client.get_image_metadata(self.img)
764
        self.assertNotEqual('mykey1' in r)
765
    """