Fix context storage bug

Fix a bug with storing the request context in thread local where the
storage was not properly cleared between requests.

(This was causing the same request to always be logged.)

Closes-Bug: 1384911
Change-Id: Ie30b93900451b0f9f6fd8158457b0b66903af470
This commit is contained in:
Brian D. Elliott 2014-10-27 18:44:57 +00:00
parent ded0852cd0
commit 2652a3a528
5 changed files with 37 additions and 2 deletions

View File

@ -20,6 +20,7 @@ import webob.exc
from glance.api import policy
from glance.common import wsgi
import glance.context
from glance.openstack.common import local
import glance.openstack.common.log as logging
@ -48,6 +49,10 @@ class BaseContextMiddleware(wsgi.Middleware):
def process_response(self, resp):
try:
request_id = resp.request.context.request_id
# NOTE(belliott) clear context stored in thread local
if hasattr(local.store, 'context'):
delattr(local.store, 'context')
except AttributeError:
LOG.warn(_('Unable to retrieve request id from context'))
else:

View File

@ -82,6 +82,7 @@ class Server(object):
self.process_pid = None
self.server_module = None
self.stop_kill = False
self.use_user_token = False
def write_conf(self, **kwargs):
"""
@ -317,6 +318,7 @@ cert_file = %(cert_file)s
metadata_encryption_key = %(metadata_encryption_key)s
registry_host = 127.0.0.1
registry_port = %(registry_port)s
use_user_token = %(use_user_token)s
log_file = %(log_file)s
image_size_cap = %(image_size_cap)d
delayed_delete = %(delayed_delete)s

View File

@ -1995,7 +1995,10 @@ class TestImages(functional.FunctionalTest):
self.cleanup()
self.api_server.deployment_flavor = 'fakeauth'
self.registry_server.deployment_flavor = 'fakeauth'
self.start_servers(**self.__dict__.copy())
kwargs = self.__dict__.copy()
kwargs['use_user_token'] = True
self.start_servers(**kwargs)
owners = ['admin', 'tenant1', 'tenant2', 'none']
visibilities = ['public', 'private']

View File

@ -23,6 +23,7 @@ from oslo.config import cfg
from oslo.db import options
from oslo.serialization import jsonutils
from glance.openstack.common import local
from glance.tests import stubs
from glance.tests import utils as test_utils
@ -79,6 +80,10 @@ class IsolatedUnitTest(StoreClearingUnitTest):
self.test_dir,
registry=self.registry)
# clear context left-over from any previous test executions
if hasattr(local.store, 'context'):
delattr(local.store, 'context')
def _copy_data_file(self, file_name, dst_dir):
src_file_name = os.path.join('glance/tests/etc', file_name)
shutil.copy(src_file_name, dst_dir)

View File

@ -16,6 +16,7 @@ import webob
from glance.api.middleware import context
import glance.context
from glance.openstack.common import local
from glance.tests.unit import base
@ -118,6 +119,23 @@ class TestContextMiddleware(base.IsolatedUnitTest):
self.assertRaises(webob.exc.HTTPInternalServerError,
middleware.process_request, req)
def test_clear_context(self):
# context should be cleared between requests
middleware = self._build_middleware()
req = self._build_request()
middleware.process_request(req)
self.assertTrue(hasattr(local.store, 'context'))
# response processing should clear reference to
# the context
resp = webob.Response()
resp.request = req
resp = middleware.process_response(resp)
self.assertFalse(hasattr(local.store, 'context'))
class TestUnauthenticatedContextMiddleware(base.IsolatedUnitTest):
def test_request(self):
@ -134,8 +152,10 @@ class TestUnauthenticatedContextMiddleware(base.IsolatedUnitTest):
middleware = context.UnauthenticatedContextMiddleware(None)
req = webob.Request.blank('/')
req.context = glance.context.RequestContext()
request_id = req.context.request_id
resp = webob.Response()
resp.request = req
middleware.process_response(resp)
self.assertEqual(resp.headers['x-openstack-request-id'],
'req-%s' % req.context.request_id)
'req-%s' % request_id)