From 610177a779b95f931356c1e90b05a5bffd2616b3 Mon Sep 17 00:00:00 2001 From: Ravi Jethani Date: Fri, 22 Apr 2016 13:54:49 +0000 Subject: Add request id to returned objects Adding two classes RequestIdProxy and GeneratorProxy derived from wrapt.ObjectProxy to wrap objects returned from the API. GeneratorProxy class is used to wrap generator objects returned by cases like images.list() etc. whereas RequestIdProxy class is used to wrap non-generator object cases like images.create() etc. In all cases the returned object will have the same behavior as the wrapped(original) object. However now the returned objects will have an extra property 'request_ids' which is a list of exactly one request id. For generator cases the request_ids property will be an empty list until the underlying generator is invoked at-least once. Co-Authored-By: Abhishek Kekane Closes-Bug: #1525259 Blueprint: return-request-id-to-caller Change-Id: If8c0e0843270ff718a37ca2697afeb8da22aa3b1 --- glanceclient/tests/unit/test_utils.py | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'glanceclient/tests/unit/test_utils.py') diff --git a/glanceclient/tests/unit/test_utils.py b/glanceclient/tests/unit/test_utils.py index 272ad59..c081026 100644 --- a/glanceclient/tests/unit/test_utils.py +++ b/glanceclient/tests/unit/test_utils.py @@ -17,6 +17,7 @@ import sys import mock from oslo_utils import encodeutils +from requests import Response import six # NOTE(jokke): simplified transition to py3, behaves like py2 xrange from six.moves import range @@ -25,6 +26,15 @@ import testtools from glanceclient.common import utils +REQUEST_ID = 'req-1234' + + +def create_response_obj_with_req_id(req_id): + resp = Response() + resp.headers['x-openstack-request-id'] = req_id + return resp + + class TestUtils(testtools.TestCase): def test_make_size_human_readable(self): @@ -178,3 +188,43 @@ class TestUtils(testtools.TestCase): (name, value) = utils.safe_header(sensitive_header, None) self.assertEqual(sensitive_header, name) self.assertIsNone(value) + + def test_generator_proxy(self): + def _test_decorator(): + i = 1 + resp = create_response_obj_with_req_id(REQUEST_ID) + while True: + yield i, resp + i += 1 + + gen_obj = _test_decorator() + proxy = utils.GeneratorProxy(gen_obj) + + # Proxy object should succeed in behaving as the + # wrapped object + self.assertTrue(isinstance(proxy, type(gen_obj))) + + # Initially request_ids should be empty + self.assertEqual([], proxy.request_ids) + + # Only after we have started iterating we should + # see non-empty `request_ids` property + proxy.next() + self.assertEqual([REQUEST_ID], proxy.request_ids) + + # Even after multiple iterations `request_ids` property + # should only contain one request id + proxy.next() + proxy.next() + self.assertEqual(1, len(proxy.request_ids)) + + def test_request_id_proxy(self): + def test_data(val): + resp = create_response_obj_with_req_id(REQUEST_ID) + return val, resp + + # Object of any type except decorator can be passed to test_data + proxy = utils.RequestIdProxy(test_data(11)) + # Verify that proxy object has a property `request_ids` and it is + # a list of one request id + self.assertEqual([REQUEST_ID], proxy.request_ids) -- cgit v1.2.1