summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPádraig Brady <pbrady@redhat.com>2012-05-23 23:52:49 +0100
committerPádraig Brady <pbrady@redhat.com>2012-06-07 16:46:21 +0100
commitb71fb3de8965fe9a461604cd3cbb14f95c284940 (patch)
treeb95cc520687e3ca98e411b86d82b454a7c740925
parent987bc6932625d11f62584e6e1a8570a4d61c872e (diff)
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
-rw-r--r--keystone/middleware/auth_token.py2
-rw-r--r--keystone/test.py45
-rw-r--r--tests/test_auth_token_middleware.py13
3 files changed, 53 insertions, 7 deletions
diff --git a/keystone/middleware/auth_token.py b/keystone/middleware/auth_token.py
index a6f2af66..1551883d 100644
--- a/keystone/middleware/auth_token.py
+++ b/keystone/middleware/auth_token.py
@@ -161,7 +161,7 @@ class AuthProtocol(object):
LOG.info('Using memcache for caching token')
self._cache = memcache.Client(memcache_servers.split(','))
self._iso8601 = iso8601
- except NameError as e:
+ except ImportError as e:
LOG.warn('disabled caching due to missing libraries %s', e)
def __call__(self, env, start_response):
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]
diff --git a/tests/test_auth_token_middleware.py b/tests/test_auth_token_middleware.py
index 3ba1c57b..ccf7bba7 100644
--- a/tests/test_auth_token_middleware.py
+++ b/tests/test_auth_token_middleware.py
@@ -313,6 +313,19 @@ class AuthTokenMiddlewareTest(BaseAuthTokenMiddlewareTest):
self.middleware(req.environ, self.start_fake_response)
self.assertEqual(len(self.middleware._cache.set_value), 2)
+ def test_nomemcache(self):
+ self.disable_module('memcache')
+
+ conf = {
+ 'admin_token': 'admin_token1',
+ 'auth_host': 'keystone.example.com',
+ 'auth_port': 1234,
+ 'memcache_servers': 'localhost:11211',
+ }
+
+ auth_token.AuthProtocol(FakeApp(), conf)
+
+
if __name__ == '__main__':
import unittest
unittest.main()