From 64c5e50bc7517a85402d508dc430d2ed9af1e8db Mon Sep 17 00:00:00 2001
From: "Hiroyasu.OHYAMA" <user.localhost2000@gmail.com>
Date: Thu, 22 Sep 2016 18:59:47 +0900
Subject: [PATCH] [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
---
 .../zmq_driver/matchmaker/zmq_matchmaker_redis.py |  4 +++-
 .../zmq/matchmaker/test_impl_matchmaker.py        | 15 +++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

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