def configure_host(self): osutils = osutils_factory.get_os_utils() osutils.wait_for_boot_completion() service = metadata_factory.get_metadata_service() LOG.info('Metadata service loaded: \'%s\'' % service.get_name()) instance_id = service.get_instance_id() LOG.debug('Instance id: %s', instance_id) plugins = plugins_factory.load_plugins() plugins_shared_data = {} reboot_required = False try: for plugin in plugins: if self._check_plugin_os_requirements(osutils, plugin): if self._exec_plugin(osutils, service, plugin, instance_id, plugins_shared_data): reboot_required = True if CONF.allow_reboot: break finally: service.cleanup() if reboot_required and CONF.allow_reboot: try: osutils.reboot() except Exception as ex: LOG.error('reboot failed with error \'%s\'' % ex) elif CONF.stop_service_on_exit: osutils.terminate()
def test_load_plugins(self, mock_load_class): expected = [] for path in CONF.plugins: expected.append(mock.call(path)) response = factory.load_plugins() self.assertEqual(expected, mock_load_class.call_args_list) self.assertTrue(response is not None)
def test_load_plugins_plugin_failed(self): with testutils.LogSnatcher('cloudbaseinit.plugins.' 'factory') as snatcher: plugins = factory.load_plugins(None) self.assertEqual([], plugins) self.assertEqual(["Could not import plugin module 'missing.plugin'"], snatcher.output)
def test_old_plugin_mapping(self, mock_load_class): with testutils.LogSnatcher('cloudbaseinit.plugins.' 'factory') as snatcher: factory.load_plugins(None) expected = [ "Old plugin module 'cloudbaseinit.plugins.windows." "localscripts.LocalScriptsPlugin' was found. " "The new name is 'cloudbaseinit.plugins.common." "localscripts.LocalScriptsPlugin'. The old name will not " "be supported starting with cloudbaseinit 1.0", ] expected_call = mock.call('cloudbaseinit.plugins.common.' 'localscripts.LocalScriptsPlugin') self.assertEqual(expected, snatcher.output) called = mock_load_class.mock_calls[0] self.assertEqual(expected_call, called)
def _handle_plugins_stage(self, osutils, service, instance_id, stage): plugins_shared_data = {} reboot_required = False plugins = plugins_factory.load_plugins(stage) LOG.info('Executing plugins for stage %r:', stage) for plugin in plugins: if self._check_plugin_os_requirements(osutils, plugin): if self._exec_plugin(osutils, service, plugin, instance_id, plugins_shared_data): reboot_required = True if CONF.allow_reboot: break return reboot_required
def _test_load_plugins(self, mock_load_class, stage=None): if stage: expected_plugins = STAGE.get(stage, []) else: expected_plugins = list(itertools.chain(*STAGE.values())) expected_load = [mock.call(path) for path in CONF.plugins] side_effect = [] for path in expected_plugins: plugin = mock.Mock() plugin.execution_stage = (stage if stage in STAGE.keys() else None) plugin.return_value = path side_effect.append(plugin) mock_load_class.side_effect = (side_effect + [ mock.Mock() for _ in range(len(expected_load) - len(side_effect)) ]) response = factory.load_plugins(stage) self.assertEqual(expected_load, mock_load_class.call_args_list) self.assertEqual(sorted(expected_plugins), sorted(response))
def _test_load_plugins(self, mock_load_class, stage=None): if stage: expected_plugins = STAGE.get(stage, []) else: expected_plugins = list(itertools.chain(*STAGE.values())) expected_load = [mock.call(path) for path in CONF.plugins] side_effect = [] for path in expected_plugins: plugin = mock.Mock() plugin.execution_stage = (stage if stage in STAGE.keys() else None) plugin.return_value = path side_effect.append(plugin) mock_load_class.side_effect = ( side_effect + [mock.Mock() for _ in range(len(expected_load) - len(side_effect))]) response = factory.load_plugins(stage) self.assertEqual(expected_load, mock_load_class.call_args_list) self.assertEqual(sorted(expected_plugins), sorted(response))