diff --git a/swift/common/wsgi.py b/swift/common/wsgi.py index 986e86d640..fdd4a203ed 100644 --- a/swift/common/wsgi.py +++ b/swift/common/wsgi.py @@ -1099,7 +1099,8 @@ def make_env(env, method=None, path=None, agent='Swift', query_string=None, 'SERVER_PROTOCOL', 'swift.cache', 'swift.source', 'swift.trans_id', 'swift.authorize_override', 'swift.authorize', 'HTTP_X_USER_ID', 'HTTP_X_PROJECT_ID', - 'HTTP_REFERER', 'swift.orig_req_method', 'swift.log_info'): + 'HTTP_REFERER', 'swift.orig_req_method', 'swift.log_info', + 'swift.infocache'): if name in env: newenv[name] = env[name] if method: diff --git a/swift/proxy/controllers/base.py b/swift/proxy/controllers/base.py index 7dcc1ca3de..0cd209db65 100644 --- a/swift/proxy/controllers/base.py +++ b/swift/proxy/controllers/base.py @@ -381,6 +381,7 @@ def _set_info_cache(app, env, account, container, resp): :param container: the unquoted container name or None :param resp: the response received or None if info cache should be cleared """ + infocache = env.setdefault('swift.infocache', {}) if container: cache_time = app.recheck_container_existence @@ -399,7 +400,7 @@ def _set_info_cache(app, env, account, container, resp): # Next actually set both memcache and the env cache memcache = getattr(app, 'memcache', None) or env.get('swift.cache') if not cache_time: - env.pop(env_key, None) + infocache.pop(env_key, None) if memcache: memcache.delete(cache_key) return @@ -410,7 +411,7 @@ def _set_info_cache(app, env, account, container, resp): info = headers_to_account_info(resp.headers, resp.status_int) if memcache: memcache.set(cache_key, info, time=cache_time) - env[env_key] = info + infocache[env_key] = info def _set_object_info_cache(app, env, account, container, obj, resp): @@ -433,12 +434,12 @@ def _set_object_info_cache(app, env, account, container, obj, resp): env_key = get_object_env_key(account, container, obj) - if not resp: - env.pop(env_key, None) + if 'swift.infocache' in env and not resp: + env['swift.infocache'].pop(env_key, None) return info = headers_to_object_info(resp.headers, resp.status_int) - env[env_key] = info + env.setdefault('swift.infocache', {})[env_key] = info def clear_info_cache(app, env, account, container=None): @@ -464,8 +465,8 @@ def _get_info_cache(app, env, account, container=None): """ cache_key, env_key = _get_cache_key(account, container) - if env_key in env: - return env[env_key] + if 'swift.infocache' in env and env_key in env['swift.infocache']: + return env['swift.infocache'][env_key] memcache = getattr(app, 'memcache', None) or env.get('swift.cache') if memcache: info = memcache.get(cache_key) @@ -473,11 +474,11 @@ def _get_info_cache(app, env, account, container=None): for key in info: if isinstance(info[key], six.text_type): info[key] = info[key].encode("utf-8") - if isinstance(info[key], dict): + elif isinstance(info[key], dict): for subkey, value in info[key].items(): if isinstance(value, six.text_type): info[key][subkey] = value.encode("utf-8") - env[env_key] = info + env.setdefault('swift.infocache', {})[env_key] = info return info return None @@ -497,6 +498,7 @@ def _prepare_pre_auth_info_request(env, path, swift_source): # This is a sub request for container metadata- drop the Origin header from # the request so the it is not treated as a CORS request. newenv.pop('HTTP_ORIGIN', None) + # Note that Request.blank expects quoted path return Request.blank(quote(path), environ=newenv) @@ -513,6 +515,10 @@ def get_info(app, env, account, container=None, ret_not_found=False, :param env: the environment used by the current request :param account: The unquoted name of the account :param container: The unquoted name of the container (or None if account) + :param ret_not_found: if True, return info dictionary on 404; + if False, return None on 404 + :param swift_source: swift source logged for any subrequests made while + retrieving the account or container info :returns: the cached info or None if cannot be retrieved """ info = _get_info_cache(app, env, account, container) @@ -531,14 +537,15 @@ def get_info(app, env, account, container=None, ret_not_found=False, req = _prepare_pre_auth_info_request( env, path, (swift_source or 'GET_INFO')) - # Whenever we do a GET/HEAD, the GETorHEAD_base will set the info in - # the environment under environ[env_key] and in memcache. We will - # pick the one from environ[env_key] and use it to set the caller env + # Whenever we do a GET/HEAD, the GETorHEAD_base will set the info in the + # environment under environ['swift.infocache'][env_key] and in memcache. + # We will pick the one from environ['swift.infocache'][env_key] and use + # it to set the caller env resp = req.get_response(app) cache_key, env_key = _get_cache_key(account, container) try: - info = resp.environ[env_key] - env[env_key] = info + info = resp.environ['swift.infocache'][env_key] + env.setdefault('swift.infocache', {})[env_key] = info if ret_not_found or is_success(info['status']): return info except (KeyError, AttributeError): @@ -561,7 +568,7 @@ def _get_object_info(app, env, account, container, obj, swift_source=None): :returns: the cached info or None if cannot be retrieved """ env_key = get_object_env_key(account, container, obj) - info = env.get(env_key) + info = env.get('swift.infocache', {}).get(env_key) if info: return info # Not in cached, let's try the object servers @@ -572,8 +579,8 @@ def _get_object_info(app, env, account, container, obj, swift_source=None): # pick the one from environ[env_key] and use it to set the caller env resp = req.get_response(app) try: - info = resp.environ[env_key] - env[env_key] = info + info = resp.environ['swift.infocache'][env_key] + env.setdefault('swift.infocache', {})[env_key] = info return info except (KeyError, AttributeError): pass diff --git a/test/unit/common/middleware/test_account_quotas.py b/test/unit/common/middleware/test_account_quotas.py index b443b4a28d..87574bd14f 100644 --- a/test/unit/common/middleware/test_account_quotas.py +++ b/test/unit/common/middleware/test_account_quotas.py @@ -59,7 +59,8 @@ class FakeApp(object): 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) + 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': @@ -67,7 +68,8 @@ class FakeApp(object): 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) + env.setdefault('swift.infocache', {})[env_key] = \ + headers_to_account_info(self.headers, 200) start_response('200 OK', self.headers) return [] diff --git a/test/unit/common/middleware/test_container_sync.py b/test/unit/common/middleware/test_container_sync.py index 61a4735f15..9d5f1dd332 100644 --- a/test/unit/common/middleware/test_container_sync.py +++ b/test/unit/common/middleware/test_container_sync.py @@ -205,7 +205,8 @@ cluster_dfw1 = http://dfw1.host/v1/ def test_invalid_sig(self): req = swob.Request.blank( '/v1/a/c', headers={'x-container-sync-auth': 'US nonce sig'}) - req.environ[_get_cache_key('a', 'c')[1]] = {'sync_key': 'abc'} + infocache = req.environ.setdefault('swift.infocache', {}) + infocache[_get_cache_key('a', 'c')[1]] = {'sync_key': 'abc'} resp = req.get_response(self.sync) self.assertEqual(resp.status, '401 Unauthorized') self.assertEqual( @@ -224,7 +225,8 @@ cluster_dfw1 = http://dfw1.host/v1/ req = swob.Request.blank('/v1/a/c', headers={ 'x-container-sync-auth': 'US nonce ' + sig, 'x-backend-inbound-x-timestamp': ts}) - req.environ[_get_cache_key('a', 'c')[1]] = {'sync_key': 'abc'} + infocache = req.environ.setdefault('swift.infocache', {}) + infocache[_get_cache_key('a', 'c')[1]] = {'sync_key': 'abc'} resp = req.get_response(self.sync) self.assertEqual(resp.status, '200 OK') self.assertEqual(resp.body, 'Response to Authorized Request') @@ -238,7 +240,8 @@ cluster_dfw1 = http://dfw1.host/v1/ self.sync.realms_conf.key2('US'), 'abc') req = swob.Request.blank( '/v1/a/c', headers={'x-container-sync-auth': 'US nonce ' + sig}) - req.environ[_get_cache_key('a', 'c')[1]] = {'sync_key': 'abc'} + infocache = req.environ.setdefault('swift.infocache', {}) + infocache[_get_cache_key('a', 'c')[1]] = {'sync_key': 'abc'} resp = req.get_response(self.sync) self.assertEqual(resp.status, '200 OK') self.assertEqual(resp.body, 'Response to Authorized Request') diff --git a/test/unit/common/middleware/test_formpost.py b/test/unit/common/middleware/test_formpost.py index 4e6f24826f..6e9da72857 100644 --- a/test/unit/common/middleware/test_formpost.py +++ b/test/unit/common/middleware/test_formpost.py @@ -130,8 +130,9 @@ class TestFormPost(unittest.TestCase): meta[meta_name] = key _junk, account, _junk, _junk = split_path(path, 2, 4) - req.environ['swift.account/' + account] = self._fake_cache_env( - account, tempurl_keys) + req.environ.setdefault('swift.infocache', {}) + req.environ['swift.infocache']['swift.account/' + account] = \ + self._fake_cache_env(account, tempurl_keys) return req def _fake_cache_env(self, account, tempurl_keys=()): @@ -221,6 +222,7 @@ class TestFormPost(unittest.TestCase): 'SERVER_NAME': '172.16.83.128', 'SERVER_PORT': '8080', 'SERVER_PROTOCOL': 'HTTP/1.0', + 'swift.infocache': {}, 'wsgi.errors': wsgi_errors, 'wsgi.multiprocess': False, 'wsgi.multithread': True, @@ -247,8 +249,8 @@ class TestFormPost(unittest.TestCase): sig, env, body = self._make_sig_env_body( '/v1/AUTH_test/container', '', 1024, 10, int(time() - 10), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -351,9 +353,10 @@ class TestFormPost(unittest.TestCase): 'SERVER_NAME': '172.16.83.128', 'SERVER_PORT': '8080', 'SERVER_PROTOCOL': 'HTTP/1.0', - 'swift.account/AUTH_test': self._fake_cache_env( - 'AUTH_test', [key]), - 'swift.container/AUTH_test/container': {'meta': {}}, + 'swift.infocache': { + 'swift.account/AUTH_test': self._fake_cache_env( + 'AUTH_test', [key]), + 'swift.container/AUTH_test/container': {'meta': {}}}, 'wsgi.errors': wsgi_errors, 'wsgi.input': wsgi_input, 'wsgi.multiprocess': False, @@ -467,9 +470,10 @@ class TestFormPost(unittest.TestCase): 'SERVER_NAME': '172.16.83.128', 'SERVER_PORT': '8080', 'SERVER_PROTOCOL': 'HTTP/1.0', - 'swift.account/AUTH_test': self._fake_cache_env( - 'AUTH_test', [key]), - 'swift.container/AUTH_test/container': {'meta': {}}, + 'swift.infocache': { + 'swift.account/AUTH_test': self._fake_cache_env( + 'AUTH_test', [key]), + 'swift.container/AUTH_test/container': {'meta': {}}}, 'wsgi.errors': wsgi_errors, 'wsgi.input': wsgi_input, 'wsgi.multiprocess': False, @@ -586,9 +590,10 @@ class TestFormPost(unittest.TestCase): 'SERVER_NAME': '172.16.83.128', 'SERVER_PORT': '8080', 'SERVER_PROTOCOL': 'HTTP/1.0', - 'swift.account/AUTH_test': self._fake_cache_env( - 'AUTH_test', [key]), - 'swift.container/AUTH_test/container': {'meta': {}}, + 'swift.infocache': { + 'swift.account/AUTH_test': self._fake_cache_env( + 'AUTH_test', [key]), + 'swift.container/AUTH_test/container': {'meta': {}}}, 'wsgi.errors': wsgi_errors, 'wsgi.input': wsgi_input, 'wsgi.multiprocess': False, @@ -701,9 +706,10 @@ class TestFormPost(unittest.TestCase): 'SERVER_NAME': '172.16.83.128', 'SERVER_PORT': '8080', 'SERVER_PROTOCOL': 'HTTP/1.0', - 'swift.account/AUTH_test': self._fake_cache_env( - 'AUTH_test', [key]), - 'swift.container/AUTH_test/container': {'meta': {}}, + 'swift.infocache': { + 'swift.account/AUTH_test': self._fake_cache_env( + 'AUTH_test', [key]), + 'swift.container/AUTH_test/container': {'meta': {}}}, 'wsgi.errors': wsgi_errors, 'wsgi.input': wsgi_input, 'wsgi.multiprocess': False, @@ -747,9 +753,10 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', 'http://brim.net', 5, 10, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'XX' + b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': {}} self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -783,9 +790,10 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', 'http://brim.net', 5, 10, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': {}} self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -814,9 +822,10 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', 'http://brim.net', 1024, 1, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': {}} self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -855,9 +864,10 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', '', 1024, 10, int(time() + 86400), key) env['QUERY_STRING'] = 'this=should¬=get&passed' env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': {}} self.app = FakeApp( iter([('201 Created', {}, ''), ('201 Created', {}, '')]), @@ -890,9 +900,10 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', 'http://brim.net', 1024, 10, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': {}} self.app = FakeApp(iter([('404 Not Found', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -976,9 +987,10 @@ class TestFormPost(unittest.TestCase): if six.PY3: wsgi_input = wsgi_input.encode('utf-8') env['wsgi.input'] = BytesIO(wsgi_input) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': {}} self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1047,9 +1059,10 @@ class TestFormPost(unittest.TestCase): if six.PY3: wsgi_input = wsgi_input.encode('utf-8') env['wsgi.input'] = BytesIO(wsgi_input) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': {}} self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1087,9 +1100,10 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', 'http://redirect', 1024, 10, int(time() + 86400), key, user_agent=False) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': {}} self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1108,9 +1122,10 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', 'http://redirect', 1024, 10, int(time() + 86400), key, user_agent=False) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': {}} env['HTTP_ORIGIN'] = 'http://localhost:5000' self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', @@ -1137,9 +1152,10 @@ class TestFormPost(unittest.TestCase): int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) # Stick it in X-Account-Meta-Temp-URL-Key-2 and make sure we get it - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', ['bert', key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', ['bert', key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = \ + {'meta': {}} self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1173,9 +1189,11 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', 'http://redirect', 1024, 10, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env('AUTH_test') + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test')) # Stick it in X-Container-Meta-Temp-URL-Key-2 and ensure we get it - env['swift.container/AUTH_test/container'] = {'meta': meta} + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': meta} self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1199,9 +1217,10 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', 'http://redirect', 1024, 10, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': {}} self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1237,9 +1256,10 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', 'http://redirect?one=two', 1024, 10, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': {}} self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1275,9 +1295,10 @@ class TestFormPost(unittest.TestCase): sig, env, body = self._make_sig_env_body( '/v1/AUTH_test/container', '', 1024, 10, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': {}} self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1312,8 +1333,8 @@ class TestFormPost(unittest.TestCase): sig, env, body = self._make_sig_env_body( '/v1/AUTH_test/container', '', 1024, 10, int(time() - 10), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1346,8 +1367,8 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', '', 1024, 10, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) # Change key to invalidate sig - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key + ' is bogus now']) + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key + ' is bogus now'])) self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1379,8 +1400,8 @@ class TestFormPost(unittest.TestCase): sig, env, body = self._make_sig_env_body( '/v1/AUTH_test/container', '', 1024, 10, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'XX' + b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1412,8 +1433,8 @@ class TestFormPost(unittest.TestCase): sig, env, body = self._make_sig_env_body( '/v2/AUTH_test/container', '', 1024, 10, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1445,8 +1466,8 @@ class TestFormPost(unittest.TestCase): sig, env, body = self._make_sig_env_body( '//AUTH_test/container', '', 1024, 10, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1478,8 +1499,8 @@ class TestFormPost(unittest.TestCase): sig, env, body = self._make_sig_env_body( '/v1//container', '', 1024, 10, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1511,8 +1532,8 @@ class TestFormPost(unittest.TestCase): sig, env, body = self._make_sig_env_body( '/v1/AUTH_tst/container', '', 1024, 10, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) self.app = FakeApp(iter([ ('200 Ok', {'x-account-meta-temp-url-key': 'def'}, ''), ('201 Created', {}, ''), @@ -1546,8 +1567,8 @@ class TestFormPost(unittest.TestCase): sig, env, body = self._make_sig_env_body( '/v1/AUTH_test', '', 1024, 10, int(time() + 86400), key) env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1584,8 +1605,8 @@ class TestFormPost(unittest.TestCase): body[i] = 'badvalue' break env['wsgi.input'] = BytesIO(b'\r\n'.join(body)) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1625,9 +1646,10 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', '', 1024, 10, int(time() + 86400), key) wsgi_input = b'\r\n'.join(x_delete_body_part + body) env['wsgi.input'] = BytesIO(wsgi_input) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': {}} self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1668,8 +1690,8 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', '', 1024, 10, int(time() + 86400), key) wsgi_input = b'\r\n'.join(x_delete_body_part + body) env['wsgi.input'] = BytesIO(wsgi_input) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1703,9 +1725,10 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', '', 1024, 10, int(time() + 86400), key) wsgi_input = b'\r\n'.join(x_delete_body_part + body) env['wsgi.input'] = BytesIO(wsgi_input) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) - env['swift.container/AUTH_test/container'] = {'meta': {}} + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) + env['swift.infocache']['swift.container/AUTH_test/container'] = { + 'meta': {}} self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) @@ -1746,8 +1769,8 @@ class TestFormPost(unittest.TestCase): '/v1/AUTH_test/container', '', 1024, 10, int(time() + 86400), key) wsgi_input = b'\r\n'.join(x_delete_body_part + body) env['wsgi.input'] = BytesIO(wsgi_input) - env['swift.account/AUTH_test'] = self._fake_cache_env( - 'AUTH_test', [key]) + env['swift.infocache']['swift.account/AUTH_test'] = ( + self._fake_cache_env('AUTH_test', [key])) self.app = FakeApp(iter([('201 Created', {}, ''), ('201 Created', {}, '')])) self.auth = tempauth.filter_factory({})(self.app) diff --git a/test/unit/common/middleware/test_keystoneauth.py b/test/unit/common/middleware/test_keystoneauth.py index 08aa86b3de..4b82c88dc4 100644 --- a/test/unit/common/middleware/test_keystoneauth.py +++ b/test/unit/common/middleware/test_keystoneauth.py @@ -252,7 +252,7 @@ class SwiftAuth(unittest.TestCase): path = '/v1/' + account # fake cached account info _, info_key = _get_cache_key(account, None) - env = {info_key: {'status': 0, 'sysmeta': {}}, + env = {'swift.infocache': {info_key: {'status': 0, 'sysmeta': {}}}, 'keystone.token_info': _fake_token_info(version='3')} req = Request.blank(path, environ=env, headers=headers) req.method = 'POST' @@ -281,7 +281,7 @@ class SwiftAuth(unittest.TestCase): path = '/v1/' + account # fake cached account info _, info_key = _get_cache_key(account, None) - env = {info_key: {'status': 0, 'sysmeta': {}}, + env = {'swift.infocache': {info_key: {'status': 0, 'sysmeta': {}}}, 'keystone.token_info': _fake_token_info(version='3')} req = Request.blank(path, environ=env, headers=headers) req.method = 'POST' @@ -303,7 +303,7 @@ class SwiftAuth(unittest.TestCase): path = '/v1/' + account _, info_key = _get_cache_key(account, None) # v2 token - env = {info_key: {'status': 0, 'sysmeta': {}}, + env = {'swift.infocache': {info_key: {'status': 0, 'sysmeta': {}}}, 'keystone.token_info': _fake_token_info(version='2')} req = Request.blank(path, environ=env, headers=headers) req.method = 'POST' @@ -325,7 +325,7 @@ class SwiftAuth(unittest.TestCase): path = '/v1/' + account _, info_key = _get_cache_key(account, None) # v2 token - env = {info_key: {'status': 0, 'sysmeta': {}}, + env = {'swift.infocache': {info_key: {'status': 0, 'sysmeta': {}}}, 'keystone.token_info': _fake_token_info(version='2')} req = Request.blank(path, environ=env, headers=headers) req.method = 'POST' @@ -382,7 +382,7 @@ class ServiceTokenFunctionality(unittest.TestCase): service_role=service_role) (version, account, _junk, _junk) = split_path(path, 2, 4, True) _, info_key = _get_cache_key(account, None) - env = {info_key: {'status': 0, 'sysmeta': {}}, + env = {'swift.infocache': {info_key: {'status': 0, 'sysmeta': {}}}, 'keystone.token_info': _fake_token_info(version='2')} if environ: env.update(environ) @@ -596,8 +596,9 @@ class TestAuthorize(BaseTestAuthorize): path = '/v1/%s/c' % account # fake cached account info _, info_key = _get_cache_key(account, None) - default_env = {'REMOTE_USER': identity['HTTP_X_TENANT_ID'], - info_key: {'status': 200, 'sysmeta': {}}} + default_env = { + 'REMOTE_USER': identity['HTTP_X_TENANT_ID'], + 'swift.infocache': {info_key: {'status': 200, 'sysmeta': {}}}} default_env.update(identity) if env: default_env.update(env) @@ -986,7 +987,7 @@ class TestAuthorize(BaseTestAuthorize): info = {'sysmeta': sysmeta} _, info_key = _get_cache_key('AUTH_1234', None) env = {'PATH_INFO': '/v1/AUTH_1234', - info_key: info} + 'swift.infocache': {info_key: info}} # account does not exist info['status'] = 404 @@ -1029,7 +1030,8 @@ class TestIsNameAllowedInACL(BaseTestAuthorize): # pretend account exists info = {'status': 200, 'sysmeta': sysmeta} _, info_key = _get_cache_key(account, None) - req = Request.blank(path, environ={info_key: info}) + req = Request.blank(path, + environ={'swift.infocache': {info_key: info}}) if scoped == 'account': project_name = 'account_name' @@ -1215,7 +1217,7 @@ class TestSetProjectDomain(BaseTestAuthorize): sysmeta['project-domain-id'] = sysmeta_project_domain_id info = {'status': status, 'sysmeta': sysmeta} _, info_key = _get_cache_key(account, None) - env = {info_key: info} + env = {'swift.infocache': {info_key: info}} # create fake env identity env_id = self._get_env_id(tenant_id=req_project_id, diff --git a/test/unit/common/middleware/test_quotas.py b/test/unit/common/middleware/test_quotas.py index f99b8df663..f4eba5b76c 100644 --- a/test/unit/common/middleware/test_quotas.py +++ b/test/unit/common/middleware/test_quotas.py @@ -245,8 +245,9 @@ class ContainerQuotaCopyingTestCases(unittest.TestCase): 'status': 200, 'object_count': 1} req = Request.blank('/v1/a/c2/o2', environ={'REQUEST_METHOD': 'COPY', - 'swift.container/a/c': a_c_cache, - 'swift.container/a2/c': a2_c_cache}, + 'swift.infocache': { + 'swift.container/a/c': a_c_cache, + 'swift.container/a2/c': a2_c_cache}}, headers={'Destination': '/c/o', 'Destination-Account': 'a2'}) res = req.get_response(self.copy_filter) @@ -261,8 +262,9 @@ class ContainerQuotaCopyingTestCases(unittest.TestCase): 'status': 200, 'object_count': 1} req = Request.blank('/v1/a2/c/o', environ={'REQUEST_METHOD': 'PUT', - 'swift.container/a/c': a_c_cache, - 'swift.container/a2/c': a2_c_cache}, + 'swift.infocache': { + 'swift.container/a/c': a_c_cache, + 'swift.container/a2/c': a2_c_cache}}, headers={'X-Copy-From': '/c2/o2', 'X-Copy-From-Account': 'a'}) res = req.get_response(self.copy_filter) diff --git a/test/unit/common/middleware/test_tempurl.py b/test/unit/common/middleware/test_tempurl.py index 0fc895f9e2..fed3cbd17d 100644 --- a/test/unit/common/middleware/test_tempurl.py +++ b/test/unit/common/middleware/test_tempurl.py @@ -96,7 +96,8 @@ class TestTempURL(unittest.TestCase): if key: meta[meta_name] = key - environ['swift.account/' + account] = { + ic = environ.setdefault('swift.infocache', {}) + ic['swift.account/' + account] = { 'status': 204, 'container_count': '0', 'total_object_count': '0', @@ -109,7 +110,7 @@ class TestTempURL(unittest.TestCase): meta[meta_name] = key container_cache_key = 'swift.container/' + account + '/c' - environ.setdefault(container_cache_key, {'meta': meta}) + ic.setdefault(container_cache_key, {'meta': meta}) def test_passthrough(self): resp = self._make_request('/v1/a/c/o').get_response(self.tempurl) @@ -159,7 +160,8 @@ class TestTempURL(unittest.TestCase): self.assert_valid_sig(expires, path, [key1, key2], sig) def test_get_valid_container_keys(self): - environ = {} + ic = {} + environ = {'swift.infocache': ic} # Add two static container keys container_keys = ['me', 'other'] meta = {} @@ -167,7 +169,7 @@ class TestTempURL(unittest.TestCase): meta_name = 'Temp-URL-key' + (("-%d" % (idx + 1) if idx else "")) if key: meta[meta_name] = key - environ['swift.container/a/c'] = {'meta': meta} + ic['swift.container/a/c'] = {'meta': meta} method = 'GET' expires = int(time() + 86400) diff --git a/test/unit/common/test_wsgi.py b/test/unit/common/test_wsgi.py index 9c16daf805..cc33833714 100644 --- a/test/unit/common/test_wsgi.py +++ b/test/unit/common/test_wsgi.py @@ -839,6 +839,11 @@ class TestWSGI(unittest.TestCase): self.assertTrue('HTTP_REFERER' in newenv) self.assertEqual(newenv['HTTP_REFERER'], 'http://blah.example.com') + def test_make_env_keeps_infocache(self): + oldenv = {'swift.infocache': {}} + newenv = wsgi.make_env(oldenv) + self.assertIs(newenv.get('swift.infocache'), oldenv['swift.infocache']) + class TestServersPerPortStrategy(unittest.TestCase): def setUp(self): diff --git a/test/unit/proxy/controllers/test_account.py b/test/unit/proxy/controllers/test_account.py index d3dd9cf504..d60a017fa5 100644 --- a/test/unit/proxy/controllers/test_account.py +++ b/test/unit/proxy/controllers/test_account.py @@ -68,9 +68,11 @@ class TestAccountController(unittest.TestCase): 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']) + self.assertTrue( + 'swift.account/AUTH_bob' in resp.environ['swift.infocache']) + self.assertEqual( + headers_to_account_info(resp.headers), + resp.environ['swift.infocache']['swift.account/AUTH_bob']) def test_swift_owner(self): owner_headers = { diff --git a/test/unit/proxy/controllers/test_base.py b/test/unit/proxy/controllers/test_base.py index e85ee7ba50..1ad7624386 100644 --- a/test/unit/proxy/controllers/test_base.py +++ b/test/unit/proxy/controllers/test_base.py @@ -120,10 +120,10 @@ class FakeApp(object): def __init__(self, response_factory=None, statuses=None): self.responses = response_factory or \ DynamicResponseFactory(*statuses or []) - self.sources = [] + self.captured_envs = [] def __call__(self, environ, start_response): - self.sources.append(environ.get('swift.source')) + self.captured_envs.append(environ) response = self.responses.get_response(environ) reason = RESPONSE_REASONS[response.status_int][0] start_response('%d %s' % (response.status_int, reason), @@ -167,31 +167,37 @@ class TestFuncs(unittest.TestCase): 'http_connect', fake_http_connect(200)): resp = base.GETorHEAD_base(req, 'object', iter(nodes), 'part', '/a/c/o/with/slashes') - self.assertTrue('swift.object/a/c/o/with/slashes' in resp.environ) + infocache = resp.environ['swift.infocache'] + self.assertTrue('swift.object/a/c/o/with/slashes' in infocache) self.assertEqual( - resp.environ['swift.object/a/c/o/with/slashes']['status'], 200) + infocache['swift.object/a/c/o/with/slashes']['status'], 200) + req = Request.blank('/v1/a/c/o') with patch('swift.proxy.controllers.base.' 'http_connect', fake_http_connect(200)): resp = base.GETorHEAD_base(req, 'object', iter(nodes), 'part', '/a/c/o') - self.assertTrue('swift.object/a/c/o' in resp.environ) - self.assertEqual(resp.environ['swift.object/a/c/o']['status'], 200) + infocache = resp.environ['swift.infocache'] + self.assertTrue('swift.object/a/c/o' in infocache) + self.assertEqual(infocache['swift.object/a/c/o']['status'], 200) + req = Request.blank('/v1/a/c') with patch('swift.proxy.controllers.base.' 'http_connect', fake_http_connect(200)): resp = base.GETorHEAD_base(req, 'container', iter(nodes), 'part', '/a/c') - self.assertTrue('swift.container/a/c' in resp.environ) - self.assertEqual(resp.environ['swift.container/a/c']['status'], 200) + infocache = resp.environ['swift.infocache'] + self.assertTrue('swift.container/a/c' in infocache) + self.assertEqual(infocache['swift.container/a/c']['status'], 200) req = Request.blank('/v1/a') with patch('swift.proxy.controllers.base.' 'http_connect', fake_http_connect(200)): resp = base.GETorHEAD_base(req, 'account', iter(nodes), 'part', '/a') - self.assertTrue('swift.account/a' in resp.environ) - self.assertEqual(resp.environ['swift.account/a']['status'], 200) + infocache = resp.environ['swift.infocache'] + self.assertTrue('swift.account/a' in infocache) + self.assertEqual(infocache['swift.account/a']['status'], 200) # Run the above tests again, but this time with concurrent_reads # turned on @@ -209,26 +215,28 @@ class TestFuncs(unittest.TestCase): resp = base.GETorHEAD_base( req, 'object', iter(nodes), 'part', '/a/c/o/with/slashes', concurrency=concurrent_get_threads) - self.assertTrue('swift.object/a/c/o/with/slashes' in resp.environ) + infocache = resp.environ['swift.infocache'] + self.assertTrue('swift.object/a/c/o/with/slashes' in infocache) self.assertEqual( - resp.environ['swift.object/a/c/o/with/slashes']['status'], 200) + infocache['swift.object/a/c/o/with/slashes']['status'], 200) req = Request.blank('/v1/a/c/o') with patch('swift.proxy.controllers.base.http_connect', fake_http_connect(200, slow_connect=True)): resp = base.GETorHEAD_base( req, 'object', iter(nodes), 'part', '/a/c/o', concurrency=concurrent_get_threads) - self.assertTrue('swift.object/a/c/o' in resp.environ) - self.assertEqual(resp.environ['swift.object/a/c/o']['status'], 200) + infocache = resp.environ['swift.infocache'] + self.assertTrue('swift.object/a/c/o' in infocache) + self.assertEqual(infocache['swift.object/a/c/o']['status'], 200) req = Request.blank('/v1/a/c') with patch('swift.proxy.controllers.base.http_connect', fake_http_connect(200, slow_connect=True)): resp = base.GETorHEAD_base( req, 'container', iter(nodes), 'part', '/a/c', concurrency=concurrent_get_threads) - self.assertTrue('swift.container/a/c' in resp.environ) - self.assertEqual(resp.environ['swift.container/a/c']['status'], - 200) + infocache = resp.environ['swift.infocache'] + self.assertTrue('swift.container/a/c' in infocache) + self.assertEqual(infocache['swift.container/a/c']['status'], 200) req = Request.blank('/v1/a') with patch('swift.proxy.controllers.base.http_connect', @@ -236,8 +244,9 @@ class TestFuncs(unittest.TestCase): resp = base.GETorHEAD_base( req, 'account', iter(nodes), 'part', '/a', concurrency=concurrent_get_threads) - self.assertTrue('swift.account/a' in resp.environ) - self.assertEqual(resp.environ['swift.account/a']['status'], 200) + infocache = resp.environ['swift.infocache'] + self.assertTrue('swift.account/a' in infocache) + self.assertEqual(infocache['swift.account/a']['status'], 200) def test_get_info(self): app = FakeApp() @@ -249,7 +258,7 @@ class TestFuncs(unittest.TestCase): self.assertEqual(info_a['bytes'], 6666) self.assertEqual(info_a['total_object_count'], 1000) # Make sure the env cache is set - self.assertEqual(env.get('swift.account/a'), info_a) + self.assertEqual(env['swift.infocache'].get('swift.account/a'), info_a) # Make sure the app was called self.assertEqual(app.responses.stats['account'], 1) @@ -260,7 +269,7 @@ class TestFuncs(unittest.TestCase): self.assertEqual(info_a['bytes'], 6666) self.assertEqual(info_a['total_object_count'], 1000) # Make sure the env cache is set - self.assertEqual(env.get('swift.account/a'), info_a) + self.assertEqual(env['swift.infocache'].get('swift.account/a'), info_a) # Make sure the app was NOT called AGAIN self.assertEqual(app.responses.stats['account'], 1) @@ -271,8 +280,10 @@ class TestFuncs(unittest.TestCase): self.assertEqual(info_c['bytes'], 6666) self.assertEqual(info_c['object_count'], 1000) # Make sure the env cache is set - self.assertEqual(env.get('swift.account/a'), info_a) - self.assertEqual(env.get('swift.container/a/c'), info_c) + self.assertEqual( + env['swift.infocache'].get('swift.account/a'), info_a) + self.assertEqual( + env['swift.infocache'].get('swift.container/a/c'), info_c) # Make sure the app was called for container self.assertEqual(app.responses.stats['container'], 1) @@ -286,22 +297,25 @@ class TestFuncs(unittest.TestCase): self.assertEqual(info_c['bytes'], 6666) self.assertEqual(info_c['object_count'], 1000) # Make sure the env cache is set - self.assertEqual(env.get('swift.account/a'), info_a) - self.assertEqual(env.get('swift.container/a/c'), info_c) + self.assertEqual( + env['swift.infocache'].get('swift.account/a'), info_a) + self.assertEqual( + env['swift.infocache'].get('swift.container/a/c'), info_c) # check app calls both account and container self.assertEqual(app.responses.stats['account'], 1) self.assertEqual(app.responses.stats['container'], 1) # This time do an env cached call to container while account is not # cached - del(env['swift.account/a']) + del(env['swift.infocache']['swift.account/a']) info_c = get_info(app, env, 'a', 'c') # Check that you got proper info self.assertEqual(info_a['status'], 200) self.assertEqual(info_c['bytes'], 6666) self.assertEqual(info_c['object_count'], 1000) # Make sure the env cache is set and account still not cached - self.assertEqual(env.get('swift.container/a/c'), info_c) + self.assertEqual( + env['swift.infocache'].get('swift.container/a/c'), info_c) # no additional calls were made self.assertEqual(app.responses.stats['account'], 1) self.assertEqual(app.responses.stats['container'], 1) @@ -315,7 +329,8 @@ class TestFuncs(unittest.TestCase): self.assertEqual(info_a['bytes'], None) self.assertEqual(info_a['total_object_count'], None) # Make sure the env cache is set - self.assertEqual(env.get('swift.account/a'), info_a) + self.assertEqual( + env['swift.infocache'].get('swift.account/a'), info_a) # and account was called self.assertEqual(app.responses.stats['account'], 1) @@ -326,7 +341,8 @@ class TestFuncs(unittest.TestCase): self.assertEqual(info_a['bytes'], None) self.assertEqual(info_a['total_object_count'], None) # Make sure the env cache is set - self.assertEqual(env.get('swift.account/a'), info_a) + self.assertEqual( + env['swift.infocache'].get('swift.account/a'), info_a) # add account was NOT called AGAIN self.assertEqual(app.responses.stats['account'], 1) @@ -336,7 +352,8 @@ class TestFuncs(unittest.TestCase): info_a = get_info(app, env, 'a') # Check that you got proper info self.assertEqual(info_a, None) - self.assertEqual(env['swift.account/a']['status'], 404) + self.assertEqual( + env['swift.infocache']['swift.account/a']['status'], 404) # and account was called self.assertEqual(app.responses.stats['account'], 1) @@ -344,7 +361,8 @@ class TestFuncs(unittest.TestCase): info_a = get_info(None, env, 'a') # Check that you got proper info self.assertEqual(info_a, None) - self.assertEqual(env['swift.account/a']['status'], 404) + self.assertEqual( + env['swift.infocache']['swift.account/a']['status'], 404) # add account was NOT called AGAIN self.assertEqual(app.responses.stats['account'], 1) @@ -352,14 +370,16 @@ class TestFuncs(unittest.TestCase): app = FakeApp() req = Request.blank("/v1/a/c", environ={'swift.cache': FakeCache()}) get_container_info(req.environ, app, swift_source='MC') - self.assertEqual(app.sources, ['GET_INFO', 'MC']) + self.assertEqual([e['swift.source'] for e in app.captured_envs], + ['GET_INFO', 'MC']) def test_get_object_info_swift_source(self): app = FakeApp() req = Request.blank("/v1/a/c/o", environ={'swift.cache': FakeCache()}) get_object_info(req.environ, app, swift_source='LU') - self.assertEqual(app.sources, ['LU']) + self.assertEqual([e['swift.source'] for e in app.captured_envs], + ['LU']) def test_get_container_info_no_cache(self): req = Request.blank("/v1/AUTH_account/cont", @@ -401,9 +421,10 @@ class TestFuncs(unittest.TestCase): def test_get_container_info_env(self): cache_key = get_container_memcache_key("account", "cont") env_key = 'swift.%s' % cache_key - req = Request.blank("/v1/account/cont", - environ={env_key: {'bytes': 3867}, - 'swift.cache': FakeCache({})}) + req = Request.blank( + "/v1/account/cont", + environ={'swift.infocache': {env_key: {'bytes': 3867}}, + 'swift.cache': FakeCache({})}) resp = get_container_info(req.environ, 'xxx') self.assertEqual(resp['bytes'], 3867) @@ -411,7 +432,18 @@ class TestFuncs(unittest.TestCase): app = FakeApp() req = Request.blank("/v1/a", environ={'swift.cache': FakeCache()}) get_account_info(req.environ, app, swift_source='MC') - self.assertEqual(app.sources, ['MC']) + self.assertEqual([e['swift.source'] for e in app.captured_envs], + ['MC']) + + def test_get_account_info_infocache(self): + app = FakeApp() + ic = {} + req = Request.blank("/v1/a", environ={'swift.cache': FakeCache(), + 'swift.infocache': ic}) + get_account_info(req.environ, app) + got_infocaches = [e['swift.infocache'] for e in app.captured_envs] + self.assertEqual(1, len(got_infocaches)) + self.assertIs(ic, got_infocaches[0]) def test_get_account_info_no_cache(self): app = FakeApp() @@ -451,9 +483,10 @@ class TestFuncs(unittest.TestCase): def test_get_account_info_env(self): cache_key = get_account_memcache_key("account") env_key = 'swift.%s' % cache_key - req = Request.blank("/v1/account", - environ={env_key: {'bytes': 3867}, - 'swift.cache': FakeCache({})}) + req = Request.blank( + "/v1/account", + environ={'swift.infocache': {env_key: {'bytes': 3867}}, + 'swift.cache': FakeCache({})}) resp = get_account_info(req.environ, 'xxx') self.assertEqual(resp['bytes'], 3867) @@ -463,9 +496,10 @@ class TestFuncs(unittest.TestCase): 'type': 'application/json', 'meta': {}} env_key = get_object_env_key("account", "cont", "obj") - req = Request.blank("/v1/account/cont/obj", - environ={env_key: cached, - 'swift.cache': FakeCache({})}) + req = Request.blank( + "/v1/account/cont/obj", + environ={'swift.infocache': {env_key: cached}, + 'swift.cache': FakeCache({})}) resp = get_object_info(req.environ, 'xxx') self.assertEqual(resp['length'], 3333) self.assertEqual(resp['type'], 'application/json') diff --git a/test/unit/proxy/controllers/test_container.py b/test/unit/proxy/controllers/test_container.py index a95e058452..851ecc81d9 100644 --- a/test/unit/proxy/controllers/test_container.py +++ b/test/unit/proxy/controllers/test_container.py @@ -102,9 +102,11 @@ class TestContainerController(TestRingBase): req = Request.blank('/v1/a/c', {'PATH_INFO': '/v1/a/c'}) resp = controller.HEAD(req) self.assertEqual(2, resp.status_int // 100) - self.assertTrue("swift.container/a/c" in resp.environ) - self.assertEqual(headers_to_container_info(resp.headers), - resp.environ['swift.container/a/c']) + self.assertTrue( + "swift.container/a/c" in resp.environ['swift.infocache']) + self.assertEqual( + headers_to_container_info(resp.headers), + resp.environ['swift.infocache']['swift.container/a/c']) def test_swift_owner(self): owner_headers = { diff --git a/test/unit/proxy/test_server.py b/test/unit/proxy/test_server.py index 1fc021a542..f2e4986650 100644 --- a/test/unit/proxy/test_server.py +++ b/test/unit/proxy/test_server.py @@ -6319,22 +6319,23 @@ class TestContainerController(unittest.TestCase): res = controller.HEAD(req) self.assertEqual(res.status[:len(str(expected))], str(expected)) + infocache = res.environ.get('swift.infocache', {}) if expected < 400: - self.assertTrue('x-works' in res.headers) + self.assertIn('x-works', res.headers) self.assertEqual(res.headers['x-works'], 'yes') if c_expected: - self.assertTrue('swift.container/a/c' in res.environ) + self.assertIn('swift.container/a/c', infocache) self.assertEqual( - res.environ['swift.container/a/c']['status'], + infocache['swift.container/a/c']['status'], c_expected) else: - self.assertTrue('swift.container/a/c' not in res.environ) + self.assertNotIn('swift.container/a/c', infocache) if a_expected: - self.assertTrue('swift.account/a' in res.environ) - self.assertEqual(res.environ['swift.account/a']['status'], + self.assertIn('swift.account/a', infocache) + self.assertEqual(infocache['swift.account/a']['status'], a_expected) else: - self.assertTrue('swift.account/a' not in res.environ) + self.assertNotIn('swift.account/a', res.environ) set_http_connect(*statuses, **kwargs) self.app.memcache.store = {} @@ -6343,22 +6344,23 @@ class TestContainerController(unittest.TestCase): res = controller.GET(req) self.assertEqual(res.status[:len(str(expected))], str(expected)) + infocache = res.environ.get('swift.infocache', {}) if expected < 400: self.assertTrue('x-works' in res.headers) self.assertEqual(res.headers['x-works'], 'yes') if c_expected: - self.assertTrue('swift.container/a/c' in res.environ) + self.assertIn('swift.container/a/c', infocache) self.assertEqual( - res.environ['swift.container/a/c']['status'], + infocache['swift.container/a/c']['status'], c_expected) else: - self.assertTrue('swift.container/a/c' not in res.environ) + self.assertNotIn('swift.container/a/c', infocache) if a_expected: - self.assertTrue('swift.account/a' in res.environ) - self.assertEqual(res.environ['swift.account/a']['status'], + self.assertIn('swift.account/a', infocache) + self.assertEqual(infocache['swift.account/a']['status'], a_expected) else: - self.assertTrue('swift.account/a' not in res.environ) + self.assertNotIn('swift.account/a', infocache) # In all the following tests cache 200 for account # return and ache vary for container # return 200 and cache 200 for and container @@ -6970,8 +6972,8 @@ class TestContainerController(unittest.TestCase): self.app.update_request(req) res = controller.GET(req) self.assertEqual(res.status_int, 204) - self.assertEqual( - res.environ['swift.container/a/c']['status'], 204) + ic = res.environ['swift.infocache'] + self.assertEqual(ic['swift.container/a/c']['status'], 204) self.assertEqual(res.content_length, 0) self.assertTrue('transfer-encoding' not in res.headers) @@ -6989,7 +6991,9 @@ class TestContainerController(unittest.TestCase): req.environ['swift.authorize'] = authorize self.app.update_request(req) res = controller.GET(req) - self.assertEqual(res.environ['swift.container/a/c']['status'], 201) + self.assertEqual( + res.environ['swift.infocache']['swift.container/a/c']['status'], + 201) self.assertTrue(called[0]) def test_HEAD_calls_authorize(self): @@ -7457,16 +7461,18 @@ class TestAccountController(unittest.TestCase): self.app.update_request(req) res = method(req) self.assertEqual(res.status_int, expected) + infocache = res.environ.get('swift.infocache', {}) if env_expected: - self.assertEqual(res.environ['swift.account/a']['status'], + self.assertEqual(infocache['swift.account/a']['status'], env_expected) set_http_connect(*statuses) req = Request.blank('/v1/a/', {}) self.app.update_request(req) res = method(req) + infocache = res.environ.get('swift.infocache', {}) self.assertEqual(res.status_int, expected) if env_expected: - self.assertEqual(res.environ['swift.account/a']['status'], + self.assertEqual(infocache['swift.account/a']['status'], env_expected) def test_OPTIONS(self):