Merge "Fix import error when client module is not exist"

This commit is contained in:
Zuul 2019-06-05 01:30:38 +00:00 committed by Gerrit Code Review
commit 2952bfb7ef
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)