From 522113da4390347a9cd55e89a7eddf6e14527eff Mon Sep 17 00:00:00 2001 From: Alfredo Moralejo Date: Fri, 8 Jul 2016 11:11:13 +0200 Subject: [PATCH] Add callback function to manage missing hooks Before https://review.openstack.org/#/c/337043/, stevedore.NamedExtensionManager returned KeyError when calling with non-existing names and name_order=True. After the mentioned change this is not longer true, so we used the just added on_missing_entrypoints_callback option to customize the behavior in this cases and make it raise a custom exception MissingHookError. Change-Id: I1f1edc0b7a82a16bf9be4113db61ee1cd0080db4 Closes-Bug: #1600141 --- ironic_inspector/main.py | 5 +++-- ironic_inspector/plugins/base.py | 11 +++++++++++ ironic_inspector/test/unit/test_main.py | 1 - 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ironic_inspector/main.py b/ironic_inspector/main.py index 05d054ebe..130c810f3 100644 --- a/ironic_inspector/main.py +++ b/ironic_inspector/main.py @@ -411,8 +411,9 @@ class Service(object): hooks = [ext.name for ext in plugins_base.processing_hooks_manager()] except KeyError as exc: - # stevedore raises KeyError on missing hook - LOG.critical(_LC('Hook %s failed to load or was not found'), + # callback function raises MissingHookError derived from KeyError + # on missing hook + LOG.critical(_LC('Hook(s) %s failed to load or was not found'), str(exc)) sys.exit(1) diff --git a/ironic_inspector/plugins/base.py b/ironic_inspector/plugins/base.py index 218f7f45b..368a0ab97 100644 --- a/ironic_inspector/plugins/base.py +++ b/ironic_inspector/plugins/base.py @@ -149,6 +149,12 @@ _CONDITIONS_MGR = None _ACTIONS_MGR = None +def missing_entrypoints_callback(names): + """Raise MissingHookError with comma-separated list of missing hooks""" + missing_names = ', '.join(names) + raise MissingHookError(missing_names) + + def processing_hooks_manager(*args): """Create a Stevedore extension manager for processing hooks. @@ -164,6 +170,7 @@ def processing_hooks_manager(*args): names=names, invoke_on_load=True, invoke_args=args, + on_missing_entrypoints_callback=missing_entrypoints_callback, name_order=True) return _HOOKS_MGR @@ -204,3 +211,7 @@ def rule_actions_manager(): 'actions is deprecated (action "%s")'), act.name) return _ACTIONS_MGR + + +class MissingHookError(KeyError): + """Exception when hook is not found when processing it.""" diff --git a/ironic_inspector/test/unit/test_main.py b/ironic_inspector/test/unit/test_main.py index cb51afcb7..0906e03c3 100644 --- a/ironic_inspector/test/unit/test_main.py +++ b/ironic_inspector/test/unit/test_main.py @@ -626,7 +626,6 @@ class TestInit(test_base.BaseTest): self.service.init() self.assertFalse(mock_firewall.called) - @unittest.skip('skipped until stevedore > 1.15.0 is released') @mock.patch.object(main.LOG, 'critical') def test_init_failed_processing_hook(self, mock_log, mock_node_cache, mock_get_client, mock_auth,