def test_cache_pymemcache_retry_with_extra_opts(self): """Validate we build a valid config for the retry client.""" self.config_fixture.config(group='cache', enabled=True, config_prefix='test_prefix', backend='dogpile.cache.pymemcache', enable_retry_client=True, retry_attempts=42, retry_delay=42, hashclient_retry_attempts=100, hashclient_retry_delay=100, dead_timeout=100) config_dict = cache._build_cache_config(self.config_fixture.conf) self.assertTrue(self.config_fixture.conf.cache.enable_retry_client) self.assertEqual(config_dict['test_prefix.arguments.retry_attempts'], 42) self.assertEqual(config_dict['test_prefix.arguments.retry_delay'], 42) self.assertEqual( config_dict['test_prefix.arguments.hashclient_retry_attempts'], 100) self.assertEqual( config_dict['test_prefix.arguments.hashclient_retry_delay'], 100) self.assertEqual(config_dict['test_prefix.arguments.dead_timeout'], 100)
def test_cache_dictionary_config_builder_tls_enabled_with_config(self): """Validate the backend is reset to default if caching is disabled.""" self.config_fixture.config(group='cache', enabled=True, config_prefix='test_prefix', backend='dogpile.cache.pymemcache', tls_enabled=True, tls_cafile='path_to_ca_file', tls_keyfile='path_to_key_file', tls_certfile='path_to_cert_file', tls_allowed_ciphers='allowed_ciphers') fake_context = mock.Mock() with mock.patch.object(ssl, 'create_default_context', return_value=fake_context): config_dict = cache._build_cache_config(self.config_fixture.conf) self.assertTrue(self.config_fixture.conf.cache.tls_enabled) ssl.create_default_context.assert_called_with( cafile='path_to_ca_file', ) fake_context.load_cert_chain.assert_called_with( 'path_to_cert_file', 'path_to_key_file', ) fake_context.set_ciphers.assert_called_with('allowed_ciphers') self.assertEqual( fake_context, config_dict['test_prefix.arguments.tls_context'], )
def test_cache_dictionary_config_builder_global_disabled(self): """Validate the backend is reset to default if caching is disabled.""" self.config_fixture.config(group='cache', enabled=False, config_prefix='test_prefix', backend='some_test_backend') self.assertFalse(self.config_fixture.conf.cache.enabled) config_dict = cache._build_cache_config(self.config_fixture.conf) self.assertEqual(_opts._DEFAULT_BACKEND, config_dict['test_prefix.backend'])
def test_cache_dictionary_config_builder_global_disabled(self): """Validate the backend is reset to default if caching is disabled.""" self.config_fixture.config(group='cache', enabled=False, config_prefix='test_prefix', backend='some_test_backend') self.assertFalse(self.config_fixture.conf.cache.enabled) config_dict = cache._build_cache_config(self.config_fixture.conf) self.assertEqual( _opts._DEFAULT_BACKEND, config_dict['test_prefix.backend'])
def test_cache_dictionary_config_builder_flush_on_reconnect_disabled(self): """Validate we build a sane dogpile.cache dictionary config.""" self.config_fixture.config(group='cache', enabled=True, config_prefix='test_prefix', backend='oslo_cache.dict', memcache_pool_flush_on_reconnect=False) config_dict = cache._build_cache_config(self.config_fixture.conf) self.assertFalse( self.config_fixture.conf.cache.memcache_pool_flush_on_reconnect) self.assertFalse(config_dict['test_prefix.arguments' '.pool_flush_on_reconnect'])
def test_cache_pymemcache_retry_enabled(self): """Validate we build a dogpile.cache dict config with retry.""" self.config_fixture.config(group='cache', enabled=True, config_prefix='test_prefix', backend='dogpile.cache.pymemcache', enable_retry_client=True) config_dict = cache._build_cache_config(self.config_fixture.conf) opts = ['enable_retry_client', 'retry_attempts', 'retry_delay'] for el in opts: self.assertIn('test_prefix.arguments.{}'.format(el), config_dict)
def test_cache_pymemcache_socket_keepalive_enabled(self): """Validate we build a dogpile.cache dict config with keepalive.""" self.config_fixture.config(group='cache', enabled=True, config_prefix='test_prefix', backend='dogpile.cache.pymemcache', enable_socket_keepalive=True) config_dict = cache._build_cache_config(self.config_fixture.conf) self.assertTrue(self.config_fixture.conf.cache.enable_socket_keepalive) self.assertIsInstance( config_dict['test_prefix.arguments.socket_keepalive'], KeepaliveOpts)
def test_cache_pymemcache_socket_keepalive_disabled(self): """Validate we build a dogpile.cache dict config without keepalive.""" self.config_fixture.config(group='cache', enabled=True, config_prefix='test_prefix', backend='dogpile.cache.pymemcache', socket_keepalive_idle=2, socket_keepalive_interval=2, socket_keepalive_count=2) config_dict = cache._build_cache_config(self.config_fixture.conf) self.assertFalse( self.config_fixture.conf.cache.enable_socket_keepalive) self.assertNotIn('test_prefix.arguments.socket_keepalive', config_dict)
def test_cache_dictionary_config_builder_tls_disabled(self): """Validate the backend is reset to default if caching is disabled.""" self.config_fixture.config(group='cache', enabled=True, config_prefix='test_prefix', backend='dogpile.cache.pymemcache', tls_cafile='path_to_ca_file', tls_keyfile='path_to_key_file', tls_certfile='path_to_cert_file', tls_allowed_ciphers='allowed_ciphers') with mock.patch.object(ssl, 'create_default_context'): config_dict = cache._build_cache_config(self.config_fixture.conf) self.assertFalse(self.config_fixture.conf.cache.tls_enabled) ssl.create_default_context.assert_not_called() self.assertNotIn('test_prefix.arguments.tls_context', config_dict)
def test_cache_dictionary_config_builder(self): """Validate we build a sane dogpile.cache dictionary config.""" self.config_fixture.config( group='cache', config_prefix='test_prefix', backend='some_test_backend', expiration_time=86400, backend_argument=['arg1:test', 'arg2:test:test', 'arg3.invalid']) config_dict = cache._build_cache_config(self.config_fixture.conf) self.assertEqual(self.config_fixture.conf.cache.backend, config_dict['test_prefix.backend']) self.assertEqual(self.config_fixture.conf.cache.expiration_time, config_dict['test_prefix.expiration_time']) self.assertEqual('test', config_dict['test_prefix.arguments.arg1']) self.assertEqual('test:test', config_dict['test_prefix.arguments.arg2']) self.assertNotIn('test_prefix.arguments.arg3', config_dict)
def test_cache_pymemcache_retry_disabled(self): """Validate we build a config without the retry option when retry is disabled. """ self.config_fixture.config(group='cache', enabled=True, config_prefix='test_prefix', backend='dogpile.cache.pymemcache', retry_attempts=2, retry_delay=2) config_dict = cache._build_cache_config(self.config_fixture.conf) opts = ['enable_retry_client', 'retry_attempts', 'retry_delay'] for el in opts: self.assertNotIn('test_prefix.arguments.{}'.format(el), config_dict)
def test_cache_dictionary_config_builder(self): """Validate we build a sane dogpile.cache dictionary config.""" self.config_fixture.config(group='cache', config_prefix='test_prefix', backend='some_test_backend', expiration_time=86400, backend_argument=['arg1:test', 'arg2:test:test', 'arg3.invalid']) config_dict = cache._build_cache_config(self.config_fixture.conf) self.assertEqual( self.config_fixture.conf.cache.backend, config_dict['test_prefix.backend']) self.assertEqual( self.config_fixture.conf.cache.expiration_time, config_dict['test_prefix.expiration_time']) self.assertEqual('test', config_dict['test_prefix.arguments.arg1']) self.assertEqual('test:test', config_dict['test_prefix.arguments.arg2']) self.assertNotIn('test_prefix.arguments.arg3', config_dict)
def configure_invalidation_region(): if CACHE_INVALIDATION_REGION.is_configured: return # NOTE(dstanek): Configuring this region manually so that we control the # expiration and can ensure that the keys don't expire. config_dict = cache._build_cache_config(CONF) config_dict['expiration_time'] = None # we don't want an expiration CACHE_INVALIDATION_REGION.configure_from_config( config_dict, '%s.' % CONF.cache.config_prefix) # NOTE(breton): Wrap the cache invalidation region to avoid excessive # calls to memcached, which would result in poor performance. CACHE_INVALIDATION_REGION.wrap(_context_cache._ResponseCacheProxy) # NOTE(morganfainberg): if the backend requests the use of a # key_mangler, we should respect that key_mangler function. If a # key_mangler is not defined by the backend, use the sha1_mangle_key # mangler provided by dogpile.cache. This ensures we always use a fixed # size cache-key. if CACHE_INVALIDATION_REGION.key_mangler is None: CACHE_INVALIDATION_REGION.key_mangler = _sha1_mangle_key
def test_cache_pymemcache_socket_keepalive_with_config(self): """Validate we build a socket keepalive with the right config.""" self.config_fixture.config(group='cache', enabled=True, config_prefix='test_prefix', backend='dogpile.cache.pymemcache', enable_socket_keepalive=True, socket_keepalive_idle=12, socket_keepalive_interval=38, socket_keepalive_count=42) config_dict = cache._build_cache_config(self.config_fixture.conf) self.assertTrue(self.config_fixture.conf.cache.enable_socket_keepalive) self.assertTrue(config_dict['test_prefix.arguments.socket_keepalive'], KeepaliveOpts) self.assertEqual( 12, config_dict['test_prefix.arguments.socket_keepalive'].idle) self.assertEqual( 38, config_dict['test_prefix.arguments.socket_keepalive'].intvl) self.assertEqual( 42, config_dict['test_prefix.arguments.socket_keepalive'].cnt)