From 2f24fb9683a57b67348d65864d5af8c3a03dee67 Mon Sep 17 00:00:00 2001 From: Alistair Coles Date: Wed, 23 Mar 2016 20:49:50 +0000 Subject: [PATCH] Check marker params in SimpleClient full listing requests Follow up for change [1] to add some assertions to check that marker param is included in sequential GET requests sent during a full listing. Extract multiple FakeConn class definitions to single class at module level and share between all classes. Also, explicitly unpack the return values from base request calls made in the full listing section of base_request, and explicitly return a list to make more consistent with rest of the method. [1] Change-Id: I6892390d72f70f1bc519b482d4f72603e1570163 Change-Id: Iad038709f46364b8324d25ac79be4317add79df5 --- swift/common/internal_client.py | 18 ++++----- test/unit/common/test_internal_client.py | 50 +++++++++++------------- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/swift/common/internal_client.py b/swift/common/internal_client.py index ecffa999e4..b9c99a20f3 100644 --- a/swift/common/internal_client.py +++ b/swift/common/internal_client.py @@ -747,18 +747,18 @@ class SimpleClient(object): url = self.url if full_listing: - body_data = self.base_request(method, container, name, prefix, - headers, proxy, timeout=timeout, - marker=marker) - listing = body_data[1] + info, body_data = self.base_request( + method, container, name, prefix, headers, proxy, + timeout=timeout, marker=marker) + listing = body_data while listing: marker = listing[-1]['name'] - listing = self.base_request(method, container, name, prefix, - headers, proxy, timeout=timeout, - marker=marker)[1] + info, listing = self.base_request( + method, container, name, prefix, headers, proxy, + timeout=timeout, marker=marker) if listing: - body_data[1].extend(listing) - return body_data + body_data.extend(listing) + return [info, body_data] if headers is None: headers = {} diff --git a/test/unit/common/test_internal_client.py b/test/unit/common/test_internal_client.py index e4218a9e02..2d4c747179 100644 --- a/test/unit/common/test_internal_client.py +++ b/test/unit/common/test_internal_client.py @@ -34,6 +34,19 @@ from test.unit import with_tempdir, write_fake_ring, patch_policies from test.unit.common.middleware.helpers import FakeSwift +class FakeConn(object): + def __init__(self, body=None): + if body is None: + body = [] + self.body = body + + def read(self): + return json.dumps(self.body) + + def info(self): + return {} + + def not_sleep(seconds): pass @@ -339,17 +352,10 @@ class TestInternalClient(unittest.TestCase): # verify that base_request passes timeout arg on to urlopen body = {"some": "content"} - class FakeConn(object): - def read(self): - return json.dumps(body) - - def info(self): - return {} - for timeout in (0.0, 42.0, None): mocked_func = 'swift.common.internal_client.urllib2.urlopen' with mock.patch(mocked_func) as mock_urlopen: - mock_urlopen.side_effect = [FakeConn()] + mock_urlopen.side_effect = [FakeConn(body)] sc = internal_client.SimpleClient('http://0.0.0.0/') _, resp_body = sc.base_request('GET', timeout=timeout) mock_urlopen.assert_called_once_with(mock.ANY, timeout=timeout) @@ -361,23 +367,21 @@ class TestInternalClient(unittest.TestCase): body2 = [{'name': 'd'}] body3 = [] - class FakeConn(object): - def __init__(self, body): - self.body = body - - def read(self): - return json.dumps(self.body) - - def info(self): - return {} - mocked_func = 'swift.common.internal_client.urllib2.urlopen' with mock.patch(mocked_func) as mock_urlopen: mock_urlopen.side_effect = [ FakeConn(body1), FakeConn(body2), FakeConn(body3)] sc = internal_client.SimpleClient('http://0.0.0.0/') _, resp_body = sc.base_request('GET', full_listing=True) - self.assertEqual(body1 + body2, resp_body) + self.assertEqual(body1 + body2, resp_body) + self.assertEqual(3, mock_urlopen.call_count) + actual_requests = map( + lambda call: call[0][0], mock_urlopen.call_args_list) + self.assertEqual('/?format=json', actual_requests[0].get_selector()) + self.assertEqual( + '/?format=json&marker=c', actual_requests[1].get_selector()) + self.assertEqual( + '/?format=json&marker=d', actual_requests[2].get_selector()) def test_make_request_method_path_headers(self): class InternalClient(internal_client.InternalClient): @@ -1415,14 +1419,6 @@ class TestSimpleClient(unittest.TestCase): proxy = '%s://%s' % (scheme, proxy_host) url = 'https://127.0.0.1:1/a' - class FakeConn(object): - - def read(self): - return 'irrelevant' - - def info(self): - return {} - mocked = 'swift.common.internal_client.urllib2.urlopen' # module level methods