Reduce setup overhead in auth_plugin tests

The test runtimes have been cut in half by lowering the amount of excess
setup run by each test.

Change-Id: I96ae7c694e22d87d55d1f24db0e6cac4b941a9a9
This commit is contained in:
David Stanek 2015-11-04 05:37:36 +00:00
parent 2088906a04
commit 6872f85560
2 changed files with 65 additions and 34 deletions

View File

@ -13,6 +13,7 @@
import fixtures
from keystone import auth
from keystone.common import config as common_cfg
@ -32,3 +33,36 @@ class ConfigAuthPlugins(fixtures.Fixture):
common_cfg.setup_authentication()
if self.method_classes:
self.config_fixture.config(group='auth', **self.method_classes)
class LoadAuthPlugins(fixtures.Fixture):
def __init__(self, *method_names):
super(LoadAuthPlugins, self).__init__()
self.method_names = method_names
# NOTE(dstanek): This fixutre will load the requested auth
# methods as part of its setup. We need to save any exising
# plugins so that we care restore the in the cleanup.
self.saved = {}
def setUp(self):
super(LoadAuthPlugins, self).setUp()
if auth.controllers.AUTH_PLUGINS_LOADED:
raise Exception('auth_plugins already loaded')
AUTH_METHODS = auth.controllers.AUTH_METHODS
for method_name in self.method_names:
if method_name in AUTH_METHODS:
self.saved[method_name] = AUTH_METHODS[method_name]
AUTH_METHODS[method_name] = auth.controllers.load_auth_method(
method_name)
auth.controllers.AUTH_PLUGINS_LOADED = True
def cleanUp(self):
AUTH_METHODS = auth.controllers.AUTH_METHODS
for method_name in list(AUTH_METHODS):
if method_name in self.saved:
AUTH_METHODS[method_name] = self.saved[method_name]
else:
del AUTH_METHODS[method_name]
auth.controllers.AUTH_PLUGINS_LOADED = False

View File

@ -19,10 +19,15 @@ import mock
from keystone import auth
from keystone import exception
from keystone.tests import unit
from keystone.tests.unit.ksfixtures import auth_plugins
# for testing purposes only
METHOD_NAME = 'simple_challenge_response'
METHOD_OPTS = {
METHOD_NAME:
'keystone.tests.unit.test_auth_plugin.SimpleChallengeResponse',
}
EXPECTED_RESPONSE = uuid.uuid4().hex
DEMO_USER_ID = uuid.uuid4().hex
@ -40,21 +45,8 @@ class SimpleChallengeResponse(auth.AuthMethodHandler):
class TestAuthPlugin(unit.SQLDriverOverrides, unit.TestCase):
def setUp(self):
super(TestAuthPlugin, self).setUp()
self.load_backends()
self.api = auth.controllers.Auth()
def config_overrides(self):
super(TestAuthPlugin, self).config_overrides()
method_opts = {
METHOD_NAME:
'keystone.tests.unit.test_auth_plugin.SimpleChallengeResponse',
}
self.auth_plugin_config_override(
methods=['external', 'password', 'token', METHOD_NAME],
**method_opts)
def test_unsupported_auth_method(self):
method_name = uuid.uuid4().hex
auth_data = {'methods': [method_name]}
@ -66,6 +58,12 @@ class TestAuthPlugin(unit.SQLDriverOverrides, unit.TestCase):
auth_data)
def test_addition_auth_steps(self):
self.useFixture(
auth_plugins.ConfigAuthPlugins(self.config_fixture,
methods=[METHOD_NAME],
**METHOD_OPTS))
self.useFixture(auth_plugins.LoadAuthPlugins(METHOD_NAME))
auth_data = {'methods': [METHOD_NAME]}
auth_data[METHOD_NAME] = {
'test': 'test'}
@ -105,9 +103,9 @@ class TestAuthPlugin(unit.SQLDriverOverrides, unit.TestCase):
def test_duplicate_method(self):
# Having the same method twice doesn't cause load_auth_methods to fail.
self.auth_plugin_config_override(
methods=['external', 'external'])
self.clear_auth_plugin_registry()
self.useFixture(
auth_plugins.ConfigAuthPlugins(self.config_fixture,
['external', 'external']))
auth.controllers.load_auth_methods()
self.assertIn('external', auth.controllers.AUTH_METHODS)
@ -128,8 +126,6 @@ class TestAuthPluginDynamicOptions(TestAuthPlugin):
class TestMapped(unit.TestCase):
def setUp(self):
super(TestMapped, self).setUp()
self.load_backends()
self.api = auth.controllers.Auth()
def config_files(self):
@ -137,11 +133,6 @@ class TestMapped(unit.TestCase):
config_files.append(unit.dirs.tests_conf('test_auth_plugin.conf'))
return config_files
def auth_plugin_config_override(self, methods=None, **method_classes):
# Do not apply the auth plugin overrides so that the config file is
# tested
pass
def _test_mapped_invocation_with_method_name(self, method_name):
with mock.patch.object(auth.plugins.mapped.Mapped,
'authenticate',
@ -165,20 +156,24 @@ class TestMapped(unit.TestCase):
self.assertEqual(method_name, auth_payload['protocol'])
def test_mapped_with_remote_user(self):
# external plugin should fail and pass to mapped plugin
method_name = 'saml2'
auth_data = {'methods': [method_name]}
# put the method name in the payload so its easier to correlate
# method name with payload
auth_data[method_name] = {'protocol': method_name}
auth_data = {'identity': auth_data}
auth_context = {'extras': {},
'method_names': [],
'user_id': uuid.uuid4().hex}
self.useFixture(auth_plugins.LoadAuthPlugins(method_name))
with mock.patch.object(auth.plugins.mapped.Mapped,
'authenticate',
return_value=None) as authenticate:
# external plugin should fail and pass to mapped plugin
method_name = 'saml2'
auth_data = {'methods': [method_name]}
# put the method name in the payload so its easier to correlate
# method name with payload
auth_data[method_name] = {'protocol': method_name}
auth_data = {'identity': auth_data}
auth_info = auth.controllers.AuthInfo.create(None, auth_data)
auth_context = {'extras': {},
'method_names': [],
'user_id': uuid.uuid4().hex}
request = self.make_request(environ={'REMOTE_USER': 'foo@idp.com'})
self.api.authenticate(request, auth_info, auth_context)
# make sure Mapped plugin got invoked with the correct payload
@ -187,5 +182,7 @@ class TestMapped(unit.TestCase):
self.assertEqual(method_name, auth_payload['protocol'])
def test_supporting_multiple_methods(self):
for method_name in ['saml2', 'openid', 'x509']:
method_names = ('saml2', 'openid', 'x509')
self.useFixture(auth_plugins.LoadAuthPlugins(*method_names))
for method_name in method_names:
self._test_mapped_invocation_with_method_name(method_name)