root / kamaki / clients / astakos / test.py @ 058ee9a8
History | View | Annotate | Download (9.1 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 | ae4585f5 | Stavros Sachtouris | from mock import patch, call |
35 | 91478081 | Stavros Sachtouris | from logging import getLogger |
36 | c1004a00 | Stavros Sachtouris | from unittest import TestCase |
37 | 3c50df2e | Stavros Sachtouris | |
38 | 91478081 | Stavros Sachtouris | from kamaki.clients import ClientError |
39 | 274bcf7a | Stavros Sachtouris | |
40 | 274bcf7a | Stavros Sachtouris | |
41 | 274bcf7a | Stavros Sachtouris | example = dict(
|
42 | 508570ae | Stavros Sachtouris | access=dict(
|
43 | b44a5a37 | Stavros Sachtouris | token=dict(
|
44 | b44a5a37 | Stavros Sachtouris | expires="2013-07-14T10:07:42.481134+00:00",
|
45 | b44a5a37 | Stavros Sachtouris | id="ast@k0sT0k3n==",
|
46 | b44a5a37 | Stavros Sachtouris | tenant=dict(
|
47 | b44a5a37 | Stavros Sachtouris | id="42",
|
48 | b44a5a37 | Stavros Sachtouris | name="Simple Name 0")
|
49 | b44a5a37 | Stavros Sachtouris | ), |
50 | 508570ae | Stavros Sachtouris | serviceCatalog=[ |
51 | 508570ae | Stavros Sachtouris | dict(name='service name 1', type='compute', endpoints=[ |
52 | 508570ae | Stavros Sachtouris | dict(versionId='v1', publicUrl='http://1.1.1.1/v1'), |
53 | 508570ae | Stavros Sachtouris | dict(versionId='v2', publicUrl='http://1.1.1.1/v2')]), |
54 | 508570ae | Stavros Sachtouris | dict(name='service name 3', type='object-storage', endpoints=[ |
55 | 508570ae | Stavros Sachtouris | dict(versionId='v2', publicUrl='http://1.1.1.1/v2'), |
56 | 508570ae | Stavros Sachtouris | dict(versionId='v2.1', publicUrl='http://1.1.1.1/v2/xtra')]) |
57 | 508570ae | Stavros Sachtouris | ], |
58 | 508570ae | Stavros Sachtouris | user=dict(
|
59 | 508570ae | Stavros Sachtouris | name='Simple Name 0',
|
60 | 508570ae | Stavros Sachtouris | username='User Full Name 0',
|
61 | 508570ae | Stavros Sachtouris | auth_token_expires='1362585796000',
|
62 | 508570ae | Stavros Sachtouris | auth_token_created='1359931796000',
|
63 | 508570ae | Stavros Sachtouris | email=['user0@example.gr'],
|
64 | 508570ae | Stavros Sachtouris | id=42,
|
65 | 508570ae | Stavros Sachtouris | uuid='aus3r-uu1d-507-73s71ng-as7ak0s')
|
66 | 508570ae | Stavros Sachtouris | ) |
67 | 528550d9 | Stavros Sachtouris | ) |
68 | c1004a00 | Stavros Sachtouris | |
69 | c1004a00 | Stavros Sachtouris | |
70 | 975270f2 | Stavros Sachtouris | class FR(object): |
71 | 975270f2 | Stavros Sachtouris | json = example |
72 | 975270f2 | Stavros Sachtouris | headers = {} |
73 | 975270f2 | Stavros Sachtouris | content = json |
74 | 975270f2 | Stavros Sachtouris | status = None
|
75 | 975270f2 | Stavros Sachtouris | status_code = 200
|
76 | 975270f2 | Stavros Sachtouris | |
77 | 91478081 | Stavros Sachtouris | astakos_pkg = 'kamaki.clients.astakos'
|
78 | c1004a00 | Stavros Sachtouris | |
79 | c1004a00 | Stavros Sachtouris | |
80 | 3c50df2e | Stavros Sachtouris | class AstakosClient(TestCase): |
81 | c1004a00 | Stavros Sachtouris | |
82 | 274bcf7a | Stavros Sachtouris | cached = False
|
83 | 274bcf7a | Stavros Sachtouris | |
84 | 528550d9 | Stavros Sachtouris | def assert_dicts_are_equal(self, d1, d2): |
85 | 528550d9 | Stavros Sachtouris | for k, v in d1.items(): |
86 | 528550d9 | Stavros Sachtouris | self.assertTrue(k in d2) |
87 | 528550d9 | Stavros Sachtouris | if isinstance(v, dict): |
88 | 528550d9 | Stavros Sachtouris | self.assert_dicts_are_equal(v, d2[k])
|
89 | 528550d9 | Stavros Sachtouris | else:
|
90 | 528550d9 | Stavros Sachtouris | self.assertEqual(unicode(v), unicode(d2[k])) |
91 | 528550d9 | Stavros Sachtouris | |
92 | c1004a00 | Stavros Sachtouris | def setUp(self): |
93 | c1004a00 | Stavros Sachtouris | self.url = 'https://astakos.example.com' |
94 | c1004a00 | Stavros Sachtouris | self.token = 'ast@k0sT0k3n==' |
95 | 91478081 | Stavros Sachtouris | from kamaki.clients.astakos import AstakosClient as AC |
96 | 91478081 | Stavros Sachtouris | self.client = AC(self.url, self.token) |
97 | c1004a00 | Stavros Sachtouris | |
98 | 975270f2 | Stavros Sachtouris | def tearDown(self): |
99 | 975270f2 | Stavros Sachtouris | FR.json = example |
100 | 975270f2 | Stavros Sachtouris | |
101 | 91478081 | Stavros Sachtouris | @patch(
|
102 | 91478081 | Stavros Sachtouris | '%s.SynnefoAstakosClient.__init__' % astakos_pkg, return_value=None) |
103 | 91478081 | Stavros Sachtouris | @patch(
|
104 | 91478081 | Stavros Sachtouris | '%s.SynnefoAstakosClient.get_endpoints' % astakos_pkg,
|
105 | 91478081 | Stavros Sachtouris | return_value=example) |
106 | 91478081 | Stavros Sachtouris | def _authenticate(self, get_endpoints, sac): |
107 | 975270f2 | Stavros Sachtouris | r = self.client.authenticate()
|
108 | 91478081 | Stavros Sachtouris | self.assertEqual(
|
109 | 91478081 | Stavros Sachtouris | sac.mock_calls[-1], call(self.token, self.url, |
110 | 91478081 | Stavros Sachtouris | logger=getLogger('astakosclient')))
|
111 | 91478081 | Stavros Sachtouris | self.assertEqual(get_endpoints.mock_calls[-1], call()) |
112 | 274bcf7a | Stavros Sachtouris | return r
|
113 | c1004a00 | Stavros Sachtouris | |
114 | 274bcf7a | Stavros Sachtouris | def test_authenticate(self): |
115 | 274bcf7a | Stavros Sachtouris | r = self._authenticate()
|
116 | 528550d9 | Stavros Sachtouris | self.assert_dicts_are_equal(r, example)
|
117 | 91478081 | Stavros Sachtouris | uuid = example['access']['user']['id'] |
118 | 91478081 | Stavros Sachtouris | self.assert_dicts_are_equal(self.client._uuids, {self.token: uuid}) |
119 | 91478081 | Stavros Sachtouris | self.assert_dicts_are_equal(self.client._cache, {uuid: r}) |
120 | 91478081 | Stavros Sachtouris | from astakosclient import AstakosClient as SAC |
121 | 91478081 | Stavros Sachtouris | self.assertTrue(isinstance(self.client._astakos[uuid], SAC)) |
122 | 91478081 | Stavros Sachtouris | self.assert_dicts_are_equal(self.client._uuids2usernames, {uuid: {}}) |
123 | 91478081 | Stavros Sachtouris | self.assert_dicts_are_equal(self.client._usernames2uuids, {uuid: {}}) |
124 | 91478081 | Stavros Sachtouris | |
125 | 172ee8f9 | Stavros Sachtouris | def test_get_client(self): |
126 | 172ee8f9 | Stavros Sachtouris | if not self.cached: |
127 | 172ee8f9 | Stavros Sachtouris | self._authenticate()
|
128 | 172ee8f9 | Stavros Sachtouris | from astakosclient import AstakosClient as SNFAC |
129 | 172ee8f9 | Stavros Sachtouris | self.assertTrue(self.client.get_client(), SNFAC) |
130 | 172ee8f9 | Stavros Sachtouris | |
131 | 91478081 | Stavros Sachtouris | def test_get_token(self): |
132 | 91478081 | Stavros Sachtouris | self._authenticate()
|
133 | 91478081 | Stavros Sachtouris | uuid = self.client._uuids.values()[0] |
134 | 91478081 | Stavros Sachtouris | self.assertEqual(self.client.get_token(uuid), self.token) |
135 | 528550d9 | Stavros Sachtouris | |
136 | 528550d9 | Stavros Sachtouris | def test_get_services(self): |
137 | 528550d9 | Stavros Sachtouris | if not self.cached: |
138 | 528550d9 | Stavros Sachtouris | self._authenticate()
|
139 | 528550d9 | Stavros Sachtouris | slist = self.client.get_services()
|
140 | 508570ae | Stavros Sachtouris | self.assertEqual(slist, example['access']['serviceCatalog']) |
141 | c1004a00 | Stavros Sachtouris | |
142 | 528550d9 | Stavros Sachtouris | def test_get_service_details(self): |
143 | 528550d9 | Stavros Sachtouris | if not self.cached: |
144 | 528550d9 | Stavros Sachtouris | self._authenticate()
|
145 | 528550d9 | Stavros Sachtouris | stype = '#FAIL'
|
146 | 528550d9 | Stavros Sachtouris | self.assertRaises(ClientError, self.client.get_service_details, stype) |
147 | 528550d9 | Stavros Sachtouris | stype = 'compute'
|
148 | 508570ae | Stavros Sachtouris | expected = [s for s in example['access']['serviceCatalog'] if ( |
149 | 508570ae | Stavros Sachtouris | s['type'] == stype)]
|
150 | 528550d9 | Stavros Sachtouris | self.assert_dicts_are_equal(
|
151 | 528550d9 | Stavros Sachtouris | self.client.get_service_details(stype), expected[0]) |
152 | 528550d9 | Stavros Sachtouris | |
153 | 528550d9 | Stavros Sachtouris | def test_get_service_endpoints(self): |
154 | 528550d9 | Stavros Sachtouris | if not self.cached: |
155 | 528550d9 | Stavros Sachtouris | self._authenticate()
|
156 | 528550d9 | Stavros Sachtouris | stype, version = 'compute', 'V2' |
157 | 528550d9 | Stavros Sachtouris | self.assertRaises(
|
158 | 528550d9 | Stavros Sachtouris | ClientError, self.client.get_service_endpoints, stype)
|
159 | 508570ae | Stavros Sachtouris | expected = [s for s in example['access']['serviceCatalog'] if ( |
160 | 508570ae | Stavros Sachtouris | s['type'] == stype)]
|
161 | 528550d9 | Stavros Sachtouris | expected = [e for e in expected[0]['endpoints'] if ( |
162 | 508570ae | Stavros Sachtouris | e['versionId'] == version.lower())]
|
163 | 528550d9 | Stavros Sachtouris | self.assert_dicts_are_equal(
|
164 | 528550d9 | Stavros Sachtouris | self.client.get_service_endpoints(stype, version), expected[0]) |
165 | 528550d9 | Stavros Sachtouris | |
166 | 528550d9 | Stavros Sachtouris | def test_user_info(self): |
167 | 274bcf7a | Stavros Sachtouris | if not self.cached: |
168 | 274bcf7a | Stavros Sachtouris | self._authenticate()
|
169 | 508570ae | Stavros Sachtouris | self.assertTrue(set(example['access']['user'].keys()).issubset( |
170 | 508570ae | Stavros Sachtouris | self.client.user_info().keys()))
|
171 | c1004a00 | Stavros Sachtouris | |
172 | 528550d9 | Stavros Sachtouris | def test_item(self): |
173 | 274bcf7a | Stavros Sachtouris | if not self.cached: |
174 | 274bcf7a | Stavros Sachtouris | self._authenticate()
|
175 | 508570ae | Stavros Sachtouris | for term, val in example['access']['user'].items(): |
176 | 274bcf7a | Stavros Sachtouris | self.assertEqual(self.client.term(term, self.token), val) |
177 | 528550d9 | Stavros Sachtouris | self.assertTrue(
|
178 | 508570ae | Stavros Sachtouris | example['access']['user']['email'][0] in self.client.term('email')) |
179 | c1004a00 | Stavros Sachtouris | |
180 | 528550d9 | Stavros Sachtouris | def test_list_users(self): |
181 | 274bcf7a | Stavros Sachtouris | if not self.cached: |
182 | b44a5a37 | Stavros Sachtouris | self._authenticate()
|
183 | 91478081 | Stavros Sachtouris | FR.json = example |
184 | 975270f2 | Stavros Sachtouris | self._authenticate()
|
185 | 528550d9 | Stavros Sachtouris | r = self.client.list_users()
|
186 | 274bcf7a | Stavros Sachtouris | self.assertTrue(len(r) == 1) |
187 | 274bcf7a | Stavros Sachtouris | self.assertEqual(r[0]['auth_token'], self.token) |
188 | 85898ca4 | Stavros Sachtouris | |
189 | 91478081 | Stavros Sachtouris | @patch(
|
190 | 91478081 | Stavros Sachtouris | '%s.SynnefoAstakosClient.get_usernames' % astakos_pkg,
|
191 | 91478081 | Stavros Sachtouris | return_value={42: 'username42', 43: 'username43'}) |
192 | 91478081 | Stavros Sachtouris | def test_uuids2usernames(self, get_usernames): |
193 | 91478081 | Stavros Sachtouris | from astakosclient import AstakosClientException |
194 | 91478081 | Stavros Sachtouris | self.assertRaises(
|
195 | 91478081 | Stavros Sachtouris | AstakosClientException, self.client.uuids2usernames, [42, 43]) |
196 | 91478081 | Stavros Sachtouris | with patch(
|
197 | 91478081 | Stavros Sachtouris | '%s.SynnefoAstakosClient.__init__' % astakos_pkg,
|
198 | 91478081 | Stavros Sachtouris | return_value=None) as sac: |
199 | 91478081 | Stavros Sachtouris | with patch(
|
200 | 91478081 | Stavros Sachtouris | '%s.SynnefoAstakosClient.get_endpoints' % astakos_pkg,
|
201 | 91478081 | Stavros Sachtouris | return_value=example) as get_endpoints:
|
202 | 91478081 | Stavros Sachtouris | r = self.client.uuids2usernames([42, 43]) |
203 | 91478081 | Stavros Sachtouris | self.assert_dicts_are_equal(
|
204 | 91478081 | Stavros Sachtouris | r, {42: 'username42', 43: 'username43'}) |
205 | 91478081 | Stavros Sachtouris | self.assertEqual(sac.mock_calls[-1], call( |
206 | 91478081 | Stavros Sachtouris | self.token, self.url, logger=getLogger('astakosclient'))) |
207 | 91478081 | Stavros Sachtouris | self.assertEqual(get_endpoints.mock_calls[-1], call()) |
208 | 91478081 | Stavros Sachtouris | self.assertEqual(get_usernames.mock_calls[-1], call([42, 43])) |
209 | 91478081 | Stavros Sachtouris | |
210 | 91478081 | Stavros Sachtouris | @patch(
|
211 | 91478081 | Stavros Sachtouris | '%s.SynnefoAstakosClient.get_uuids' % astakos_pkg,
|
212 | 91478081 | Stavros Sachtouris | return_value={'username42': 42, 'username43': 43}) |
213 | 91478081 | Stavros Sachtouris | def test_usernames2uuids(self, get_uuids): |
214 | 91478081 | Stavros Sachtouris | from astakosclient import AstakosClientException |
215 | 91478081 | Stavros Sachtouris | self.assertRaises(
|
216 | 91478081 | Stavros Sachtouris | AstakosClientException, self.client.usernames2uuids, ['u1', 'u2']) |
217 | 91478081 | Stavros Sachtouris | with patch(
|
218 | 91478081 | Stavros Sachtouris | '%s.SynnefoAstakosClient.__init__' % astakos_pkg,
|
219 | 91478081 | Stavros Sachtouris | return_value=None) as sac: |
220 | 91478081 | Stavros Sachtouris | with patch(
|
221 | 91478081 | Stavros Sachtouris | '%s.SynnefoAstakosClient.get_endpoints' % astakos_pkg,
|
222 | 91478081 | Stavros Sachtouris | return_value=example) as get_endpoints:
|
223 | 91478081 | Stavros Sachtouris | r = self.client.usernames2uuids(['u1', 'u2']) |
224 | 91478081 | Stavros Sachtouris | self.assert_dicts_are_equal(
|
225 | 91478081 | Stavros Sachtouris | r, {'username42': 42, 'username43': 43}) |
226 | 91478081 | Stavros Sachtouris | self.assertEqual(sac.mock_calls[-1], call( |
227 | 91478081 | Stavros Sachtouris | self.token, self.url, logger=getLogger('astakosclient'))) |
228 | 91478081 | Stavros Sachtouris | self.assertEqual(get_endpoints.mock_calls[-1], call()) |
229 | 91478081 | Stavros Sachtouris | self.assertEqual(get_uuids.mock_calls[-1], call(['u1', 'u2'])) |
230 | 91478081 | Stavros Sachtouris | |
231 | 91478081 | Stavros Sachtouris | |
232 | 85898ca4 | Stavros Sachtouris | if __name__ == '__main__': |
233 | 85898ca4 | Stavros Sachtouris | from sys import argv |
234 | 85898ca4 | Stavros Sachtouris | from kamaki.clients.test import runTestCase |
235 | 3c50df2e | Stavros Sachtouris | runTestCase(AstakosClient, 'AstakosClient', argv[1:]) |