From 19081b834991d263d84c761dcf422a8c9faf40a1 Mon Sep 17 00:00:00 2001 From: Brant Knudson Date: Wed, 31 Jul 2013 12:01:49 -0500 Subject: Clear out the dependency registry between tests As part of the process during several tests setUp where the backends are reloaded, automatic dependency injection takes place. The REGISTRY is being updated with new providers and it's also looking up the required dependencies. Some of the providers for the requirements may not have been updated yet with the new provider object, so it loads an object that was created from a previous test run rather than the current one. This can cause tests to fail when one class gets a ref to the old one (it depends on the order that the tests are run). This change clears out the registry of providers before loading backends. It only affects testing. Part of fix for bug 1204605 Change-Id: Ib845493fa13531225e4be7e3b6cc315b9d19a0f4 --- keystone/common/dependency.py | 10 ++++++++++ keystone/test.py | 15 ++++++++++++++- tests/test_injection.py | 17 +++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/keystone/common/dependency.py b/keystone/common/dependency.py index dc3e4ac4..3ed261cc 100644 --- a/keystone/common/dependency.py +++ b/keystone/common/dependency.py @@ -65,3 +65,13 @@ def requires(*dependencies): return cls return wrapped + + +def reset(): + """Reset the registry of providers. + + This is useful for unit testing to ensure that tests don't use providers + from previous tests. + """ + + REGISTRY.clear() diff --git a/keystone/test.py b/keystone/test.py index 2c2be577..d06ea4c5 100644 --- a/keystone/test.py +++ b/keystone/test.py @@ -38,6 +38,7 @@ environment.use_eventlet() from keystone import assignment from keystone import catalog +from keystone.common import dependency from keystone.common import kvs from keystone.common import logging from keystone.common import sql @@ -51,6 +52,7 @@ from keystone import identity from keystone.openstack.common import timeutils from keystone import policy from keystone import token +from keystone.token import provider as token_provider from keystone import trust @@ -240,6 +242,11 @@ class TestCase(NoModule, unittest.TestCase): for path in self._paths: if path in sys.path: sys.path.remove(path) + + # Clear the registry of providers so that providers from previous + # tests aren't used. + dependency.reset() + kvs.INMEMDB.clear() CONF.reset() @@ -253,8 +260,14 @@ class TestCase(NoModule, unittest.TestCase): def load_backends(self): """Initializes each manager and assigns them to an attribute.""" + + # TODO(blk-u): Shouldn't need to clear the registry here, but some + # tests call load_backends multiple times. These should be fixed to + # only call load_backends once. + dependency.reset() + for manager in [assignment, catalog, credential, ec2, identity, policy, - token, trust]: + token, token_provider, trust]: manager_name = '%s_api' % manager.__name__.split('.')[-1] setattr(self, manager_name, manager.Manager()) diff --git a/tests/test_injection.py b/tests/test_injection.py index 4b6fc8ba..08ccd7c7 100644 --- a/tests/test_injection.py +++ b/tests/test_injection.py @@ -165,3 +165,20 @@ class TestDependencyInjection(unittest.TestCase): with self.assertRaises(dependency.UnresolvableDependencyException): Consumer() + + def test_reset(self): + # Can reset the registry of providers. + + p_id = uuid.uuid4().hex + + @dependency.provider(p_id) + class P(object): + pass + + p_inst = P() + + self.assertIs(dependency.REGISTRY[p_id], p_inst) + + dependency.reset() + + self.assertFalse(dependency.REGISTRY) -- cgit