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
This commit is contained in:
jiaopengju 2019-05-13 11:47:24 +08:00
parent 39bdc2a891
commit 5302c0c06d
2 changed files with 33 additions and 3 deletions

View File

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

View File

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