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