Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (27.9 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
from kamaki.clients.astakos import AstakosClient
39

    
40

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

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

    
63
        self.cloud = 'cloud.%s' % self['testcloud']
64
        aurl, self.token = self[self.cloud, 'url'], self[self.cloud, 'token']
65
        self.auth_base = AstakosClient(aurl, self.token)
66
        curl = self.auth_base.get_service_endpoints('compute')['publicURL']
67
        self.client = CycladesClient(curl, self.token)
68

    
69
    def tearDown(self):
70
        """Destoy servers used in testing"""
71
        for net in self.networks.keys():
72
            self._delete_network(net)
73
        for server in self.servers.values():
74
            self._delete_server(server['id'])
75
            print('DEL VM %s (%s)' % (server['id'], server['name']))
76

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

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

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

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

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

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

    
126
    def _wait_for_network(self, netid, status):
127

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

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

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

    
161
    def _has_status(self, servid, status):
162
        r = self.client.get_server_details(servid)
163
        #print 'MY ', servid, ' STATUS IS ', r['status']
164
        return r['status'] == status
165

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

    
172
    def test_parallel_creation(self):
173
        """test create with multiple threads
174
        Do not use this in regular livetest
175
        """
176
        from kamaki.clients import SilentEvent
177
        c = []
178
        for i in range(8):
179
            sname = '%s_%s' % (self.servname1, i)
180
            c.append(SilentEvent(
181
                self._create_server, sname, self.flavorid, self.img))
182
        for i in range(8):
183
            c[i].start()
184

    
185
    def test_create_server(self):
186
        """Test create_server"""
187
        self.server1 = self._create_server(
188
            self.servname1, self.flavorid, self.img)
189
        self._wait_for_status(self.server1['id'], 'BUILD')
190
        self._test_0010_create_server()
191

    
192
    def _test_0010_create_server(self):
193
        self.assertEqual(self.server1["name"], self.servname1)
194
        self.assertEqual(self.server1["flavor"]["id"], self.flavorid)
195
        self.assertEqual(self.server1["image"]["id"], self.img)
196
        self.assertEqual(self.server1["status"], "BUILD")
197

    
198
    def test_list_servers(self):
199
        """Test list servers"""
200
        self.server1 = self._create_server(
201
            self.servname1, self.flavorid, self.img)
202
        self.server2 = self._create_server(
203
            self.servname2, self.flavorid, self.img)
204
        self._test_0020_list_servers()
205

    
206
    def _test_0020_list_servers(self):
207
        servers = self.client.list_servers()
208
        dservers = self.client.list_servers(detail=True)
209

    
210
        """detailed and simple are same size"""
211
        self.assertEqual(len(dservers), len(servers))
212
        fields = set([
213
            'created', 'flavor', 'hostId', 'image', 'progress', 'status',
214
            'updated'])
215
        for i, srv in enumerate(servers):
216
            self.assertTrue(fields.isdisjoint(srv))
217
            self.assertTrue(fields.issubset(dservers[i]))
218

    
219
        """detailed and simple contain same names"""
220
        names = sorted(map(lambda x: x["name"], servers))
221
        dnames = sorted(map(lambda x: x["name"], dservers))
222
        self.assertEqual(names, dnames)
223

    
224
    def _test_0030_wait_test_servers_to_build(self):
225
        """Pseudo-test to wait for VMs to load"""
226
        print('')
227
        self._wait_for_status(self.server1['id'], 'BUILD')
228
        self._wait_for_status(self.server2['id'], 'BUILD')
229

    
230
    def test_get_server_details(self):
231
        """Test get_server_details"""
232
        self.server1 = self._create_server(
233
            self.servname1, self.flavorid, self.img)
234
        self._wait_for_status(self.server1['id'], 'BUILD')
235
        self._test_0040_get_server_details()
236

    
237
    def _test_0040_get_server_details(self):
238
        r = self.client.get_server_details(self.server1['id'])
239
        self.assertEqual(r["name"], self.servname1)
240
        self.assertEqual(r["flavor"]["id"], self.flavorid)
241
        self.assertEqual(r["image"]["id"], self.img)
242
        self.assertEqual(r["status"], "ACTIVE")
243

    
244
    def test_update_server_name(self):
245
        """Test update_server_name"""
246
        self.server1 = self._create_server(
247
            self.servname1, self.flavorid, self.img)
248
        self._test_0050_update_server_name()
249

    
250
    def _test_0050_update_server_name(self):
251
        new_name = self.servname1 + '_new_name'
252
        self.client.update_server_name(self.server1['id'], new_name)
253
        r = self.client.get_server_details(
254
            self.server1['id'], success=(200, 400))
255
        self.assertEqual(r['name'], new_name)
256
        changed = self.servers.pop(self.servname1)
257
        changed['name'] = new_name
258
        self.servers[new_name] = changed
259

    
260
    def test_reboot_server(self):
261
        """Test reboot server"""
262
        self.server1 = self._create_server(
263
            self.servname1, self.flavorid, self.img)
264
        self._wait_for_status(self.server1['id'], 'BUILD')
265
        self.server2 = self._create_server(
266
            self.servname2, self.flavorid + 2, self.img)
267
        self._wait_for_status(self.server2['id'], 'BUILD')
268
        self._test_0060_reboot_server()
269
        self._wait_for_status(self.server1['id'], 'REBOOT')
270
        self._wait_for_status(self.server2['id'], 'REBOOT')
271

    
272
    def _test_0060_reboot_server(self):
273
        self.client.reboot_server(self.server1['id'])
274
        self.assertTrue(self._has_status(self.server1['id'], 'REBOOT'))
275
        self.client.reboot_server(self.server2['id'], hard=True)
276
        self.assertTrue(self._has_status(self.server2['id'], 'REBOOT'))
277

    
278
    def test_resize_server(self):
279
        """Modify the flavor of a server"""
280
        self.server1 = self._create_server(
281
            self.servname1, self.flavorid, self.img)
282
        self._test_0065_resize_server()
283
        self.delete_server(self.server1['id'])
284
        self.server1 = self._create_server(
285
            self.servname1, self.flavorid, self.img)
286

    
287
    def _test_0065_resize_server(self):
288
        self.client.resize_server(self.servname1, self.flavorid + 2)
289
        srv = self.client.get_flavor_details(self.server1['id'])
290
        self.assertEqual(srv['flavor']['id'], self.flavorid + 2)
291

    
292
    def _test_0070_wait_test_servers_to_reboot(self):
293
        """Pseudo-test to wait for VMs to load"""
294
        print('')
295
        self._wait_for_status(self.server1['id'], 'REBOOT')
296
        self._wait_for_status(self.server2['id'], 'REBOOT')
297

    
298
    def test_create_server_metadata(self):
299
        """Test create_server_metadata"""
300
        self.server1 = self._create_server(
301
            self.servname1, self.flavorid, self.img)
302
        self._test_0080_create_server_metadata()
303

    
304
    def _test_0080_create_server_metadata(self):
305
        r1 = self.client.create_server_metadata(
306
            self.server1['id'], 'mymeta', 'mymeta val')
307
        self.assertTrue('mymeta' in r1)
308
        r2 = self.client.get_server_metadata(self.server1['id'], 'mymeta')
309
        self.assert_dicts_are_equal(r1, r2)
310

    
311
    def test_get_server_metadata(self):
312
        """Test get server_metadata"""
313
        self.server1 = self._create_server(
314
            self.servname1, self.flavorid, self.img)
315
        self._test_0090_get_server_metadata()
316

    
317
    def _test_0090_get_server_metadata(self):
318
        self.client.update_server_metadata(
319
            self.server1['id'], mymeta_0='val_0')
320
        r = self.client.get_server_metadata(self.server1['id'], 'mymeta_0')
321
        self.assertEqual(r['mymeta_0'], 'val_0')
322

    
323
    def test_update_server_metadata(self):
324
        """Test update_server_metadata"""
325
        self.server1 = self._create_server(
326
            self.servname1, self.flavorid, self.img)
327
        self._test_0100_update_server_metadata()
328

    
329
    def _test_0100_update_server_metadata(self):
330
        r1 = self.client.update_server_metadata(
331
            self.server1['id'], mymeta3='val2')
332
        self.assertTrue('mymeta3'in r1)
333
        r2 = self.client.update_server_metadata(
334
            self.server1['id'], mymeta3='val3')
335
        self.assertTrue(r2['mymeta3'], 'val3')
336

    
337
    def test_delete_server_metadata(self):
338
        """Test delete_server_metadata"""
339
        self.server1 = self._create_server(
340
            self.servname1, self.flavorid, self.img)
341
        self._test_0110_delete_server_metadata()
342

    
343
    def _test_0110_delete_server_metadata(self):
344
        r1 = self.client.update_server_metadata(
345
            self.server1['id'], mymeta='val')
346
        self.assertTrue('mymeta' in r1)
347
        self.client.delete_server_metadata(self.server1['id'], 'mymeta')
348
        try:
349
            self.client.get_server_metadata(self.server1['id'], 'mymeta')
350
            raise ClientError('Wrong Error', status=100)
351
        except ClientError as err:
352
            self.assertEqual(err.status, 404)
353

    
354
    def test_list_flavors(self):
355
        """Test flavors_get"""
356
        self._test_0120_list_flavors()
357

    
358
    def _test_0120_list_flavors(self):
359
        r = self.client.list_flavors()
360
        self.assertTrue(len(r) > 1)
361
        r = self.client.list_flavors(detail=True)
362
        self.assertTrue('SNF:disk_template' in r[0])
363

    
364
    def test_get_flavor_details(self):
365
        """Test test_get_flavor_details"""
366
        self._test_0130_get_flavor_details()
367

    
368
    def _test_0130_get_flavor_details(self):
369
        r = self.client.get_flavor_details(self.flavorid)
370
        self.assert_dicts_are_equal(self._flavor_details, r)
371

    
372
    def test_list_images(self):
373
        """Test list_images"""
374
        self._test_0140_list_images()
375

    
376
    def _test_0140_list_images(self):
377
        r = self.client.list_images()
378
        self.assertTrue(len(r) > 1)
379
        r = self.client.list_images(detail=True)
380
        for detailed_img in r:
381
            if detailed_img['id'] == self.img:
382
                break
383
        self.assert_dicts_are_equal(detailed_img, self.img_details)
384

    
385
    def test_get_image_details(self):
386
        """Test image_details"""
387
        self._test_0150_get_image_details()
388

    
389
    def _test_0150_get_image_details(self):
390
        r = self.client.get_image_details(self.img)
391
        self.assert_dicts_are_equal(r, self.img_details)
392

    
393
    def test_get_image_metadata(self):
394
        """Test get_image_metadata"""
395
        self._test_0160_get_image_metadata()
396

    
397
    def _test_0160_get_image_metadata(self):
398
        r = self.client.get_image_metadata(self.img)
399
        self.assert_dicts_are_equal(
400
            self.img_details['metadata'], r)
401
        for key, val in self.img_details['metadata'].items():
402
            r = self.client.get_image_metadata(self.img, key)
403
            self.assertEqual(r[key], val)
404

    
405
    def test_shutdown_server(self):
406
        """Test shutdown_server"""
407
        self.server1 = self._create_server(
408
            self.servname1, self.flavorid, self.img)
409
        self._wait_for_status(self.server1['id'], 'BUILD')
410
        self._test_0170_shutdown_server()
411

    
412
    def _test_0170_shutdown_server(self):
413
        self.client.shutdown_server(self.server1['id'])
414
        self._wait_for_status(self.server1['id'], 'ACTIVE')
415
        r = self.client.get_server_details(self.server1['id'])
416
        self.assertEqual(r['status'], 'STOPPED')
417

    
418
    def test_start_server(self):
419
        """Test start_server"""
420
        self.server1 = self._create_server(
421
            self.servname1, self.flavorid, self.img)
422
        self._wait_for_status(self.server1['id'], 'BUILD')
423
        self.client.shutdown_server(self.server1['id'])
424
        self._wait_for_status(self.server1['id'], 'ACTIVE')
425
        self._test_0180_start_server()
426

    
427
    def _test_0180_start_server(self):
428
        self.client.start_server(self.server1['id'])
429
        self._wait_for_status(self.server1['id'], 'STOPPED')
430
        r = self.client.get_server_details(self.server1['id'])
431
        self.assertEqual(r['status'], 'ACTIVE')
432

    
433
    def test_get_server_console(self):
434
        """Test get_server_console"""
435
        self.server2 = self._create_server(
436
            self.servname2, self.flavorid, self.img)
437
        self._wait_for_status(self.server2['id'], 'BUILD')
438
        self._test_0190_get_server_console()
439

    
440
    def _test_0190_get_server_console(self):
441
        r = self.client.get_server_console(self.server2['id'])
442
        self.assertTrue('host' in r)
443
        self.assertTrue('password' in r)
444
        self.assertTrue('port' in r)
445
        self.assertTrue('type' in r)
446

    
447
    def test_get_firewall_profile(self):
448
        """Test get_firewall_profile"""
449
        self.server1 = self._create_server(
450
            self.servname1, self.flavorid, self.img)
451
        self._test_0200_get_firewall_profile()
452

    
453
    def _test_0200_get_firewall_profile(self):
454
        self._wait_for_status(self.server1['id'], 'BUILD')
455
        fprofile = self.client.get_firewall_profile(self.server1['id'])
456
        self.assertTrue(fprofile in self.PROFILES)
457

    
458
    def test_set_firewall_profile(self):
459
        """Test set_firewall_profile"""
460
        self.server1 = self._create_server(
461
            self.servname1, self.flavorid, self.img)
462
        self._test_0210_set_firewall_profile()
463

    
464
    def _test_0210_set_firewall_profile(self):
465

    
466
        self._wait_for_status(self.server1['id'], 'BUILD')
467
        PROFILES = ['DISABLED', 'ENABLED', 'DISABLED', 'PROTECTED']
468
        fprofile = self.client.get_firewall_profile(self.server1['id'])
469
        print('')
470
        count_success = 0
471
        for counter, fprofile in enumerate(PROFILES):
472
            npos = counter + 1
473
            try:
474
                nprofile = PROFILES[npos]
475
            except IndexError:
476
                nprofile = PROFILES[0]
477
            print('\tprofile swap %s: %s -> %s' % (npos, fprofile, nprofile))
478
            self.client.set_firewall_profile(self.server1['id'], nprofile)
479
            time.sleep(0.5)
480
            self.client.reboot_server(self.server1['id'], hard=True)
481
            time.sleep(1)
482
            self._wait_for_status(self.server1['id'], 'REBOOT')
483
            time.sleep(0.5)
484
            changed = self.client.get_firewall_profile(self.server1['id'])
485
            try:
486
                self.assertEqual(changed, nprofile)
487
            except AssertionError as err:
488
                if count_success:
489
                    print('\tFAIL in swap #%s' % npos)
490
                    break
491
                else:
492
                    raise err
493
            count_success += 1
494

    
495
    def test_get_server_stats(self):
496
        self.server1 = self._create_server(
497
            self.servname1, self.flavorid, self.img)
498
        self._test_0220_get_server_stats()
499

    
500
    def _test_0220_get_server_stats(self):
501
        r = self.client.get_server_stats(self.server1['id'])
502
        it = ('cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries', 'refresh')
503
        for term in it:
504
            self.assertTrue(term in r)
505

    
506
    def test_create_network(self):
507
        """Test create_network"""
508
        self._test_0230_create_network()
509

    
510
    def _test_0230_create_network(self):
511
        print('\twith no params')
512
        self.network1 = self._create_network(self.netname1)
513
        self._wait_for_network(self.network1['id'], 'ACTIVE')
514
        n1id = self.network1['id']
515
        self.network1 = self.client.get_network_details(n1id)
516
        nets = self.client.list_networks(self.network1['id'])
517
        chosen = [net for net in nets if net['id'] == n1id][0]
518
        chosen.pop('updated')
519
        net1 = dict(self.network1)
520
        net1.pop('updated')
521
        self.assert_dicts_are_equal(chosen, net1)
522
        full_args = dict(
523
                cidr='192.168.1.0/24',
524
                gateway='192.168.1.1',
525
                type='MAC_FILTERED',
526
                dhcp=True)
527
        try_args = dict(all=True)
528
        try_args.update(full_args)
529
        for param, val in try_args.items():
530
            print('\tdelete %s to avoid max net limit' % n1id)
531
            self._delete_network(n1id)
532
            kwargs = full_args if param == 'all' else {param: val}
533
            print('\twith %s=%s' % (param, val))
534
            self.network1 = self._create_network(self.netname1, **kwargs)
535
            n1id = self.network1['id']
536
            self._wait_for_network(n1id, 'ACTIVE')
537
            self.network1 = self.client.get_network_details(n1id)
538
            if param == 'all':
539
                for p, v in full_args.items():
540
                    self.assertEqual(self.network1[p], v)
541
            else:
542
                self.assertEqual(self.network1[param], val)
543

    
544
    def test_connect_server(self):
545
        """Test connect_server"""
546
        self.server1 = self._create_server(
547
            self.servname1, self.flavorid, self.img)
548
        self.network1 = self._create_network(self.netname1)
549
        self._wait_for_status(self.server1['id'], 'BUILD')
550
        self._wait_for_network(self.network1['id'], 'ACTIVE')
551
        self._test_0240_connect_server()
552

    
553
    def _test_0250_connect_server(self):
554
        self.client.connect_server(self.server1['id'], self.network1['id'])
555
        self.assertTrue(
556
            self._wait_for_nic(self.network1['id'], self.server1['id']))
557

    
558
    def test_disconnect_server(self):
559
        """Test disconnect_server"""
560
        self.test_connect_server()
561
        self._test_0250_disconnect_server()
562

    
563
    def _test_0250_disconnect_server(self):
564
        self.client.disconnect_server(self.server1['id'], self.network1['id'])
565
        self.assertTrue(self._wait_for_nic(
566
            self.network1['id'], self.server1['id'], in_creation=False))
567

    
568
    def _test_0260_wait_for_second_network(self):
569
        self.network2 = self._create_network(self.netname2)
570
        self._wait_for_network(self.network2['id'], 'ACTIVE')
571

    
572
    def test_list_server_nics(self):
573
        """Test list_server_nics"""
574
        self.server1 = self._create_server(
575
            self.servname1, self.flavorid, self.img)
576
        self.network2 = self._create_network(self.netname2)
577
        self._wait_for_status(self.server1['id'], 'BUILD')
578
        self._wait_for_network(self.network2['id'], 'ACTIVE')
579
        self._test_0280_list_server_nics()
580

    
581
    def _test_0280_list_server_nics(self):
582
        r = self.client.list_server_nics(self.server1['id'])
583
        len0 = len(r)
584
        self.client.connect_server(self.server1['id'], self.network2['id'])
585
        self.assertTrue(
586
            self._wait_for_nic(self.network2['id'], self.server1['id']))
587
        r = self.client.list_server_nics(self.server1['id'])
588
        self.assertTrue(len(r) > len0)
589

    
590
    def test_list_networks(self):
591
        """Test list_network"""
592
        self.network1 = self._create_network(self.netname1)
593
        self._wait_for_network(self.network1['id'], 'ACTIVE')
594
        self._test_0290_list_networks()
595

    
596
    def _test_0290_list_networks(self):
597
        r = self.client.list_networks()
598
        self.assertTrue(len(r) > 1)
599
        ids = [net['id'] for net in r]
600
        names = [net['name'] for net in r]
601
        self.assertTrue('1' in ids)
602
        #self.assertTrue('public' in names)
603
        self.assertTrue(self.network1['id'] in ids)
604
        self.assertTrue(self.network1['name'] in names)
605

    
606
        r = self.client.list_networks(detail=True)
607
        ids = [net['id'] for net in r]
608
        names = [net['name'] for net in r]
609
        for net in r:
610
            self.assertTrue(net['id'] in ids)
611
            self.assertTrue(net['name'] in names)
612
            for term in ('status', 'updated', 'created'):
613
                self.assertTrue(term in net.keys())
614

    
615
    def test_list_network_nics(self):
616
        """Test list_server_nics"""
617
        self.server1 = self._create_server(
618
            self.servname1, self.flavorid, self.img)
619
        self.network1 = self._create_network(self.netname1)
620
        self.network2 = self._create_network(self.netname2)
621
        self._wait_for_status(self.server1['id'], 'BUILD')
622
        self._wait_for_network(self.network1['id'], 'ACTIVE')
623
        self._wait_for_network(self.network2['id'], 'ACTIVE')
624
        self.client.connect_server(self.server1['id'], self.network1['id'])
625
        self.client.connect_server(self.server1['id'], self.network2['id'])
626
        self._wait_for_nic(self.network1['id'], self.server1['id'])
627
        self._wait_for_nic(self.network2['id'], self.server1['id'])
628
        self._test_0293_list_network_nics()
629

    
630
    def _test_0293_list_network_nics(self):
631
        netid1, netid2 = self.network1['id'], self.network2['id']
632
        r = self.client.list_network_nics(netid1)
633
        expected = ['nic-%s-1' % self.server1['id']]
634
        self.assertEqual(r, expected)
635
        r = self.client.list_network_nics(netid2)
636
        expected = ['nic-%s-2' % self.server1['id']]
637
        self.assertEqual(r, expected)
638

    
639
    def test_get_network_details(self):
640
        """Test get_network_details"""
641
        self.network1 = self._create_network(self.netname1)
642
        self._test_0300_get_network_details()
643

    
644
    def _test_0300_get_network_details(self):
645
        r = self.client.get_network_details(self.network1['id'])
646
        net1 = dict(self.network1)
647
        net1.pop('status')
648
        net1.pop('updated', None)
649
        net1.pop('attachments')
650
        r.pop('status')
651
        r.pop('updated', None)
652
        r.pop('attachments')
653
        self.assert_dicts_are_equal(net1, r)
654

    
655
    def test_update_network_name(self):
656
        self.network2 = self._create_network(self.netname2)
657
        self._test_0310_update_network_name()
658

    
659
    def _test_0310_update_network_name(self):
660
        updated_name = self.netname2 + '_upd'
661
        self.client.update_network_name(self.network2['id'], updated_name)
662

    
663
        def netwait(wait):
664
            r = self.client.get_network_details(self.network2['id'])
665
            if r['name'] == updated_name:
666
                return
667
            time.sleep(wait)
668
        self.do_with_progress_bar(
669
            netwait,
670
            'Network %s name is changing:' % self.network2['id'],
671
            self._waits[:5])
672

    
673
        r = self.client.get_network_details(self.network2['id'])
674
        self.assertEqual(r['name'], updated_name)
675

    
676
    """ Don't have auth to test this
677
    def test_delete_image(self):
678
        ""Test delete_image""
679
        self._test_0330_delete_image()
680
    def _test_0330_delete_image(self):
681
        images = self.client.list_images()
682
        self.client.delete_image(images[2]['id'])
683
        try:
684
            r = self.client.get_image_details(images[2]['id'], success=(400))
685
        except ClientError as err:
686
            self.assertEqual(err.status, 404)
687

688
    def test_create_image_metadata(self):
689
        ""Test create_image_metadata""
690
        self._test_0340_create_image_metadata()
691
    def _test_0340_create_image_metadata(self):
692
        r = self.client.create_image_metadata(self.img, 'mykey', 'myval')
693
        self.assertEqual(r['mykey'], 'myval')
694

695
    def test_update_image_metadata(self):
696
        ""Test update_image_metadata""
697
        self._test_0350_update_image_metadata()
698
    def _test_0350_update_image_metadata(self):
699
        r = self.client.create_image_metadata(self.img, 'mykey0', 'myval')
700
        r = self.client.update_image_metadata(self.img, 'mykey0', 'myval0')
701
        self.assertEqual(r['mykey0'], 'myval0')
702

703
    def test_delete_image_metadata(self):
704
        ""Test delete_image_metadata""
705
        self._test_0360_delete_image_metadata()
706
    def _test_0360_delete_image_metadata(self):
707
        self.client.create_image_metadata(self.img, 'mykey1', 'myval1')
708
        self.client.delete_image_metadata(self.img, 'mykey1')
709
        r = self.client.get_image_metadata(self.img)
710
        self.assertNotEqual('mykey1' in r)
711
    """