def reload_collectors(flush=False): global available_collectors logger.info("Reload collectors") if flush: available_collectors.clear() for path in settings.COLLECTORS_MODULES: PluginManager().discover(path) for plugin in PluginManager().get_plugins(path): if plugin.plugin_name not in available_collectors: available_collectors[plugin.plugin_name] = plugin
def main(): if len(sys.argv) <= 1: print("Give me a collector name to execute!\n") print("Collectors loaded:") for path in COLLECTORS_PATHS: PluginManager().discover(path) for plugin in PluginManager().get_plugins(path): print("\t{}".format(plugin.plugin_name)) return for path in COLLECTORS_PATHS: PluginManager().discover(path) for plugin in PluginManager().get_plugins(path): if plugin.plugin_name == sys.argv[1]: _exec_collector(plugin) return "DONE executing {}".format(plugin.plugin_name) else: print(" - skipped {}".format(plugin.plugin_name))
def test_plugin_manager_discover( self, mock_iter_modules, mock_import_module, mock_get_members, mock_is_subclass ): def gen_iter_modules(): yield ("loader", "directory", "ispkg") mock_iter_modules.return_value = gen_iter_modules() mock_get_members.return_value = [] mock_import_module.return_value = "module" pm = PluginManager() pm.discover(".path.to.collector") mock_get_members.assert_called_with("module", isclass) mock_import_module.assert_called_with(".path.to.collector.directory") mock_iter_modules.assert_has_calls( [call(["/path/to/collector"]), call(["/path/to/collector/directory"])] )
def test_plugin_dependency_raises(self): mock_dependency = MagicMock() mock_dependency.verify = MagicMock() mock_dependency.verify.side_effect = ModuleDependencyMissing class basePlugin(BasePlugin): _name_ = "name" _description_ = "desc" _author_ = "author" _version_ = 1 _dependencies_ = [mock_dependency] def __init__(self): PluginManager().register_plugin(self) pm = PluginManager() pm._plugins_ = {} bp = basePlugin() mock_dependency.verify.assert_called_once()
def test_register(self): mock_dependency = MagicMock() mock_dependency.verify = MagicMock() class basePlugin(BasePlugin): _name_ = "name" _description_ = "desc" _author_ = "author" _version_ = 1 _dependencies_ = [mock_dependency] def __init__(self): PluginManager().register_plugin(self) pm = PluginManager() pm._plugins_ = {} bp = basePlugin() self.assertEqual(len(pm.get_plugins()), 1) mock_dependency.verify.assert_called_once()
def test_verify_plugin_ok(self): mock_dependency = MagicMock() mock_dependency.verify = MagicMock() class basePlugin(BasePlugin): _name_ = "name" _description_ = "desc" _author_ = "author" _version_ = 1 _dependencies_ = [mock_dependency] def __init__(self): PluginManager().register_plugin(self) verify = MagicMock() pm = PluginManager() pm._plugins_ = {} bp = basePlugin() mock_dependency.verify.assert_called_once() bp.verify.assert_called_once() self.assertEqual(bp.status[0], PluginStatus.READY)
def test_plugin_manager_empty(self, mock): pm = PluginManager() pm._plugins_ = {"test.a": basePlugin(), "test.b": basePlugin()} plugins_inst = pm.get_plugins() plugins = pm.get_plugins(instance=False) plugins_from_pkg = pm.get_plugins(package="test.", instance=False) self.assertEqual(len(plugins), 2) self.assertEqual(len(plugins_inst), 2) self.assertEqual(len(plugins_from_pkg), 2)
def __init__(self): PluginManager().register_plugin(self)