Merge "Don't hard fail when a plugin can't be imported"

This commit is contained in:
Jenkins 2015-05-11 14:55:07 +00:00 committed by Gerrit Code Review
commit d687248000
2 changed files with 21 additions and 1 deletions

View File

@ -14,6 +14,7 @@
from oslo.config import cfg
from cloudbaseinit.openstack.common import log as logging
from cloudbaseinit.utils import classloader
opts = [
@ -44,11 +45,19 @@ opts = [
CONF = cfg.CONF
CONF.register_opts(opts)
LOG = logging.getLogger(__name__)
def load_plugins():
plugins = []
cl = classloader.ClassLoader()
for class_path in CONF.plugins:
plugins.append(cl.load_class(class_path)())
try:
plugin_cls = cl.load_class(class_path)
except ImportError:
LOG.error("Could not import plugin module %r", class_path)
continue
plugin = plugin_cls()
plugins.append(plugin)
return plugins

View File

@ -21,6 +21,7 @@ except ImportError:
from oslo.config import cfg
from cloudbaseinit.plugins.common import factory
from cloudbaseinit.tests import testutils
CONF = cfg.CONF
@ -35,3 +36,13 @@ class PluginFactoryTests(unittest.TestCase):
response = factory.load_plugins()
self.assertEqual(expected, mock_load_class.call_args_list)
self.assertTrue(response is not None)
@testutils.ConfPatcher('plugins', ['missing.plugin'])
def test_load_plugins_plugin_failed(self):
with testutils.LogSnatcher('cloudbaseinit.plugins.'
'common.factory') as snatcher:
plugins = factory.load_plugins()
self.assertEqual([], plugins)
self.assertEqual(["Could not import plugin module 'missing.plugin'"],
snatcher.output)