root / kamaki / clients / test / pithos.py @ d12e8569
History | View | Annotate | Download (6.4 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 as PC |
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 |
container_info = { |
56 |
'content-language': 'en-us', |
57 |
'content-type': 'text/html; charset=utf-8', |
58 |
'date': 'Wed, 06 Mar 2013 15:11:05 GMT', |
59 |
'last-modified': 'Wed, 27 Feb 2013 15:56:13 GMT', |
60 |
'server': 'gunicorn/0.14.5', |
61 |
'vary': 'Accept-Language', |
62 |
'x-container-block-hash': 'sha256', |
63 |
'x-container-block-size': 4194304, |
64 |
'x-container-bytes-used': 309528938, |
65 |
'x-container-object-count': 14, |
66 |
'x-container-object-meta': '', |
67 |
'x-container-policy-quota': 53687091200, |
68 |
'x-container-policy-versioning': 'auto'} |
69 |
|
70 |
|
71 |
class Pithos(TestCase): |
72 |
|
73 |
class FR(object): |
74 |
"""FR stands for Fake Response"""
|
75 |
json = dict()
|
76 |
headers = {} |
77 |
content = json |
78 |
status = None
|
79 |
status_code = 200
|
80 |
headers = dict()
|
81 |
|
82 |
def release(self): |
83 |
pass
|
84 |
|
85 |
files = [] |
86 |
|
87 |
def assert_dicts_are_equal(self, d1, d2): |
88 |
for k, v in d1.items(): |
89 |
self.assertTrue(k in d2) |
90 |
if isinstance(v, dict): |
91 |
self.assert_dicts_are_equal(v, d2[k])
|
92 |
else:
|
93 |
self.assertEqual(unicode(v), unicode(d2[k])) |
94 |
|
95 |
def setUp(self): |
96 |
self.url = 'https://www.example.com/pithos' |
97 |
self.token = 'p17h0570k3n' |
98 |
self.client = PC(self.url, self.token) |
99 |
self.client.account = user_id
|
100 |
self.client.container = 'c0nt@1n3r_i' |
101 |
|
102 |
def tearDown(self): |
103 |
self.FR.headers = dict() |
104 |
self.FR.status_code = 200 |
105 |
|
106 |
def test_get_account_info(self): |
107 |
self.FR.headers = account_info
|
108 |
self.FR.status_code = 204 |
109 |
with patch.object(C, 'perform_request', return_value=self.FR()): |
110 |
r = self.client.get_account_info()
|
111 |
self.assertEqual(self.client.http_client.url, self.url) |
112 |
self.assertEqual(self.client.http_client.path, '/%s' % user_id) |
113 |
self.assert_dicts_are_equal(r, account_info)
|
114 |
PC.set_param = Mock() |
115 |
untils = ['date 1', 'date 2', 'date 3'] |
116 |
for unt in untils: |
117 |
r = self.client.get_account_info(until=unt)
|
118 |
self.assert_dicts_are_equal(r, account_info)
|
119 |
for i in range(len(untils)): |
120 |
self.assertEqual(
|
121 |
PC.set_param.mock_calls[i], |
122 |
call('until', untils[i], iff=untils[i]))
|
123 |
self.FR.status_code = 401 |
124 |
self.assertRaises(ClientError, self.client.get_account_info) |
125 |
|
126 |
def test_replace_account_meta(self): |
127 |
self.FR.status_code = 202 |
128 |
metas = dict(k1='v1', k2='v2', k3='v3') |
129 |
PC.set_header = Mock() |
130 |
with patch.object(C, 'perform_request', return_value=self.FR()): |
131 |
self.client.replace_account_meta(metas)
|
132 |
self.assertEqual(self.client.http_client.url, self.url) |
133 |
self.assertEqual(self.client.http_client.path, '/%s' % user_id) |
134 |
prfx = 'X-Account-Meta-'
|
135 |
expected = [call('%s%s' % (prfx, k), v) for k, v in metas.items()] |
136 |
self.assertEqual(PC.set_header.mock_calls, expected)
|
137 |
|
138 |
def test_del_account_meta(self): |
139 |
keys = ['k1', 'k2', 'k3'] |
140 |
with patch.object(PC, 'account_post', return_value=self.FR()) as ap: |
141 |
expected = [] |
142 |
for key in keys: |
143 |
self.client.del_account_meta(key)
|
144 |
expected.append(call(update=True, metadata={key: ''})) |
145 |
self.assertEqual(ap.mock_calls, expected)
|
146 |
|
147 |
def test_create_container(self): |
148 |
self.FR.status_code = 201 |
149 |
with patch.object(PC, 'put', return_value=self.FR()) as put: |
150 |
cont = 's0m3c0n731n3r'
|
151 |
self.client.create_container(cont)
|
152 |
expected = [call('/%s/%s' % (user_id, cont), success=(201, 202))] |
153 |
self.assertEqual(put.mock_calls, expected)
|
154 |
self.FR.status_code = 202 |
155 |
self.assertRaises(ClientError, self.client.create_container, cont) |
156 |
|
157 |
def test_get_container_info(self): |
158 |
self.FR.headers = container_info
|
159 |
with patch.object(PC, 'container_head', return_value=self.FR()) as ch: |
160 |
r = self.client.get_container_info()
|
161 |
self.assert_dicts_are_equal(r, container_info)
|
162 |
u = 'some date'
|
163 |
r = self.client.get_container_info(until=u)
|
164 |
self.assertEqual(ch.mock_calls, [call(until=None), call(until=u)]) |