Revision 6d192774

b/kamaki/clients/_tests.py
1
# Copyright 2011 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
from argparse import ArgumentParser
35
import unittest
36
import time
37
import datetime
38
import os
39
import sys
40
import tempfile
41
from logging import getLogger
42

  
43
kloger = getLogger('kamaki')
44

  
45
try:
46
    from progress.bar import FillingCirclesBar as IncrementalBar
47
except ImportError:
48
    kloger.warning('No progress bars in testing!')
49
    pass
50

  
51
from kamaki.clients import ClientError
52
from kamaki.clients.pithos import PithosClient as pithos
53
from kamaki.clients.cyclades import CycladesClient as cyclades
54
from kamaki.clients.image import ImageClient as image
55
from kamaki.clients.astakos import AstakosClient as astakos
56
from kamaki.cli.config import Config
57

  
58
TEST_ALL = False
59

  
60
cnf = Config()
61
global_username = None
62
token = None
63

  
64

  
65
def _init_cnf():
66
    global cnf
67
    global global_username
68
    global_username = cnf.get('test', 'account') or\
69
        cnf.get('global', 'account')
70
    global token
71
    token = cnf.get('test', 'token') or cnf.get('global', 'token')
72

  
73

  
74
class testAstakos(unittest.TestCase):
75
    def setUp(self):
76
        _init_cnf()
77
        global cnf
78
        url = cnf.get('test', 'astakos_url') or cnf.get('astakos', 'url')
79
        global token
80
        self.client = astakos(url, token)
81

  
82
    def tearDown(self):
83
        pass
84

  
85
    def test_authenticate(self):
86
        r = self.client.authenticate()
87
        for term in ('username',
88
            'auth_token_expires',
89
            'auth_token',
90
            'auth_token_created',
91
            'groups',
92
            'uniq',
93
            'has_credits',
94
            'has_signed_terms'):
95
            self.assertTrue(term in r)
96

  
97

  
98
class testImage(unittest.TestCase):
99
    def setUp(self):
100
        _init_cnf()
101
        global cnf
102
        cyclades_url = cnf.get('compute', 'url')
103
        url = cnf.get('image', 'url')
104
        global token
105
        self.token = token
106
        self.imgid = 'b2dffe52-64a4-48c3-8a4c-8214cc3165cf'
107
        self.now = time.mktime(time.gmtime())
108
        self.imgname = 'img_%s' % self.now
109
        global global_username
110
        self.imglocation = 'pithos://%s/pithos/my.img'\
111
        % global_username
112
        self.client = image(url, self.token)
113
        self.cyclades = cyclades(cyclades_url, self.token)
114
        self._imglist = {}
115

  
116
    def _prepare_img(self):
117
        global cnf
118
        global global_username
119
        username = global_username.split('@')[0]
120
        imglocalpath =\
121
        '/home/%s/src/kamaki-settings/files/centos.diskdump' % username
122
        f = open(imglocalpath, 'rb')
123
        pithcli = pithos(cnf.get('store', 'url'),
124
            self.token,
125
            global_username,
126
            'pithos')
127
        print('\t- Upload an image at %s...' % imglocalpath)
128
        pithcli.upload_object('my.img', f)
129
        print('\t- ok')
130
        f.close()
131

  
132
        self.client.register(self.imgname,
133
            self.imglocation,
134
            params=dict(is_public=True))
135
        img = self._get_img_by_name(self.imgname)
136
        self._imglist[self.imgname] = img
137

  
138
    def tearDown(self):
139
        for img in self._imglist.values():
140
            self.cyclades.delete_image(img['id'])
141

  
142
    def _get_img_by_name(self, name):
143
        r = self.cyclades.list_images()
144
        for img in r:
145
            if img['name'] == name:
146
                return img
147
        return None
148

  
149
    def assert_dicts_are_deeply_equal(self, d1, d2):
150
        for k, v in d1.items():
151
            self.assertTrue(k in d2)
152
            if isinstance(v, dict):
153
                self.assert_dicts_are_deeply_equal(v, d2[k])
154
            else:
155
                self.assertEqual(unicode(v), unicode(d2[k]))
156

  
157
    def test_list_public(self):
158
        """Test list_public"""
159
        r = self.client.list_public()
160
        r0 = self.client.list_public(order='-')
161
        self.assertTrue(len(r) > 0)
162
        for img in r:
163
            for term in ('status',
164
                'name',
165
                'container_format',
166
                'disk_format',
167
                'id',
168
                'size'):
169
                self.assertTrue(term in img)
170
        self.assertTrue(len(r), len(r0))
171
        r0.reverse()
172
        for i, img in enumerate(r):
173
            self.assert_dicts_are_deeply_equal(img, r0[i])
174
        r1 = self.client.list_public(detail=True)
175
        for img in r1:
176
            for term in ('status',
177
                'name',
178
                'checksum',
179
                'created_at',
180
                'disk_format',
181
                'updated_at',
182
                'id',
183
                'location',
184
                'container_format',
185
                'owner',
186
                'is_public',
187
                'deleted_at',
188
                'properties',
189
                'size'):
190
                self.assertTrue(term in img)
191
                for interm in (
192
                    'osfamily',
193
                    'users',
194
                    'os',
195
                    'root_partition',
196
                    'description'):
197
                    self.assertTrue(interm in img['properties'])
198
        size_max = 1000000000
199
        r2 = self.client.list_public(filters=dict(size_max=size_max))
200
        self.assertTrue(len(r2) <= len(r))
201
        for img in r2:
202
            self.assertTrue(int(img['size']) <= size_max)
203

  
204
    def test_get_meta(self):
205
        """Test get_meta"""
206
        r = self.client.get_meta(self.imgid)
207
        self.assertEqual(r['id'], self.imgid)
208
        for term in ('status',
209
            'name',
210
            'checksum',
211
            'updated-at',
212
            'created-at',
213
            'deleted-at',
214
            'location',
215
            'is-public',
216
            'owner',
217
            'disk-format',
218
            'size',
219
            'container-format'):
220
            self.assertTrue(term in r)
221
            for interm in ('kernel',
222
                'osfamily',
223
                'users',
224
                'gui', 'sortorder',
225
                'root-partition',
226
                'os',
227
                'description'):
228
                self.assertTrue(interm in r['properties'])
229

  
230
    def test_register(self):
231
        """Test register"""
232
        self._prepare_img()
233
        self.assertTrue(len(self._imglist) > 0)
234
        for img in self._imglist.values():
235
            self.assertTrue(img != None)
236

  
237
    def test_reregister(self):
238
        """Test reregister"""
239
        self._prepare_img()
240
        self.client.reregister(self.imglocation,
241
            properties=dict(my_property='some_value'))
242

  
243
    def test_set_members(self):
244
        """Test set_members"""
245
        self._prepare_img()
246
        members = ['%s@fake.net' % self.now]
247
        for img in self._imglist.values():
248
            self.client.set_members(img['id'], members)
249
            r = self.client.list_members(img['id'])
250
            self.assertEqual(r[0]['member_id'], members[0])
251

  
252
    def test_list_members(self):
253
        """Test list_members"""
254
        self.test_set_members()
255

  
256
    def test_remove_members(self):
257
        """Test remove_members - NO CHECK"""
258
        return
259
        self._prepare_img()
260
        members = ['%s@fake.net' % self.now, '%s_v2@fake.net' % self.now]
261
        for img in self._imglist.values():
262
            self.client.set_members(img['id'], members)
263
            r = self.client.list_members(img['id'])
264
            self.assertTrue(len(r) > 1)
265
            self.client.remove_member(img['id'], members[0])
266
            r0 = self.client.list_members(img['id'])
267
            self.assertEqual(len(r), 1 + len(r0))
268
            self.assertEqual(r0[0]['member_id'], members[1])
269

  
270
    def test_list_shared(self):
271
        """Test list_shared - NOT CHECKED"""
272
        #No way to test this, if I dont have member images
273
        pass
274

  
275

  
276
class testCyclades(unittest.TestCase):
277
    """Set up a Cyclades thorough test"""
278
    def setUp(self):
279
        """okeanos"""
280
        _init_cnf()
281
        global cnf
282
        url = cnf.get('compute', 'url')
283
        global token
284
        global global_username
285
        self.img = 'b2dffe52-64a4-48c3-8a4c-8214cc3165cf'
286
        self.img_details = {
287
            u'status': u'ACTIVE',
288
            u'updated': u'2012-11-19T13:52:16+00:00',
289
            u'name': u'Debian Base',
290
            u'created': u'2012-10-16T09:03:12+00:00',
291
            u'progress': 100,
292
            u'id': self.img,
293
            u'metadata': {
294
                u'values': {
295
                    u'kernel': u'2.6.32',
296
                    u'osfamily': u'linux',
297
                    u'users': u'root',
298
                    u'gui': u'No GUI',
299
                    u'sortorder': u'1',
300
                    u'os': u'debian',
301
                    u'root_partition': u'1',
302
                    u'description': u'Debian 6.0.6 (Squeeze) Base System'}
303
                }
304
            }
305
        self.flavor_details = {u'name': u'C1R1024D20',
306
            u'ram': 1024,
307
            u'id': 1,
308
            u'SNF:disk_template': u'drbd',
309
            u'disk': 20,
310
            u'cpu': 1}
311
        self.PROFILES = ('ENABLED', 'DISABLED', 'PROTECTED')
312

  
313
        """okeanos.io """
314
        """
315
        self.img = 'b3e68235-3abd-4d60-adfe-1379a4f8d3fe'
316
        self.img_details = {
317
            u'status': u'ACTIVE',
318
            u'updated': u'2012-11-19T15:29:51+00:00',
319
            u'name': u'Debian Base',
320
            u'created': u'2012-11-19T14:54:57+00:00',
321
            u'progress': 100,
322
            u'id': self.img,
323
            u'metadata': {
324
                u'values': {
325
                    u'kernel': u'2.6.32',
326
                    u'osfamily': u'linux',
327
                    u'users': u'root',
328
                    u'gui': u'No GUI',
329
                    u'sortorder': u'1',
330
                    u'os': u'debian',
331
                    u'root_partition': u'1',
332
                    u'description': u'Debian 6.0.6 (Squeeze) Base System'}
333
                }
334
            }
335
            """
336

  
337
        self.servers = {}
338
        self.now = time.mktime(time.gmtime())
339
        self.servname1 = 'serv' + unicode(self.now)
340
        self.servname2 = self.servname1 + '_v2'
341
        self.flavorid = 1
342
        #servers have to be created at the begining...
343
        self.networks = {}
344
        self.netname1 = 'net' + unicode(self.now)
345
        self.netname2 = 'net' + unicode(self.now) + '_v2'
346

  
347
        self.client = cyclades(url, token)
348
        pass
349

  
350
    def tearDown(self):
351
        """Destoy servers used in testing"""
352
        print
353
        for netid in self.networks.keys():
354
            self._delete_network(netid)
355
        if 0 >= len(self.servers):
356
            return
357
        print('-> Found %s servers to delete' % len(self.servers))
358
        for server in self.servers.values():
359
            self._delete_server(server['id'])
360

  
361
    def _create_server(self, servername, flavorid, imageid, personality=None):
362
        server = self.client.create_server(servername,
363
            flavorid,
364
            imageid,
365
            personality)
366
        self.servers[servername] = server
367
        return server
368

  
369
    def _delete_server(self, servid):
370
        try:
371
            current_state = self.client.get_server_details(servid)
372
            current_state = current_state['status']
373
            if current_state == 'DELETED':
374
                return
375
        except:
376
            return
377
        self.client.delete_server(servid)
378
        self._wait_for_status(servid, current_state)
379

  
380
    def _create_network(self, netname, **kwargs):
381
        net = self.client.create_network(netname, **kwargs)
382
        self.networks[net['id']] = net
383
        return net
384

  
385
    def _delete_network(self, netid):
386
        sys.stdout.write('\tDelete network %s ' % netid)
387
        self.client.disconnect_network_nics(netid)
388
        wait = 3
389
        while True:
390
            try:
391
                self.client.delete_network(netid)
392
                print('\n\tSUCCESFULL COMMIT delete network %s' % netid)
393
                break
394
            except ClientError as err:
395
                self.assertEqual(err.status, 421)
396
                time.sleep(wait)
397
                wait += 3
398
                sys.stdout.write('.')
399

  
400
    def if_not_all(foo):
401
        global TEST_ALL
402
        if TEST_ALL:
403
            return None
404
        return foo
405

  
406
    def assert_dicts_are_deeply_equal(self, d1, d2):
407
        for k, v in d1.items():
408
            self.assertTrue(k in d2)
409
            if isinstance(v, dict):
410
                self.assert_dicts_are_deeply_equal(v, d2[k])
411
            else:
412
                self.assertEqual(unicode(v), unicode(d2[k]))
413

  
414
    def test_000(self):
415
        "Prepare a full Cyclades test scenario"
416
        global TEST_ALL
417
        TEST_ALL = True
418

  
419
        self.server1 = self._create_server(self.servname1,
420
            self.flavorid,
421
            self.img)
422
        self.server2 = self._create_server(self.servname2,
423
            self.flavorid + 2,
424
            self.img)
425

  
426
        print('testing')
427
        sys.stdout.write(' test create server')
428
        self._test_create_server()
429
        print('...ok')
430

  
431
        sys.stdout.write(' test list servers')
432
        self._test_list_servers()
433
        print('...ok')
434

  
435
        print('- wait for test servers to build')
436
        self._wait_for_status(self.server1['id'], 'BUILD')
437
        self._wait_for_status(self.server2['id'], 'BUILD')
438
        print('- ok')
439

  
440
        sys.stdout.write(' test get server details')
441
        self._test_get_server_details()
442
        print('...ok')
443

  
444
        sys.stdout.write(' test get image details')
445
        self._test_get_image_details()
446
        print('...ok')
447

  
448
        sys.stdout.write(' test update_server_name')
449
        self._test_update_server_name()
450
        print('...ok')
451

  
452
        sys.stdout.write(' test reboot_server')
453
        self._test_reboot_server()
454
        print('...ok')
455

  
456
        print('- wait for test servers to boot')
457
        self._wait_for_status(self.server1['id'], 'REBOOT')
458
        self._wait_for_status(self.server2['id'], 'REBOOT')
459
        print('- ok')
460

  
461
        sys.stdout.write(' test create_server_metadata')
462
        self._test_create_server_metadata()
463
        print('...ok')
464

  
465
        sys.stdout.write(' test get_server_metadata')
466
        self._test_get_server_metadata()
467
        print('...ok')
468

  
469
        sys.stdout.write(' test update_server_metadata')
470
        self._test_update_server_metadata()
471
        print('...ok')
472

  
473
        sys.stdout.write(' test delete_server_metadata')
474
        self._test_delete_server_metadata()
475
        print('...ok')
476

  
477
        sys.stdout.write(' test list_flavors')
478
        self._test_list_flavors()
479
        print('...ok')
480

  
481
        sys.stdout.write(' test get_flavor_details')
482
        self._test_get_flavor_details()
483
        print('...ok')
484

  
485
        sys.stdout.write(' test list_images')
486
        self._test_list_images()
487
        print('...ok')
488

  
489
        sys.stdout.write(' test get_image_details')
490
        self._test_get_image_details()
491
        print('...ok')
492

  
493
        sys.stdout.write(' test get_image_metadata')
494
        self._test_get_image_metadata()
495
        print('...ok')
496

  
497
        sys.stdout.write(' test shutdown_server')
498
        self._test_shutdown_server()
499
        print('...ok')
500

  
501
        sys.stdout.write(' test start_server')
502
        self._test_start_server()
503
        print('...ok')
504

  
505
        sys.stdout.write(' test get_server_console')
506
        self._test_get_server_console()
507
        print('...ok')
508

  
509
        sys.stdout.write(' test get_firewall_profile')
510
        self._test_get_firewall_profile()
511
        print('...ok')
512

  
513
        sys.stdout.write(' test set_firewall_profile')
514
        self._test_set_firewall_profile()
515
        print('...ok')
516

  
517
        sys.stdout.write(' test get_server_stats')
518
        self._test_get_server_stats()
519
        print('...ok')
520

  
521
        self.network1 = self._create_network(self.netname1)
522

  
523
        sys.stdout.write(' test create_network')
524
        self._test_create_network()
525
        print('...ok')
526

  
527
        print('- wait for network to be activated')
528
        self._wait_for_network(self.network1['id'], 'ACTIVE')
529
        print('- ok')
530

  
531
        sys.stdout.write(' test connect_server')
532
        self._test_connect_server()
533
        print('...ok')
534

  
535
        sys.stdout.write(' test disconnect_server')
536
        self._test_disconnect_server()
537
        print('...ok')
538

  
539
        self.network2 = self._create_network(self.netname2)
540
        print('- wait for network to be activated')
541
        self._wait_for_network(self.network2['id'], 'ACTIVE')
542
        print('- ok')
543

  
544
        sys.stdout.write(' test list_server_nics')
545
        self._test_list_server_nics()
546
        print('...ok')
547

  
548
        sys.stdout.write(' test list_networks')
549
        self._test_list_networks()
550
        print('...ok')
551

  
552
        sys.stdout.write(' test get_network_details')
553
        self._test_get_network_details()
554
        print('...ok')
555

  
556
        sys.stdout.write(' test update_network_name')
557
        self._test_update_network_name()
558
        print('...ok')
559

  
560
        """Don't have auth for these:
561
        sys.stdout.write(' test delete_image')
562
        self._test_delete_image()
563
        print('...ok')
564
        sys.stdout.write(' test create_image_metadata')
565
        self._test_create_image_metadata()
566
        print('...ok')
567
        sys.stdout.write(' test update_image_metadata')
568
        self._test_update_image_metadata()
569
        print('...ok')
570
        sys.stdout.write(' test delete_image_metadata')
571
        self._test_delete_image_metadata()
572
        print('...ok')
573
        """
574

  
575
    @if_not_all
576
    def test_parallel_creation(self):
577
        """test create with multiple threads"""
578
        from kamaki.clients import SilentEvent
579
        c1 = SilentEvent(self._create_server,
580
            self.servname1,
581
            self.flavorid,
582
            self.img)
583
        c2 = SilentEvent(self._create_server,
584
            self.servname2,
585
            self.flavorid + 2,
586
            self.img)
587
        c3 = SilentEvent(self._create_server,
588
            self.servname1,
589
            self.flavorid,
590
            self.img)
591
        c4 = SilentEvent(self._create_server,
592
            self.servname2,
593
            self.flavorid + 2,
594
            self.img)
595
        c5 = SilentEvent(self._create_server,
596
            self.servname1,
597
            self.flavorid,
598
            self.img)
599
        c6 = SilentEvent(self._create_server,
600
            self.servname2,
601
            self.flavorid + 2,
602
            self.img)
603
        c7 = SilentEvent(self._create_server,
604
            self.servname1,
605
            self.flavorid,
606
            self.img)
607
        c8 = SilentEvent(self._create_server,
608
            self.servname2,
609
            self.flavorid + 2,
610
            self.img)
611
        c1.start()
612
        c2.start()
613
        c3.start()
614
        c4.start()
615
        c5.start()
616
        c6.start()
617
        c7.start()
618
        c8.start()
619

  
620
    def _wait_for_network(self, netid, status):
621
        wait = 3
622
        limit = 50
623
        c = ['|', '/', '-', '\\']
624
        sys.stdout.write('\t- make net %s %s  ' % (netid, status))
625
        while wait < limit:
626
            r = self.client.get_network_details(netid)
627
            if r['status'] == status:
628
                print('\tOK')
629
                return True
630
            sys.stdout.write('\tit is now %s, wait %ss  '\
631
                % (r['status'], wait))
632
            for i in range(wait * 4):
633
                sys.stdout.write('\b%s' % c[i % 4])
634
                sys.stdout.flush()
635
                time.sleep(0.25)
636
            print('\b ')
637
            wait += 3
638
        return False
639

  
640
    def _wait_for_nic(self, netid, servid, in_creation=True):
641
        self._wait_for_network(netid, 'ACTIVE')
642
        c = ['|', '/', '-', '\\']
643
        limit = 50
644
        wait = 3
645
        largetry = 0
646
        while wait < limit:
647
            nics = self.client.list_server_nics(servid)
648
            for net in nics:
649
                found_nic = net['network_id'] == netid
650
                if (in_creation and found_nic)\
651
                or not (in_creation or found_nic):
652
                    return True
653
            dis = '' if in_creation else 'dis'
654
            sys.stdout.write('\twait nic %s to %sconnect to %s: %ss  '\
655
                % (netid, dis, servid, wait))
656
            for i in range(wait * 4):
657
                sys.stdout.write('\b%s' % c[i % 4])
658
                sys.stdout.flush()
659
                time.sleep(0.25)
660
            print('\b ')
661
            wait += 3
662
            if wait >= limit and largetry < 3:
663
                wait = 3
664
                largetry += 1
665
        return False
666

  
667
    def _has_status(self, servid, status):
668
        r = self.client.get_server_details(servid)
669
        return r['status'] == status
670

  
671
    def _wait_for_status(self, servid, status):
672
        withbar = True
673
        try:
674
            wait_bar = IncrementalBar('\tServer[%s] in %s ' % (servid, status))
675
        except NameError:
676
            withbar = False
677

  
678
        wait_cb = None
679
        if withbar:
680
            wait_bar.start()
681

  
682
            def progress_gen(n):
683
                for i in wait_bar.iter(range(int(n))):
684
                    yield
685
                yield
686

  
687
            wait_cb = progress_gen
688

  
689
        time.sleep(0.5)
690
        self.client.wait_server(servid, status, wait_cb=wait_cb)
691
        if withbar:
692
            wait_bar.finish()
693

  
694
    @if_not_all
695
    def test_list_servers(self):
696
        """Test list servers"""
697
        self.server1 = self._create_server(self.servname1,
698
            self.flavorid,
699
            self.img)
700
        self.server2 = self._create_server(self.servname2,
701
            self.flavorid + 1,
702
            self.img)
703
        self._test_list_servers()
704

  
705
    def _test_list_servers(self):
706
        servers = self.client.list_servers()
707
        dservers = self.client.list_servers(detail=True)
708

  
709
        """detailed and simple are same size"""
710
        self.assertEqual(len(dservers), len(servers))
711
        for i in range(len(servers)):
712
            for field in ('created',
713
            'flavorRef',
714
            'hostId',
715
            'imageRef',
716
            'progress',
717
            'status',
718
            'updated'):
719
                self.assertFalse(field in servers[i])
720
                self.assertTrue(field in dservers[i])
721

  
722
        """detailed and simple contain same names"""
723
        names = sorted(map(lambda x: x["name"], servers))
724
        dnames = sorted(map(lambda x: x["name"], dservers))
725
        self.assertEqual(names, dnames)
726

  
727
    @if_not_all
728
    def test_create_server(self):
729
        """Test create_server"""
730
        self.server1 = self._create_server(self.servname1,
731
            self.flavorid,
732
            self.img)
733
        self._wait_for_status(self.server1['id'], 'BUILD')
734
        self._test_create_server()
735

  
736
    def _test_create_server(self):
737
        self.assertEqual(self.server1["name"], self.servname1)
738
        self.assertEqual(self.server1["flavorRef"], self.flavorid)
739
        self.assertEqual(self.server1["imageRef"], self.img)
740
        self.assertEqual(self.server1["status"], "BUILD")
741

  
742
    @if_not_all
743
    def test_get_server_details(self):
744
        """Test get_server_details"""
745
        self.server1 = self._create_server(self.servname1,
746
            self.flavorid,
747
            self.img)
748
        self._wait_for_status(self.server1['id'], 'BUILD')
749
        self._test_get_server_details()
750

  
751
    def _test_get_server_details(self):
752
        r = self.client.get_server_details(self.server1['id'])
753
        self.assertEqual(r["name"], self.servname1)
754
        self.assertEqual(r["flavorRef"], self.flavorid)
755
        self.assertEqual(r["imageRef"], self.img)
756
        self.assertEqual(r["status"], "ACTIVE")
757

  
758
    @if_not_all
759
    def test_update_server_name(self):
760
        """Test update_server_name"""
761
        self.server1 = self._create_server(self.servname1,
762
            self.flavorid,
763
            self.img)
764
        self._test_update_server_name()
765

  
766
    def _test_update_server_name(self):
767
        new_name = self.servname1 + '_new_name'
768
        self.client.update_server_name(self.server1['id'], new_name)
769
        r = self.client.get_server_details(self.server1['id'],
770
         success=(200, 400))
771
        self.assertEqual(r['name'], new_name)
772
        changed = self.servers.pop(self.servname1)
773
        changed['name'] = new_name
774
        self.servers[new_name] = changed
775

  
776
    @if_not_all
777
    def test_reboot_server(self):
778
        """Test reboot server"""
779
        self.server1 = self._create_server(self.servname1,
780
            self.flavorid,
781
            self.img)
782
        self._wait_for_status(self.server1['id'], 'BUILD')
783
        self.server2 = self._create_server(self.servname2,
784
            self.flavorid + 1,
785
            self.img)
786
        self._wait_for_status(self.server2['id'], 'BUILD')
787
        self._test_reboot_server()
788
        self._wait_for_status(self.server1['id'], 'REBOOT')
789
        self._wait_for_status(self.server2['id'], 'REBOOT')
790

  
791
    def _test_reboot_server(self):
792
        self.client.reboot_server(self.server1['id'])
793
        self.assertTrue(self._has_status(self.server1['id'], 'REBOOT'))
794
        self.client.reboot_server(self.server2['id'], hard=True)
795
        self.assertTrue(self._has_status(self.server2['id'], 'REBOOT'))
796

  
797
    @if_not_all
798
    def test_get_server_metadata(self):
799
        """Test get server_metadata"""
800
        self.server1 = self._create_server(self.servname1,
801
            self.flavorid,
802
            self.img)
803
        self._test_get_server_metadata()
804

  
805
    def _test_get_server_metadata(self):
806
        self.client.create_server_metadata(self.server1['id'],
807
            'mymeta_0',
808
            'val_0')
809
        r = self.client.get_server_metadata(self.server1['id'], 'mymeta_0')
810
        self.assertEqual(r['mymeta_0'], 'val_0')
811

  
812
    @if_not_all
813
    def test_create_server_metadata(self):
814
        """Test create_server_metadata"""
815
        self.server1 = self._create_server(self.servname1,
816
            self.flavorid,
817
            self.img)
818
        self._test_create_server_metadata()
819

  
820
    def _test_create_server_metadata(self):
821
        r1 = self.client.create_server_metadata(self.server1['id'],
822
            'mymeta',
823
            'mymeta val')
824
        self.assertTrue('mymeta' in r1)
825
        r2 = self.client.get_server_metadata(self.server1['id'], 'mymeta')
826
        self.assert_dicts_are_deeply_equal(r1, r2)
827

  
828
    @if_not_all
829
    def test_update_server_metadata(self):
830
        """Test update_server_metadata"""
831
        self.server1 = self._create_server(self.servname1,
832
            self.flavorid,
833
            self.img)
834
        self._test_update_server_metadata()
835

  
836
    def _test_update_server_metadata(self):
837
        r1 = self.client.create_server_metadata(self.server1['id'],
838
            'mymeta3',
839
            'val2')
840
        self.assertTrue('mymeta3'in r1)
841
        r2 = self.client.update_server_metadata(self.server1['id'],
842
            mymeta3='val3')
843
        self.assertTrue(r2['mymeta3'], 'val3')
844

  
845
    @if_not_all
846
    def test_delete_server_metadata(self):
847
        """Test delete_server_metadata"""
848
        self.server1 = self._create_server(self.servname1,
849
            self.flavorid,
850
            self.img)
851
        self._test_delete_server_metadata()
852

  
853
    def _test_delete_server_metadata(self):
854
        r1 = self.client.create_server_metadata(self.server1['id'],
855
            'mymeta',
856
            'val')
857
        self.assertTrue('mymeta' in r1)
858
        self.client.delete_server_metadata(self.server1['id'], 'mymeta')
859
        try:
860
            self.client.get_server_metadata(self.server1['id'], 'mymeta')
861
            raise ClientError('Wrong Error', status=100)
862
        except ClientError as err:
863
            self.assertEqual(err.status, 404)
864

  
865
    @if_not_all
866
    def test_list_flavors(self):
867
        """Test flavors_get"""
868
        self._test_list_flavors()
869

  
870
    def _test_list_flavors(self):
871
        r = self.client.list_flavors()
872
        self.assertTrue(len(r) > 1)
873
        r = self.client.list_flavors(detail=True)
874
        self.assertTrue('SNF:disk_template' in r[0])
875

  
876
    @if_not_all
877
    def test_get_flavor_details(self):
878
        """Test test_get_flavor_details"""
879
        self._test_get_flavor_details()
880

  
881
    def _test_get_flavor_details(self):
882
        r = self.client.get_flavor_details(self.flavorid)
883
        self.assert_dicts_are_deeply_equal(self.flavor_details, r)
884

  
885
    @if_not_all
886
    def test_list_images(self):
887
        """Test list_images"""
888
        self._test_list_images()
889

  
890
    def _test_list_images(self):
891
        r = self.client.list_images()
892
        self.assertTrue(len(r) > 1)
893
        r = self.client.list_images(detail=True)
894
        for detailed_img in r:
895
            if detailed_img['id'] == self.img:
896
                break
897
        self.assert_dicts_are_deeply_equal(detailed_img, self.img_details)
898

  
899
    @if_not_all
900
    def test_get_image_details(self):
901
        """Test image_details"""
902
        self._test_get_image_details()
903

  
904
    def _test_get_image_details(self):
905
        r = self.client.get_image_details(self.img)
906
        r.pop('updated')
907
        self.assert_dicts_are_deeply_equal(r, self.img_details)
908

  
909
    @if_not_all
910
    def test_get_image_metadata(self):
911
        """Test get_image_metadata"""
912
        self._test_get_image_metadata()
913

  
914
    def _test_get_image_metadata(self):
915
        r = self.client.get_image_metadata(self.img)
916
        self.assert_dicts_are_deeply_equal(
917
            self.img_details['metadata']['values'], r)
918
        for key, val in self.img_details['metadata']['values'].items():
919
            r = self.client.get_image_metadata(self.img, key)
920
            self.assertEqual(r[key], val)
921

  
922
    @if_not_all
923
    def test_start_server(self):
924
        """Test start_server"""
925
        self.server1 = self._create_server(self.servname1,
926
            self.flavorid,
927
            self.img)
928
        self._wait_for_status(self.server1['id'], 'BUILD')
929
        self.client.shutdown_server(self.server1['id'])
930
        self._wait_for_status(self.server1['id'], 'ACTIVE')
931
        self._test_start_server()
932

  
933
    def _test_start_server(self):
934
        self.client.start_server(self.server1['id'])
935
        self._wait_for_status(self.server1['id'], 'STOPPED')
936
        r = self.client.get_server_details(self.server1['id'])
937
        self.assertEqual(r['status'], 'ACTIVE')
938

  
939
    @if_not_all
940
    def test_shutdown_server(self):
941
        """Test shutdown_server"""
942
        self.server1 = self._create_server(self.servname1,
943
            self.flavorid,
944
            self.img)
945
        self._wait_for_status(self.server1['id'], 'BUILD')
946
        self._test_shutdown_server()
947

  
948
    def _test_shutdown_server(self):
949
        self.client.shutdown_server(self.server1['id'])
950
        self._wait_for_status(self.server1['id'], 'ACTIVE')
951
        r = self.client.get_server_details(self.server1['id'])
952
        self.assertEqual(r['status'], 'STOPPED')
953

  
954
    @if_not_all
955
    def test_get_server_console(self):
956
        """Test get_server_console"""
957
        self.server2 = self._create_server(self.servname2,
958
            self.flavorid + 2,
959
            self.img)
960
        self._wait_for_status(self.server2['id'], 'BUILD')
961
        self._test_get_server_console()
962

  
963
    def _test_get_server_console(self):
964
        r = self.client.get_server_console(self.server2['id'])
965
        self.assertTrue('host' in r)
966
        self.assertTrue('password' in r)
967
        self.assertTrue('port' in r)
968
        self.assertTrue('type' in r)
969

  
970
    @if_not_all
971
    def test_get_firewall_profile(self):
972
        """Test get_firewall_profile"""
973
        self.server1 = self._create_server(self.servname1,
974
            self.flavorid,
975
            self.img)
976
        self._test_get_firewall_profile()
977

  
978
    def _test_get_firewall_profile(self):
979
        self._wait_for_status(self.server1['id'], 'BUILD')
980
        fprofile = self.client.get_firewall_profile(self.server1['id'])
981
        self.assertTrue(fprofile in self.PROFILES)
982

  
983
    @if_not_all
984
    def test_set_firewall_profile(self):
985
        """Test set_firewall_profile"""
986
        self.server1 = self._create_server(self.servname1,
987
            self.flavorid,
988
            self.img)
989
        self._test_set_firewall_profile()
990

  
991
    def _test_set_firewall_profile(self):
992

  
993
        self._wait_for_status(self.server1['id'], 'BUILD')
994
        PROFILES = ['DISABLED', 'ENABLED', 'DISABLED', 'PROTECTED']
995
        fprofile = self.client.get_firewall_profile(self.server1['id'])
996
        print('')
997
        count_success = 0
998
        for counter, fprofile in enumerate(PROFILES):
999
            npos = counter + 1
1000
            try:
1001
                nprofile = PROFILES[npos]
1002
            except IndexError:
1003
                nprofile = PROFILES[0]
1004
            print('\tprofile swap %s: %s -> %s' % (npos, fprofile, nprofile))
1005
            self.client.set_firewall_profile(self.server1['id'], nprofile)
1006
            time.sleep(0.5)
1007
            self.client.reboot_server(self.server1['id'], hard=True)
1008
            time.sleep(1)
1009
            self._wait_for_status(self.server1['id'], 'REBOOT')
1010
            time.sleep(0.5)
1011
            changed = self.client.get_firewall_profile(self.server1['id'])
1012
            try:
1013
                self.assertEqual(changed, nprofile)
1014
            except AssertionError as err:
1015
                if count_success:
1016
                    print('\tFAIL in swap #%s' % npos)
1017
                    break
1018
                else:
1019
                    raise err
1020
            count_success += 1
1021

  
1022
    @if_not_all
1023
    def test_get_server_stats(self):
1024
        self.server1 = self._create_server(self.servname1,
1025
            self.flavorid,
1026
            self.img)
1027
        self._test_get_server_stats()
1028

  
1029
    def _test_get_server_stats(self):
1030
        r = self.client.get_server_stats(self.server1['id'])
1031
        for term in ('cpuBar',
1032
        'cpuTimeSeries',
1033
        'netBar',
1034
        'netTimeSeries',
1035
        'refresh'):
1036
            self.assertTrue(term in r)
1037

  
1038
    @if_not_all
1039
    def test_list_networks(self):
1040
        """Test list_network"""
1041
        self.network1 = self._create_network(self.netname1)
1042
        self._wait_for_network(self.network1['id'], 'ACTIVE')
1043
        self._test_list_networks()
1044

  
1045
    def _test_list_networks(self):
1046
        r = self.client.list_networks()
1047
        self.assertTrue(len(r) > 1)
1048
        ids = [net['id'] for net in r]
1049
        names = [net['name'] for net in r]
1050
        self.assertTrue('1' in ids)
1051
        #self.assertTrue('public' in names)
1052
        self.assertTrue(self.network1['id'] in ids)
1053
        self.assertTrue(self.network1['name'] in names)
1054

  
1055
        r = self.client.list_networks(detail=True)
1056
        ids = [net['id'] for net in r]
1057
        names = [net['name'] for net in r]
1058
        for net in r:
1059
            self.assertTrue(net['id'] in ids)
1060
            self.assertTrue(net['name'] in names)
1061
            for term in ('status', 'updated', 'created'):
1062
                self.assertTrue(term in net.keys())
1063

  
1064
    @if_not_all
1065
    def test_create_network(self):
1066
        """Test create_network"""
1067
        self.network1 = self._create_network(self.netname1)
1068
        self._test_create_network()
1069

  
1070
    def _test_create_network(self):
1071
        nets = self.client.list_networks(self.network1['id'])
1072
        chosen = [net for net in nets if net['id'] == self.network1['id']][0]
1073
        chosen.pop('updated')
1074
        net1 = dict(self.network1)
1075
        net1.pop('updated')
1076
        self.assert_dicts_are_deeply_equal(chosen, net1)
1077

  
1078
    @if_not_all
1079
    def test_connect_server(self):
1080
        """Test connect_server"""
1081
        self.server1 = self._create_server(self.servname1,
1082
            self.flavorid,
1083
            self.img)
1084
        self.network1 = self._create_network(self.netname1)
1085
        self._wait_for_status(self.server1['id'], 'BUILD')
1086
        self._wait_for_network(self.network1['id'], 'ACTIVE')
1087
        self._test_connect_server()
1088

  
1089
    def _test_connect_server(self):
1090
        self.client.connect_server(self.server1['id'], self.network1['id'])
1091
        self.assertTrue(self._wait_for_nic(self.network1['id'],
1092
            self.server1['id']))
1093

  
1094
    @if_not_all
1095
    def test_disconnect_server(self):
1096
        """Test disconnect_server"""
1097
        self.test_connect_server()
1098
        self._test_disconnect_server()
1099

  
1100
    def _test_disconnect_server(self):
1101
        self.client.disconnect_server(self.server1['id'], self.network1['id'])
1102
        self.assertTrue(self._wait_for_nic(self.network1['id'],
1103
            self.server1['id'],
1104
            in_creation=False))
1105

  
1106
    @if_not_all
1107
    def test_list_server_nics(self):
1108
        """Test list_server_nics"""
1109
        self.server1 = self._create_server(self.servname1,
1110
            self.flavorid,
1111
            self.img)
1112
        self.network2 = self._create_network(self.netname2)
1113
        self._wait_for_status(self.server1['id'], 'BUILD')
1114
        self._wait_for_network(self.network2['id'], 'ACTIVE')
1115
        self._test_list_server_nics()
1116

  
1117
    def _test_list_server_nics(self):
1118
        r = self.client.list_server_nics(self.server1['id'])
1119
        len0 = len(r)
1120

  
1121
        self.client.connect_server(self.server1['id'], self.network2['id'])
1122
        self.assertTrue(self._wait_for_nic(self.network2['id'],
1123
            self.server1['id']))
1124
        r = self.client.list_server_nics(self.server1['id'])
1125
        self.assertTrue(len(r) > len0)
1126

  
1127
    @if_not_all
1128
    def test_get_network_details(self):
1129
        """Test get_network_details"""
1130
        self.network1 = self._create_network(self.netname1)
1131
        self._test_get_network_details()
1132

  
1133
    def _test_get_network_details(self):
1134
        r = self.client.get_network_details(self.network1['id'])
1135
        net1 = dict(self.network1)
1136
        net1.pop('status')
1137
        net1.pop('updated', None)
1138
        net1.pop('attachments')
1139
        r.pop('status')
1140
        r.pop('updated', None)
1141
        r.pop('attachments')
1142
        self.assert_dicts_are_deeply_equal(net1, r)
1143

  
1144
    @if_not_all
1145
    def test_update_network_name(self):
1146
        self.network2 = self._create_network(self.netname2)
1147
        self._test_update_network_name()
1148

  
1149
    def _test_update_network_name(self):
1150
        updated_name = self.netname2 + '_upd'
1151
        self.client.update_network_name(self.network2['id'], updated_name)
1152
        wait = 3
1153
        c = ['|', '/', '-', '\\']
1154
        r = self.client.get_network_details(self.network2['id'])
1155
        while wait < 50:
1156
            if r['name'] == updated_name:
1157
                break
1158
            sys.stdout.write(
1159
                '\twait for %s renaming (%s->%s) %ss  ' % (self.network2['id'],
1160
                self.network2['name'],
1161
                updated_name, wait))
1162
            for i in range(4 * wait):
1163
                sys.stdout.write('\b%s' % c[i % 4])
1164
                sys.stdout.flush()
1165
                time.sleep(0.25)
1166
            print('')
1167
            wait += 3
1168
            r = self.client.get_network_details(self.network2['id'])
1169
        self.assertEqual(r['name'], updated_name)
1170

  
1171
    """ Don't have auth to test this
1172
    @if_not_all
1173
    def test_delete_image(self):
1174
        ""Test delete_image""
1175
        self._test_delete_image()
1176
    def _test_delete_image(self):
1177
        images = self.client.list_images()
1178
        self.client.delete_image(images[2]['id'])
1179
        try:
1180
            r = self.client.get_image_details(images[2]['id'], success=(400))
1181
        except ClientError as err:
1182
            self.assertEqual(err.status, 404)
1183

  
1184
    @if_not_all
1185
    def test_create_image_metadata(self):
1186
        ""Test create_image_metadata""
1187
        self._test_create_image_metadata()
1188
    def _test_create_image_metadata(self):
1189
        r = self.client.create_image_metadata(self.img, 'mykey', 'myval')
1190
        self.assertEqual(r['mykey'], 'myval')
1191

  
1192
    @if_not_all
1193
    def test_update_image_metadata(self):
1194
        ""Test update_image_metadata""
1195
        self._test_update_image_metadata()
1196
    def _test_update_image_metadata(self):
1197
        r = self.client.create_image_metadata(self.img, 'mykey0', 'myval')
1198
        r = self.client.update_image_metadata(self.img, 'mykey0', 'myval0')
1199
        self.assertEqual(r['mykey0'], 'myval0')
1200

  
1201
    @if_not_all
1202
    def test_delete_image_metadata(self):
1203
        ""Test delete_image_metadata""
1204
        self._test_delete_image_metadata()
1205
    def _test_delete_image_metadata(self):
1206
        self.client.create_image_metadata(self.img, 'mykey1', 'myval1')
1207
        self.client.delete_image_metadata(self.img, 'mykey1')
1208
        r = self.client.get_image_metadata(self.img)
1209
        self.assertNotEqual('mykey1' in r)
1210
    """
1211

  
1212

  
1213
class testPithos(unittest.TestCase):
1214
    """Set up a Pithos+ thorough test"""
1215
    def setUp(self):
1216
        _init_cnf()
1217
        global cnf
1218
        url = cnf.get('store', 'url')
1219

  
1220
        global token
1221
        global global_username
1222
        account = global_username
1223

  
1224
        """
1225
        url='https://pithos.okeanos.io/v1'
1226
        """
1227

  
1228
        """
1229
        def add_handler(name, level, prefix=''):
1230
            h = logging.StreamHandler()
1231
            fmt = logging.Formatter(prefix + '%(message)s')
1232
            h.setFormatter(fmt)
1233
            logger = logging.getLogger(name)
1234
            logger.addHandler(h)
1235
            logger.setLevel(level)
1236
        import logging
1237
        sendlog = logging.getLogger('clients.send')
1238
        recvlog = logging.getLogger('clients.recv')
1239
        add_handler('requests', logging.INFO, prefix='* ')
1240
        add_handler('clients.send', logging.INFO, prefix='> ')
1241
        add_handler('clients.recv', logging.INFO, prefix='< ')
1242
        """
1243

  
1244
        self.fname = None
1245
        container = None
1246
        self.client = pithos(url, token, account, container)
1247
        self.now = time.mktime(time.gmtime())
1248
        self.c1 = 'c1_' + unicode(self.now)
1249
        self.c2 = 'c2_' + unicode(self.now)
1250
        self.c3 = 'c3_' + unicode(self.now)
1251

  
1252
        self.client.create_container(self.c1)
1253
        self.client.create_container(self.c2)
1254
        self.client.create_container(self.c3)
1255
        self.makeNewObject(self.c1, 'test')
1256
        self.makeNewObject(self.c2, 'test')
1257
        self.now_unformated = datetime.datetime.utcnow()
1258
        self.makeNewObject(self.c1, 'test1')
1259
        self.makeNewObject(self.c2, 'test1')
1260
        """Prepare an object to be shared - also its container"""
1261
        self.client.container = self.c1
1262
        self.client.object_post('test',
1263
            update=True,
1264
            permissions={'read': 'someUser'})
1265

  
1266
        self.makeNewObject(self.c1, 'another.test')
1267

  
1268
    def makeNewObject(self, container, obj):
1269
        self.client.container = container
1270
        self.client.object_put(obj,
1271
            content_type='application/octet-stream',
1272
            data='file %s that lives in %s' % (obj, container),
1273
            metadata={'incontainer': container})
1274

  
1275
    def forceDeleteContainer(self, container):
1276
        self.client.container = container
1277
        try:
1278
            r = self.client.list_objects()
1279
        except ClientError:
1280
            return
1281
        for obj in r:
1282
            name = obj['name']
1283
            self.client.del_object(name)
1284
        r = self.client.container_delete()
1285
        self.container = ''
1286

  
1287
    def tearDown(self):
1288
        """Destroy test cases"""
1289
        if self.fname is not None:
1290
            try:
1291
                os.remove(self.fname)
1292
            except OSError:
1293
                pass
1294
            self.fname = None
1295
        self.forceDeleteContainer(self.c1)
1296
        self.forceDeleteContainer(self.c2)
1297
        try:
1298
            self.forceDeleteContainer(self.c3)
1299
        except ClientError:
1300
            pass
1301
        self.client.container = ''
1302

  
1303
    def test_000(self):
1304
        """Perform a full Pithos+ kamaki support test"""
1305

  
1306
    def test_account_head(self):
1307
        """Test account_HEAD"""
1308
        r = self.client.account_head()
1309
        self.assertEqual(r.status_code, 204)
1310

  
1311
        r = self.client.account_head(until='1000000000')
1312
        self.assertEqual(r.status_code, 204)
1313

  
1314
        r = self.client.get_account_info(until='1000000000')
1315
        datestring = unicode(r['x-account-until-timestamp'])
1316
        self.assertEqual(u'Sun, 09 Sep 2001 01:46:40 GMT', datestring)
1317

  
1318
        r = self.client.get_account_quota()
1319
        self.assertTrue('x-account-policy-quota' in r)
1320

  
1321
        r = self.client.get_account_versioning()
1322
        self.assertTrue('x-account-policy-versioning' in r)
1323

  
1324
        """Check if(un)modified_since"""
1325
        for format in self.client.DATE_FORMATS:
1326
            now_formated = self.now_unformated.strftime(format)
1327
            r1 = self.client.account_head(if_modified_since=now_formated,
1328
                success=(204, 304, 412))
1329
            sc1 = r1.status_code
1330
            r1.release()
1331
            r2 = self.client.account_head(if_unmodified_since=now_formated,
1332
                success=(204, 304, 412))
1333
            sc2 = r2.status_code
1334
            r2.release()
1335
            self.assertNotEqual(sc1, sc2)
1336

  
1337
    def test_account_get(self):
1338
        """Test account_GET"""
1339
        #r = self.client.account_get()
1340
        #self.assertEqual(r.status_code, 200)
1341
        r = self.client.list_containers()
1342
        fullLen = len(r)
1343
        self.assertTrue(fullLen > 2)
1344

  
1345
        r = self.client.account_get(limit=1)
1346
        self.assertEqual(len(r.json), 1)
1347

  
1348
        r = self.client.account_get(marker='c2_')
1349
        temp_c0 = r.json[0]['name']
1350
        temp_c2 = r.json[2]['name']
1351

  
1352
        r = self.client.account_get(limit=2, marker='c2_')
1353
        conames = [container['name'] for container in r.json \
1354
            if container['name'].lower().startswith('c2_')]
1355
        self.assertTrue(temp_c0 in conames)
1356
        self.assertFalse(temp_c2 in conames)
1357

  
1358
        r = self.client.account_get(show_only_shared=True)
1359
        self.assertTrue(self.c1 in [c['name'] for c in r.json])
1360

  
1361
        r = self.client.account_get(until=1342609206)
1362
        self.assertTrue(len(r.json) <= fullLen)
1363

  
1364
        """Check if(un)modified_since"""
1365
        for format in self.client.DATE_FORMATS:
1366
            now_formated = self.now_unformated.strftime(format)
1367
            r1 = self.client.account_get(if_modified_since=now_formated,
1368
                success=(200, 304, 412))
1369
            sc1 = r1.status_code
1370
            r1.release()
1371
            r2 = self.client.account_get(if_unmodified_since=now_formated,
1372
                success=(200, 304, 412))
1373
            sc2 = r2.status_code
1374
            r2.release()
1375
            self.assertNotEqual(sc1, sc2)
1376

  
1377
        """Check sharing_accounts"""
1378
        r = self.client.get_sharing_accounts()
1379
        self.assertTrue(len(r) > 0)
1380

  
1381
    def test_account_post(self):
1382
        """Test account_POST"""
1383
        r = self.client.account_post()
1384
        self.assertEqual(r.status_code, 202)
1385
        grpName = 'grp' + unicode(self.now)
1386

  
1387
        """Method set/del_account_meta and set_account_groupcall use
1388
            account_post internally
1389
        """
1390
        self.client.set_account_group(grpName, ['u1', 'u2'])
1391
        r = self.client.get_account_group()
1392
        self.assertEqual(r['x-account-group-' + grpName], 'u1,u2')
1393
        self.client.del_account_group(grpName)
1394
        r = self.client.get_account_group()
1395
        self.assertTrue('x-account-group-' + grpName not in r)
1396

  
1397
        mprefix = 'meta' + unicode(self.now)
1398
        self.client.set_account_meta({mprefix + '1': 'v1',
1399
            mprefix + '2': 'v2'})
1400
        r = self.client.get_account_meta()
1401
        self.assertEqual(r['x-account-meta-' + mprefix + '1'], 'v1')
1402
        self.assertEqual(r['x-account-meta-' + mprefix + '2'], 'v2')
1403

  
1404
        self.client.del_account_meta(mprefix + '1')
1405
        r = self.client.get_account_meta()
1406
        self.assertTrue('x-account-meta-' + mprefix + '1' not in r)
1407

  
1408
        self.client.del_account_meta(mprefix + '2')
1409
        r = self.client.get_account_meta()
1410
        self.assertTrue('x-account-meta-' + mprefix + '2' not in r)
1411

  
1412
        """Missing testing for quota, versioning, because normally
1413
        you don't have permissions to modify those at account level
1414
        """
1415

  
1416
        newquota = 1000000
1417
        self.client.set_account_quota(newquota)
1418
        #r = self.client.get_account_info()
1419
        #print(unicode(r))
1420
        #r = self.client.get_account_quota()
1421
        #self.assertEqual(r['x-account-policy-quota'], newquota)
1422
        self.client.set_account_versioning('auto')
1423

  
1424
    def test_container_head(self):
1425
        """Test container_HEAD"""
1426
        self.client.container = self.c1
1427

  
1428
        r = self.client.container_head()
1429
        self.assertEqual(r.status_code, 204)
1430

  
1431
        """Check until"""
1432
        r = self.client.container_head(until=1000000, success=(204, 404))
1433
        self.assertEqual(r.status_code, 404)
1434

  
1435
        """Check and if(un)modified_since"""
1436
        for format in self.client.DATE_FORMATS:
1437
            now_formated = self.now_unformated.strftime(format)
1438
            r1 = self.client.container_head(if_modified_since=now_formated,
1439
                success=(204, 304, 412))
1440
            sc1 = r1.status_code
1441
            r1.release()
1442
            r2 = self.client.container_head(if_unmodified_since=now_formated,
1443
                success=(204, 304, 412))
1444
            sc2 = r2.status_code
1445
            r2.release()
1446
            self.assertNotEqual(sc1, sc2)
1447

  
1448
        """Check container object meta"""
1449
        r = self.client.get_container_object_meta()
1450
        self.assertEqual(r['x-container-object-meta'], 'Incontainer')
1451

  
1452
    def test_container_get(self):
1453
        """Test container_GET"""
1454
        self.client.container = self.c1
1455

  
1456
        r = self.client.container_get()
1457
        self.assertEqual(r.status_code, 200)
1458
        fullLen = len(r.json)
1459

  
1460
        r = self.client.container_get(prefix='test')
1461
        lalobjects = [obj for obj in r.json if obj['name'].startswith('test')]
1462
        self.assertTrue(len(r.json) > 1)
1463
        self.assertEqual(len(r.json), len(lalobjects))
1464

  
1465
        r = self.client.container_get(limit=1)
1466
        self.assertEqual(len(r.json), 1)
1467

  
1468
        r = self.client.container_get(marker='another')
1469
        self.assertTrue(len(r.json) > 1)
1470
        neobjects = [obj for obj in r.json if obj['name'] > 'another']
1471
        self.assertEqual(len(r.json), len(neobjects))
1472

  
1473
        r = self.client.container_get(prefix='another.test', delimiter='.')
1474
        self.assertTrue(fullLen > len(r.json))
1475

  
1476
        r = self.client.container_get(path='/')
1477
        self.assertEqual(fullLen, len(r.json))
1478

  
1479
        r = self.client.container_get(format='xml')
1480
        self.assertEqual(r.text.split()[4], 'name="' + self.c1 + '">')
1481

  
1482
        r = self.client.container_get(meta=['incontainer'])
1483
        self.assertTrue(len(r.json) > 0)
1484

  
1485
        r = self.client.container_get(show_only_shared=True)
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff