diff options
author | Pádraig Brady <pbrady@redhat.com> | 2012-05-23 23:52:49 +0100 |
---|---|---|
committer | Pádraig Brady <pbrady@redhat.com> | 2012-06-07 16:46:21 +0100 |
commit | b71fb3de8965fe9a461604cd3cbb14f95c284940 (patch) | |
tree | b95cc520687e3ca98e411b86d82b454a7c740925 /keystone/test.py | |
parent | 987bc6932625d11f62584e6e1a8570a4d61c872e (diff) | |
download | keystone-b71fb3de8965fe9a461604cd3cbb14f95c284940.tar.gz keystone-b71fb3de8965fe9a461604cd3cbb14f95c284940.tar.xz keystone-b71fb3de8965fe9a461604cd3cbb14f95c284940.zip |
fix importing of optional modules in auth_token
* keystone/middleware/auth_token.py: Catch the
correct exception so that the memcache and iso8601
modules can be optional as intended.
* tests/test_auth_token_middleware.py: Test
the ImportError path
* keystone/test.py: Add a new mixin class to
support disabling importing of a module.
Bug: 1003715
Change-Id: I87cc2f3bc79b17a52ea672bac7e0ebcf9e1fce57
Diffstat (limited to 'keystone/test.py')
-rw-r--r-- | keystone/test.py | 45 |
1 files changed, 39 insertions, 6 deletions
diff --git a/keystone/test.py b/keystone/test.py index f968b4bb..54dc01b0 100644 --- a/keystone/test.py +++ b/keystone/test.py @@ -117,7 +117,45 @@ class TestClient(object): return self.request('PUT', path=path, headers=headers, body=body) -class TestCase(unittest.TestCase): +class NoModule(object): + """A mixin class to provide support for unloading/disabling modules.""" + + def __init__(self, *args, **kw): + super(NoModule, self).__init__(*args, **kw) + self._finders = [] + self._cleared_modules = {} + + def tearDown(self): + super(NoModule, self).tearDown() + for finder in self._finders: + sys.meta_path.remove(finder) + sys.modules.update(self._cleared_modules) + + def clear_module(self, module): + cleared_modules = {} + for fullname in sys.modules.keys(): + if fullname == module or fullname.startswith(module + '.'): + cleared_modules[fullname] = sys.modules.pop(fullname) + return cleared_modules + + def disable_module(self, module): + """Ensure ImportError for the specified module.""" + + # Clear 'module' references in sys.modules + self._cleared_modules.update(self.clear_module(module)) + + # Disallow further imports of 'module' + class NoModule(object): + def find_module(self, fullname, path): + if fullname == module or fullname.startswith(module + '.'): + raise ImportError + + finder = NoModule() + self._finders.append(finder) + sys.meta_path.insert(0, finder) + + +class TestCase(NoModule, unittest.TestCase): def __init__(self, *args, **kw): super(TestCase, self).__init__(*args, **kw) self._paths = [] @@ -243,8 +281,3 @@ class TestCase(unittest.TestCase): def add_path(self, path): sys.path.insert(0, path) self._paths.append(path) - - def clear_module(self, module): - for x in sys.modules.keys(): - if x.startswith(module): - del sys.modules[x] |