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
This commit is contained in:
parent
987bc69326
commit
b71fb3de89
@ -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):
|
||||
|
@ -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]
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user