diff --git a/oslo_messaging/_drivers/zmq_driver/matchmaker/zmq_matchmaker_redis.py b/oslo_messaging/_drivers/zmq_driver/matchmaker/zmq_matchmaker_redis.py index e3efff9a6..1681d5491 100644 --- a/oslo_messaging/_drivers/zmq_driver/matchmaker/zmq_matchmaker_redis.py +++ b/oslo_messaging/_drivers/zmq_driver/matchmaker/zmq_matchmaker_redis.py @@ -19,7 +19,7 @@ from oslo_utils import importutils from oslo_messaging._drivers.zmq_driver.matchmaker import zmq_matchmaker_base from oslo_messaging._drivers.zmq_driver import zmq_address -from oslo_messaging._i18n import _LW +from oslo_messaging._i18n import _LW, _LE redis = importutils.try_import('redis') redis_sentinel = importutils.try_import('redis.sentinel') @@ -112,6 +112,8 @@ class MatchmakerRedis(zmq_matchmaker_base.MatchmakerBase): def __init__(self, conf, *args, **kwargs): super(MatchmakerRedis, self).__init__(conf, *args, **kwargs) self.conf.register_opts(matchmaker_redis_opts, "matchmaker_redis") + if redis is None: + raise ImportError(_LE("Redis package is not available!")) self.sentinel_hosts = self._extract_sentinel_options() if not self.sentinel_hosts: diff --git a/oslo_messaging/tests/drivers/zmq/matchmaker/test_impl_matchmaker.py b/oslo_messaging/tests/drivers/zmq/matchmaker/test_impl_matchmaker.py index 2e369f790..5eda0cc6b 100644 --- a/oslo_messaging/tests/drivers/zmq/matchmaker/test_impl_matchmaker.py +++ b/oslo_messaging/tests/drivers/zmq/matchmaker/test_impl_matchmaker.py @@ -13,6 +13,7 @@ # under the License. from fixtures._fixtures import timeout +import inspect import retrying from stevedore import driver import testscenarios @@ -100,3 +101,17 @@ class TestImplMatchmaker(test_utils.BaseTestCase): except (timeout.TimeoutException, retrying.RetryError): pass self.assertEqual([], hosts) + + def test_handle_redis_package_error(self): + if self.rpc_zmq_matchmaker == "redis": + # move 'redis' variable to prevent this case affect others + module = inspect.getmodule(self.test_matcher) + redis_package = module.redis + + # 'redis' variable is set None, when importing package is failed + module.redis = None + self.assertRaises(ImportError, self.test_matcher.__init__, + self.conf) + + # retrieve 'redis' variable wihch is set originally + module.redis = redis_package