Fix client ping in redis scenario tests
StrictRedis.ping will return True if the server is online, otherwise raise a ConnectionError exception, so it's not correct to compare the return value with "PONG". Redis manager updates 'requirepass' at runtime only in 'update_overrides' previously, this is incorrect as enable_root/disable_root has been implemented already. This patch adds admin client refreshing in 'apply_overrides' which was removed in [1]. A helper function for Redis root credential is also added RedisHelper. And RedisHelper will cache Redis client, when enable_root/disable_root is requested, cached client should be updated. [1] https://review.openstack.org/#/c/522205/ Change-Id: I558059edbc85f283a02f42920998351cefeb2d08 Signed-off-by: Zhao Chao <zhaochao1984@gmail.com>
This commit is contained in:
parent
d6bd37f3f3
commit
8541170ca9
|
@ -154,6 +154,9 @@ class RedisApp(object):
|
|||
if overrides:
|
||||
self.configuration_manager.apply_user_override(overrides)
|
||||
# apply requirepass at runtime
|
||||
# TODO(zhaochao): updating 'requirepass' here will be removed
|
||||
# in the future releases, Redis only use enable_root/disable_root
|
||||
# to set this parameter.
|
||||
if 'requirepass' in overrides:
|
||||
self.admin.config_set('requirepass', overrides['requirepass'])
|
||||
self._refresh_admin_client()
|
||||
|
@ -178,9 +181,13 @@ class RedisApp(object):
|
|||
for prop_name, prop_args in overrides.items():
|
||||
args_string = self._join_lists(
|
||||
self._value_converter.to_strings(prop_args), ' ')
|
||||
# requirepass applied at runtime during update_overrides
|
||||
if prop_name != "requirepass":
|
||||
client.config_set(prop_name, args_string)
|
||||
client.config_set(prop_name, args_string)
|
||||
# NOTE(zhaochao): requirepass applied in update_overrides is
|
||||
# only kept for back compatibility. Now requirepass is set
|
||||
# via enable_root/disable_root, Redis admin client should be
|
||||
# refreshed here.
|
||||
if prop_name == "requirepass":
|
||||
client = self._refresh_admin_client()
|
||||
|
||||
def _join_lists(self, items, sep):
|
||||
"""Join list items (including items from sub-lists) into a string.
|
||||
|
|
|
@ -31,6 +31,9 @@ class RedisHelper(TestHelper):
|
|||
|
||||
self._ds_client_cache = dict()
|
||||
|
||||
def get_helper_credentials_root(self):
|
||||
return {'name': '-', 'password': 'rootpass'}
|
||||
|
||||
def get_client(self, host, *args, **kwargs):
|
||||
# We need to cache the Redis client in order to prevent Error 99
|
||||
# (Cannot assign requested address) when working with large data sets.
|
||||
|
@ -40,7 +43,23 @@ class RedisHelper(TestHelper):
|
|||
# (TIME_WAIT) before the port can be released.
|
||||
# This is a feature of the operating system that helps it dealing with
|
||||
# packets that arrive after the connection is closed.
|
||||
if host not in self._ds_client_cache:
|
||||
#
|
||||
# NOTE(zhaochao): when connecting to Redis server with a password,
|
||||
# current cached client may not updated to use the same password,
|
||||
# connection_kwargs of the ConnectPool object should be checked,
|
||||
# if the new password is different, A new client instance will be
|
||||
# created.
|
||||
recreate_client = True
|
||||
|
||||
if host in self._ds_client_cache:
|
||||
new_password = kwargs.get('password')
|
||||
cached_password = (self._ds_client_cache[host]
|
||||
.connection_pool
|
||||
.connection_kwargs.get('password'))
|
||||
if new_password == cached_password:
|
||||
recreate_client = False
|
||||
|
||||
if recreate_client:
|
||||
self._ds_client_cache[host] = (
|
||||
self.create_client(host, *args, **kwargs))
|
||||
|
||||
|
@ -180,6 +199,6 @@ class RedisHelper(TestHelper):
|
|||
def ping(self, host, *args, **kwargs):
|
||||
try:
|
||||
client = self.get_client(host, *args, **kwargs)
|
||||
return client.ping() == 'PONG'
|
||||
return client.ping()
|
||||
except Exception:
|
||||
return False
|
||||
|
|
Loading…
Reference in New Issue