From 5352ff068dc0466b3a376e802512c2065c130455 Mon Sep 17 00:00:00 2001 From: Brant Knudson Date: Mon, 14 Sep 2015 18:18:32 -0500 Subject: [PATCH] auth_token tests use clean config The auth_token tests were failing randomly (depending on which tests were run together) because the global config had options left in it by other tests (options were added as part of auth plugin initialization and oslo.config provides no way to remove registered options). This change makes it so that auth_token gets a fresh config for every test (other than one test that requires using the global CONF object). Closes-Bug: 1494327 Change-Id: I647f7fade01e2a619f4a5d12a4e71897423469c1 --- keystonemiddleware/auth_token/__init__.py | 5 +++- .../tests/unit/auth_token/base.py | 24 ++++++++++++------- .../auth_token/test_auth_token_middleware.py | 16 ++++++++++++- .../unit/auth_token/test_user_auth_plugin.py | 6 +++++ 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/keystonemiddleware/auth_token/__init__.py b/keystonemiddleware/auth_token/__init__.py index 66837c4c..d09f91b6 100644 --- a/keystonemiddleware/auth_token/__init__.py +++ b/keystonemiddleware/auth_token/__init__.py @@ -648,7 +648,10 @@ class AuthProtocol(_BaseAuthProtocol): else: default_config_files = None - self._local_oslo_config = cfg.ConfigOpts() + # For unit tests, support passing in a ConfigOpts in + # oslo_config_config. + self._local_oslo_config = conf.get('oslo_config_config', + cfg.ConfigOpts()) self._local_oslo_config( {}, project=conf['oslo_config_project'], default_config_files=default_config_files, diff --git a/keystonemiddleware/tests/unit/auth_token/base.py b/keystonemiddleware/tests/unit/auth_token/base.py index 94c84d4b..d76572a8 100644 --- a/keystonemiddleware/tests/unit/auth_token/base.py +++ b/keystonemiddleware/tests/unit/auth_token/base.py @@ -13,6 +13,7 @@ import logging import fixtures +from oslo_config import cfg from oslo_config import fixture as cfg_fixture from requests_mock.contrib import fixture as rm_fixture import six @@ -28,29 +29,36 @@ class BaseAuthTokenTestCase(utils.BaseTestCase): super(BaseAuthTokenTestCase, self).setUp() self.requests_mock = self.useFixture(rm_fixture.Fixture()) self.logger = fixtures.FakeLogger(level=logging.DEBUG) - self.cfg = self.useFixture(cfg_fixture.Config()) + self.cfg = self.useFixture(cfg_fixture.Config(conf=cfg.ConfigOpts())) - @classmethod - def create_middleware(cls, cb, conf=None): + def create_middleware(self, cb, conf=None, use_global_conf=False): @webob.dec.wsgify def _do_cb(req): return cb(req) - return auth_token.AuthProtocol(_do_cb, conf or {}) + if use_global_conf: + opts = conf or {} + else: + opts = { + 'oslo_config_project': 'keystonemiddleware', + 'oslo_config_config': self.cfg.conf, + } + opts.update(conf or {}) - @classmethod - def create_simple_middleware(cls, + return auth_token.AuthProtocol(_do_cb, opts) + + def create_simple_middleware(self, status='200 OK', body='', headers=None, - conf=None): + **kwargs): def cb(req): resp = webob.Response(body, status) resp.headers.update(headers or {}) return resp - return cls.create_middleware(cb, conf) + return self.create_middleware(cb, **kwargs) @classmethod def call(cls, middleware, method='GET', path='/', headers=None): diff --git a/keystonemiddleware/tests/unit/auth_token/test_auth_token_middleware.py b/keystonemiddleware/tests/unit/auth_token/test_auth_token_middleware.py index ab12a6b5..bb572aa3 100644 --- a/keystonemiddleware/tests/unit/auth_token/test_auth_token_middleware.py +++ b/keystonemiddleware/tests/unit/auth_token/test_auth_token_middleware.py @@ -2371,6 +2371,9 @@ class AuthProtocolLoadingTests(BaseAuthTokenMiddlewareTest): project_id = uuid.uuid4().hex + # Register the authentication options + auth.register_conf_options(self.cfg.conf, group=_base.AUTHTOKEN_GROUP) + # configure the authentication options self.cfg.config(auth_plugin='password', username='testuser', @@ -2391,6 +2394,7 @@ class AuthProtocolLoadingTests(BaseAuthTokenMiddlewareTest): return app._identity_server._adapter.auth def test_invalid_plugin_fails_to_initialize(self): + auth.register_conf_options(self.cfg.conf, group=_base.AUTHTOKEN_GROUP) self.cfg.config(auth_plugin=uuid.uuid4().hex, group=_base.AUTHTOKEN_GROUP) @@ -2406,6 +2410,9 @@ class AuthProtocolLoadingTests(BaseAuthTokenMiddlewareTest): username = 'testuser' password = 'testpass' + # Register the authentication options + auth.register_conf_options(self.cfg.conf, group=_base.AUTHTOKEN_GROUP) + # configure the authentication options self.cfg.config(auth_plugin='password', password=password, @@ -2438,6 +2445,9 @@ class AuthProtocolLoadingTests(BaseAuthTokenMiddlewareTest): opts = auth.get_plugin_options('password') self.cfg.register_opts(opts, group=section) + # Register the authentication options + auth.register_conf_options(self.cfg.conf, group=_base.AUTHTOKEN_GROUP) + # configure the authentication options self.cfg.config(auth_section=section, group=_base.AUTHTOKEN_GROUP) self.cfg.config(auth_plugin='password', @@ -2477,6 +2487,9 @@ class TestAuthPluginUserAgentGeneration(BaseAuthTokenMiddlewareTest): opts = auth.get_plugin_options('password') self.cfg.register_opts(opts, group=self.section) + # Register the authentication options + auth.register_conf_options(self.cfg.conf, group=_base.AUTHTOKEN_GROUP) + # configure the authentication options self.cfg.config(auth_section=self.section, group=_base.AUTHTOKEN_GROUP) self.cfg.config(auth_plugin='password', @@ -2520,7 +2533,8 @@ class TestAuthPluginUserAgentGeneration(BaseAuthTokenMiddlewareTest): body = uuid.uuid4().hex with mock.patch('keystonemiddleware.auth_token.pkg_resources', new=fake_pkg_resources): - return self.create_simple_middleware(body=body, conf=conf) + return self.create_simple_middleware(body=body, conf=conf, + use_global_conf=True) def _assert_user_agent(self, app, project, ksm_version): sess = app._identity_server._adapter.session diff --git a/keystonemiddleware/tests/unit/auth_token/test_user_auth_plugin.py b/keystonemiddleware/tests/unit/auth_token/test_user_auth_plugin.py index 83da69bd..52d29737 100644 --- a/keystonemiddleware/tests/unit/auth_token/test_user_auth_plugin.py +++ b/keystonemiddleware/tests/unit/auth_token/test_user_auth_plugin.py @@ -15,6 +15,7 @@ import uuid from keystoneclient import auth from keystoneclient import fixture +from keystonemiddleware.auth_token import _base from keystonemiddleware.tests.unit.auth_token import base # NOTE(jamielennox): just some sample values that we can use for testing @@ -31,6 +32,11 @@ class BaseUserPluginTests(object): opts = auth.get_plugin_class(auth_plugin).get_options() self.cfg.register_opts(opts, group=group) + # Since these tests cfg.config() themselves rather than waiting for + # auth_token to do it on __init__ we need to register the base auth + # options (e.g., auth_plugin) + auth.register_conf_options(self.cfg.conf, group=_base.AUTHTOKEN_GROUP) + self.cfg.config(group=group, auth_plugin=auth_plugin, **kwargs)