Restore backward compat of paste factory

Some application inherits from our middleware class

When we homogenize the signature and configuration handling
of all middlewares we break them.

This change fixes that.

Closes-bug: #1486735

Change-Id: I40c3d59110c6f8c5a1b3d3ccc734dc441069b025
This commit is contained in:
Mehdi Abaakouk 2015-08-20 07:52:59 +02:00
parent a5a0a2f7a7
commit c78b156723
10 changed files with 57 additions and 29 deletions

View File

@ -21,7 +21,7 @@ import webob.dec
from oslo_config import cfg
class Middleware(object):
class ConfigurableMiddleware(object):
"""Base WSGI middleware wrapper.
These classes require an application to be initialized that will be called
@ -41,14 +41,19 @@ class Middleware(object):
return middleware_filter
def __init__(self, application, conf=None):
"""Base middleware constructor
:param conf: a dict of options or a cfg.ConfigOpts object
"""
self.application = application
# NOTE(sileht): If the configuration come from oslo.config
# just use it.
if isinstance(conf, cfg.ConfigOpts):
self.conf = []
self.conf = {}
self.oslo_conf = conf
else:
self.conf = conf or []
self.conf = conf or {}
if "oslo_config_project" in self.conf:
if 'oslo_config_file' in self.conf:
default_config_files = [self.conf['oslo_config_file']]
@ -96,3 +101,16 @@ class Middleware(object):
if 'request' in args:
return self.process_response(response, request=req)
return self.process_response(response)
class Middleware(ConfigurableMiddleware):
"""Legacy base WSGI middleware wrapper.
Legacy interface that doesn't pass configuration options
to the middleware when it's loaded via paste.deploy.
"""
@classmethod
def factory(cls, global_conf, **local_conf):
"""Factory method for paste.deploy."""
return cls

View File

@ -25,7 +25,7 @@ from oslo_middleware import base
LOG = logging.getLogger(__name__)
class CatchErrors(base.Middleware):
class CatchErrors(base.ConfigurableMiddleware):
"""Middleware that provides high-level error handling.
It catches all exceptions from subsequent applications in WSGI pipeline

View File

@ -18,7 +18,7 @@ import uuid
from oslo_middleware import base
class CorrelationId(base.Middleware):
class CorrelationId(base.ConfigurableMiddleware):
"Middleware that attaches a correlation id to WSGI request"
def process_request(self, req):

View File

@ -53,7 +53,7 @@ CORS_OPTS = [
]
class CORS(base.Middleware):
class CORS(base.ConfigurableMiddleware):
"""CORS Middleware.
This middleware allows a WSGI app to serve CORS headers for multiple
@ -71,8 +71,8 @@ class CORS(base.Middleware):
'Pragma'
]
def __init__(self, application, conf=None):
super(CORS, self).__init__(application, conf)
def __init__(self, application, *args, **kwargs):
super(CORS, self).__init__(application, *args, **kwargs)
# Begin constructing our configuration hash.
self.allowed_origins = {}
self._init_conf()

View File

@ -25,7 +25,7 @@ import webob.dec
from oslo_middleware import base
class Debug(base.Middleware):
class Debug(base.ConfigurableMiddleware):
"""Helper class that returns debug information.
Can be inserted into any WSGI application chain to get information about

View File

@ -21,7 +21,7 @@ import webob.response
from oslo_middleware import base
class Healthcheck(base.Middleware):
class Healthcheck(base.ConfigurableMiddleware):
"""Healthcheck middleware used for monitoring.
If the path is /healthcheck, it will respond 200 with "OK" as the body.

View File

@ -23,7 +23,7 @@ ENV_REQUEST_ID = 'openstack.request_id'
HTTP_RESP_HEADER_REQUEST_ID = 'x-openstack-request-id'
class RequestId(base.Middleware):
class RequestId(base.ConfigurableMiddleware):
"""Middleware that ensures request ID.
It ensures to assign request ID for each API request and set it to

View File

@ -75,7 +75,7 @@ class LimitingReader(object):
return result
class RequestBodySizeLimiter(base.Middleware):
class RequestBodySizeLimiter(base.ConfigurableMiddleware):
"""Limit the size of incoming requests."""
def __init__(self, application, conf=None):

View File

@ -23,7 +23,7 @@ OPTS = [
]
class SSLMiddleware(base.Middleware):
class SSLMiddleware(base.ConfigurableMiddleware):
"""SSL termination proxies middleware.
This middleware overloads wsgi.url_scheme with the one provided in
@ -31,8 +31,8 @@ class SSLMiddleware(base.Middleware):
termination proxy.
"""
def __init__(self, application, conf=None):
super(SSLMiddleware, self).__init__(application, conf)
def __init__(self, application, *args, **kwargs):
super(SSLMiddleware, self).__init__(application, *args, **kwargs)
self.oslo_conf.register_opts(OPTS, group='oslo_middleware')
def process_request(self, req):

View File

@ -14,28 +14,25 @@
import webob
from oslo_middleware.base import ConfigurableMiddleware
from oslo_middleware.base import Middleware
from oslotest.base import BaseTestCase
@webob.dec.wsgify
def application(req):
return 'Hello, World!!!'
class TestBase(BaseTestCase):
"""Test the base middleware class."""
def setUp(self):
"""Setup the tests."""
super(BaseTestCase, self).setUp()
def test_extend_with_request(self):
"""Assert that a newer middleware behaves as appropriate.
This tests makes sure that the request is passed to the
middleware's implementation.
"""
# Create an application.
@webob.dec.wsgify
def application(req):
return 'Hello, World!!!'
# Bootstrap the application
self.application = RequestBase(application)
@ -52,11 +49,6 @@ class TestBase(BaseTestCase):
middleware's implementation, and that there are no other expected
errors.
"""
# Create an application.
@webob.dec.wsgify
def application(req):
return 'Hello, World!!!'
# Bootstrap the application
self.application = NoRequestBase(application)
@ -66,6 +58,16 @@ class TestBase(BaseTestCase):
self.assertTrue(self.application.called_without_request)
def test_paste_deploy_legacy(self):
app = LegacyMiddlewareTest.factory(
{'global': True}, local=True)(application)
self.assertEqual(app.conf, {})
def test_paste_deploy_configurable(self):
app = ConfigurableMiddlewareTest.factory(
{'global': True}, local=True)(application)
self.assertEqual(app.conf, {'global': True, 'local': True})
class NoRequestBase(Middleware):
"""Test middleware, implements old model."""
@ -79,3 +81,11 @@ class RequestBase(Middleware):
def process_response(self, response, request):
self.called_with_request = True
return response
class ConfigurableMiddlewareTest(ConfigurableMiddleware):
pass
class LegacyMiddlewareTest(Middleware):
pass