Skip to content

Commit

Permalink
Check marker params in SimpleClient full listing requests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Alistair Coles committed Mar 23, 2016
1 parent a696c1e commit 2f24fb9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
18 changes: 9 additions & 9 deletions swift/common/internal_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,18 +747,18 @@ def base_request(self, method, container=None, name=None, prefix=None,
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 = {}
Expand Down
50 changes: 23 additions & 27 deletions test/unit/common/test_internal_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@
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

Expand Down Expand Up @@ -339,17 +352,10 @@ def test_base_request_timeout(self):
# 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)
Expand All @@ -361,23 +367,21 @@ def test_base_full_listing(self):
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):
Expand Down Expand Up @@ -1415,14 +1419,6 @@ def test_proxy(self):
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
Expand Down

0 comments on commit 2f24fb9

Please sign in to comment.