Debian Codegraph @

keystoneclient/tests/unit/test_base.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13
14 import uuid
15
16 import fixtures
17 from keystoneauth1.identity import v2
18 from keystoneauth1 import session
19 import requests
20
21 from keystoneclient import base
22 from keystoneclient import exceptions
23 from keystoneclient.tests.unit import utils
24 from keystoneclient import utils as base_utils
25 from keystoneclient.v2_0 import client
26 from keystoneclient.v2_0 import roles
27 from keystoneclient.v3 import users
28
29 TEST_REQUEST_ID = uuid.uuid4().hex
30 TEST_REQUEST_ID_1 = uuid.uuid4().hex
31
32
33 def create_response_with_request_id_header():
34 resp = requests.Response()
35 resp.headers['x-openstack-request-id'] = TEST_REQUEST_ID
36 return resp
37
38
39 class HumanReadable(base.Resource):
40 HUMAN_ID = True
41
42
43 class BaseTest(utils.TestCase):
44
45 def test_resource_repr(self):
46 r = base.Resource(None, dict(foo="bar", baz="spam"))
47 self.assertEqual(repr(r), "<Resource baz=spam, foo=bar>")
48
49 def test_getid(self):
50 self.assertEqual(base.getid(4), 4)
51
52 class TmpObject(object):
53 id = 4
54 self.assertEqual(base.getid(TmpObject), 4)
55
56 def test_resource_lazy_getattr(self):
57 auth = v2.Token(token=self.TEST_TOKEN,
58 auth_url='http://127.0.0.1:5000')
59 session_ = session.Session(auth=auth)
60 self.client = client.Client(session=session_)
61
62 self.useFixture(fixtures.MockPatchObject(
63 self.client._adapter, 'get', side_effect=AttributeError,
64 autospec=True))
65
66 f = roles.Role(self.client.roles, {'id': 1, 'name': 'Member'})
67 self.assertEqual(f.name, 'Member')
68
69 # Missing stuff still fails after a second get
70 self.assertRaises(AttributeError, getattr, f, 'blahblah')
71
72 def test_eq(self):
73 # Two resources with same ID: never equal if their info is not equal
74 r1 = base.Resource(None, {'id': 1, 'name': 'hi'})
75 r2 = base.Resource(None, {'id': 1, 'name': 'hello'})
76 self.assertNotEqual(r1, r2)
77 self.assertTrue(r1 != r2)
78
79 # Two resources with same ID: equal if their info is equal
80 # The truth of r1==r2 does not imply that r1!=r2 is false in PY2.
81 # Test that inequality operator is defined and that comparing equal
82 # items returns False
83 r1 = base.Resource(None, {'id': 1, 'name': 'hello'})
84 r2 = base.Resource(None, {'id': 1, 'name': 'hello'})
85 self.assertTrue(r1 == r2)
86 self.assertFalse(r1 != r2)
87
88 # Two resources of different types: never equal
89 r1 = base.Resource(None, {'id': 1})
90 r2 = roles.Role(None, {'id': 1})
91 self.assertNotEqual(r1, r2)
92 self.assertTrue(r1 != r2)
93
94 # Two resources with no ID: equal if their info is equal
95 # The truth of r1==r2 does not imply that r1!=r2 is false in PY2.
96 # Test that inequality operator is defined and that comparing equal
97 # items returns False.
98 r1 = base.Resource(None, {'name': 'joe', 'age': 12})
99 r2 = base.Resource(None, {'name': 'joe', 'age': 12})
100 self.assertTrue(r1 == r2)
101 self.assertFalse(r1 != r2)
102
103 r1 = base.Resource(None, {'id': 1})
104 self.assertNotEqual(r1, object())
105 self.assertTrue(r1 != object())
106 self.assertNotEqual(r1, {'id': 1})
107 self.assertTrue(r1 != {'id': 1})
108
109 def test_human_id(self):
110 r = base.Resource(None, {"name": "1 of !"})
111 self.assertIsNone(r.human_id)
112 r = HumanReadable(None, {"name": "1 of !"})
113 self.assertEqual(r.human_id, "1-of")
114
115 def test_non_ascii_attr(self):
116 r_dict = {"name": "foobar",
117 u"тест": "1234",
118 u"тест2": u"привет мир"}
119
120 r = base.Resource(None, r_dict)
121 self.assertEqual(r.name, "foobar")
122 self.assertEqual(r.to_dict(), r_dict)
123
124
125 class ManagerTest(utils.TestCase):
126 body = {"hello": {"hi": 1}}
127 url = "/test-url"
128
129 def setUp(self):
130 super(ManagerTest, self).setUp()
131
132 auth = v2.Token(auth_url='http://127.0.0.1:5000',
133 token=self.TEST_TOKEN)
134 session_ = session.Session(auth=auth)
135 self.client = client.Client(session=session_)._adapter
136
137 self.mgr = base.Manager(self.client)
138 self.mgr.resource_class = base.Resource
139
140 def test_api(self):
141 with self.deprecations.expect_deprecations_here():
142 self.assertEqual(self.mgr.api, self.client)
143
144 def test_get(self):
145 get_mock = self.useFixture(fixtures.MockPatchObject(
146 self.client, 'get', autospec=True, return_value=(None, self.body))
147 ).mock
148 rsrc = self.mgr._get(self.url, "hello")
149 get_mock.assert_called_once_with(self.url)
150 self.assertEqual(rsrc.hi, 1)
151
152 def test_post(self):
153 post_mock = self.useFixture(fixtures.MockPatchObject(
154 self.client, 'post', autospec=True, return_value=(None, self.body))
155 ).mock
156
157 rsrc = self.mgr._post(self.url, self.body, "hello")
158 post_mock.assert_called_once_with(self.url, body=self.body)
159 self.assertEqual(rsrc.hi, 1)
160
161 post_mock.reset_mock()
162
163 rsrc = self.mgr._post(self.url, self.body, "hello", return_raw=True)
164 post_mock.assert_called_once_with(self.url, body=self.body)
165 self.assertEqual(rsrc["hi"], 1)
166
167 def test_put(self):
168 put_mock = self.useFixture(fixtures.MockPatchObject(
169 self.client, 'put', autospec=True, return_value=(None, self.body))
170 ).mock
171
172 rsrc = self.mgr._put(self.url, self.body, "hello")
173 put_mock.assert_called_once_with(self.url, body=self.body)
174 self.assertEqual(rsrc.hi, 1)
175
176 put_mock.reset_mock()
177
178 rsrc = self.mgr._put(self.url, self.body)
179 put_mock.assert_called_once_with(self.url, body=self.body)
180 self.assertEqual(rsrc.hello["hi"], 1)
181
182 def test_patch(self):
183 patch_mock = self.useFixture(fixtures.MockPatchObject(
184 self.client, 'patch', autospec=True,
185 return_value=(None, self.body))
186 ).mock
187
188 rsrc = self.mgr._patch(self.url, self.body, "hello")
189 patch_mock.assert_called_once_with(self.url, body=self.body)
190 self.assertEqual(rsrc.hi, 1)
191
192 patch_mock.reset_mock()
193
194 rsrc = self.mgr._patch(self.url, self.body)
195 patch_mock.assert_called_once_with(self.url, body=self.body)
196 self.assertEqual(rsrc.hello["hi"], 1)
197
198 def test_update(self):
199 patch_mock = self.useFixture(fixtures.MockPatchObject(
200 self.client, 'patch', autospec=True,
201 return_value=(None, self.body))
202 ).mock
203
204 put_mock = self.useFixture(fixtures.MockPatchObject(
205 self.client, 'put', autospec=True, return_value=(None, self.body))
206 ).mock
207
208 rsrc = self.mgr._update(
209 self.url, body=self.body, response_key="hello", method="PATCH",
210 management=False)
211 patch_mock.assert_called_once_with(
212 self.url, management=False, body=self.body)
213 self.assertEqual(rsrc.hi, 1)
214
215 rsrc = self.mgr._update(
216 self.url, body=None, response_key="hello", method="PUT",
217 management=True)
218 put_mock.assert_called_once_with(self.url, management=True, body=None)
219 self.assertEqual(rsrc.hi, 1)
220
221
222 class ManagerRequestIdTest(utils.TestCase):
223 url = "/test-url"
224 resp = create_response_with_request_id_header()
225
226 def setUp(self):
227 super(ManagerRequestIdTest, self).setUp()
228
229 auth = v2.Token(auth_url='http://127.0.0.1:5000',
230 token=self.TEST_TOKEN)
231 session_ = session.Session(auth=auth)
232 self.client = client.Client(session=session_,
233 include_metadata='True')._adapter
234
235 self.mgr = base.Manager(self.client)
236 self.mgr.resource_class = base.Resource
237
238 def mock_request_method(self, request_method, body):
239 return self.useFixture(fixtures.MockPatchObject(
240 self.client, request_method, autospec=True,
241 return_value=(self.resp, body))
242 ).mock
243
244 def test_get(self):
245 body = {"hello": {"hi": 1}}
246 get_mock = self.mock_request_method('get', body)
247 rsrc = self.mgr._get(self.url, "hello")
248 get_mock.assert_called_once_with(self.url)
249 self.assertEqual(rsrc.data.hi, 1)
250 self.assertEqual(rsrc.request_ids[0], TEST_REQUEST_ID)
251
252 def test_list(self):
253 body = {"hello": [{"name": "admin"}, {"name": "admin"}]}
254 get_mock = self.mock_request_method('get', body)
255
256 returned_list = self.mgr._list(self.url, "hello")
257 self.assertEqual(returned_list.request_ids[0], TEST_REQUEST_ID)
258 get_mock.assert_called_once_with(self.url)
259
260 def test_list_with_multiple_response_objects(self):
261 body = {"hello": [{"name": "admin"}, {"name": "admin"}]}
262 resp_1 = requests.Response()
263 resp_1.headers['x-openstack-request-id'] = TEST_REQUEST_ID
264 resp_2 = requests.Response()
265 resp_2.headers['x-openstack-request-id'] = TEST_REQUEST_ID_1
266
267 resp_result = [resp_1, resp_2]
268 get_mock = self.useFixture(fixtures.MockPatchObject(
269 self.client, 'get', autospec=True,
270 return_value=(resp_result, body))
271 ).mock
272
273 returned_list = self.mgr._list(self.url, "hello")
274 self.assertIn(returned_list.request_ids[0], [
275 TEST_REQUEST_ID, TEST_REQUEST_ID_1])
276 self.assertIn(returned_list.request_ids[1], [
277 TEST_REQUEST_ID, TEST_REQUEST_ID_1])
278 get_mock.assert_called_once_with(self.url)
279
280 def test_post(self):
281 body = {"hello": {"hi": 1}}
282 post_mock = self.mock_request_method('post', body)
283 rsrc = self.mgr._post(self.url, body, "hello")
284 post_mock.assert_called_once_with(self.url, body=body)
285 self.assertEqual(rsrc.data.hi, 1)
286
287 post_mock.reset_mock()
288
289 rsrc = self.mgr._post(self.url, body, "hello", return_raw=True)
290 post_mock.assert_called_once_with(self.url, body=body)
291 self.assertNotIsInstance(rsrc, base.Response)
292 self.assertEqual(rsrc["hi"], 1)
293
294 def test_put(self):
295 body = {"hello": {"hi": 1}}
296 put_mock = self.mock_request_method('put', body)
297 rsrc = self.mgr._put(self.url, body, "hello")
298 put_mock.assert_called_once_with(self.url, body=body)
299 self.assertEqual(rsrc.data.hi, 1)
300
301 put_mock.reset_mock()
302
303 rsrc = self.mgr._put(self.url, body)
304 put_mock.assert_called_once_with(self.url, body=body)
305 self.assertEqual(rsrc.data.hello["hi"], 1)
306 self.assertEqual(rsrc.request_ids[0], TEST_REQUEST_ID)
307
308 def test_head(self):
309 get_mock = self.mock_request_method('head', None)
310 rsrc = self.mgr._head(self.url)
311 get_mock.assert_called_once_with(self.url)
312 self.assertFalse(rsrc.data)
313 self.assertEqual(rsrc.request_ids[0], TEST_REQUEST_ID)
314
315 def test_delete(self):
316 delete_mock = self.mock_request_method('delete', None)
317 resp, base_resp = self.mgr._delete(self.url, name="hello")
318
319 delete_mock.assert_called_once_with('/test-url', name='hello')
320 self.assertEqual(base_resp.request_ids[0], TEST_REQUEST_ID)
321 self.assertEqual(base_resp.data, None)
322 self.assertIsInstance(resp, requests.Response)
323
324 def test_patch(self):
325 body = {"hello": {"hi": 1}}
326 patch_mock = self.mock_request_method('patch', body)
327 rsrc = self.mgr._patch(self.url, body, "hello")
328 patch_mock.assert_called_once_with(self.url, body=body)
329 self.assertEqual(rsrc.data.hi, 1)
330
331 patch_mock.reset_mock()
332
333 rsrc = self.mgr._patch(self.url, body)
334 patch_mock.assert_called_once_with(self.url, body=body)
335 self.assertEqual(rsrc.data.hello["hi"], 1)
336 self.assertEqual(rsrc.request_ids[0], TEST_REQUEST_ID)
337
338 def test_update(self):
339 body = {"hello": {"hi": 1}}
340 patch_mock = self.mock_request_method('patch', body)
341 put_mock = self.mock_request_method('put', body)
342
343 rsrc = self.mgr._update(
344 self.url, body=body, response_key="hello", method="PATCH",
345 management=False)
346 patch_mock.assert_called_once_with(
347 self.url, management=False, body=body)
348 self.assertEqual(rsrc.data.hi, 1)
349
350 rsrc = self.mgr._update(
351 self.url, body=None, response_key="hello", method="PUT",
352 management=True)
353 put_mock.assert_called_once_with(self.url, management=True, body=None)
354 self.assertEqual(rsrc.data.hi, 1)
355 self.assertEqual(rsrc.request_ids[0], TEST_REQUEST_ID)
356
357
358 class ManagerWithFindRequestIdTest(utils.TestCase):
359 url = "/fakes"
360 resp = create_response_with_request_id_header()
361
362 def setUp(self):
363 super(ManagerWithFindRequestIdTest, self).setUp()
364
365 auth = v2.Token(auth_url='http://127.0.0.1:5000',
366 token=self.TEST_TOKEN)
367 session_ = session.Session(auth=auth)
368 self.client = client.Client(session=session_,
369 include_metadata='True')._adapter
370
371 def test_find_resource(self):
372 body = {"roles": [{"name": 'entity_one'}, {"name": 'entity_one_1'}]}
373 request_resp = requests.Response()
374 request_resp.headers['x-openstack-request-id'] = TEST_REQUEST_ID
375
376 get_mock = self.useFixture(fixtures.MockPatchObject(
377 self.client, 'get', autospec=True,
378 side_effect=[exceptions.NotFound, (request_resp, body)])
379 ).mock
380
381 mgr = roles.RoleManager(self.client)
382 mgr.resource_class = roles.Role
383 response = base_utils.find_resource(mgr, 'entity_one')
384 get_mock.assert_called_with('/OS-KSADM/roles')
385 self.assertEqual(response.request_ids[0], TEST_REQUEST_ID)
386
387
388 class CrudManagerRequestIdTest(utils.TestCase):
389 resp = create_response_with_request_id_header()
390 request_resp = requests.Response()
391 request_resp.headers['x-openstack-request-id'] = TEST_REQUEST_ID
392
393 def setUp(self):
394 super(CrudManagerRequestIdTest, self).setUp()
395
396 auth = v2.Token(auth_url='http://127.0.0.1:5000',
397 token=self.TEST_TOKEN)
398 session_ = session.Session(auth=auth)
399 self.client = client.Client(session=session_,
400 include_metadata='True')._adapter
401
402 def test_find_resource(self):
403 body = {"users": [{"name": 'entity_one'}]}
404 get_mock = self.useFixture(fixtures.MockPatchObject(
405 self.client, 'get', autospec=True,
406 side_effect=[exceptions.NotFound, (self.request_resp, body)])
407 ).mock
408 mgr = users.UserManager(self.client)
409 mgr.resource_class = users.User
410 response = base_utils.find_resource(mgr, 'entity_one')
411 get_mock.assert_called_with('/users?name=entity_one')
412 self.assertEqual(response.request_ids[0], TEST_REQUEST_ID)
413
414 def test_list(self):
415 body = {"users": [{"name": "admin"}, {"name": "admin"}]}
416
417 get_mock = self.useFixture(fixtures.MockPatchObject(
418 self.client, 'get', autospec=True,
419 return_value=(self.request_resp, body))
420 ).mock
421 mgr = users.UserManager(self.client)
422 mgr.resource_class = users.User
423 returned_list = mgr.list()
424 self.assertEqual(returned_list.request_ids[0], TEST_REQUEST_ID)
425 get_mock.assert_called_once_with('/users?')