summaryrefslogtreecommitdiff
path: root/glanceclient/tests/unit/test_utils.py
diff options
context:
space:
mode:
authorRavi Jethani <ravishekar.jethani@nttdata.com>2016-04-22 13:54:49 +0000
committerAbhishek Kekane <abhishek.kekane@nttdata.com>2017-01-20 14:50:42 +0530
commit610177a779b95f931356c1e90b05a5bffd2616b3 (patch)
treed0404ab930701c9e8743244d6c00d80b482b8707 /glanceclient/tests/unit/test_utils.py
parent54e6faadf216b61d2d2f1b2ad6f4dd925f053117 (diff)
downloadpython-glanceclient-610177a779b95f931356c1e90b05a5bffd2616b3.tar.gz
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 <abhishek.kekane@nttdata.com> Closes-Bug: #1525259 Blueprint: return-request-id-to-caller Change-Id: If8c0e0843270ff718a37ca2697afeb8da22aa3b1
Diffstat (limited to 'glanceclient/tests/unit/test_utils.py')
-rw-r--r--glanceclient/tests/unit/test_utils.py50
1 files changed, 50 insertions, 0 deletions
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)