Merge "Made internal client handle failed requests better"

This commit is contained in:
Jenkins 2013-02-11 19:47:02 +00:00 committed by Gerrit Code Review
commit 89d4a51359
2 changed files with 50 additions and 4 deletions

View File

@ -186,7 +186,7 @@ class InternalClient(object):
"""
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 = {}
@ -223,7 +223,7 @@ class InternalClient(object):
'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:
@ -334,6 +334,8 @@ class InternalClient(object):
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)))
@ -406,7 +408,7 @@ class InternalClient(object):
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,)):
@ -596,8 +598,9 @@ class InternalClient(object):
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')

View File

@ -559,6 +559,7 @@ class TestInternalClient(unittest.TestCase):
'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):
@ -583,6 +584,29 @@ class TestInternalClient(unittest.TestCase):
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)
@ -805,6 +829,25 @@ class TestInternalClient(unittest.TestCase):
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)