def load_plugins() -> None: for plugin, exc in chain( load_plugins_with_exceptions("cmk.post_rename_site.plugins.actions"), load_plugins_with_exceptions("cmk.post_rename_site.cee.plugins.actions") if not is_raw_edition() else []): logger.error("Error in action plugin %s: %s\n", plugin, exc) if cmk.utils.debug.enabled(): raise exc
def load_all_plugins(): for plugin, exception in load_plugins_with_exceptions( "cmk.base.plugins.agent_based"): console.error("Error in agent based plugin %s: %s\n", plugin, exception) if cmk.utils.debug.enabled(): raise exception
def load_all_plugins() -> List[str]: errors = [] for plugin, exception in load_plugins_with_exceptions( "cmk.base.plugins.agent_based"): errors.append(f"Error in agent based plugin {plugin}: {exception}\n") if cmk.utils.debug.enabled(): raise exception return errors
def test_load_plugins_with_exceptions(tmp_package: str) -> None: assert list( load_plugins_with_exceptions(f"{tmp_package}.plugins.abc")) == [] imported = [n for n in sys.modules if n.startswith(tmp_package)] assert sorted(imported) == sorted([ tmp_package, f"{tmp_package}.plugins", f"{tmp_package}.plugins.abc", f"{tmp_package}.plugins.abc.level1", f"{tmp_package}.plugins.abc.level1.plugin", f"{tmp_package}.plugins.abc.plugin", ]) for name in imported: del sys.modules[name]
def test_load_plugins_with_exceptions_handle_exception( exc_package: str) -> None: package_name = exc_package errors = list(load_plugins_with_exceptions(package_name)) assert len(errors) == 1 assert errors[0][0] == "ding" assert isinstance(errors[0][1], SyntaxError) imported = [n for n in sys.modules if n.startswith(package_name)] assert sorted(imported) == sorted([ package_name, f"{package_name}.dong", ]) for name in imported: del sys.modules[name]
def test_load_plugins_with_exceptions_handle_import_error( import_error_package: str) -> None: package_name = import_error_package errors = list(load_plugins_with_exceptions(package_name)) assert len(errors) == 1 assert errors[0][0] == "level0.ding" assert isinstance(errors[0][1], ModuleNotFoundError) imported = [n for n in sys.modules if n.startswith(package_name)] assert sorted(imported) == sorted([ package_name, f"{package_name}.level0", f"{package_name}.level0.dong", ]) for name in imported: del sys.modules[name]
def _import_main_module_plugins(main_modules: List[ModuleType]) -> None: logger.debug("Importing main module plugins") for module in main_modules: main_module_name = module.__name__.split(".")[-1] for plugin_package_name in _plugin_package_names(main_module_name): if not _is_plugin_namespace(plugin_package_name): logger.debug(" Skip loading plugins from %s", plugin_package_name) continue logger.debug(" Importing plugins from %s", plugin_package_name) for plugin_name, exc in load_plugins_with_exceptions(plugin_package_name): logger.error( " Error in %s plugin '%s'\n", main_module_name, plugin_name, exc_info=exc ) utils.add_failed_plugin(main_module_name, plugin_name, exc) logger.debug("Main module plugins imported")
def __init__(self) -> None: # Local import to have faster pytest initialization import cmk.base.api.agent_based.register as register # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.check_api as check_api # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.config as config # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.inventory_plugins as inventory_plugins # pylint: disable=bad-option-value,import-outside-toplevel config._initialize_data_structures() assert config.check_info == {} config.load_all_agent_based_plugins( check_api.get_check_api_context, inventory_plugins.load_legacy_inventory_plugins, ) # our test environment does not deal with namespace packages properly. load plus plugins: try: load_plugins = list( load_plugins_with_exceptions( "plus.cmk.base.plugins.agent_based")) except ModuleNotFoundError: pass else: for _plugin, exception in load_plugins: raise exception inventory_plugins.load_legacy_inventory_plugins( check_api.get_check_api_context, register.inventory_plugins_legacy.get_inventory_context, ) self._snmp_sections = copy.deepcopy( register._config.registered_snmp_sections) self._agent_sections = copy.deepcopy( register._config.registered_agent_sections) self._check_plugins = copy.deepcopy( register._config.registered_check_plugins) self._inventory_plugins = copy.deepcopy( register._config.registered_inventory_plugins)
yield cmk.utils.log.logger.setLevel(old_root_log_level) @pytest.fixture(scope="session", autouse=True) def load_plugins() -> None: main_modules.load_plugins() if errors := get_failed_plugins(): raise Exception( f"The following errors occured during plugin loading: {errors}") # our test environment does not deal with namespace packages properly. # Load plus plugins explicitly: try: load_plugins = list( load_plugins_with_exceptions("plus.cmk.gui.plugins")) except ModuleNotFoundError: pass else: for _plugin, exception in load_plugins: raise exception @pytest.fixture(name="patch_json", autouse=True) def fixture_patch_json() -> Iterator[None]: with patch_json(json): yield @pytest.fixture() def with_user(request_context: None,