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

36
from kamaki.clients import ClientError
37
38


39
class Cyclades(TestCase):
40
41
    """Set up a Cyclades thorough test"""
    def setUp(self):
42
43
44
45
        self.url = 'http://cyclades.example.com'
        self.token = 'cyc14d3s70k3n'
        from kamaki.clients.cyclades import CycladesClient
        self.cyclades = CycladesClient(self.url, self.token)
46
47

    def tearDown(self):
48
        pass
49

50
    """
51
    def test_parallel_creation(self):
52
        ""test create with multiple threads
53
        Do not use this in regular livetest
54
        ""
55
        from kamaki.clients import SilentEvent
56
57
        c1 = SilentEvent(
            self._create_server,
58
59
60
            self.servname1,
            self.flavorid,
            self.img)
61
62
        c2 = SilentEvent(
            self._create_server,
63
64
65
            self.servname2,
            self.flavorid + 2,
            self.img)
66
67
        c3 = SilentEvent(
            self._create_server,
68
69
70
            self.servname1,
            self.flavorid,
            self.img)
71
72
        c4 = SilentEvent(
            self._create_server,
73
74
75
            self.servname2,
            self.flavorid + 2,
            self.img)
76
77
        c5 = SilentEvent(
            self._create_server,
78
79
80
            self.servname1,
            self.flavorid,
            self.img)
81
82
        c6 = SilentEvent(
            self._create_server,
83
84
85
            self.servname2,
            self.flavorid + 2,
            self.img)
86
87
        c7 = SilentEvent(
            self._create_server,
88
89
90
            self.servname1,
            self.flavorid,
            self.img)
91
92
        c8 = SilentEvent(
            self._create_server,
93
94
95
96
97
98
99
100
101
102
103
104
105
            self.servname2,
            self.flavorid + 2,
            self.img)
        c1.start()
        c2.start()
        c3.start()
        c4.start()
        c5.start()
        c6.start()
        c7.start()
        c8.start()

    def test_create_server(self):
106
        ""Test create_server""
107
108
        self.server1 = self._create_server(
            self.servname1,
109
110
111
112
113
114
115
116
117
118
119
            self.flavorid,
            self.img)
        self._wait_for_status(self.server1['id'], 'BUILD')
        self._test_0010_create_server()

    def _test_0010_create_server(self):
        self.assertEqual(self.server1["name"], self.servname1)
        self.assertEqual(self.server1["flavorRef"], self.flavorid)
        self.assertEqual(self.server1["imageRef"], self.img)
        self.assertEqual(self.server1["status"], "BUILD")

120
    def test_list_servers(self):
121
        ""Test list servers""
122
123
124
125
126
127
128
129
        self.server1 = self._create_server(
            self.servname1,
            self.flavorid,
            self.img)
        self.server2 = self._create_server(
            self.servname2,
            self.flavorid + 2,
            self.img)
130
        self._test_0020_list_servers()
131

132
    def _test_0020_list_servers(self):
133
134
135
        servers = self.client.list_servers()
        dservers = self.client.list_servers(detail=True)

136
        ""detailed and simple are same size""
137
138
        self.assertEqual(len(dservers), len(servers))
        for i in range(len(servers)):
139
            for field in (
140
141
142
143
144
145
146
                    'created',
                    'flavorRef',
                    'hostId',
                    'imageRef',
                    'progress',
                    'status',
                    'updated'):
147
148
149
                self.assertFalse(field in servers[i])
                self.assertTrue(field in dservers[i])

150
        ""detailed and simple contain same names""
151
152
153
        names = sorted(map(lambda x: x["name"], servers))
        dnames = sorted(map(lambda x: x["name"], dservers))
        self.assertEqual(names, dnames)
154
155

    def _test_0030_wait_test_servers_to_build(self):
156
        ""Pseudo-test to wait for VMs to load""
157
        print('')
158
159
160
161
        self._wait_for_status(self.server1['id'], 'BUILD')
        self._wait_for_status(self.server2['id'], 'BUILD')

    def test_get_server_details(self):
162
        ""Test get_server_details""
163
164
        self.server1 = self._create_server(
            self.servname1,
165
166
167
168
169
170
171
172
173
174
175
176
177
            self.flavorid,
            self.img)
        self._wait_for_status(self.server1['id'], 'BUILD')
        self._test_0040_get_server_details()

    def _test_0040_get_server_details(self):
        r = self.client.get_server_details(self.server1['id'])
        self.assertEqual(r["name"], self.servname1)
        self.assertEqual(r["flavorRef"], self.flavorid)
        self.assertEqual(r["imageRef"], self.img)
        self.assertEqual(r["status"], "ACTIVE")

    def test_update_server_name(self):
178
        ""Test update_server_name""
179
180
        self.server1 = self._create_server(
            self.servname1,
181
182
183
184
185
186
187
            self.flavorid,
            self.img)
        self._test_0050_update_server_name()

    def _test_0050_update_server_name(self):
        new_name = self.servname1 + '_new_name'
        self.client.update_server_name(self.server1['id'], new_name)
188
189
190
        r = self.client.get_server_details(
            self.server1['id'],
            success=(200, 400))
191
192
193
194
195
196
        self.assertEqual(r['name'], new_name)
        changed = self.servers.pop(self.servname1)
        changed['name'] = new_name
        self.servers[new_name] = changed

    def test_reboot_server(self):
197
        ""Test reboot server""
198
199
        self.server1 = self._create_server(
            self.servname1,
200
201
202
            self.flavorid,
            self.img)
        self._wait_for_status(self.server1['id'], 'BUILD')
203
204
        self.server2 = self._create_server(
            self.servname2,
205
206
207
208
209
210
211
212
213
214
215
216
217
218
            self.flavorid + 1,
            self.img)
        self._wait_for_status(self.server2['id'], 'BUILD')
        self._test_0060_reboot_server()
        self._wait_for_status(self.server1['id'], 'REBOOT')
        self._wait_for_status(self.server2['id'], 'REBOOT')

    def _test_0060_reboot_server(self):
        self.client.reboot_server(self.server1['id'])
        self.assertTrue(self._has_status(self.server1['id'], 'REBOOT'))
        self.client.reboot_server(self.server2['id'], hard=True)
        self.assertTrue(self._has_status(self.server2['id'], 'REBOOT'))

    def _test_0070_wait_test_servers_to_reboot(self):
219
        ""Pseudo-test to wait for VMs to load""
220
        print('')
221
222
223
224
        self._wait_for_status(self.server1['id'], 'REBOOT')
        self._wait_for_status(self.server2['id'], 'REBOOT')

    def test_create_server_metadata(self):
225
        ""Test create_server_metadata""
226
227
        self.server1 = self._create_server(
            self.servname1,
228
229
230
231
232
            self.flavorid,
            self.img)
        self._test_0080_create_server_metadata()

    def _test_0080_create_server_metadata(self):
233
234
        r1 = self.client.create_server_metadata(
            self.server1['id'],
235
236
237
238
239
240
241
            'mymeta',
            'mymeta val')
        self.assertTrue('mymeta' in r1)
        r2 = self.client.get_server_metadata(self.server1['id'], 'mymeta')
        self.assert_dicts_are_deeply_equal(r1, r2)

    def test_get_server_metadata(self):
242
        ""Test get server_metadata""
243
244
        self.server1 = self._create_server(
            self.servname1,
245
246
247
248
249
            self.flavorid,
            self.img)
        self._test_0090_get_server_metadata()

    def _test_0090_get_server_metadata(self):
250
251
        self.client.create_server_metadata(
            self.server1['id'],
252
253
254
255
256
257
            'mymeta_0',
            'val_0')
        r = self.client.get_server_metadata(self.server1['id'], 'mymeta_0')
        self.assertEqual(r['mymeta_0'], 'val_0')

    def test_update_server_metadata(self):
258
        ""Test update_server_metadata""
259
260
        self.server1 = self._create_server(
            self.servname1,
261
262
263
264
265
            self.flavorid,
            self.img)
        self._test_0100_update_server_metadata()

    def _test_0100_update_server_metadata(self):
266
267
        r1 = self.client.create_server_metadata(
            self.server1['id'],
268
269
270
            'mymeta3',
            'val2')
        self.assertTrue('mymeta3'in r1)
271
272
        r2 = self.client.update_server_metadata(
            self.server1['id'],
273
274
275
276
            mymeta3='val3')
        self.assertTrue(r2['mymeta3'], 'val3')

    def test_delete_server_metadata(self):
277
        ""Test delete_server_metadata""
278
279
        self.server1 = self._create_server(
            self.servname1,
280
281
282
283
284
            self.flavorid,
            self.img)
        self._test_0110_delete_server_metadata()

    def _test_0110_delete_server_metadata(self):
285
286
        r1 = self.client.create_server_metadata(
            self.server1['id'],
287
288
289
290
291
292
293
294
295
296
297
            'mymeta',
            'val')
        self.assertTrue('mymeta' in r1)
        self.client.delete_server_metadata(self.server1['id'], 'mymeta')
        try:
            self.client.get_server_metadata(self.server1['id'], 'mymeta')
            raise ClientError('Wrong Error', status=100)
        except ClientError as err:
            self.assertEqual(err.status, 404)

    def test_list_flavors(self):
298
        ""Test flavors_get""
299
300
301
302
303
304
305
306
307
        self._test_0120_list_flavors()

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

    def test_get_flavor_details(self):
308
        ""Test test_get_flavor_details""
309
310
311
312
313
314
315
        self._test_0130_get_flavor_details()

    def _test_0130_get_flavor_details(self):
        r = self.client.get_flavor_details(self.flavorid)
        self.assert_dicts_are_deeply_equal(self._flavor_details, r)

    def test_list_images(self):
316
        ""Test list_images""
317
318
319
320
321
322
323
324
325
326
327
328
        self._test_0140_list_images()

    def _test_0140_list_images(self):
        r = self.client.list_images()
        self.assertTrue(len(r) > 1)
        r = self.client.list_images(detail=True)
        for detailed_img in r:
            if detailed_img['id'] == self.img:
                break
        self.assert_dicts_are_deeply_equal(detailed_img, self.img_details)

    def test_get_image_details(self):
329
        ""Test image_details""
330
331
332
333
334
335
336
        self._test_0150_get_image_details()

    def _test_0150_get_image_details(self):
        r = self.client.get_image_details(self.img)
        self.assert_dicts_are_deeply_equal(r, self.img_details)

    def test_get_image_metadata(self):
337
        ""Test get_image_metadata""
338
339
340
341
342
343
344
345
346
347
348
        self._test_0160_get_image_metadata()

    def _test_0160_get_image_metadata(self):
        r = self.client.get_image_metadata(self.img)
        self.assert_dicts_are_deeply_equal(
            self.img_details['metadata']['values'], r)
        for key, val in self.img_details['metadata']['values'].items():
            r = self.client.get_image_metadata(self.img, key)
            self.assertEqual(r[key], val)

    def test_shutdown_server(self):
349
        ""Test shutdown_server""
350
351
        self.server1 = self._create_server(
            self.servname1,
352
353
354
355
356
357
358
359
360
361
362
363
            self.flavorid,
            self.img)
        self._wait_for_status(self.server1['id'], 'BUILD')
        self._test_0170_shutdown_server()

    def _test_0170_shutdown_server(self):
        self.client.shutdown_server(self.server1['id'])
        self._wait_for_status(self.server1['id'], 'ACTIVE')
        r = self.client.get_server_details(self.server1['id'])
        self.assertEqual(r['status'], 'STOPPED')

    def test_start_server(self):
364
        ""Test start_server""
365
366
        self.server1 = self._create_server(
            self.servname1,
367
368
369
370
371
372
373
374
375
376
377
378
            self.flavorid,
            self.img)
        self._wait_for_status(self.server1['id'], 'BUILD')
        self.client.shutdown_server(self.server1['id'])
        self._wait_for_status(self.server1['id'], 'ACTIVE')
        self._test_0180_start_server()

    def _test_0180_start_server(self):
        self.client.start_server(self.server1['id'])
        self._wait_for_status(self.server1['id'], 'STOPPED')
        r = self.client.get_server_details(self.server1['id'])
        self.assertEqual(r['status'], 'ACTIVE')
379
380

    def test_get_server_console(self):
381
        ""Test get_server_console""
382
383
        self.server2 = self._create_server(
            self.servname2,
384
385
386
387
388
389
390
391
392
393
394
395
396
            self.flavorid + 2,
            self.img)
        self._wait_for_status(self.server2['id'], 'BUILD')
        self._test_0190_get_server_console()

    def _test_0190_get_server_console(self):
        r = self.client.get_server_console(self.server2['id'])
        self.assertTrue('host' in r)
        self.assertTrue('password' in r)
        self.assertTrue('port' in r)
        self.assertTrue('type' in r)

    def test_get_firewall_profile(self):
397
        ""Test get_firewall_profile""
398
399
        self.server1 = self._create_server(
            self.servname1,
400
401
402
403
404
405
406
407
408
409
            self.flavorid,
            self.img)
        self._test_0200_get_firewall_profile()

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

    def test_set_firewall_profile(self):
410
        ""Test set_firewall_profile""
411
412
        self.server1 = self._create_server(
            self.servname1,
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
            self.flavorid,
            self.img)
        self._test_0210_set_firewall_profile()

    def _test_0210_set_firewall_profile(self):

        self._wait_for_status(self.server1['id'], 'BUILD')
        PROFILES = ['DISABLED', 'ENABLED', 'DISABLED', 'PROTECTED']
        fprofile = self.client.get_firewall_profile(self.server1['id'])
        print('')
        count_success = 0
        for counter, fprofile in enumerate(PROFILES):
            npos = counter + 1
            try:
                nprofile = PROFILES[npos]
            except IndexError:
                nprofile = PROFILES[0]
            print('\tprofile swap %s: %s -> %s' % (npos, fprofile, nprofile))
            self.client.set_firewall_profile(self.server1['id'], nprofile)
            time.sleep(0.5)
            self.client.reboot_server(self.server1['id'], hard=True)
            time.sleep(1)
            self._wait_for_status(self.server1['id'], 'REBOOT')
            time.sleep(0.5)
            changed = self.client.get_firewall_profile(self.server1['id'])
            try:
                self.assertEqual(changed, nprofile)
            except AssertionError as err:
                if count_success:
                    print('\tFAIL in swap #%s' % npos)
                    break
                else:
                    raise err
            count_success += 1

    def test_get_server_stats(self):
449
450
        self.server1 = self._create_server(
            self.servname1,
451
452
453
454
455
456
            self.flavorid,
            self.img)
        self._test_0220_get_server_stats()

    def _test_0220_get_server_stats(self):
        r = self.client.get_server_stats(self.server1['id'])
457
458
        it = ('cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries', 'refresh')
        for term in it:
459
460
461
            self.assertTrue(term in r)

    def test_create_network(self):
462
        ""Test create_network""
463
464
465
        self._test_0230_create_network()

    def _test_0230_create_network(self):
466
        print('\twith no params')
467
468
        self.network1 = self._create_network(self.netname1)
        self._wait_for_network(self.network1['id'], 'ACTIVE')
469
470
        n1id = self.network1['id']
        self.network1 = self.client.get_network_details(n1id)
471
        nets = self.client.list_networks(self.network1['id'])
472
        chosen = [net for net in nets if net['id'] == n1id][0]
473
474
475
476
        chosen.pop('updated')
        net1 = dict(self.network1)
        net1.pop('updated')
        self.assert_dicts_are_deeply_equal(chosen, net1)
477
478
479
480
481
482
483
484
485
486
487
488
489
490
        for param, val in dict(
                cidr='192.168.0.0/24',
                gateway='192.168.0.1',
                type='MAC_FILTERED',
                dhcp=True).items():
            print('\tdelete %s to avoid max net limit' % n1id)
            self._delete_network(n1id)
            kwargs = {param: val}
            print('\twith %s=%s' % (param, val))
            self.network1 = self._create_network(self.netname1, **kwargs)
            n1id = self.network1['id']
            self._wait_for_network(n1id, 'ACTIVE')
            self.network1 = self.client.get_network_details(n1id)
            self.assertEqual(self.network1[param], val)
491
492

    def test_connect_server(self):
493
        ""Test connect_server""
494
495
        self.server1 = self._create_server(
            self.servname1,
496
497
498
499
500
501
502
503
504
505
506
507
508
509
            self.flavorid,
            self.img)
        self.network1 = self._create_network(self.netname1)
        self._wait_for_status(self.server1['id'], 'BUILD')
        self._wait_for_network(self.network1['id'], 'ACTIVE')
        self._test_0240_connect_server()

    def _test_0250_connect_server(self):
        self.client.connect_server(self.server1['id'], self.network1['id'])
        self.assertTrue(self._wait_for_nic(
            self.network1['id'],
            self.server1['id']))

    def test_disconnect_server(self):
510
        ""Test disconnect_server""
511
512
513
514
515
        self.test_connect_server()
        self._test_0250_disconnect_server()

    def _test_0250_disconnect_server(self):
        self.client.disconnect_server(self.server1['id'], self.network1['id'])
516
517
        self.assertTrue(self._wait_for_nic(
            self.network1['id'],
518
519
520
521
522
523
524
525
            self.server1['id'],
            in_creation=False))

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

    def test_list_server_nics(self):
526
        ""Test list_server_nics""
527
528
        self.server1 = self._create_server(
            self.servname1,
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
            self.flavorid,
            self.img)
        self.network2 = self._create_network(self.netname2)
        self._wait_for_status(self.server1['id'], 'BUILD')
        self._wait_for_network(self.network2['id'], 'ACTIVE')
        self._test_0280_list_server_nics()

    def _test_0280_list_server_nics(self):
        r = self.client.list_server_nics(self.server1['id'])
        len0 = len(r)
        self.client.connect_server(self.server1['id'], self.network2['id'])
        self.assertTrue(self._wait_for_nic(
            self.network2['id'],
            self.server1['id']))
        r = self.client.list_server_nics(self.server1['id'])
        self.assertTrue(len(r) > len0)

    def test_list_networks(self):
547
        ""Test list_network""
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
        self.network1 = self._create_network(self.netname1)
        self._wait_for_network(self.network1['id'], 'ACTIVE')
        self._test_0290_list_networks()

    def _test_0290_list_networks(self):
        r = self.client.list_networks()
        self.assertTrue(len(r) > 1)
        ids = [net['id'] for net in r]
        names = [net['name'] for net in r]
        self.assertTrue('1' in ids)
        #self.assertTrue('public' in names)
        self.assertTrue(self.network1['id'] in ids)
        self.assertTrue(self.network1['name'] in names)

        r = self.client.list_networks(detail=True)
        ids = [net['id'] for net in r]
        names = [net['name'] for net in r]
        for net in r:
            self.assertTrue(net['id'] in ids)
            self.assertTrue(net['name'] in names)
            for term in ('status', 'updated', 'created'):
                self.assertTrue(term in net.keys())

    def test_get_network_details(self):
        self.network1 = self._create_network(self.netname1)
        self._test_0300_get_network_details()

    def _test_0300_get_network_details(self):
        r = self.client.get_network_details(self.network1['id'])
        net1 = dict(self.network1)
        net1.pop('status')
        net1.pop('updated', None)
        net1.pop('attachments')
        r.pop('status')
        r.pop('updated', None)
        r.pop('attachments')
        self.assert_dicts_are_deeply_equal(net1, r)

    def test_update_network_name(self):
        self.network2 = self._create_network(self.netname2)
        self._test_0310_update_network_name()

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

        def netwait(wait):
            r = self.client.get_network_details(self.network2['id'])
            if r['name'] == updated_name:
                return
            time.sleep(wait)
        self.do_with_progress_bar(
            netwait,
            'Network %s name is changing:' % self.network2['id'],
            self._waits[:5])

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

607
    "" Don't have auth to test this
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
    def test_delete_image(self):
        ""Test delete_image""
        self._test_0330_delete_image()
    def _test_0330_delete_image(self):
        images = self.client.list_images()
        self.client.delete_image(images[2]['id'])
        try:
            r = self.client.get_image_details(images[2]['id'], success=(400))
        except ClientError as err:
            self.assertEqual(err.status, 404)

    def test_create_image_metadata(self):
        ""Test create_image_metadata""
        self._test_0340_create_image_metadata()
    def _test_0340_create_image_metadata(self):
        r = self.client.create_image_metadata(self.img, 'mykey', 'myval')
        self.assertEqual(r['mykey'], 'myval')

    def test_update_image_metadata(self):
        ""Test update_image_metadata""
        self._test_0350_update_image_metadata()
    def _test_0350_update_image_metadata(self):
        r = self.client.create_image_metadata(self.img, 'mykey0', 'myval')
        r = self.client.update_image_metadata(self.img, 'mykey0', 'myval0')
        self.assertEqual(r['mykey0'], 'myval0')

    def test_delete_image_metadata(self):
        ""Test delete_image_metadata""
        self._test_0360_delete_image_metadata()
    def _test_0360_delete_image_metadata(self):
        self.client.create_image_metadata(self.img, 'mykey1', 'myval1')
        self.client.delete_image_metadata(self.img, 'mykey1')
        r = self.client.get_image_metadata(self.img)
        self.assertNotEqual('mykey1' in r)
    """