def test_one_nonsnmp_source(self, hostname, ipaddress, mode, config_cache, host_config, source): source = source(hostname, ipaddress, mode=mode) assert source.source_type is SourceType.HOST mhs = MultiHostSections() update_host_sections( mhs, make_nodes( config_cache, host_config, ipaddress, mode=mode, sources=[source], ), max_cachefile_age=0, selected_raw_sections=None, host_config=host_config, fetcher_messages=[ FetcherMessage.from_raw_data( result.OK(source.default_raw_data), L3Stats(Snapshot.null()), source.fetcher_type, ), ], ) assert len(mhs) == 1 key = HostKey(hostname, ipaddress, source.source_type) assert key in mhs section = mhs[key] assert isinstance(section, AgentHostSections) assert len(section.sections) == 1 assert section.sections[SectionName("section_name_%s" % hostname)] == [["section_content"]]
def test_get_host_sections_cluster(mode, monkeypatch, mocker): hostname = "testhost" hosts = { "host0": "10.0.0.0", "host1": "10.0.0.1", "host2": "10.0.0.2", } address = "1.2.3.4" tags = {"agent": "no-agent"} section_name = SectionName("test_section") config_cache = make_scenario(hostname, tags).apply(monkeypatch) host_config = config.HostConfig.make_host_config(hostname) def lookup_ip_address(host_config, family=None, for_mgmt_board=False): return hosts[host_config.hostname] def make_piggybacked_sections(hc): if hc.nodes == host_config.nodes: return {section_name: True} return {} def check(_, *args, **kwargs): return result.OK(AgentHostSections(sections={section_name: [[str(section_name)]]})) monkeypatch.setattr( ip_lookup, "lookup_ip_address", lookup_ip_address, ) monkeypatch.setattr( _checkers, "_make_piggybacked_sections", make_piggybacked_sections, ) monkeypatch.setattr( Source, "parse", check, ) mocker.patch.object( cmk.utils.piggyback, "remove_source_status_file", autospec=True, ) mocker.patch.object( cmk.utils.piggyback, "_store_status_file_of", autospec=True, ) # Create a cluster host_config.nodes = list(hosts.keys()) mhs = MultiHostSections() sources = make_sources(host_config, address, mode=mode) update_host_sections( mhs, make_nodes( config_cache, host_config, address, mode=mode, sources=sources, ), max_cachefile_age=host_config.max_cachefile_age, selected_raw_sections=None, host_config=host_config, fetcher_messages=[ FetcherMessage.from_raw_data( result.OK(source.default_raw_data), L3Stats(CPUTracker()), source.fetcher_type, ) for source in sources ], ) assert len(mhs) == len(hosts) == 3 cmk.utils.piggyback._store_status_file_of.assert_not_called() # type: ignore[attr-defined] assert cmk.utils.piggyback.remove_source_status_file.call_count == 3 # type: ignore[attr-defined] for host, addr in hosts.items(): remove_source_status_file = cmk.utils.piggyback.remove_source_status_file remove_source_status_file.assert_any_call(host) # type: ignore[attr-defined] key = HostKey(host, addr, SourceType.HOST) assert key in mhs section = mhs[key] assert len(section.sections) == 1 assert next(iter(section.sections)) == section_name assert not section.cache_info assert not section.piggybacked_raw_data assert not section.persisted_sections
def test_raw_data_tcp_standard(self, agent_raw_data, stats, fetcher_type): raw_data: result.Result[AgentRawData, Exception] = result.OK(agent_raw_data) message = FetcherMessage.from_raw_data(raw_data, stats, fetcher_type) assert message.raw_data == raw_data
def test_raw_data_snmp(self, snmp_raw_data, stats): raw_data: result.Result[SNMPRawData, Exception] = result.OK(snmp_raw_data) message = FetcherMessage.from_raw_data(raw_data, stats, FetcherType.SNMP) assert message.raw_data == raw_data
def test_from_bytes_success(self, message): assert FetcherMessage.from_bytes(bytes(message) + 42 * b"*") == message
def test_from_bytes_failure(self): with pytest.raises(ValueError): FetcherMessage.from_bytes(b"random bytes")
def message(self, header, payload, stats): return FetcherMessage(header, payload, stats)
def test_raw_data_exception(self, stats): raw_data: result.Result[AgentRawData, Exception] = result.Error(Exception("zomg!")) message = FetcherMessage.from_raw_data(raw_data, stats, FetcherType.TCP) assert isinstance(message.raw_data.error, Exception) assert str(message.raw_data.error) == "zomg!"
def test_from_raw_data_snmp(self, snmp_raw_data, stats): raw_data: result.Result[SNMPRawData, Exception] = result.OK(snmp_raw_data) message = FetcherMessage.from_raw_data(raw_data, stats, FetcherType.SNMP) assert message.header.fetcher_type is FetcherType.SNMP assert message.header.payload_type is PayloadType.SNMP assert message.raw_data == raw_data
def test_from_raw_data_standard(self, agent_raw_data, stats, fetcher_type): raw_data: result.Result[AgentRawData, Exception] = result.OK(agent_raw_data) message = FetcherMessage.from_raw_data(raw_data, stats, fetcher_type) assert message.header.fetcher_type is fetcher_type assert message.header.payload_type is PayloadType.AGENT assert message.raw_data == raw_data
def test_raw_data_tcp(self, agent_raw_data): raw_data: result.Result[AgentRawData, Exception] = result.OK(agent_raw_data) message = FetcherMessage.from_raw_data(raw_data, FetcherType.TCP) assert message.raw_data == raw_data