From 5302c0c06def6561c167ec3de8f00261d515fced Mon Sep 17 00:00:00 2001 From: jiaopengju Date: Mon, 13 May 2019 11:47:24 +0800 Subject: [PATCH] Fix import error when client module is not exist When we do not want use some plugins that require some specify client, it is allowed not install the client moduele. But currently, karbor will throw ImportError. This patch fix it. Change-Id: I4a6c8cc53f4dc2f91d2ff91bf3498491e12d8ea2 --- karbor/services/protection/client_factory.py | 10 ++++--- .../unit/protection/test_client_factory.py | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 karbor/tests/unit/protection/test_client_factory.py diff --git a/karbor/services/protection/client_factory.py b/karbor/services/protection/client_factory.py index cb77a640..1cf41f15 100644 --- a/karbor/services/protection/client_factory.py +++ b/karbor/services/protection/client_factory.py @@ -81,9 +81,13 @@ class ClientFactory(object): def get_client_module(cls, service): if not cls._factory: cls._factory = {} - for module in cls._list_clients(): - module = importutils.import_module(module) - cls._factory[module.SERVICE] = module + for client_module in cls._list_clients(): + try: + client_module = importutils.import_module(client_module) + except ImportError: + LOG.error('No module named %s', client_module) + else: + cls._factory[client_module.SERVICE] = client_module return cls._factory.get(service) @classmethod diff --git a/karbor/tests/unit/protection/test_client_factory.py b/karbor/tests/unit/protection/test_client_factory.py new file mode 100644 index 00000000..516252b3 --- /dev/null +++ b/karbor/tests/unit/protection/test_client_factory.py @@ -0,0 +1,26 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock + +from karbor.services.protection.client_factory import ClientFactory +from karbor.tests import base + + +class ClientFactoryTest(base.TestCase): + + @mock.patch('oslo_utils.importutils.import_module') + def test_get_client_module_with_import_error(self, mock_import_module): + mock_import_module.side_effect = ImportError + ClientFactory._factory = None + client_module = ClientFactory.get_client_module('cinder') + self.assertEqual(None, client_module)