Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / _tests.py @ 6d192774

History | View | Annotate | Download (82.7 kB)

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)
1486
        self.assertTrue(len(r.json) < fullLen)
1487

    
1488
        try:
1489
            r = self.client.container_get(until=1000000000)
1490
            datestring = unicode(r.headers['x-account-until-timestamp'])
1491
            self.assertEqual(u'Sun, 09 Sep 2001 01:46:40 GMT', datestring)
1492

    
1493
        except ClientError:
1494

    
1495
            pass
1496

    
1497
        """Check and if un/modified_since"""
1498
        for format in self.client.DATE_FORMATS:
1499
            now_formated = self.now_unformated.strftime(format)
1500
            r1 = self.client.container_get(if_modified_since=now_formated,
1501
                success=(200, 304, 412))
1502
            sc1 = r1.status_code
1503
            r1.release()
1504
            r2 = self.client.container_get(if_unmodified_since=now_formated,
1505
                success=(200, 304, 412))
1506
            sc2 = r2.status_code
1507
            r2.release()
1508
            self.assertNotEqual(sc1, sc2)
1509

    
1510
    def test_container_put(self):
1511
        """Test container_PUT"""
1512
        self.client.container = self.c2
1513

    
1514
        r = self.client.container_put()
1515
        self.assertEqual(r.status_code, 202)
1516

    
1517
        r = self.client.get_container_quota(self.client.container)
1518
        cquota = r.values()[0]
1519
        newquota = 2 * int(cquota)
1520

    
1521
        r = self.client.container_put(quota=newquota)
1522
        self.assertEqual(r.status_code, 202)
1523

    
1524
        r = self.client.get_container_quota(self.client.container)
1525
        xquota = int(r.values()[0])
1526
        self.assertEqual(newquota, xquota)
1527

    
1528
        r = self.client.container_put(versioning='auto')
1529
        self.assertEqual(r.status_code, 202)
1530

    
1531
        r = self.client.get_container_versioning(self.client.container)
1532
        nvers = r.values()[0]
1533
        self.assertEqual('auto', nvers)
1534

    
1535
        r = self.client.container_put(versioning='none')
1536
        self.assertEqual(r.status_code, 202)
1537

    
1538
        r = self.client.get_container_versioning(self.client.container)
1539
        nvers = r.values()[0]
1540
        self.assertEqual('none', nvers)
1541

    
1542
        r = self.client.container_put(metadata={'m1': 'v1', 'm2': 'v2'})
1543
        self.assertEqual(r.status_code, 202)
1544

    
1545
        r = self.client.get_container_meta(self.client.container)
1546
        self.assertTrue('x-container-meta-m1' in r)
1547
        self.assertEqual(r['x-container-meta-m1'], 'v1')
1548
        self.assertTrue('x-container-meta-m2' in r)
1549
        self.assertEqual(r['x-container-meta-m2'], 'v2')
1550

    
1551
        r = self.client.container_put(metadata={'m1': '', 'm2': 'v2a'})
1552
        self.assertEqual(r.status_code, 202)
1553

    
1554
        r = self.client.get_container_meta(self.client.container)
1555
        self.assertTrue('x-container-meta-m1' not in r)
1556
        self.assertTrue('x-container-meta-m2' in r)
1557
        self.assertEqual(r['x-container-meta-m2'], 'v2a')
1558

    
1559
        self.client.del_container_meta(self.client.container)
1560

    
1561
    def test_container_post(self):
1562
        """Test container_POST"""
1563
        self.client.container = self.c2
1564

    
1565
        """Simple post"""
1566
        r = self.client.container_post()
1567
        self.assertEqual(r.status_code, 202)
1568

    
1569
        """post meta"""
1570
        self.client.set_container_meta({'m1': 'v1', 'm2': 'v2'})
1571
        r = self.client.get_container_meta(self.client.container)
1572
        self.assertTrue('x-container-meta-m1' in r)
1573
        self.assertEqual(r['x-container-meta-m1'], 'v1')
1574
        self.assertTrue('x-container-meta-m2' in r)
1575
        self.assertEqual(r['x-container-meta-m2'], 'v2')
1576

    
1577
        """post/2del meta"""
1578
        r = self.client.del_container_meta('m1')
1579
        r = self.client.set_container_meta({'m2': 'v2a'})
1580
        r = self.client.get_container_meta(self.client.container)
1581
        self.assertTrue('x-container-meta-m1' not in r)
1582
        self.assertTrue('x-container-meta-m2' in r)
1583
        self.assertEqual(r['x-container-meta-m2'], 'v2a')
1584

    
1585
        """check quota"""
1586
        r = self.client.get_container_quota(self.client.container)
1587
        cquota = r.values()[0]
1588
        newquota = 2 * int(cquota)
1589
        r = self.client.set_container_quota(newquota)
1590
        r = self.client.get_container_quota(self.client.container)
1591
        xquota = int(r.values()[0])
1592
        self.assertEqual(newquota, xquota)
1593
        r = self.client.set_container_quota(cquota)
1594
        r = self.client.get_container_quota(self.client.container)
1595
        xquota = r.values()[0]
1596
        self.assertEqual(cquota, xquota)
1597

    
1598
        """Check versioning"""
1599
        self.client.set_container_versioning('auto')
1600
        r = self.client.get_container_versioning(self.client.container)
1601
        nvers = r.values()[0]
1602
        self.assertEqual('auto', nvers)
1603
        self.client.set_container_versioning('none')
1604
        r = self.client.get_container_versioning(self.client.container)
1605
        nvers = r.values()[0]
1606
        self.assertEqual('none', nvers)
1607

    
1608
        """put_block uses content_type and content_length to
1609
        post blocks of data 2 container. All that in upload_object"""
1610
        """Change a file at fs"""
1611
        self.fname = 'l100M.' + unicode(self.now)
1612
        self.create_large_file(1024 * 1024 * 100, self.fname)
1613
        """Upload it at a directory in container"""
1614
        self.client.create_directory('dir')
1615
        newf = open(self.fname, 'rb')
1616
        self.client.upload_object('/dir/sample.file', newf)
1617
        newf.close()
1618
        """Check if file has been uploaded"""
1619
        r = self.client.get_object_info('/dir/sample.file')
1620
        self.assertTrue(int(r['content-length']) > 100000000)
1621

    
1622
        """What is tranfer_encoding? What should I check about it? """
1623
        #TODO
1624

    
1625
        """Check update=False"""
1626
        r = self.client.object_post('test',
1627
            update=False,
1628
            metadata={'newmeta': 'newval'})
1629

    
1630
        r = self.client.get_object_info('test')
1631
        self.assertTrue('x-object-meta-newmeta' in r)
1632
        self.assertFalse('x-object-meta-incontainer' in r)
1633

    
1634
        r = self.client.del_container_meta('m2')
1635

    
1636
    def test_container_delete(self):
1637
        """Test container_DELETE"""
1638

    
1639
        """Fail to delete a non-empty container"""
1640
        self.client.container = self.c2
1641
        r = self.client.container_delete(success=409)
1642
        self.assertEqual(r.status_code, 409)
1643

    
1644
        """Fail to delete c3 (empty) container"""
1645
        self.client.container = self.c3
1646
        r = self.client.container_delete(until='1000000000')
1647
        self.assertEqual(r.status_code, 204)
1648

    
1649
        """Delete c3 (empty) container"""
1650
        r = self.client.container_delete()
1651
        self.assertEqual(r.status_code, 204)
1652

    
1653
        """Purge container(empty a container), check versionlist"""
1654
        self.client.container = self.c1
1655
        r = self.client.object_head('test', success=(200, 404))
1656
        self.assertEqual(r.status_code, 200)
1657
        self.client.del_container(delimiter='/')
1658
        r = self.client.object_head('test', success=(200, 404))
1659
        self.assertEqual(r.status_code, 404)
1660
        r = self.client.get_object_versionlist('test')
1661
        self.assertTrue(len(r) > 0)
1662
        self.assertTrue(len(r[0]) > 1)
1663
        self.client.purge_container()
1664
        self.assertRaises(ClientError,
1665
            self.client.get_object_versionlist,
1666
            'test')
1667

    
1668
    def test_object_head(self):
1669
        """Test object_HEAD"""
1670
        self.client.container = self.c2
1671
        obj = 'test'
1672

    
1673
        r = self.client.object_head(obj)
1674
        self.assertEqual(r.status_code, 200)
1675
        etag = r.headers['etag']
1676

    
1677
        r = self.client.object_head(obj, version=40)
1678
        self.assertEqual(r.headers['x-object-version'], '40')
1679

    
1680
        r = self.client.object_head(obj, if_etag_match=etag)
1681
        self.assertEqual(r.status_code, 200)
1682

    
1683
        r = self.client.object_head(obj,
1684
            if_etag_not_match=etag,
1685
            success=(200, 412, 304))
1686
        self.assertNotEqual(r.status_code, 200)
1687

    
1688
        r = self.client.object_head(obj,
1689
            version=40,
1690
            if_etag_match=etag,
1691
            success=412)
1692
        self.assertEqual(r.status_code, 412)
1693

    
1694
        """Check and if(un)modified_since"""
1695
        for format in self.client.DATE_FORMATS:
1696
            now_formated = self.now_unformated.strftime(format)
1697
            r1 = self.client.object_head(obj, if_modified_since=now_formated,
1698
                success=(200, 304, 412))
1699
            sc1 = r1.status_code
1700
            r1.release()
1701
            r2 = self.client.object_head(obj, if_unmodified_since=now_formated,
1702
                success=(200, 304, 412))
1703
            sc2 = r2.status_code
1704
            r2.release()
1705
            self.assertNotEqual(sc1, sc2)
1706

    
1707
    def test_object_get(self):
1708
        """Test object_GET"""
1709
        self.client.container = self.c1
1710
        obj = 'test'
1711

    
1712
        r = self.client.object_get(obj)
1713
        self.assertEqual(r.status_code, 200)
1714

    
1715
        osize = int(r.headers['content-length'])
1716
        etag = r.headers['etag']
1717

    
1718
        r = self.client.object_get(obj, hashmap=True)
1719
        self.assertTrue('hashes' in r.json\
1720
            and 'block_hash' in r.json\
1721
            and 'block_size' in r.json\
1722
            and 'bytes' in r.json)
1723

    
1724
        r = self.client.object_get(obj, format='xml', hashmap=True)
1725
        self.assertEqual(len(r.text.split('hash>')), 3)
1726

    
1727
        rangestr = 'bytes=%s-%s' % (osize / 3, osize / 2)
1728
        r = self.client.object_get(obj,
1729
            data_range=rangestr,
1730
            success=(200, 206))
1731
        partsize = int(r.headers['content-length'])
1732
        self.assertTrue(0 < partsize and partsize <= 1 + osize / 3)
1733

    
1734
        rangestr = 'bytes=%s-%s' % (osize / 3, osize / 2)
1735
        r = self.client.object_get(obj,
1736
            data_range=rangestr,
1737
            if_range=True,
1738
            success=(200, 206))
1739
        partsize = int(r.headers['content-length'])
1740
        self.assertTrue(0 < partsize and partsize <= 1 + osize / 3)
1741

    
1742
        r = self.client.object_get(obj, if_etag_match=etag)
1743
        self.assertEqual(r.status_code, 200)
1744

    
1745
        r = self.client.object_get(obj, if_etag_not_match=etag + 'LALALA')
1746
        self.assertEqual(r.status_code, 200)
1747

    
1748
        """Check and if(un)modified_since"""
1749
        for format in self.client.DATE_FORMATS:
1750
            now_formated = self.now_unformated.strftime(format)
1751
            r1 = self.client.object_get(obj, if_modified_since=now_formated,
1752
                success=(200, 304, 412))
1753
            sc1 = r1.status_code
1754
            r1.release()
1755
            r2 = self.client.object_get(obj,
1756
                if_unmodified_since=now_formated,
1757
                success=(200, 304, 412))
1758
            sc2 = r2.status_code
1759
            r2.release()
1760
            self.assertNotEqual(sc1, sc2)
1761

    
1762
        """Upload an object to download"""
1763
        src_file = tempfile.NamedTemporaryFile(delete=False)
1764
        dnl_file = tempfile.NamedTemporaryFile(delete=False)
1765

    
1766
        src_fname = src_file.name
1767
        dnl_fname = dnl_file.name
1768

    
1769
        src_file.close()
1770
        dnl_file.close()
1771

    
1772
        trg_fname = 'remotefile_%s' % self.now
1773
        f_size = 59247824
1774
        self.create_large_file(f_size, src_fname)
1775
        src_f = open(src_fname, 'rb+')
1776
        print('\tUploading...')
1777
        self.client.upload_object(trg_fname, src_f)
1778
        src_f.close()
1779
        print('\tDownloading...')
1780
        dnl_f = open(dnl_fname, 'wb+')
1781
        self.client.download_object(trg_fname, dnl_f)
1782
        dnl_f.close()
1783

    
1784
        print('\tCheck if files match...')
1785
        src_f = open(src_fname)
1786
        dnl_f = open(dnl_fname)
1787
        for pos in (0, f_size / 2, f_size - 20):
1788
            src_f.seek(pos)
1789
            dnl_f.seek(pos)
1790
            self.assertEqual(src_f.read(10), dnl_f.read(10))
1791
        src_f.close()
1792
        dnl_f.close()
1793

    
1794
        os.remove(src_fname)
1795
        os.remove(dnl_fname)
1796

    
1797
    def test_object_put(self):
1798
        """Test object_PUT"""
1799

    
1800
        self.client.container = self.c2
1801
        obj = 'another.test'
1802

    
1803
        self.client.create_object(obj + '.FAKE')
1804
        r = self.client.get_object_info(obj + '.FAKE')
1805
        self.assertEqual(r['content-type'], 'application/octet-stream')
1806

    
1807
        """create the object"""
1808
        r = self.client.object_put(obj,
1809
            data='a',
1810
            content_type='application/octer-stream',
1811
            permissions={
1812
                'read': ['accX:groupA', 'u1', 'u2'],
1813
                'write': ['u2', 'u3']},
1814
            metadata={'key1': 'val1', 'key2': 'val2'},
1815
            content_encoding='UTF-8',
1816
            content_disposition='attachment; filename="fname.ext"')
1817
        self.assertEqual(r.status_code, 201)
1818
        etag = r.headers['etag']
1819

    
1820
        """Check content-disposition"""
1821
        r = self.client.get_object_info(obj)
1822
        self.assertTrue('content-disposition' in r)
1823

    
1824
        """Check permissions"""
1825
        r = self.client.get_object_sharing(obj)
1826
        self.assertTrue('accx:groupa' in r['read'])
1827
        self.assertTrue('u1' in r['read'])
1828
        self.assertTrue('u2' in r['write'])
1829
        self.assertTrue('u3' in r['write'])
1830

    
1831
        """Check metadata"""
1832
        r = self.client.get_object_meta(obj)
1833
        self.assertEqual(r['x-object-meta-key1'], 'val1')
1834
        self.assertEqual(r['x-object-meta-key2'], 'val2')
1835

    
1836
        """Check public and if_etag_match"""
1837
        r = self.client.object_put(obj, if_etag_match=etag, data='b',
1838
            content_type='application/octet-stream', public=True)
1839

    
1840
        r = self.client.object_get(obj)
1841
        self.assertTrue('x-object-public' in r.headers)
1842
        vers2 = int(r.headers['x-object-version'])
1843
        etag = r.headers['etag']
1844
        self.assertEqual(r.text, 'b')
1845

    
1846
        """Check if_etag_not_match"""
1847
        r = self.client.object_put(obj, if_etag_not_match=etag, data='c',
1848
            content_type='application/octet-stream', success=(201, 412))
1849
        self.assertEqual(r.status_code, 412)
1850

    
1851
        """Check content_type and content_length"""
1852
        tmpdir = 'dir' + unicode(self.now)
1853
        r = self.client.object_put(tmpdir,
1854
            content_type='application/directory',
1855
            content_length=0)
1856

    
1857
        r = self.client.get_object_info(tmpdir)
1858
        self.assertEqual(r['content-type'], 'application/directory')
1859

    
1860
        """Check copy_from, content_encoding"""
1861
        r = self.client.object_put('%s/%s' % (tmpdir, obj),
1862
            format=None,
1863
            copy_from='/%s/%s' % (self.client.container, obj),
1864
            content_encoding='application/octet-stream',
1865
            source_account=self.client.account,
1866
            content_length=0, success=201)
1867
        self.assertEqual(r.status_code, 201)
1868

    
1869
        """Test copy_object for cross-conctainer copy"""
1870
        self.client.copy_object(src_container=self.c2,
1871
            src_object='%s/%s' % (tmpdir, obj),
1872
            dst_container=self.c1,
1873
            dst_object=obj)
1874
        self.client.container = self.c1
1875
        r1 = self.client.get_object_info(obj)
1876
        self.client.container = self.c2
1877
        r2 = self.client.get_object_info('%s/%s' % (tmpdir, obj))
1878
        self.assertEqual(r1['x-object-hash'], r2['x-object-hash'])
1879

    
1880
        """Check cross-container copy_from, content_encoding"""
1881
        self.client.container = self.c1
1882
        fromstr = '/%s/%s/%s' % (self.c2, tmpdir, obj)
1883
        r = self.client.object_put(obj,
1884
            format=None,
1885
            copy_from=fromstr,
1886
            content_encoding='application/octet-stream',
1887
            source_account=self.client.account,
1888
            content_length=0, success=201)
1889

    
1890
        self.assertEqual(r.status_code, 201)
1891
        r = self.client.get_object_info(obj)
1892
        self.assertEqual(r['etag'], etag)
1893

    
1894
        """Check source_account"""
1895
        self.client.container = self.c2
1896
        fromstr = '/%s/%s' % (self.c1, obj)
1897
        r = self.client.object_put(obj + 'v2',
1898
            format=None,
1899
            move_from=fromstr,
1900
            content_encoding='application/octet-stream',
1901
            source_account='nonExistendAddress@NeverLand.com',
1902
            content_length=0,
1903
            success=(201, 403))
1904
        self.assertEqual(r.status_code, 403)
1905

    
1906
        """Check cross-container move_from"""
1907
        self.client.container = self.c1
1908
        r1 = self.client.get_object_info(obj)
1909
        self.client.container = self.c2
1910
        self.client.move_object(src_container=self.c1,
1911
            src_object=obj,
1912
            dst_container=self.c2,
1913
            dst_object=obj + 'v0')
1914
        r0 = self.client.get_object_info(obj + 'v0')
1915
        self.assertEqual(r1['x-object-hash'], r0['x-object-hash'])
1916

    
1917
        """Check move_from"""
1918
        r = self.client.object_put(obj + 'v1',
1919
            format=None,
1920
            move_from='/%s/%s' % (self.c2, obj),
1921
            source_version=vers2,
1922
            content_encoding='application/octet-stream',
1923
            content_length=0, success=201)
1924

    
1925
        """Check manifest"""
1926
        mobj = 'manifest.test'
1927
        txt = ''
1928
        for i in range(10):
1929
            txt += '%s' % i
1930
            r = self.client.object_put('%s/%s' % (mobj, i),
1931
                data='%s' % i,
1932
                content_length=1,
1933
                success=201,
1934
                content_type='application/octet-stream',
1935
                content_encoding='application/octet-stream')
1936

    
1937
        r = self.client.object_put(mobj,
1938
            content_length=0,
1939
            content_type='application/octet-stream',
1940
            manifest='%s/%s' % (self.client.container, mobj))
1941

    
1942
        r = self.client.object_get(mobj)
1943
        self.assertEqual(r.text, txt)
1944

    
1945
        """Upload a local file with one request"""
1946
        self.fname = 'l10K.' + unicode(self.now)
1947
        self.create_large_file(1024 * 10, self.fname)
1948
        newf = open(self.fname, 'rb')
1949
        self.client.upload_object('sample.file', newf)
1950
        newf.close()
1951
        """Check if file has been uploaded"""
1952
        r = self.client.get_object_info('sample.file')
1953
        self.assertEqual(int(r['content-length']), 10260)
1954

    
1955
        """Some problems with transfer-encoding?"""
1956

    
1957
    def test_object_copy(self):
1958
        """Test object_COPY"""
1959
        self.client.container = self.c2
1960
        obj = 'test2'
1961

    
1962
        data = '{"key1":"val1", "key2":"val2"}'
1963
        r = self.client.object_put(obj + 'orig',
1964
            content_type='application/octet-stream',
1965
            data=data,
1966
            metadata={'mkey1': 'mval1', 'mkey2': 'mval2'},
1967
            permissions={
1968
                'read': ['accX:groupA', 'u1', 'u2'],
1969
                'write': ['u2', 'u3']},
1970
            content_disposition='attachment; filename="fname.ext"')
1971

    
1972
        r = self.client.object_copy(obj + 'orig',
1973
            destination='/%s/%s' % (self.client.container, obj),
1974
            ignore_content_type=False, content_type='application/json',
1975
            metadata={'mkey2': 'mval2a', 'mkey3': 'mval3'},
1976
            permissions={'write': ['u5', 'accX:groupB']})
1977
        self.assertEqual(r.status_code, 201)
1978

    
1979
        """Check content-disposition"""
1980
        r = self.client.get_object_info(obj)
1981
        self.assertTrue('content-disposition' in r)
1982

    
1983
        """Check Metadata"""
1984
        r = self.client.get_object_meta(obj)
1985
        self.assertEqual(r['x-object-meta-mkey1'], 'mval1')
1986
        self.assertEqual(r['x-object-meta-mkey2'], 'mval2a')
1987
        self.assertEqual(r['x-object-meta-mkey3'], 'mval3')
1988

    
1989
        """Check permissions"""
1990
        r = self.client.get_object_sharing(obj)
1991
        self.assertFalse('read' in r or 'u2' in r['write'])
1992
        self.assertTrue('accx:groupb' in r['write'])
1993

    
1994
        """Check destination account"""
1995
        r = self.client.object_copy(obj,
1996
            destination='/%s/%s' % (self.c1, obj),
1997
            content_encoding='utf8',
1998
            content_type='application/json',
1999
            destination_account='nonExistendAddress@NeverLand.com',
2000
            success=(201, 403))
2001
        self.assertEqual(r.status_code, 403)
2002

    
2003
        """Check destination being another container
2004
        and also content_type and content encoding"""
2005
        r = self.client.object_copy(obj,
2006
            destination='/%s/%s' % (self.c1, obj),
2007
            content_encoding='utf8',
2008
            content_type='application/json')
2009
        self.assertEqual(r.status_code, 201)
2010
        self.assertEqual(r.headers['content-type'],
2011
            'application/json; charset=UTF-8')
2012

    
2013
        """Check ignore_content_type and content_type"""
2014
        r = self.client.object_get(obj)
2015
        etag = r.headers['etag']
2016
        ctype = r.headers['content-type']
2017
        self.assertEqual(ctype, 'application/json')
2018

    
2019
        r = self.client.object_copy(obj + 'orig',
2020
            destination='/%s/%s0' % (self.client.container, obj),
2021
            ignore_content_type=True,
2022
            content_type='application/json')
2023
        self.assertEqual(r.status_code, 201)
2024
        self.assertNotEqual(r.headers['content-type'], 'application/json')
2025

    
2026
        """Check if_etag_(not_)match"""
2027
        r = self.client.object_copy(obj,
2028
            destination='/%s/%s1' % (self.client.container, obj),
2029
            if_etag_match=etag)
2030
        self.assertEqual(r.status_code, 201)
2031

    
2032
        r = self.client.object_copy(obj,
2033
            destination='/%s/%s2' % (self.client.container, obj),
2034
            if_etag_not_match='lalala')
2035
        self.assertEqual(r.status_code, 201)
2036
        vers2 = r.headers['x-object-version']
2037

    
2038
        """Check source_version, public and format """
2039
        r = self.client.object_copy(obj + '2',
2040
            destination='/%s/%s3' % (self.client.container, obj),
2041
            source_version=vers2,
2042
            format='xml',
2043
            public=True)
2044
        self.assertEqual(r.status_code, 201)
2045
        self.assertTrue(r.headers['content-type'].index('xml') > 0)
2046

    
2047
        r = self.client.get_object_info(obj + '3')
2048
        self.assertTrue('x-object-public' in r)
2049

    
2050
    def test_object_move(self):
2051
        """Test object_MOVE"""
2052
        self.client.container = self.c2
2053
        obj = 'test2'
2054

    
2055
        data = '{"key1": "val1", "key2": "val2"}'
2056
        r = self.client.object_put(obj + 'orig',
2057
            content_type='application/octet-stream',
2058
            data=data,
2059
            metadata={'mkey1': 'mval1', 'mkey2': 'mval2'},
2060
            permissions={'read': ['accX:groupA', 'u1', 'u2'],
2061
                'write': ['u2', 'u3']})
2062

    
2063
        r = self.client.object_move(obj + 'orig',
2064
            destination='/%s/%s' % (self.client.container, obj),
2065
            ignore_content_type=False,
2066
            content_type='application/json',
2067
            metadata={'mkey2': 'mval2a', 'mkey3': 'mval3'},
2068
            permissions={'write': ['u5', 'accX:groupB']})
2069
        self.assertEqual(r.status_code, 201)
2070

    
2071
        """Check Metadata"""
2072
        r = self.client.get_object_meta(obj)
2073
        self.assertEqual(r['x-object-meta-mkey1'], 'mval1')
2074
        self.assertEqual(r['x-object-meta-mkey2'], 'mval2a')
2075
        self.assertEqual(r['x-object-meta-mkey3'], 'mval3')
2076

    
2077
        """Check permissions"""
2078
        r = self.client.get_object_sharing(obj)
2079
        self.assertFalse('read' in r)
2080
        self.assertTrue('u5' in r['write'])
2081
        self.assertTrue('accx:groupb' in r['write'])
2082

    
2083
        """Check destination account"""
2084
        r = self.client.object_move(obj,
2085
            destination='/%s/%s' % (self.c1, obj),
2086
            content_encoding='utf8',
2087
            content_type='application/json',
2088
            destination_account='nonExistendAddress@NeverLand.com',
2089
            success=(201, 403))
2090
        self.assertEqual(r.status_code, 403)
2091

    
2092
        """Check destination being another container and also
2093
        content_type, content_disposition and content encoding"""
2094
        r = self.client.object_move(obj,
2095
            destination='/%s/%s' % (self.c1, obj),
2096
            content_encoding='utf8',
2097
            content_type='application/json',
2098
            content_disposition='attachment; filename="fname.ext"')
2099
        self.assertEqual(r.status_code, 201)
2100
        self.assertEqual(r.headers['content-type'],
2101
            'application/json; charset=UTF-8')
2102
        self.client.container = self.c1
2103
        r = self.client.get_object_info(obj)
2104
        self.assertTrue('content-disposition' in r\
2105
            and 'fname.ext' in r['content-disposition'])
2106
        etag = r['etag']
2107
        ctype = r['content-type']
2108
        self.assertEqual(ctype, 'application/json')
2109

    
2110
        """Check ignore_content_type and content_type"""
2111
        r = self.client.object_move(obj,
2112
            destination='/%s/%s' % (self.c2, obj),
2113
            ignore_content_type=True,
2114
            content_type='application/json')
2115
        self.assertEqual(r.status_code, 201)
2116
        self.assertNotEqual(r.headers['content-type'], 'application/json')
2117

    
2118
        """Check if_etag_(not_)match"""
2119
        self.client.container = self.c2
2120
        r = self.client.object_move(obj,
2121
            destination='/%s/%s0' % (self.client.container, obj),
2122
            if_etag_match=etag)
2123
        self.assertEqual(r.status_code, 201)
2124

    
2125
        r = self.client.object_move(obj + '0',
2126
            destination='/%s/%s1' % (self.client.container, obj),
2127
            if_etag_not_match='lalala')
2128
        self.assertEqual(r.status_code, 201)
2129

    
2130
        """Check public and format """
2131
        r = self.client.object_move(obj + '1',
2132
            destination='/%s/%s2' % (self.client.container, obj),
2133
            format='xml', public=True)
2134
        self.assertEqual(r.status_code, 201)
2135
        self.assertTrue(r.headers['content-type'].index('xml') > 0)
2136

    
2137
        r = self.client.get_object_info(obj + '2')
2138
        self.assertTrue('x-object-public' in r)
2139

    
2140
    def test_object_post(self):
2141
        """Test object_POST"""
2142
        self.client.container = self.c2
2143
        obj = 'test2'
2144
        """create a filesystem file"""
2145
        self.fname = obj
2146
        newf = open(self.fname, 'w')
2147
        newf.writelines(['ello!\n',
2148
            'This is a test line\n',
2149
            'inside a test file\n'])
2150
        newf.close()
2151
        """create a file on container"""
2152
        r = self.client.object_put(obj,
2153
            content_type='application/octet-stream',
2154
            data='H',
2155
            metadata={'mkey1': 'mval1', 'mkey2': 'mval2'},
2156
            permissions={'read': ['accX:groupA', 'u1', 'u2'],
2157
                'write': ['u2', 'u3']})
2158

    
2159
        """Append tests update, content_range, content_type, content_length"""
2160
        newf = open(obj, 'r')
2161
        self.client.append_object(obj, newf)
2162
        r = self.client.object_get(obj)
2163
        self.assertTrue(r.text.startswith('Hello!'))
2164

    
2165
        """Overwrite tests update,
2166
            content_type, content_length, content_range
2167
        """
2168
        newf.seek(0)
2169
        r = self.client.overwrite_object(obj, 0, 10, newf)
2170
        r = self.client.object_get(obj)
2171
        self.assertTrue(r.text.startswith('ello!'))
2172
        newf.close()
2173

    
2174
        """Truncate tests update,
2175
            content_range, content_type, object_bytes and source_object"""
2176
        r = self.client.truncate_object(obj, 5)
2177
        r = self.client.object_get(obj)
2178
        self.assertEqual(r.text, 'ello!')
2179

    
2180
        """Check metadata"""
2181
        self.client.set_object_meta(obj, {'mkey2': 'mval2a', 'mkey3': 'mval3'})
2182
        r = self.client.get_object_meta(obj)
2183
        self.assertEqual(r['x-object-meta-mkey1'], 'mval1')
2184
        self.assertEqual(r['x-object-meta-mkey2'], 'mval2a')
2185
        self.assertEqual(r['x-object-meta-mkey3'], 'mval3')
2186
        self.client.del_object_meta(obj, 'mkey1')
2187
        r = self.client.get_object_meta(obj)
2188
        self.assertFalse('x-object-meta-mkey1' in r)
2189

    
2190
        """Check permissions"""
2191
        self.client.set_object_sharing(obj,
2192
            read_permition=['u4', 'u5'], write_permition=['u4'])
2193
        r = self.client.get_object_sharing(obj)
2194
        self.assertTrue('read' in r)
2195
        self.assertTrue('u5' in r['read'])
2196
        self.assertTrue('write' in r)
2197
        self.assertTrue('u4' in r['write'])
2198
        self.client.del_object_sharing(obj)
2199
        r = self.client.get_object_sharing(obj)
2200
        self.assertTrue(len(r) == 0)
2201

    
2202
        """Check publish"""
2203
        self.client.publish_object(obj)
2204
        r = self.client.get_object_info(obj)
2205
        self.assertTrue('x-object-public' in r)
2206
        self.client.unpublish_object(obj)
2207
        r = self.client.get_object_info(obj)
2208
        self.assertFalse('x-object-public' in r)
2209

    
2210
        """Check if_etag_(not)match"""
2211
        etag = r['etag']
2212
        """
2213
        r = self.client.object_post(obj,
2214
            update=True,
2215
            public=True,
2216
            if_etag_not_match=etag,
2217
            success=(412, 202, 204))
2218
        self.assertEqual(r.status_code, 412)
2219
        """
2220

    
2221
        r = self.client.object_post(obj, update=True, public=True,
2222
            if_etag_match=etag, content_encoding='application/json')
2223

    
2224
        r = self.client.get_object_info(obj)
2225
        helloVersion = r['x-object-version']
2226
        self.assertTrue('x-object-public' in r)
2227
        self.assertEqual(r['content-encoding'], 'application/json')
2228

    
2229
        """Check source_version and source_account and content_disposition"""
2230
        r = self.client.object_post(obj,
2231
            update=True,
2232
            content_type='application/octet-srteam',
2233
            content_length=5,
2234
            content_range='bytes 1-5/*',
2235
            source_object='/%s/%s' % (self.c2, obj),
2236
            source_account='thisAccountWillNeverExist@adminland.com',
2237
            source_version=helloVersion,
2238
            data='12345',
2239
            success=(403, 202, 204))
2240
        self.assertEqual(r.status_code, 403)
2241

    
2242
        r = self.client.object_post(obj,
2243
            update=True,
2244
            content_type='application/octet-srteam',
2245
            content_length=5,
2246
            content_range='bytes 1-5/*',
2247
            source_object='/%s/%s' % (self.c2, obj),
2248
            source_account=self.client.account,
2249
            source_version=helloVersion,
2250
            data='12345',
2251
            content_disposition='attachment; filename="fname.ext"')
2252

    
2253
        r = self.client.object_get(obj)
2254
        self.assertEqual(r.text, 'eello!')
2255
        self.assertTrue('content-disposition' in r.headers\
2256
            and 'fname.ext' in r.headers['content-disposition'])
2257

    
2258
        """Check manifest"""
2259
        mobj = 'manifest.test'
2260
        txt = ''
2261
        for i in range(10):
2262
            txt += '%s' % i
2263
            r = self.client.object_put('%s/%s' % (mobj, i),
2264
            data='%s' % i,
2265
            content_length=1,
2266
            success=201,
2267
            content_encoding='application/octet-stream',
2268
            content_type='application/octet-stream')
2269

    
2270
        self.client.create_object_by_manifestation(mobj,
2271
            content_type='application/octet-stream')
2272

    
2273
        r = self.client.object_post(mobj,
2274
            manifest='%s/%s' % (self.client.container, mobj))
2275

    
2276
        r = self.client.object_get(mobj)
2277
        self.assertEqual(r.text, txt)
2278

    
2279
        """We need to check transfer_encoding """
2280

    
2281
    def test_object_delete(self):
2282
        """Test object_DELETE"""
2283
        self.client.container = self.c2
2284
        obj = 'test2'
2285
        """create a file on container"""
2286
        r = self.client.object_put(obj,
2287
            content_type='application/octet-stream',
2288
            data='H',
2289
            metadata={'mkey1': 'mval1', 'mkey2': 'mval2'},
2290
            permissions={'read': ['accX:groupA', 'u1', 'u2'],
2291
                'write': ['u2', 'u3']})
2292

    
2293
        """Check with false until"""
2294
        r = self.client.object_delete(obj, until=1000000)
2295

    
2296
        r = self.client.object_get(obj, success=(200, 404))
2297
        self.assertEqual(r.status_code, 200)
2298

    
2299
        """Check normal case"""
2300
        r = self.client.object_delete(obj)
2301
        self.assertEqual(r.status_code, 204)
2302

    
2303
        r = self.client.object_get(obj, success=(200, 404))
2304
        self.assertEqual(r.status_code, 404)
2305

    
2306
    def create_large_file(self, size, name):
2307
        """Create a large file at fs"""
2308
        self.fname = name
2309
        f = open(self.fname, 'w')
2310
        sys.stdout.write(
2311
            ' create random file %s of size %s      ' % (name, size))
2312
        for hobyte_id in xrange(size / 8):
2313
            random_bytes = os.urandom(8)
2314
            f.write(random_bytes)
2315
            if 0 == (hobyte_id * 800) % size:
2316
                f.write('\n')
2317
                f.flush()
2318
                prs = (hobyte_id * 800) // size
2319
                sys.stdout.write('\b\b')
2320
                if prs > 10:
2321
                    sys.stdout.write('\b')
2322
                sys.stdout.write('%s%%' % prs)
2323
                sys.stdout.flush()
2324
        print('\b\b\b100%')
2325
        f.flush()
2326
        f.close()
2327
        """"""
2328

    
2329

    
2330
def init_parser():
2331
    parser = ArgumentParser(add_help=False)
2332
    parser.add_argument('-h', '--help',
2333
        dest='help',
2334
        action='store_true',
2335
        default=False,
2336
        help="Show this help message and exit")
2337
    return parser
2338

    
2339

    
2340
def main(argv):
2341

    
2342
    suiteFew = unittest.TestSuite()
2343
    if len(argv) == 0 or argv[0] == 'pithos':
2344
        if len(argv) == 1:
2345
            suiteFew.addTest(unittest.makeSuite(testPithos))
2346
        else:
2347
            suiteFew.addTest(testPithos('test_' + argv[1]))
2348
    if len(argv) == 0 or argv[0] == 'cyclades':
2349
        if len(argv) == 1:
2350
            #suiteFew.addTest(unittest.makeSuite(testCyclades))
2351
            suiteFew.addTest(testCyclades('test_000'))
2352
        else:
2353
            suiteFew.addTest(testCyclades('test_' + argv[1]))
2354
    if len(argv) == 0 or argv[0] == 'image':
2355
        if len(argv) == 1:
2356
            suiteFew.addTest(unittest.makeSuite(testImage))
2357
        else:
2358
            suiteFew.addTest(testImage('test_' + argv[1]))
2359
    if len(argv) == 0 or argv[0] == 'astakos':
2360
        if len(argv) == 1:
2361
            suiteFew.addTest(unittest.makeSuite(testAstakos))
2362
        else:
2363
            suiteFew.addTest(testAstakos('test_' + argv[1]))
2364

    
2365
    unittest.TextTestRunner(verbosity=2).run(suiteFew)
2366

    
2367
if __name__ == '__main__':
2368
    parser = init_parser()
2369
    args, argv = parser.parse_known_args()
2370
    if len(argv) > 2 or getattr(args, 'help') or len(argv) < 1:
2371
        raise Exception('\tusage: tests.py <group> [command]')
2372
    main(argv)