Skip to content

Commit

Permalink
Merge "Made internal client handle failed requests better"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Feb 11, 2013
2 parents 2dca894 + 07d85d3 commit 89d4a51
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
11 changes: 7 additions & 4 deletions swift/common/internal_client.py
Expand Up @@ -186,7 +186,7 @@ def _get_metadata(
"""

resp = self.make_request('HEAD', path, {}, acceptable_statuses)
if resp.status_int // 100 != 2:
if not resp.status_int // 100 == 2:
return {}
metadata_prefix = metadata_prefix.lower()
metadata = {}
Expand Down Expand Up @@ -223,7 +223,7 @@ def _iter_items(
'GET', '%s?format=json&marker=%s&end_marker=%s' %
(path, quote(marker), quote(end_marker)),
{}, acceptable_statuses)
if resp.status_int != 200:
if not resp.status_int == 200:
break
data = json.loads(resp.body)
if not data:
Expand Down Expand Up @@ -334,6 +334,8 @@ def get_account_info(

path = self.make_path(account)
resp = self.make_request('HEAD', path, {}, acceptable_statuses)
if not resp.status_int // 100 == 2:
return (0, 0)
return (int(resp.headers.get('x-account-container-count', 0)),
int(resp.headers.get('x-account-object-count', 0)))

Expand Down Expand Up @@ -406,7 +408,7 @@ def container_exists(self, account, container):

path = self.make_path(account, container)
resp = self.make_request('HEAD', path, {}, (2, HTTP_NOT_FOUND))
return resp.status_int != HTTP_NOT_FOUND
return not resp.status_int == HTTP_NOT_FOUND

def create_container(
self, account, container, headers=None, acceptable_statuses=(2,)):
Expand Down Expand Up @@ -596,8 +598,9 @@ def iter_object_lines(

headers = headers or {}
path = self.make_path(account, container, obj)

resp = self.make_request('GET', path, headers, acceptable_statuses)
if not resp.status_int // 100 == 2:
return

last_part = ''
compressed = obj.endswith('.gz')
Expand Down
43 changes: 43 additions & 0 deletions test/unit/common/test_internal_client.py
Expand Up @@ -559,6 +559,7 @@ def __init__(self, containers, objects):
'x-account-container-count': containers,
'x-account-object-count': objects,
}
self.status_int = 200

class InternalClient(internal_client.InternalClient):
def __init__(self, test, path, resp):
Expand All @@ -583,6 +584,29 @@ def make_request(
info = client.get_account_info(account)
self.assertEquals((containers, objects), info)

def test_get_account_info_404(self):
class Response(object):
def __init__(self):
self.headers = {
'x-account-container-count': 10,
'x-account-object-count': 100,
}
self.status_int = 404

class InternalClient(internal_client.InternalClient):
def __init__(self):
pass

def make_path(self, *a, **kw):
return 'some_path'

def make_request(self, *a, **kw):
return Response()

client = InternalClient()
info = client.get_account_info('some_account')
self.assertEquals((0, 0), info)

def test_get_account_metadata(self):
account, container, obj = path_parts()
path = make_path(account)
Expand Down Expand Up @@ -805,6 +829,25 @@ def fake_app(self, env, start_response):
ret_lines.append(line)
self.assertEquals(lines, ret_lines)

def test_iter_object_lines_404(self):
class InternalClient(internal_client.InternalClient):
def __init__(self):
self.app = self.fake_app
self.user_agent = 'some_agent'
self.request_tries = 3

def fake_app(self, env, start_response):
start_response('404 Not Found', [])
return ['one\ntwo\nthree']

client = InternalClient()
lines = []
for line in client.iter_object_lines(
'some_account', 'some_container', 'some_object',
acceptable_statuses=(2, 404)):
lines.append(line)
self.assertEquals([], lines)

def test_set_object_metadata(self):
account, container, obj = path_parts()
path = make_path(account, container, obj)
Expand Down

0 comments on commit 89d4a51

Please sign in to comment.