From 3411d7200fcd08e1972b8283005100be754fe623 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Tue, 23 Jun 2015 21:50:54 -0400 Subject: [PATCH] Enable all commented out tests The first attempt at getting all the tests to work missed out the config_overrides in the base test class and the 'role' and 'assignment' groups. Also, sha1_mangle_key was added to encode the key before calling dogpile's sha1_mangle_key to support all the python34 tests. Removed the skipIf's added for py34 as the tests are now working. Also removed the testtools from requirements as we don't need it anymore. Change-Id: I0e09429e6739dc946f6e15398ccb264b4d62bb42 --- oslo_cache/_opts.py | 23 +++++ oslo_cache/core.py | 11 ++- oslo_cache/tests/test_cache.py | 174 ++++++++++++++++----------------- test-requirements.txt | 1 - 4 files changed, 119 insertions(+), 90 deletions(-) diff --git a/oslo_cache/_opts.py b/oslo_cache/_opts.py index 677a4776..2dffc9a4 100644 --- a/oslo_cache/_opts.py +++ b/oslo_cache/_opts.py @@ -92,6 +92,29 @@ FILE_OPTIONS = { help='Number of seconds that an operation will wait to get ' 'a memcache client connection.'), ], + 'role': [ + # The role driver has no default for backward compatibility reasons. + # If role driver is not specified, the assignment driver chooses + # the backend + cfg.StrOpt('driver', + help='Role backend driver.'), + cfg.BoolOpt('caching', default=True, + help='Toggle for role caching. This has no effect ' + 'unless global caching is enabled.'), + cfg.IntOpt('cache_time', + help='TTL (in seconds) to cache role data. This has ' + 'no effect unless global caching is enabled.'), + cfg.IntOpt('list_limit', + help='Maximum number of entities that will be returned ' + 'in a role collection.'), + ], + 'assignment': [ + # assignment has no default for backward compatibility reasons. + # If assignment driver is not specified, the identity driver chooses + # the backend + cfg.StrOpt('driver', + help='Assignment backend driver.'), + ], } diff --git a/oslo_cache/core.py b/oslo_cache/core.py index 830ed63c..cfe9796b 100644 --- a/oslo_cache/core.py +++ b/oslo_cache/core.py @@ -23,6 +23,7 @@ from oslo_utils import importutils from oslo_cache import exception from oslo_cache._i18n import _, _LE +from oslo_cache import _opts CONF = cfg.CONF @@ -119,6 +120,10 @@ def build_cache_config(): return conf_dict +def sha1_mangle_key(key): + return util.sha1_mangle_key(key.encode('UTF-8')) + + def configure_cache_region(region): """Configure a cache region. @@ -149,7 +154,7 @@ def configure_cache_region(region): # mangler provided by dogpile.cache. This ensures we always use a fixed # size cache-key. if region.key_mangler is None: - region.key_mangler = util.sha1_mangle_key + region.key_mangler = sha1_mangle_key for class_path in CONF.cache.proxies: # NOTE(morganfainberg): if we have any proxy wrappers, we should @@ -306,3 +311,7 @@ def get_memoization_decorator(section, expiration_section=None): memoize.get_expiration_time = expiration_time return memoize + + +def configure(conf): + _opts.configure(conf) diff --git a/oslo_cache/tests/test_cache.py b/oslo_cache/tests/test_cache.py index a717a28a..34cd353f 100644 --- a/oslo_cache/tests/test_cache.py +++ b/oslo_cache/tests/test_cache.py @@ -21,8 +21,6 @@ from dogpile.cache import proxy import mock from oslo_config import cfg from oslotest import base -import six -import testtools from oslo_cache import core as cache from oslo_cache import exception @@ -42,6 +40,17 @@ class BaseTestCase(base.BaseTestCase): self.config_fixture = self.useFixture(config_fixture.Config(CONF)) self.addCleanup(delattr, self, 'config_fixture') + self.config_overrides() + + def config_overrides(self): + self.config_fixture.config( + # TODO(morganfainberg): Make Cache Testing a separate test case + # in tempest, and move it out of the base unit tests. + group='cache', + backend='dogpile.cache.memory', + enabled=True, + proxies=['oslo_cache.tests.test_cache.CacheIsolatingProxy']) + def _copy_value(value): if value is not NO_VALUE: @@ -111,13 +120,12 @@ class CacheRegionTest(BaseTestCase): return cacheable_function - # FIXME(dims) : Need to resurrect this test ported from keystone - # def test_region_built_with_proxy_direct_cache_test(self): - # # Verify cache regions are properly built with proxies. - # test_value = TestProxyValue('Direct Cache Test') - # self.region.set('cache_test', test_value) - # cached_value = self.region.get('cache_test') - # self.assertTrue(cached_value.cached) + def test_region_built_with_proxy_direct_cache_test(self): + # Verify cache regions are properly built with proxies. + test_value = TestProxyValue('Direct Cache Test') + self.region.set('cache_test', test_value) + cached_value = self.region.get('cache_test') + self.assertTrue(cached_value.cached) def test_cache_region_no_error_multiple_config(self): # Verify configuring the CacheRegion again doesn't error. @@ -167,48 +175,44 @@ class CacheRegionTest(BaseTestCase): return _do_test - # FIXME(dims) : Need to resurrect this test ported from keystone - # def test_cache_no_fallthrough_expiration_time_fn(self): - # # Since we do not re-configure the cache region, for ease of testing - # # this value is set the same as the expiration_time default in the - # # [cache] section - # cache_time = 600 - # expiration_time = cache.get_expiration_time_fn('role') - # do_test = self._get_cache_fallthrough_fn(cache_time) - # # Run the test with the assignment cache_time value - # self.config_fixture.config(cache_time=cache_time, - # group='role') - # test_value = TestProxyValue(uuid.uuid4().hex) - # self.assertEqual(cache_time, expiration_time()) - # do_test(value=test_value) + def test_cache_no_fallthrough_expiration_time_fn(self): + # Since we do not re-configure the cache region, for ease of testing + # this value is set the same as the expiration_time default in the + # [cache] section + cache_time = 600 + expiration_time = cache.get_expiration_time_fn('role') + do_test = self._get_cache_fallthrough_fn(cache_time) + # Run the test with the assignment cache_time value + self.config_fixture.config(cache_time=cache_time, + group='role') + test_value = TestProxyValue(uuid.uuid4().hex) + self.assertEqual(cache_time, expiration_time()) + do_test(value=test_value) - # FIXME(dims) : Need to resurrect this test ported from keystone - # def test_cache_fallthrough_expiration_time_fn(self): - # # Since we do not re-configure the cache region, for ease of testing - # # this value is set the same as the expiration_time default in the - # # [cache] section - # cache_time = 599 - # expiration_time = cache.get_expiration_time_fn('role') - # do_test = self._get_cache_fallthrough_fn(cache_time) - # # Run the test with the assignment cache_time value set to None and - # # the global value set. - # self.config_fixture.config(cache_time=None, group='role') - # test_value = TestProxyValue(uuid.uuid4().hex) - # self.assertIsNone(expiration_time()) - # do_test(value=test_value) + def test_cache_fallthrough_expiration_time_fn(self): + # Since we do not re-configure the cache region, for ease of testing + # this value is set the same as the expiration_time default in the + # [cache] section + cache_time = 599 + expiration_time = cache.get_expiration_time_fn('role') + do_test = self._get_cache_fallthrough_fn(cache_time) + # Run the test with the assignment cache_time value set to None and + # the global value set. + self.config_fixture.config(cache_time=None, group='role') + test_value = TestProxyValue(uuid.uuid4().hex) + self.assertIsNone(expiration_time()) + do_test(value=test_value) - # FIXME(dims) : Need to resurrect this test ported from keystone - # def test_should_cache_fn_global_cache_enabled(self): - # # Verify should_cache_fn generates a sane function for subsystem and - # # functions as expected with caching globally enabled. - # cacheable_function = self._get_cacheable_function() - # - # self.config_fixture.config(group='cache', enabled=True) - # cacheable_function(self.test_value) - # cached_value = cacheable_function(self.test_value) - # self.assertTrue(cached_value.cached) + def test_should_cache_fn_global_cache_enabled(self): + # Verify should_cache_fn generates a sane function for subsystem and + # functions as expected with caching globally enabled. + cacheable_function = self._get_cacheable_function() + + self.config_fixture.config(group='cache', enabled=True) + cacheable_function(self.test_value) + cached_value = cacheable_function(self.test_value) + self.assertTrue(cached_value.cached) - @testtools.skipIf(six.PY3, 'FIXME: this test does not work python 3.x') def test_should_cache_fn_global_cache_disabled(self): # Verify should_cache_fn generates a sane function for subsystem and # functions as expected with caching globally disabled. @@ -219,7 +223,6 @@ class CacheRegionTest(BaseTestCase): cached_value = cacheable_function(self.test_value) self.assertFalse(cached_value.cached) - @testtools.skipIf(six.PY3, 'FIXME: this test does not work python 3.x') def test_should_cache_fn_global_cache_disabled_section_cache_enabled(self): # Verify should_cache_fn generates a sane function for subsystem and # functions as expected with caching globally disabled and the specific @@ -234,7 +237,6 @@ class CacheRegionTest(BaseTestCase): cached_value = cacheable_function(self.test_value) self.assertFalse(cached_value.cached) - @testtools.skipIf(six.PY3, 'FIXME: this test does not work python 3.x') def test_should_cache_fn_global_cache_enabled_section_cache_disabled(self): # Verify should_cache_fn generates a sane function for subsystem and # functions as expected with caching globally enabled and the specific @@ -249,21 +251,19 @@ class CacheRegionTest(BaseTestCase): cached_value = cacheable_function(self.test_value) self.assertFalse(cached_value.cached) - # FIXME(dims) : Need to resurrect this test ported from keystone - # def test_should_cache_fn_global_cache_enabled_section_cache_enabled( - # self): - # #Verify should_cache_fn generates a sane function for subsystem and - # #functions as expected with caching globally enabled and the specific - # #section caching enabled. - # cacheable_function = self._get_cacheable_function() - # - # self._add_test_caching_option() - # self.config_fixture.config(group='cache', enabled=True) - # self.config_fixture.config(group='cache', caching=True) - # - # cacheable_function(self.test_value) - # cached_value = cacheable_function(self.test_value) - # self.assertTrue(cached_value.cached) + def test_should_cache_fn_global_cache_enabled_section_cache_enabled(self): + # Verify should_cache_fn generates a sane function for subsystem and + # functions as expected with caching globally enabled and the specific + # section caching enabled. + cacheable_function = self._get_cacheable_function() + + self._add_test_caching_option() + self.config_fixture.config(group='cache', enabled=True) + self.config_fixture.config(group='cache', caching=True) + + cacheable_function(self.test_value) + cached_value = cacheable_function(self.test_value) + self.assertTrue(cached_value.cached) def test_cache_dictionary_config_builder(self): """Validate we build a sane dogpile.cache dictionary config.""" @@ -286,27 +286,26 @@ class CacheRegionTest(BaseTestCase): config_dict['test_prefix.arguments.arg2']) self.assertNotIn('test_prefix.arguments.arg3', config_dict) - # FIXME(dims) : Need to resurrect this test ported from keystone - # def test_cache_debug_proxy(self): - # single_value = 'Test Value' - # single_key = 'testkey' - # multi_values = {'key1': 1, 'key2': 2, 'key3': 3} - # - # self.region.set(single_key, single_value) - # self.assertEqual(single_value, self.region.get(single_key)) - # - # self.region.delete(single_key) - # self.assertEqual(NO_VALUE, self.region.get(single_key)) - # - # self.region.set_multi(multi_values) - # cached_values = self.region.get_multi(multi_values.keys()) - # for value in multi_values.values(): - # self.assertIn(value, cached_values) - # self.assertEqual(len(multi_values.values()), len(cached_values)) - # - # self.region.delete_multi(multi_values.keys()) - # for value in self.region.get_multi(multi_values.keys()): - # self.assertEqual(NO_VALUE, value) + def test_cache_debug_proxy(self): + single_value = 'Test Value' + single_key = 'testkey' + multi_values = {'key1': 1, 'key2': 2, 'key3': 3} + + self.region.set(single_key, single_value) + self.assertEqual(single_value, self.region.get(single_key)) + + self.region.delete(single_key) + self.assertEqual(NO_VALUE, self.region.get(single_key)) + + self.region.set_multi(multi_values) + cached_values = self.region.get_multi(multi_values.keys()) + for value in multi_values.values(): + self.assertIn(value, cached_values) + self.assertEqual(len(multi_values.values()), len(cached_values)) + + self.region.delete_multi(multi_values.keys()) + for value in self.region.get_multi(multi_values.keys()): + self.assertEqual(NO_VALUE, value) def test_configure_non_region_object_raises_error(self): self.assertRaises(exception.ValidationError, @@ -324,9 +323,8 @@ class CacheNoopBackendTest(BaseTestCase): def config_overrides(self): super(CacheNoopBackendTest, self).config_overrides() self.config_fixture.config(group='cache', - backend='oslo_cache.cache.noop') + backend='oslo_cache.noop') - @testtools.skipIf(six.PY3, 'FIXME: this test does not work python 3.x') def test_noop_backend(self): single_value = 'Test Value' single_key = 'testkey' diff --git a/test-requirements.txt b/test-requirements.txt index 489c0faf..d8ef9cc6 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -5,7 +5,6 @@ hacking<0.11,>=0.10.0 oslotest>=1.5.1 # Apache-2.0 oslosphinx>=2.5.0 # Apache-2.0 sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2 -testtools>=1.4.0 # Optional dogpile backend: MongoDB pymongo>=3.0.2