[zmq] Added a processing to handle ImportError in Redis plugin of Matchmaker

The MatchmakerRedis depends on 'redis' package, but there is no error
handler in it.
This patch adds processing to raise an exception when 'redis' package
doesn't exist.

Change-Id: Ib611543c76336ed1d4a204cd9b3c97cf468ad833
Closes-Bug: #1624256
This commit is contained in:
Hiroyasu.OHYAMA 2016-09-22 18:59:47 +09:00
parent 885904e9ae
commit 64c5e50bc7
2 changed files with 18 additions and 1 deletions

View File

@ -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:

View File

@ -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