diff --git a/swift/proxy/controllers/base.py b/swift/proxy/controllers/base.py index b3af8e99a2..40a636e12b 100644 --- a/swift/proxy/controllers/base.py +++ b/swift/proxy/controllers/base.py @@ -387,7 +387,10 @@ class Controller(object): container_count = 0 else: result_code = cache_value['status'] - container_count = cache_value['container_count'] + try: + container_count = int(cache_value['container_count']) + except ValueError: + container_count = 0 if result_code == HTTP_OK: return partition, nodes, container_count elif result_code == HTTP_NOT_FOUND and not autocreate: @@ -456,7 +459,11 @@ class Controller(object): account_info, time=cache_timeout) if result_code == HTTP_OK: - return partition, nodes, account_info['container_count'] + try: + container_count = int(account_info['container_count']) + except ValueError: + container_count = 0 + return partition, nodes, container_count return None, None, None def container_info(self, account, container, account_autocreate=False): diff --git a/test/unit/proxy/test_server.py b/test/unit/proxy/test_server.py index 96cd3bda15..1ee6415425 100644 --- a/test/unit/proxy/test_server.py +++ b/test/unit/proxy/test_server.py @@ -247,7 +247,7 @@ def fake_http_connect(*code_iter, **kwargs): 'x-object-meta-test': 'testing', 'etag': etag, 'x-works': 'yes', - 'x-account-container-count': 12345} + 'x-account-container-count': kwargs.get('count', 12345)} if not self.timestamp: del headers['x-timestamp'] try: @@ -445,6 +445,32 @@ class TestController(unittest.TestCase): self.assertEqual(p, partition) self.assertEqual(n, nodes) + def test_account_info_container_count(self): + with save_globals(): + set_http_connect(200, count=123) + partition, nodes, count = \ + self.controller.account_info(self.account) + self.assertEquals(count, 123) + with save_globals(): + set_http_connect(200, count='123') + partition, nodes, count = \ + self.controller.account_info(self.account) + self.assertEquals(count, 123) + with save_globals(): + cache_key = get_account_memcache_key(self.account) + account_info = {'status': 200, 'container_count': 1234} + self.memcache.set(cache_key, account_info) + partition, nodes, count = \ + self.controller.account_info(self.account) + self.assertEquals(count, 1234) + with save_globals(): + cache_key = get_account_memcache_key(self.account) + account_info = {'status': 200, 'container_count': '1234'} + self.memcache.set(cache_key, account_info) + partition, nodes, count = \ + self.controller.account_info(self.account) + self.assertEquals(count, 1234) + def test_make_requests(self): with save_globals(): set_http_connect(200)