def test__perform_host_label_discovery_on_cluster(cluster_scenario, discovery_test_case): scenario = cluster_scenario discovery_parameters = discovery_test_case.parameters with cmk_debug_enabled(): host_label_result = discovery._host_labels.analyse_cluster_host_labels( host_config=scenario.host_config, ipaddress=scenario.ipaddress, parsed_sections_broker=scenario.parsed_sections_broker, discovery_parameters=discovery_parameters, ) assert (host_label_result.vanished == discovery_test_case.on_cluster.expected_vanished_host_labels) assert host_label_result.old == discovery_test_case.on_cluster.expected_old_host_labels assert host_label_result.new == discovery_test_case.on_cluster.expected_new_host_labels assert DiscoveredHostLabelsStore(scenario.host_config.hostname).load( ) == discovery_test_case.on_cluster.expected_stored_labels_cluster assert (DiscoveredHostLabelsStore(scenario.node1_hostname).load() == discovery_test_case.on_cluster.expected_stored_labels_node1) assert (DiscoveredHostLabelsStore(scenario.node2_hostname).load() == discovery_test_case.on_cluster.expected_stored_labels_node2)
def test_mode_inventory_as_check(): with cmk_debug_enabled(): exit_code = cmk.base.modes.check_mk.mode_inventory_as_check( {}, "ds-test-host1") assert exit_code == 0 assert ABCChecker.check.call_count == 2 # type: ignore[attr-defined]
def __init__(self): # 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 == {} with cmk_debug_enabled(): # fail if a plugin can't be loaded config.load_all_agent_based_plugins( check_api.get_check_api_context) inventory_plugins.load_legacy_inventory_plugins( check_api.get_check_api_context, register.inventory_plugins_legacy.get_inventory_context, ) # some sanitiy checks, may decrease as we migrate assert len(config.check_info) > 1000 assert len(config.snmp_info) > 400 assert len(inventory_plugins._inv_info) > 60 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)
def test__discover_host_labels_and_services_on_realhost( realhost_scenario, discovery_test_case): scenario = realhost_scenario discovery_parameters = discovery_test_case.parameters # we're depending on the changed host labels: _ = discovery.analyse_host_labels( host_name=scenario.hostname, ipaddress=scenario.ipaddress, parsed_sections_broker=scenario.parsed_sections_broker, discovery_parameters=discovery_parameters, ) with cmk_debug_enabled(): discovered_services = ( [] if discovery_parameters.only_host_labels else discovery._discovered_services._discover_services( host_name=scenario.hostname, ipaddress=scenario.ipaddress, parsed_sections_broker=scenario.parsed_sections_broker, discovery_parameters=discovery_parameters, run_plugin_names=EVERYTHING, )) services = {(s.check_plugin_name, s.item) for s in discovered_services} assert services == discovery_test_case.expected_services
def test__discover_host_labels_and_services_on_cluster(cluster_scenario, discovery_test_case): scenario = cluster_scenario discovery_parameters = discovery_test_case.parameters # we need the sideeffects of this call. TODO: guess what. _ = discovery._host_labels.analyse_cluster_host_labels( host_config=scenario.host_config, ipaddress=scenario.ipaddress, parsed_sections_broker=scenario.parsed_sections_broker, discovery_parameters=discovery_parameters, ) with cmk_debug_enabled(): discovered_services = discovery._get_cluster_services( scenario.host_config, scenario.ipaddress, scenario.parsed_sections_broker, discovery_parameters, ) services = set(discovered_services) assert services == discovery_test_case.expected_services
def _config_load_all_checks(): # this is needed in cmk/base *and* checks/ :-( import cmk.base.config as config # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.check_api as check_api # pylint: disable=bad-option-value,import-outside-toplevel config._initialize_data_structures() assert config.check_info == {} with cmk_debug_enabled(): # fail if a plugin can't be loaded config.load_all_checks(check_api.get_check_api_context) assert len(config.check_info) > 1000 # sanitiy check
def _config_load_all_checks(): # Local import to have faster pytest initialization import cmk.base.config as config # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.check_api as check_api # pylint: disable=bad-option-value,import-outside-toplevel config._initialize_data_structures() assert config.check_info == {} with cmk_debug_enabled(): # fail if a plugin can't be loaded config.load_all_agent_based_plugins(check_api.get_check_api_context) assert len(config.check_info) > 1000 # sanitiy check
def test_mode_check_discovery_cached(mocker): _patch_data_source( mocker, max_age=120, use_outdated=True, maybe=True, ) cmk.base.modes.check_mk.option_cache() with cmk_debug_enabled(): assert cmk.base.modes.check_mk.mode_check_discovery( "ds-test-host1") == 1 assert ABCChecker.check.call_count == 2 # type: ignore[attr-defined]
def test_mode_check_discovery_cached(mocker): _patch_data_source_run( mocker, _max_cachefile_age=120, _use_outdated_cache_file=True, _may_use_cache_file=True, ) cmk.base.modes.check_mk.option_cache() with cmk_debug_enabled(): assert cmk.base.modes.check_mk.mode_check_discovery( "ds-test-host1") == 1 assert ABCDataSource._run.call_count == 2 # type: ignore[attr-defined]
def _config_load_all_inventory_plugins(): # Local import to have faster pytest initialization import cmk.base.inventory as inventory # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.inventory_plugins as inventory_plugins # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.check_api as check_api # pylint: disable=bad-option-value,import-outside-toplevel with cmk_debug_enabled(): # fail if a plugin can't be loaded inventory_plugins.load_legacy_inventory_plugins( check_api.get_check_api_context, inventory.get_inventory_context, ) assert len(inventory_plugins.inv_info ) > 90 # sanitiy check, may decrease as we migrate
def test_do_discovery(monkeypatch): ts = Scenario().add_host("test-host", ipaddress="127.0.0.1") ts.fake_standard_linux_agent_output("test-host") ts.apply(monkeypatch) with cmk_debug_enabled(): discovery.do_discovery( arg_hostnames={"test-host"}, selected_sections=NO_SELECTION, run_only_plugin_names=None, arg_only_new=False, ) services = autochecks.parse_autochecks_file("test-host", config.service_description) found = {(s.check_plugin_name, s.item): s.service_labels.to_dict() for s in services} assert found == _expected_services store = DiscoveredHostLabelsStore("test-host") assert store.load() == _expected_host_labels
def test__perform_host_label_discovery_on_realhost(realhost_scenario, discovery_test_case): scenario = realhost_scenario discovery_parameters = discovery_test_case.parameters with cmk_debug_enabled(): host_label_result = discovery.analyse_host_labels( host_name=scenario.hostname, ipaddress=scenario.ipaddress, parsed_sections_broker=scenario.parsed_sections_broker, discovery_parameters=discovery_parameters, ) assert host_label_result.vanished == discovery_test_case.on_realhost.expected_vanished_host_labels assert host_label_result.old == discovery_test_case.on_realhost.expected_old_host_labels assert host_label_result.new == discovery_test_case.on_realhost.expected_new_host_labels assert DiscoveredHostLabelsStore( scenario.hostname).load() == discovery_test_case.on_realhost.expected_stored_labels
def test_mode_check_discovery_default(mocker): _patch_data_source(mocker, max_age=0) with cmk_debug_enabled(): assert cmk.base.modes.check_mk.mode_check_discovery( "ds-test-host1") == 1 assert ABCChecker.check.call_count == 2 # type: ignore[attr-defined]
def enable_debug_mode(): # `debug.disabled()` hides exceptions and makes it # *impossible* to debug anything. with cmk_debug_enabled(): yield