Files
python-keystoneclient/keystoneclient/middleware/test.py
Dolph Mathews 9196be6664 Remove test dep on name of dir (bug 1124283)
The name of the project's directory was previously hardcoded into the
tests, so tests would fail if executed from another directory (such as
"python-keystoneclient-master", as checked-out by keystone for
integration testing).

Also, the tests should now be executable on Windows.

Change-Id: I0a1e052054e509b0f795fd13f95a804e0c255907
2013-03-04 11:19:14 -06:00

68 lines
2.0 KiB
Python

# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2012 OpenStack LLC
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Test support for middleware authentication
#
import os
import sys
ROOTDIR = os.path.abspath(os.curdir)
def rootdir(*p):
return os.path.join(ROOTDIR, *p)
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)