root / kamaki / clients / test / pithos.py @ 514f9b88
History | View | Annotate | Download (4.5 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from unittest import TestCase |
35 |
from mock import patch, call, Mock |
36 |
|
37 |
from kamaki.clients import ClientError |
38 |
from kamaki.clients.pithos import PithosClient |
39 |
from kamaki.clients.astakos import AstakosClient |
40 |
from kamaki.clients.connection.kamakicon import KamakiHTTPConnection as C |
41 |
|
42 |
user_id = 'ac0un7-1d-5tr1ng'
|
43 |
|
44 |
account_info = { |
45 |
'content-language': 'en-us', |
46 |
'content-type': 'text/html; charset=utf-8', |
47 |
'date': 'Wed, 06 Mar 2013 13:25:51 GMT', |
48 |
'last-modified': 'Mon, 04 Mar 2013 18:22:31 GMT', |
49 |
'server': 'gunicorn/0.14.5', |
50 |
'vary': 'Accept-Language', |
51 |
'x-account-bytes-used': '751615526', |
52 |
'x-account-container-count': 7, |
53 |
'x-account-policy-quota': 53687091200, |
54 |
'x-account-policy-versioning': 'auto'} |
55 |
|
56 |
|
57 |
class Pithos(TestCase): |
58 |
|
59 |
class FR(object): |
60 |
"""FR stands for Fake Response"""
|
61 |
json = dict()
|
62 |
headers = {} |
63 |
content = json |
64 |
status = None
|
65 |
status_code = 204
|
66 |
headers = dict()
|
67 |
|
68 |
def release(self): |
69 |
pass
|
70 |
|
71 |
files = [] |
72 |
|
73 |
def assert_dicts_are_equal(self, d1, d2): |
74 |
for k, v in d1.items(): |
75 |
self.assertTrue(k in d2) |
76 |
if isinstance(v, dict): |
77 |
self.assert_dicts_are_equal(v, d2[k])
|
78 |
else:
|
79 |
self.assertEqual(unicode(v), unicode(d2[k])) |
80 |
|
81 |
def setUp(self): |
82 |
self.url = 'https://www.example.com/pithos' |
83 |
self.token = 'p17h0570k3n' |
84 |
self.client = PithosClient(self.url, self.token) |
85 |
self.client.account = user_id
|
86 |
self.client.container = 'c0nt@1n3r_i' |
87 |
|
88 |
def tearDown(self): |
89 |
self.FR.headers = dict() |
90 |
|
91 |
def test_get_account_info(self): |
92 |
self.FR.headers = account_info
|
93 |
with patch.object(C, 'perform_request', return_value=self.FR()): |
94 |
r = self.client.get_account_info()
|
95 |
self.assertEqual(self.client.http_client.url, self.url) |
96 |
self.assertEqual(self.client.http_client.path, '/%s' % user_id) |
97 |
self.assert_dicts_are_equal(r, account_info)
|
98 |
PithosClient.set_param = Mock() |
99 |
untils = ['date 1', 'date 2', 'date 3'] |
100 |
for unt in untils: |
101 |
r = self.client.get_account_info(until=unt)
|
102 |
self.assert_dicts_are_equal(r, account_info)
|
103 |
for i in range(len(untils)): |
104 |
self.assertEqual(
|
105 |
PithosClient.set_param.mock_calls[i], |
106 |
call('until', untils[i], iff=untils[i]))
|
107 |
|
108 |
def test_replace_account_meta(self): |
109 |
self.FR.status_code = 202 |
110 |
metas = dict(k1='v1', k2='v2', k3='v3') |
111 |
PithosClient.set_header = Mock() |
112 |
with patch.object(C, 'perform_request', return_value=self.FR()): |
113 |
self.client.replace_account_meta(metas)
|
114 |
self.assertEqual(self.client.http_client.url, self.url) |
115 |
self.assertEqual(self.client.http_client.path, '/%s' % user_id) |
116 |
prfx = 'X-Account-Meta-'
|
117 |
expected = [call('%s%s' % (prfx, k), v) for k, v in metas.items()] |
118 |
self.assertEqual(PithosClient.set_header.mock_calls, expected)
|