def test_get_section_cluster_kwargs(monkeypatch, required_sections, expected_result): _set_up(monkeypatch, "cluster", ["node2", "node1"], {"node1": "cluster", "node2": "cluster"}) node1_section_content = { SectionName("one"): NODE_1, # TODO (mo): CMK-4232 # SectionName("two"): NODE_1, SectionName("three"): NODE_1 } node2_section_content = { SectionName("two"): NODE_2, SectionName("three"): NODE_2, } multi_host_sections = MultiHostSections() multi_host_sections.setdefault( HostKey("node1", "127.0.0.1", SourceType.HOST), AgentHostSections(sections=node1_section_content), ) multi_host_sections.setdefault( HostKey("node2", "127.0.0.1", SourceType.HOST), AgentHostSections(sections=node2_section_content), ) kwargs = multi_host_sections.get_section_cluster_kwargs( HostKey("cluster", None, SourceType.HOST), [ParsedSectionName(n) for n in required_sections], "_service_description", ) assert expected_result == kwargs,\ "Section content: Expected '%s' but got '%s'" % (expected_result, kwargs)
def test__find_candidates(): mhs = MultiHostSections() mhs._data = { # we just care about the keys here, content set to [] for simplicity # section names have been are chosen arbitrarily. # any HostSections type is fine. HostKey("test_node", "1.2.3.4", SourceType.HOST): AgentHostSections({ SectionName("kernel"): [], # host only SectionName("uptime"): [['123']], # host & mgmt }), HostKey("test_node", "1.2.3.4", SourceType.MANAGEMENT): AgentHostSections({ SectionName("uptime"): [['123']], # host & mgmt SectionName("liebert_fans"): [[]], # mgmt only SectionName("mgmt_snmp_info"): [[]], # is already mgmt_ prefixed }), } preliminary_candidates = list(agent_based_register.iter_all_check_plugins()) parsed_sections_of_interest = { parsed_section_name for plugin in preliminary_candidates for parsed_section_name in plugin.sections } assert discovery._find_host_candidates( mhs, preliminary_candidates, parsed_sections_of_interest, ) == { CheckPluginName('docker_container_status_uptime'), CheckPluginName("kernel"), CheckPluginName('kernel_performance'), CheckPluginName('kernel_util'), CheckPluginName("uptime"), } assert discovery._find_mgmt_candidates( mhs, preliminary_candidates, parsed_sections_of_interest, ) == { CheckPluginName('mgmt_docker_container_status_uptime'), CheckPluginName("mgmt_liebert_fans"), CheckPluginName("mgmt_uptime"), CheckPluginName("mgmt_snmp_info"), # not mgmt_mgmt_... } assert discovery._find_candidates( mhs, selected_check_plugins=None, ) == { CheckPluginName('docker_container_status_uptime'), CheckPluginName("kernel"), CheckPluginName('kernel_performance'), CheckPluginName('kernel_util'), CheckPluginName('mgmt_docker_container_status_uptime'), CheckPluginName("mgmt_liebert_fans"), CheckPluginName("mgmt_uptime"), CheckPluginName("mgmt_snmp_info"), # not mgmt_mgmt_... CheckPluginName("uptime"), }
def test_get_section_cluster_kwargs(patch_register, required_sections, expected_result): node1_section_content = { SectionName("one"): NODE_1, SectionName("two"): NODE_1, SectionName("three"): NODE_1 } node2_section_content = { SectionName("two"): NODE_2, SectionName("three"): NODE_2, } multi_host_sections = MultiHostSections() multi_host_sections.setdefault( HostKey("node1", "127.0.0.1", SourceType.HOST), AgentHostSections(sections=node1_section_content), ) multi_host_sections.setdefault( HostKey("node2", "127.0.0.1", SourceType.HOST), AgentHostSections(sections=node2_section_content), ) kwargs = multi_host_sections.get_section_cluster_kwargs( [ HostKey("node1", "127.0.0.1", SourceType.HOST), HostKey("node2", "127.0.0.1", SourceType.HOST), ], [ParsedSectionName(n) for n in required_sections], ) assert expected_result == kwargs,\ "Section content: Expected '%s' but got '%s'" % (expected_result, kwargs)
def test_get_section_kwargs(monkeypatch, required_sections, expected_result): _set_up(monkeypatch, "node1", None, {}) node_section_content = { SectionName("one"): NODE_1, # TODO (mo): CMK-4232 # SectionName("two"): NODE_1, SectionName("three"): NODE_1 } host_key = HostKey("node1", "127.0.0.1", SourceType.HOST) multi_host_sections = MultiHostSections() multi_host_sections.setdefault( host_key, AgentHostSections(sections=node_section_content), ) kwargs = multi_host_sections.get_section_kwargs( host_key, [ParsedSectionName(n) for n in required_sections], ) assert expected_result == kwargs,\ "Section content: Expected '%s' but got '%s'" % (expected_result, kwargs)
def test__find_candidates(): mhs = MultiHostSections() mhs._data = { # we just care about the keys here, content set to [] for simplicity # section names have been are chosen arbitrarily. # any HostSections type is fine. HostKey("test_node", "1.2.3.4", SourceType.HOST): AgentHostSections({ SectionName("kernel"): [], # host only SectionName("uptime"): [], # host & mgmt }), HostKey("test_node", "1.2.3.4", SourceType.MANAGEMENT): AgentHostSections({ SectionName("uptime"): [], # host & mgmt SectionName("liebert_fans"): [], # mgmt only SectionName("mgmt_snmp_info"): [], # is already mgmt_ prefixed }), } assert discovery._find_candidates_by_source_type(mhs, SourceType.HOST) == { CheckPluginName("kernel"), CheckPluginName('kernel_performance'), CheckPluginName('kernel_util'), CheckPluginName("uptime"), } assert discovery._find_candidates_by_source_type( mhs, SourceType.MANAGEMENT) == { CheckPluginName("liebert_fans"), CheckPluginName("uptime"), CheckPluginName("mgmt_snmp_info"), } assert discovery._find_candidates(mhs) == { CheckPluginName("kernel"), CheckPluginName('kernel_performance'), CheckPluginName('kernel_util'), CheckPluginName("uptime"), CheckPluginName("mgmt_liebert_fans"), CheckPluginName("mgmt_uptime"), CheckPluginName("mgmt_snmp_info"), # not mgmt_mgmt_... }
def test_defaults(self, ipaddress, mode, monkeypatch): hostname = "testhost" Scenario().add_host(hostname).apply(monkeypatch) summarizer = TCPConfigurator( hostname, ipaddress, mode=mode, ).make_summarizer() assert summarizer.summarize( AgentHostSections()) == (0, "Version: unknown, OS: unknown", [])
def _get_host_section_for_parse_sections_test(): node_section_content = { SectionName("one"): NODE_1, SectionName("four"): NODE_1, } host_key = HostKey("node1", "127.0.0.1", SourceType.HOST) mhs = MultiHostSections() mhs.setdefault( host_key, AgentHostSections(sections=node_section_content), ) return host_key, mhs
def test_get_section_content(hostname, host_entries, cluster_node_keys, expected_result): multi_host_sections = MultiHostSections() for nodename, node_section_content in host_entries: multi_host_sections.setdefault( HostKey(nodename, "127.0.0.1", SourceType.HOST), AgentHostSections( sections={ SectionName("section_plugin_name"): node_section_content }), ) section_content = multi_host_sections.get_section_content( HostKey(hostname, "127.0.0.1", SourceType.HOST), check_api_utils.HOST_ONLY, "section_plugin_name", False, cluster_node_keys=cluster_node_keys, check_legacy_info= {}, # only for parse_function lookup, not needed in this test ) assert expected_result == section_content section_content = multi_host_sections.get_section_content( HostKey(hostname, "127.0.0.1", SourceType.HOST), check_api_utils.HOST_PRECEDENCE, "section_plugin_name", False, cluster_node_keys=cluster_node_keys, check_legacy_info= {}, # only for parse_function lookup, not needed in this test ) assert expected_result == section_content section_content = multi_host_sections.get_section_content( HostKey(hostname, "127.0.0.1", SourceType.MANAGEMENT), check_api_utils.MGMT_ONLY, "section_plugin_name", False, cluster_node_keys=None if cluster_node_keys is None else [ HostKey(hn, ip, SourceType.MANAGEMENT) for (hn, ip, _st) in cluster_node_keys ], check_legacy_info= {}, # only for parse_function lookup, not needed in this test ) assert section_content is None
def test_attribute_defaults(monkeypatch, ipaddress, mode): hostname = "testhost" Scenario().add_host(hostname).apply(monkeypatch) configurator = PiggybackConfigurator(hostname, ipaddress, mode=mode) assert configurator.hostname == hostname assert configurator.ipaddress == ipaddress assert configurator.mode is mode assert configurator.description.startswith("Process piggyback data from") summarizer = configurator.make_summarizer() assert summarizer.summarize(AgentHostSections()) == (0, "", []) checker = configurator.make_checker() assert checker.configurator is configurator assert checker.id == "piggyback"
def test_get_parsed_section(patch_register, node_section_content, expected_result): multi_host_sections = MultiHostSections() multi_host_sections.setdefault( HostKey("node1", "127.0.0.1", SourceType.HOST), AgentHostSections(sections=node_section_content), ) content = multi_host_sections.get_parsed_section( HostKey("node1", "127.0.0.1", SourceType.HOST), ParsedSectionName("parsed"), ) assert expected_result == content,\ "Section content: Expected '%s' but got '%s'" % (expected_result, content)
def test_get_summary_result_requires_host_sections(monkeypatch, ipaddress): hostname = "testhost" Scenario().add_host(hostname).apply(monkeypatch) source = TCPDataSource(hostname, ipaddress) with pytest.raises(AssertionError): source.get_summary_result_for_discovery() with pytest.raises(AssertionError): source.get_summary_result_for_inventory() with pytest.raises(AssertionError): source.get_summary_result_for_checking() source._host_sections = AgentHostSections() defaults: ServiceCheckResult = (0, "Version: unknown, OS: unknown", []) assert source.get_summary_result_for_discovery() == defaults assert source.get_summary_result_for_inventory() == defaults assert source.get_summary_result_for_checking() == defaults
def test_get_section_content(monkeypatch, hostname, nodes, host_entries, cluster_mapping, service_descr, expected_result): _set_up(monkeypatch, hostname, nodes, cluster_mapping) multi_host_sections = MultiHostSections() for nodename, node_section_content in host_entries: multi_host_sections.setdefault( HostKey(nodename, "127.0.0.1", SourceType.HOST), AgentHostSections( sections={ SectionName("section_plugin_name"): node_section_content }), ) section_content = multi_host_sections.get_section_content( HostKey(hostname, "127.0.0.1", SourceType.HOST), check_api_utils.HOST_ONLY, "section_plugin_name", False, service_description=service_descr, ) assert expected_result == section_content,\ "Section content: Expected '%s' but got '%s'" % (expected_result, section_content) section_content = multi_host_sections.get_section_content( HostKey(hostname, "127.0.0.1", SourceType.HOST), check_api_utils.HOST_PRECEDENCE, "section_plugin_name", False, service_description=service_descr, ) assert expected_result == section_content,\ "Section content: Expected '%s' but got '%s'" % (expected_result, section_content) section_content = multi_host_sections.get_section_content( HostKey(hostname, "127.0.0.1", SourceType.MANAGEMENT), check_api_utils.MGMT_ONLY, "section_plugin_name", False, service_description=service_descr, ) assert section_content is None, \ "Section content: Expected 'None' but got '%s'" % (section_content,)
def test_attribute_defaults(mode, monkeypatch): hostname = "testhost" Scenario().add_host(hostname).apply(monkeypatch) host_config = config.get_config_cache().get_host_config(hostname) ipaddress = ip_lookup.lookup_mgmt_board_ip_address(host_config) configurator = IPMIConfigurator(hostname, ipaddress, mode=mode) assert configurator.hostname == hostname assert configurator.ipaddress == ipaddress assert configurator.mode is mode assert configurator.description == "Management board - IPMI" assert configurator.source_type is SourceType.MANAGEMENT summarizer = configurator.make_summarizer() assert summarizer.summarize(AgentHostSections()) == (0, "Version: unknown", []) checker = configurator.make_checker() assert checker.id == "mgmt_ipmi" assert checker._cpu_tracking_id == checker.id
def check(_, *args, **kwargs): return AgentHostSections(sections={section_name: [[str(section_name)]]})
def run(_, *, selected_raw_sections): sections = {} if selected_raw_sections is not None and section_name in selected_raw_sections: sections = {section_name: [[str(section_name)]]} return AgentHostSections(sections=sections)
def test_defaults(self, source): summarizer = source.summarizer assert summarizer.summarize(AgentHostSections()) == (0, "Success", [])
def test_defaults(self, configurator): summarizer = configurator.make_summarizer() assert summarizer.summarize(AgentHostSections()) == (0, "Success", [])