diff options
Diffstat (limited to 'keystone-moon/keystone/tests/unit/ksfixtures')
8 files changed, 240 insertions, 106 deletions
diff --git a/keystone-moon/keystone/tests/unit/ksfixtures/__init__.py b/keystone-moon/keystone/tests/unit/ksfixtures/__init__.py index 81b80298..4b914752 100644 --- a/keystone-moon/keystone/tests/unit/ksfixtures/__init__.py +++ b/keystone-moon/keystone/tests/unit/ksfixtures/__init__.py @@ -11,5 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +from keystone.tests.unit.ksfixtures.auth_plugins import ConfigAuthPlugins # noqa from keystone.tests.unit.ksfixtures.cache import Cache # noqa from keystone.tests.unit.ksfixtures.key_repository import KeyRepository # noqa +from keystone.tests.unit.ksfixtures.policy import Policy # noqa diff --git a/keystone-moon/keystone/tests/unit/ksfixtures/appserver.py b/keystone-moon/keystone/tests/unit/ksfixtures/appserver.py index ea1e6255..a23b804f 100644 --- a/keystone-moon/keystone/tests/unit/ksfixtures/appserver.py +++ b/keystone-moon/keystone/tests/unit/ksfixtures/appserver.py @@ -29,8 +29,7 @@ ADMIN = 'admin' class AppServer(fixtures.Fixture): - """A fixture for managing an application server instance. - """ + """A fixture for managing an application server instance.""" def __init__(self, config, name, cert=None, key=None, ca=None, cert_required=False, host='127.0.0.1', port=0): @@ -72,7 +71,8 @@ class AppServer(fixtures.Fixture): def _update_config_opt(self): """Updates the config with the actual port used.""" opt_name = self._get_config_option_for_section_name() - CONF.set_override(opt_name, self.port, group='eventlet_server') + CONF.set_override(opt_name, self.port, group='eventlet_server', + enforce_type=True) def _get_config_option_for_section_name(self): """Maps Paster config section names to port option names.""" diff --git a/keystone-moon/keystone/tests/unit/ksfixtures/auth_plugins.py b/keystone-moon/keystone/tests/unit/ksfixtures/auth_plugins.py new file mode 100644 index 00000000..68ba6f3a --- /dev/null +++ b/keystone-moon/keystone/tests/unit/ksfixtures/auth_plugins.py @@ -0,0 +1,34 @@ +# 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. + +import fixtures + +from keystone.common import config as common_cfg + + +class ConfigAuthPlugins(fixtures.Fixture): + """A fixture for setting up and tearing down a auth plugins.""" + + def __init__(self, config_fixture, methods, **method_classes): + super(ConfigAuthPlugins, self).__init__() + self.methods = methods + self.config_fixture = config_fixture + self.method_classes = method_classes + + def setUp(self): + super(ConfigAuthPlugins, self).setUp() + if self.methods: + self.config_fixture.config(group='auth', methods=self.methods) + common_cfg.setup_authentication() + if self.method_classes: + self.config_fixture.config(group='auth', **self.method_classes) diff --git a/keystone-moon/keystone/tests/unit/ksfixtures/cache.py b/keystone-moon/keystone/tests/unit/ksfixtures/cache.py index 74566f1e..e0833ae2 100644 --- a/keystone-moon/keystone/tests/unit/ksfixtures/cache.py +++ b/keystone-moon/keystone/tests/unit/ksfixtures/cache.py @@ -13,11 +13,17 @@ import fixtures +from keystone import catalog from keystone.common import cache +CACHE_REGIONS = (cache.CACHE_REGION, catalog.COMPUTED_CATALOG_REGION) + + class Cache(fixtures.Fixture): - """A fixture for setting up and tearing down the cache between test cases. + """A fixture for setting up the cache between test cases. + + This will also tear down an existing cache if one is already configured. """ def setUp(self): @@ -29,8 +35,9 @@ class Cache(fixtures.Fixture): # NOTE(morganfainberg): The only way to reconfigure the CacheRegion # object on each setUp() call is to remove the .backend property. - if cache.REGION.is_configured: - del cache.REGION.backend + for region in CACHE_REGIONS: + if region.is_configured: + del region.backend - # ensure the cache region instance is setup - cache.configure_cache_region(cache.REGION) + # ensure the cache region instance is setup + cache.configure_cache(region=region) diff --git a/keystone-moon/keystone/tests/unit/ksfixtures/database.py b/keystone-moon/keystone/tests/unit/ksfixtures/database.py index 6f23a99d..52c35cee 100644 --- a/keystone-moon/keystone/tests/unit/ksfixtures/database.py +++ b/keystone-moon/keystone/tests/unit/ksfixtures/database.py @@ -28,12 +28,13 @@ CONF = cfg.CONF def run_once(f): """A decorator to ensure the decorated function is only executed once. - The decorated function cannot expect any arguments. + The decorated function is assumed to have a one parameter. + """ @functools.wraps(f) - def wrapper(): + def wrapper(one): if not wrapper.already_ran: - f() + f(one) wrapper.already_ran = True wrapper.already_ran = False return wrapper @@ -51,7 +52,7 @@ def initialize_sql_session(): @run_once -def _load_sqlalchemy_models(): +def _load_sqlalchemy_models(version_specifiers): """Find all modules containing SQLAlchemy models and import them. This creates more consistent, deterministic test runs because tables @@ -66,6 +67,24 @@ def _load_sqlalchemy_models(): as more models are imported. Importing all models at the start of the test run avoids this problem. + version_specifiers is a dict that contains any specific driver versions + that have been requested. The dict is of the form: + + {<module_name> : {'versioned_backend' : <name of backend requested>, + 'versionless_backend' : <name of default backend>} + } + + For example: + + {'keystone.assignment': {'versioned_backend' : 'V8_backends', + 'versionless_backend' : 'backends'}, + 'keystone.identity': {'versioned_backend' : 'V9_backends', + 'versionless_backend' : 'backends'} + } + + The version_specifiers will be used to load the correct driver. The + algorithm for this assumes that versioned drivers begin in 'V'. + """ keystone_root = os.path.normpath(os.path.join( os.path.dirname(__file__), '..', '..', '..')) @@ -78,25 +97,59 @@ def _load_sqlalchemy_models(): # The root will be prefixed with an instance of os.sep, which will # make the root after replacement '.<root>', the 'keystone' part # of the module path is always added to the front - module_name = ('keystone.%s.sql' % + module_root = ('keystone.%s' % root.replace(os.sep, '.').lstrip('.')) + module_components = module_root.split('.') + module_without_backends = '' + for x in range(0, len(module_components) - 1): + module_without_backends += module_components[x] + '.' + module_without_backends = module_without_backends.rstrip('.') + this_backend = module_components[len(module_components) - 1] + + # At this point module_without_backends might be something like + # 'keystone.assignment', while this_backend might be something + # 'V8_backends'. + + if module_without_backends.startswith('keystone.contrib'): + # All the sql modules have now been moved into the core tree + # so no point in loading these again here (and, in fact, doing + # so might break trying to load a versioned driver. + continue + + if module_without_backends in version_specifiers: + # OK, so there is a request for a specific version of this one. + # We therefore should skip any other versioned backend as well + # as the non-versioned one. + version = version_specifiers[module_without_backends] + if ((this_backend != version['versioned_backend'] and + this_backend.startswith('V')) or + this_backend == version['versionless_backend']): + continue + else: + # No versioned driver requested, so ignore any that are + # versioned + if this_backend.startswith('V'): + continue + + module_name = module_root + '.sql' __import__(module_name) class Database(fixtures.Fixture): - """A fixture for setting up and tearing down a database. - - """ + """A fixture for setting up and tearing down a database.""" - def __init__(self): + def __init__(self, version_specifiers=None): super(Database, self).__init__() initialize_sql_session() - _load_sqlalchemy_models() + if version_specifiers is None: + version_specifiers = {} + _load_sqlalchemy_models(version_specifiers) def setUp(self): super(Database, self).setUp() - self.engine = sql.get_engine() + with sql.session_for_write() as session: + self.engine = session.get_bind() self.addCleanup(sql.cleanup) sql.ModelBase.metadata.create_all(bind=self.engine) self.addCleanup(sql.ModelBase.metadata.drop_all, bind=self.engine) diff --git a/keystone-moon/keystone/tests/unit/ksfixtures/hacking.py b/keystone-moon/keystone/tests/unit/ksfixtures/hacking.py index 918087ad..9977b206 100644 --- a/keystone-moon/keystone/tests/unit/ksfixtures/hacking.py +++ b/keystone-moon/keystone/tests/unit/ksfixtures/hacking.py @@ -112,73 +112,6 @@ class HackingCode(fixtures.Fixture): (8, 8, 'K004'), ]} - assert_no_translations_for_debug_logging = { - 'code': """ - import logging - import logging as stlib_logging - from keystone.i18n import _ - from keystone.i18n import _ as oslo_i18n - from oslo_log import log - from oslo_log import log as oslo_logging - - # stdlib logging - L0 = logging.getLogger() - L0.debug(_('text')) - class C: - def __init__(self): - L0.debug(oslo_i18n('text', {})) - - # stdlib logging w/ alias and specifying a logger - class C: - def __init__(self): - self.L1 = logging.getLogger(__name__) - def m(self): - self.L1.debug( - _('text'), {} - ) - - # oslo logging and specifying a logger - L2 = logging.getLogger(__name__) - L2.debug(oslo_i18n('text')) - - # oslo logging w/ alias - class C: - def __init__(self): - self.L3 = oslo_logging.getLogger() - self.L3.debug(_('text')) - - # translation on a separate line - msg = _('text') - L2.debug(msg) - - # this should not fail - if True: - msg = _('message %s') % X - L2.error(msg) - raise TypeError(msg) - if True: - msg = 'message' - L2.debug(msg) - - # this should not fail - if True: - if True: - msg = _('message') - else: - msg = _('message') - L2.debug(msg) - raise Exception(msg) - """, - 'expected_errors': [ - (10, 9, 'K005'), - (13, 17, 'K005'), - (21, 12, 'K005'), - (26, 9, 'K005'), - (32, 22, 'K005'), - (36, 9, 'K005'), - ] - } - dict_constructor = { 'code': """ lower_res = {k.lower(): v for k, v in six.iteritems(res[1])} @@ -219,12 +152,12 @@ class HackingLogging(fixtures.Fixture): LOG.info(_('text')) class C: def __init__(self): - LOG.warn(oslo_i18n('text', {})) - LOG.warn(_LW('text', {})) + LOG.warning(oslo_i18n('text', {})) + LOG.warning(_LW('text', {})) """, 'expected_errors': [ (3, 9, 'K006'), - (6, 17, 'K006'), + (6, 20, 'K006'), ], }, { @@ -287,13 +220,13 @@ class HackingLogging(fixtures.Fixture): LOG = logging.getLogger() # ensure the correct helper is being used - LOG.warn(_LI('this should cause an error')) + LOG.warning(_LI('this should cause an error')) # debug should not allow any helpers either LOG.debug(_LI('this should cause an error')) """, 'expected_errors': [ - (4, 9, 'K006'), + (4, 12, 'K006'), (7, 10, 'K005'), ], }, @@ -302,7 +235,7 @@ class HackingLogging(fixtures.Fixture): # this should not be an error L = log.getLogger(__name__) msg = _('text') - L.warn(msg) + L.warning(msg) raise Exception(msg) """, 'expected_errors': [], @@ -312,7 +245,7 @@ class HackingLogging(fixtures.Fixture): L = log.getLogger(__name__) def f(): msg = _('text') - L2.warn(msg) + L2.warning(msg) something = True # add an extra statement here raise Exception(msg) """, @@ -323,11 +256,11 @@ class HackingLogging(fixtures.Fixture): LOG = log.getLogger(__name__) def func(): msg = _('text') - LOG.warn(msg) + LOG.warning(msg) raise Exception('some other message') """, 'expected_errors': [ - (4, 13, 'K006'), + (4, 16, 'K006'), ], }, { @@ -337,7 +270,7 @@ class HackingLogging(fixtures.Fixture): msg = _('text') else: msg = _('text') - LOG.warn(msg) + LOG.warning(msg) raise Exception(msg) """, 'expected_errors': [ @@ -350,28 +283,28 @@ class HackingLogging(fixtures.Fixture): msg = _('text') else: msg = _('text') - LOG.warn(msg) + LOG.warning(msg) """, 'expected_errors': [ - (6, 9, 'K006'), + (6, 12, 'K006'), ], }, { 'code': """ LOG = log.getLogger(__name__) msg = _LW('text') - LOG.warn(msg) + LOG.warning(msg) raise Exception(msg) """, 'expected_errors': [ - (3, 9, 'K007'), + (3, 12, 'K007'), ], }, { 'code': """ LOG = log.getLogger(__name__) msg = _LW('text') - LOG.warn(msg) + LOG.warning(msg) msg = _('something else') raise Exception(msg) """, @@ -381,18 +314,18 @@ class HackingLogging(fixtures.Fixture): 'code': """ LOG = log.getLogger(__name__) msg = _LW('hello %s') % 'world' - LOG.warn(msg) + LOG.warning(msg) raise Exception(msg) """, 'expected_errors': [ - (3, 9, 'K007'), + (3, 12, 'K007'), ], }, { 'code': """ LOG = log.getLogger(__name__) msg = _LW('hello %s') % 'world' - LOG.warn(msg) + LOG.warning(msg) """, 'expected_errors': [], }, @@ -409,3 +342,76 @@ class HackingLogging(fixtures.Fixture): 'expected_errors': [], }, ] + + assert_not_using_deprecated_warn = { + 'code': """ + # Logger.warn has been deprecated in Python3 in favor of + # Logger.warning + LOG = log.getLogger(__name__) + LOG.warn(_LW('text')) + """, + 'expected_errors': [ + (4, 9, 'K009'), + ], + } + + assert_no_translations_for_debug_logging = { + 'code': """ + # stdlib logging + L0 = logging.getLogger() + L0.debug(_('text')) + class C: + def __init__(self): + L0.debug(oslo_i18n('text', {})) + + # stdlib logging w/ alias and specifying a logger + class C: + def __init__(self): + self.L1 = logging.getLogger(__name__) + def m(self): + self.L1.debug( + _('text'), {} + ) + + # oslo logging and specifying a logger + L2 = logging.getLogger(__name__) + L2.debug(oslo_i18n('text')) + + # oslo logging w/ alias + class C: + def __init__(self): + self.L3 = oslo_logging.getLogger() + self.L3.debug(_('text')) + + # translation on a separate line + msg = _('text') + L2.debug(msg) + + # this should not fail + if True: + msg = _('message %s') % X + L2.error(msg) + raise TypeError(msg) + if True: + msg = 'message' + L2.debug(msg) + + # this should not fail + if True: + if True: + msg = _('message') + else: + msg = _('message') + L2.debug(msg) + raise Exception(msg) + """, + 'expected_errors': [ + (3, 9, 'K005'), + (6, 17, 'K005'), + (14, 12, 'K005'), + (19, 9, 'K005'), + (25, 22, 'K005'), + (29, 9, 'K005'), + ] + } + diff --git a/keystone-moon/keystone/tests/unit/ksfixtures/ldapdb.py b/keystone-moon/keystone/tests/unit/ksfixtures/ldapdb.py index b2cbe067..6cd8cc0b 100644 --- a/keystone-moon/keystone/tests/unit/ksfixtures/ldapdb.py +++ b/keystone-moon/keystone/tests/unit/ksfixtures/ldapdb.py @@ -19,8 +19,7 @@ from keystone.tests.unit import fakeldap class LDAPDatabase(fixtures.Fixture): - """A fixture for setting up and tearing down an LDAP database. - """ + """A fixture for setting up and tearing down an LDAP database.""" def setUp(self): super(LDAPDatabase, self).setUp() diff --git a/keystone-moon/keystone/tests/unit/ksfixtures/policy.py b/keystone-moon/keystone/tests/unit/ksfixtures/policy.py new file mode 100644 index 00000000..b883f980 --- /dev/null +++ b/keystone-moon/keystone/tests/unit/ksfixtures/policy.py @@ -0,0 +1,33 @@ +# 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. + +import fixtures +from oslo_policy import opts + +from keystone.policy.backends import rules + + +class Policy(fixtures.Fixture): + """A fixture for working with policy configuration.""" + + def __init__(self, policy_file, config_fixture): + self._policy_file = policy_file + self._config_fixture = config_fixture + + def setUp(self): + super(Policy, self).setUp() + opts.set_defaults(self._config_fixture.conf) + self._config_fixture.config(group='oslo_policy', + policy_file=self._policy_file) + rules.init() + self.addCleanup(rules.reset) |