def test_headers_to_account_info_values(self): headers = {"x-account-object-count": "10", "x-account-container-count": "20"} resp = headers_to_account_info(headers.items(), 200) self.assertEquals(resp["total_object_count"], "10") self.assertEquals(resp["container_count"], "20") headers["x-unused-header"] = "blahblahblah" self.assertEquals(resp, headers_to_account_info(headers.items(), 200))
def test_headers_to_account_info_values(self): headers = { 'x-account-object-count': '10', 'x-account-container-count': '20', } resp = headers_to_account_info(headers.items(), 200) self.assertEqual(resp['total_object_count'], '10') self.assertEqual(resp['container_count'], '20') headers['x-unused-header'] = 'blahblahblah' self.assertEqual(resp, headers_to_account_info(headers.items(), 200))
def test_headers_to_account_info_values(self): headers = { 'x-account-object-count': '10', 'x-account-container-count': '20', } resp = headers_to_account_info(headers.items(), 200) self.assertEqual(resp['total_object_count'], '10') self.assertEqual(resp['container_count'], '20') headers['x-unused-header'] = 'blahblahblah' self.assertEqual( resp, headers_to_account_info(headers.items(), 200))
def test_headers_to_account_info_sys_meta(self): prefix = get_sys_meta_prefix("account") headers = {"%sWhatevs" % prefix: 14, "%ssomethingelse" % prefix: 0} resp = headers_to_account_info(headers.items(), 200) self.assertEquals(len(resp["sysmeta"]), 2) self.assertEquals(resp["sysmeta"]["whatevs"], 14) self.assertEquals(resp["sysmeta"]["somethingelse"], 0)
def test_headers_to_account_info_storage_policies(self): headers = { 'x-account-storage-policy-zero-object-count': '13', 'x-account-storage-policy-zero-container-count': '120', 'x-account-storage-policy-zero-bytes-used': '1002', 'x-account-storage-policy-one-object-count': '10', 'x-account-storage-policy-one-container-count': '20', } spc = StoragePolicyCollection( [StoragePolicy(0, 'zero', True), StoragePolicy(1, 'one', False)]) with PatchPolicies(spc): resp = headers_to_account_info(headers.items(), 200) self.assertEqual( resp['storage_policies'], { 0: { 'object_count': 13, 'container_count': 120, 'bytes': 1002 }, 1: { 'object_count': 10, 'container_count': 20, 'bytes': 0 }, })
def test_headers_to_account_info_meta(self): headers = {'X-Account-Meta-Whatevs': 14, 'x-account-meta-somethingelse': 0} resp = headers_to_account_info(headers.items(), 200) self.assertEqual(len(resp['meta']), 2) self.assertEqual(resp['meta']['whatevs'], 14) self.assertEqual(resp['meta']['somethingelse'], 0)
def test_headers_to_account_info_meta(self): headers = {'X-Account-Meta-Whatevs': 14, 'x-account-meta-somethingelse': 0} resp = headers_to_account_info(headers.items(), 200) self.assertEquals(len(resp['meta']), 2) self.assertEquals(resp['meta']['whatevs'], 14) self.assertEquals(resp['meta']['somethingelse'], 0)
def test_headers_to_account_info_sys_meta(self): prefix = get_sys_meta_prefix('account') headers = {'%sWhatevs' % prefix: 14, '%ssomethingelse' % prefix: 0} resp = headers_to_account_info(headers.items(), 200) self.assertEqual(len(resp['sysmeta']), 2) self.assertEqual(resp['sysmeta']['whatevs'], 14) self.assertEqual(resp['sysmeta']['somethingelse'], 0)
def test_account_info_in_response_env(self): controller = proxy_server.AccountController(self.app, 'AUTH_bob') with mocked_http_conn(200) as mock_conn: req = Request.blank('/v1/AUTH_bob') resp = controller.HEAD(req) self.assertEqual(2, resp.status_int // 100) self.assertEqual( ['/AUTH_bob'], # requests are like /sdX/0/.. [r['path'][6:] for r in mock_conn.requests]) info_cache = resp.environ['swift.infocache'] self.assertIn('account/AUTH_bob', info_cache) header_info = headers_to_account_info(resp.headers) self.assertEqual(header_info, info_cache['account/AUTH_bob']) # The failure doesn't lead to cache eviction errors = [500] * self.ACCOUNT_REPLICAS with mocked_http_conn(*errors) as mock_conn: req = Request.blank('/v1/AUTH_bob', { 'PATH_INFO': '/v1/AUTH_bob', 'swift.infocache': info_cache }) resp = controller.HEAD(req) self.assertEqual(5, resp.status_int // 100) self.assertEqual( ['/AUTH_bob'] * self.ACCOUNT_REPLICAS, # requests are like /sdX/0/.. [r['path'][6:] for r in mock_conn.requests]) self.assertIs(info_cache, resp.environ['swift.infocache']) # The *old* header info is all still there self.assertIn('account/AUTH_bob', info_cache) self.assertEqual(header_info, info_cache['account/AUTH_bob'])
def test_account_info_in_response_env(self): controller = proxy_server.AccountController(self.app, 'AUTH_bob') with mock.patch('swift.proxy.controllers.base.http_connect', fake_http_connect(200, body='')): req = Request.blank('/v1/AUTH_bob', {'PATH_INFO': '/v1/AUTH_bob'}) resp = controller.HEAD(req) self.assertEqual(2, resp.status_int // 100) self.assertTrue('swift.account/AUTH_bob' in resp.environ) self.assertEqual(headers_to_account_info(resp.headers), resp.environ['swift.account/AUTH_bob'])
def __init__(self, headers, env, account, container): self.headers = headers self.status_int = FakeResponse_status_int self.environ = env cache_key, env_key = _get_cache_key(account, container) if container: info = headers_to_container_info(headers, FakeResponse_status_int) else: info = headers_to_account_info(headers, FakeResponse_status_int) env[env_key] = info
def test_account_info(self): req = Request.blank( '/v1/AUTH_openio', {'PATH_INFO': '/v1/AUTH_openio'}, method='HEAD') info = get_fake_info() self.storage.account_show = Mock(return_value=info) resp = req.get_response(self.app) self.assertEqual(2, resp.status_int // 100) self.assertIn('swift.infocache', resp.environ) self.assertIn('account/AUTH_openio', resp.environ['swift.infocache']) self.assertEqual( headers_to_account_info(resp.headers, resp.status_int), resp.environ['swift.infocache']['account/AUTH_openio'])
def _mock_infocache(self, env): if not self.skip_metadata: return req = Request(env) # don't fake obj metadata account, container, _ = self._extract_path(req.path_info) req.environ.setdefault('swift.infocache', {}) req.environ['swift.infocache'][get_cache_key(account)] = \ headers_to_account_info({}, 0) if container: key = get_cache_key(account, container) req.environ['swift.infocache'][key] = \ headers_to_container_info({}, 0)
def __call__(self, env, start_response): if env['REQUEST_METHOD'] == "HEAD" and \ env['PATH_INFO'] == '/v1/a/c2/o2': env_key = get_object_env_key('a', 'c2', 'o2') env[env_key] = headers_to_object_info(self.headers, 200) start_response('200 OK', self.headers) elif env['REQUEST_METHOD'] == "HEAD" and \ env['PATH_INFO'] == '/v1/a/c2/o3': start_response('404 Not Found', []) else: # Cache the account_info (same as a real application) cache_key, env_key = _get_cache_key('a', None) env[env_key] = headers_to_account_info(self.headers, 200) start_response('200 OK', self.headers) return []
def __init__(self, headers, env, account, container, obj): self.headers = headers self.status_int = FakeResponse_status_int self.environ = env if obj: env_key = get_object_env_key(account, container, obj) else: cache_key, env_key = _get_cache_key(account, container) if account and container and obj: info = headers_to_object_info(headers, FakeResponse_status_int) elif account and container: info = headers_to_container_info(headers, FakeResponse_status_int) else: info = headers_to_account_info(headers, FakeResponse_status_int) env[env_key] = info
def test_headers_to_account_info_storage_policies(self): headers = { 'x-account-storage-policy-zero-object-count': '13', 'x-account-storage-policy-zero-container-count': '120', 'x-account-storage-policy-zero-bytes-used': '1002', 'x-account-storage-policy-one-object-count': '10', 'x-account-storage-policy-one-container-count': '20', } spc = StoragePolicyCollection([StoragePolicy(0, 'zero', True), StoragePolicy(1, 'one', False)]) with PatchPolicies(spc): resp = headers_to_account_info(headers.items(), 200) self.assertEqual(resp['storage_policies'], { 0: {'object_count': 13, 'container_count': 120, 'bytes': 1002}, 1: {'object_count': 10, 'container_count': 20, 'bytes': 0}, })
def __call__(self, env, start_response): if 'swift.authorize' in env: aresp = env['swift.authorize'](Request(env)) if aresp: return aresp(env, start_response) if env['REQUEST_METHOD'] == "HEAD" and \ env['PATH_INFO'] == '/v1/a/c2/o2': env_key = get_object_env_key('a', 'c2', 'o2') env.setdefault('swift.infocache', {})[env_key] = \ headers_to_object_info(self.headers, 200) start_response('200 OK', self.headers) elif env['REQUEST_METHOD'] == "HEAD" and \ env['PATH_INFO'] == '/v1/a/c2/o3': start_response('404 Not Found', []) else: # Cache the account_info (same as a real application) cache_key, env_key = _get_cache_key('a', None) env.setdefault('swift.infocache', {})[env_key] = \ headers_to_account_info(self.headers, 200) start_response('200 OK', self.headers) return []
def __call__(self, env, start_response): if 'swift.authorize' in env: aresp = env['swift.authorize'](Request(env)) if aresp: return aresp(env, start_response) if env['REQUEST_METHOD'] == "HEAD" and \ env['PATH_INFO'] == '/v1/a/c2/o2': cache_key = get_cache_key('a', 'c2', 'o2') env.setdefault('swift.infocache', {})[cache_key] = \ headers_to_object_info(self.headers, 200) start_response('200 OK', self.headers) elif env['REQUEST_METHOD'] == "HEAD" and \ env['PATH_INFO'] == '/v1/a/c2/o3': start_response('404 Not Found', []) else: # Cache the account_info (same as a real application) cache_key = get_cache_key('a') env.setdefault('swift.infocache', {})[cache_key] = \ headers_to_account_info(self.headers, 200) start_response('200 OK', self.headers) return []
def test_account_info_in_response_env(self): controller = proxy_server.AccountController(self.app, 'AUTH_bob') with mock.patch('swift.proxy.controllers.base.http_connect', fake_http_connect(200, body='')): req = Request.blank('/v1/AUTH_bob', {'PATH_INFO': '/v1/AUTH_bob'}) resp = controller.HEAD(req) self.assertEqual(2, resp.status_int // 100) info_cache = resp.environ['swift.infocache'] self.assertIn('account/AUTH_bob', info_cache) header_info = headers_to_account_info(resp.headers) self.assertEqual(header_info, info_cache['account/AUTH_bob']) with mock.patch('swift.proxy.controllers.base.http_connect', fake_http_connect(500, body='')): req = Request.blank('/v1/AUTH_bob', { 'PATH_INFO': '/v1/AUTH_bob', 'swift.infocache': info_cache}) resp = controller.HEAD(req) self.assertEqual(5, resp.status_int // 100) self.assertIs(info_cache, resp.environ['swift.infocache']) # The *old* header info is all still there self.assertIn('account/AUTH_bob', info_cache) self.assertEqual(header_info, info_cache['account/AUTH_bob'])
def __call__(self, env, start_response): # Cache the account_info (same as a real application) cache_key, env_key = _get_cache_key('a', None) env[env_key] = headers_to_account_info(self.headers, 200) start_response('200 OK', self.headers) return []
def test_headers_to_account_info_missing(self): resp = headers_to_account_info({}, 404) self.assertEquals(resp["status"], 404) self.assertEquals(resp["bytes"], None) self.assertEquals(resp["container_count"], None)
def test_headers_to_account_info_meta(self): headers = {"X-Account-Meta-Whatevs": 14, "x-account-meta-somethingelse": 0} resp = headers_to_account_info(headers.items(), 200) self.assertEquals(len(resp["meta"]), 2) self.assertEquals(resp["meta"]["whatevs"], 14) self.assertEquals(resp["meta"]["somethingelse"], 0)
def test_headers_to_account_info_missing(self): resp = headers_to_account_info({}, 404) self.assertEqual(resp['status'], 404) self.assertIsNone(resp['bytes']) self.assertIsNone(resp['container_count'])
def test_headers_to_account_info_missing(self): resp = headers_to_account_info({}, 404) self.assertEqual(resp['status'], 404) self.assertEqual(resp['bytes'], None) self.assertEqual(resp['container_count'], None)