def test_attribute_defaults(mode, monkeypatch): ipaddress = "1.2.3.4" hostname = "testhost" Scenario().add_host(hostname).apply(monkeypatch) source = TCPSource(hostname, ipaddress, mode=mode) source.file_cache.path = Path("/my/path/") assert source.configure_fetcher() == { "file_cache": { "disabled": False, "max_age": None, "path": "/my/path", "simulation": False, "use_outdated": False, }, "family": socket.AF_INET, "address": (ipaddress, 6556), "timeout": 5.0, "encryption_settings": { "use_realtime": "enforce", "use_regular": "disable", }, "use_only_cache": False, } assert source.description == "TCP: %s:%s" % (ipaddress, 6556) assert source.id == "agent" assert source.file_cache.maybe is False
def test_defaults(self, ipaddress, mode, monkeypatch): hostname = "testhost" Scenario().add_host(hostname).apply(monkeypatch) source = TCPSource(hostname, ipaddress, mode=mode) assert source.summarize(result.OK(AgentHostSections())) == ( 0, "Version: unknown, OS: unknown", [], )
def test_tcpdatasource_restricted_address_mismatch( mode, monkeypatch, restricted_address_mismatch_state, only_from, rule, res, ): hostname = "hostname" ts = Scenario().add_host(hostname) ts.set_option("agent_config", {"only_from": [(rule, [], [hostname], {})]}) if restricted_address_mismatch_state is not None: ts.set_ruleset("check_mk_exit_status", [ ({ "restricted_address_mismatch": restricted_address_mismatch_state, }, [], [hostname], {}), ]) ts.apply(monkeypatch) source = TCPSource(hostname, "ipaddress", mode=mode) summarizer = AgentSummarizerDefault(source.exit_spec, source) assert summarizer._sub_result_only_from({"onlyfrom": only_from}) == res
def test_multiple_sources_from_different_hosts(self, hostname, ipaddress, mode, config_cache, host_config): sources = [ ProgramSource.ds(hostname + "0", ipaddress, mode=mode, template="",), TCPSource(hostname + "1", ipaddress, mode=mode,), TCPSource(hostname + "2", ipaddress, mode=mode,), ] mhs = MultiHostSections() update_host_sections( mhs, make_nodes( config_cache, host_config, ipaddress, mode=mode, sources=sources, ), 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(CPUTracker()), source.fetcher_type, ) for source in sources ], ) assert len(mhs) == 1 key = HostKey(hostname, ipaddress, SourceType.HOST) assert key in mhs section = mhs[key] assert isinstance(section, AgentHostSections) assert len(section.sections) == len(sources) for source in sources: # yapf: disable assert ( section.sections[SectionName("section_name_%s" % source.hostname)] == [["section_content"]])
def test_multiple_sources_from_different_hosts(self, hostname, ipaddress, mode, config_cache, host_config): sources = [ ProgramSource.ds(hostname + "0", ipaddress, mode=mode, template=""), TCPSource(hostname + "1", ipaddress, mode=mode), TCPSource(hostname + "2", ipaddress, mode=mode), ] nodes = make_nodes( config_cache, host_config, ipaddress, mode=mode, sources=sources, ) mhs = MultiHostSections() update_host_sections( mhs, nodes, max_cachefile_age=0, host_config=host_config, fetcher_messages=[ FetcherMessage.from_raw_data( result.OK(source.default_raw_data), Snapshot.null(), source.fetcher_type, ) for _h, _i, sources in nodes for source in sources ], selected_sections=NO_SELECTION, ) assert len(mhs) == 1 key = HostKey(hostname, ipaddress, SourceType.HOST) assert key in mhs section = mhs[key] assert len(section.sections) == len(sources) for source in sources: # yapf: disable assert ( section.sections[SectionName("section_name_%s" % source.hostname)] == [["section_content"]])
def test_multiple_sources_from_the_same_host( self, hostname, ipaddress, mode, config_cache, host_config, ): sources = [ ProgramSource.ds( hostname, ipaddress, mode=mode, preselected_sections=AUTO_DETECT, template="", ), TCPSource( hostname, ipaddress, mode=mode, preselected_sections=AUTO_DETECT, ), ] mhs = MultiHostSections() update_host_sections( mhs, make_nodes( config_cache, host_config, ipaddress, mode=mode, sources=sources, ), max_cachefile_age=0, host_config=host_config, fetcher_messages=[ FetcherMessage.from_raw_data( result.OK(source.default_raw_data), Snapshot.null(), source.fetcher_type, ) for source in sources ], ) assert len(mhs) == 1 key = HostKey(hostname, ipaddress, SourceType.HOST) assert key in mhs section = mhs[key] assert isinstance(section, AgentHostSections) assert len(section.sections) == 1 # yapf: disable assert (section.sections[SectionName("section_name_%s" % hostname)] == len(sources) * [["section_content"]])
def test_tcpdatasource_only_from(mode, monkeypatch, res, reported, rule): ts = Scenario().add_host("hostname") ts.set_option("agent_config", {"only_from": [rule]} if rule else {}) config_cache = ts.apply(monkeypatch) source = TCPSource("hostname", "ipaddress", mode=mode) monkeypatch.setattr(config_cache, "host_extra_conf", lambda host, ruleset: ruleset) summarizer = AgentSummarizerDefault(source.exit_spec, source) assert summarizer._sub_result_only_from({"onlyfrom": reported}) == res
def test_multiple_sources_from_the_same_host( self, hostname, ipaddress, mode, config_cache, host_config, ): sources = [ ProgramSource.ds( hostname, ipaddress, mode=mode, template="", ), TCPSource( hostname, ipaddress, mode=mode, ), ] mhs = MultiHostSections() update_host_sections( mhs, make_nodes( config_cache, host_config, ipaddress, mode=mode, sources=sources, ), max_cachefile_age=0, selected_raw_sections=None, host_config=host_config, ) assert len(mhs) == 1 key = HostKey(hostname, ipaddress, SourceType.HOST) assert key in mhs section = mhs[key] assert isinstance(section, AgentHostSections) assert len(section.sections) == 1 # yapf: disable assert (section.sections[SectionName("section_name_%s" % hostname)] == len(sources) * [["section_content"]])
class TestMakeHostSectionsHosts: @pytest.fixture(autouse=False) def patch_fs(self, fs): # piggyback.store_piggyback_raw_data() writes to disk. pass @pytest.fixture(autouse=True) def patch_io(self, monkeypatch): class DummyHostSection(HostSections): def _extend_section(self, section_name, section_content): pass for fetcher in (IPMIFetcher, PiggybackFetcher, ProgramFetcher, SNMPFetcher, TCPFetcher): monkeypatch.setattr(fetcher, "__enter__", lambda self: self) monkeypatch.setattr( fetcher, "fetch", lambda self, mode, fetcher=fetcher: {} if fetcher is SNMPFetcher else b"", ) monkeypatch.setattr( Source, "parse", lambda self, raw_data, *, selection: result.OK( DummyHostSection( sections={ SectionName("section_name_%s" % self.hostname): [["section_content"]] }, cache_info={}, piggybacked_raw_data={}, )), ) @pytest.fixture def hostname(self): return "testhost" @pytest.fixture def ipaddress(self): return "1.2.3.4" @pytest.fixture def host_config(self, hostname): return config.HostConfig.make_host_config(hostname) @pytest.fixture def config_cache(self, hostname, ipaddress, monkeypatch): ts = Scenario().add_host(hostname) return ts.apply(monkeypatch) def test_no_sources(self, hostname, ipaddress, mode, config_cache, host_config): mhs = MultiHostSections() update_host_sections( mhs, make_nodes( config_cache, host_config, ipaddress, mode=mode, sources=(), ), max_cachefile_age=0, host_config=host_config, fetcher_messages=(), selected_sections=NO_SELECTION, ) # The length is not zero because the function always sets, # at least, a piggy back section. assert len(mhs) == 1 key = HostKey(hostname, ipaddress, SourceType.HOST) assert key in mhs section = mhs[key] # Public attributes from HostSections: assert not section.sections assert not section.cache_info assert not section.piggybacked_raw_data def test_one_snmp_source(self, hostname, ipaddress, mode, config_cache, host_config): mhs = MultiHostSections() update_host_sections( mhs, make_nodes( config_cache, host_config, ipaddress, mode=mode, sources=[ SNMPSource.snmp( hostname, ipaddress, mode=mode, selected_sections=NO_SELECTION, ), ], ), max_cachefile_age=0, host_config=host_config, fetcher_messages=[ FetcherMessage.from_raw_data( result.OK(SNMPRawData({})), Snapshot.null(), FetcherType.SNMP, ), ], selected_sections=NO_SELECTION, ) assert len(mhs) == 1 key = HostKey(hostname, ipaddress, SourceType.HOST) assert key in mhs section = mhs[key] assert len(section.sections) == 1 assert section.sections[SectionName("section_name_%s" % hostname)] == [["section_content"]] @pytest.mark.parametrize( "source", [ lambda hostname, ipaddress, *, mode: PiggybackSource( hostname, ipaddress, mode=mode, ), lambda hostname, ipaddress, *, mode: ProgramSource.ds( hostname, ipaddress, mode=mode, template="", ), lambda hostname, ipaddress, *, mode: TCPSource( hostname, ipaddress, mode=mode, ), ], ) 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, host_config=host_config, fetcher_messages=[ FetcherMessage.from_raw_data( result.OK(source.default_raw_data), Snapshot.null(), source.fetcher_type, ), ], selected_sections=NO_SELECTION, ) assert len(mhs) == 1 key = HostKey(hostname, ipaddress, source.source_type) assert key in mhs section = mhs[key] assert len(section.sections) == 1 assert section.sections[SectionName("section_name_%s" % hostname)] == [["section_content"]] def test_multiple_sources_from_the_same_host( self, hostname, ipaddress, mode, config_cache, host_config, ): sources = [ ProgramSource.ds( hostname, ipaddress, mode=mode, template="", ), TCPSource( hostname, ipaddress, mode=mode, ), ] mhs = MultiHostSections() update_host_sections( mhs, make_nodes( config_cache, host_config, ipaddress, mode=mode, sources=sources, ), max_cachefile_age=0, host_config=host_config, fetcher_messages=[ FetcherMessage.from_raw_data( result.OK(source.default_raw_data), Snapshot.null(), source.fetcher_type, ) for source in sources ], selected_sections=NO_SELECTION, ) assert len(mhs) == 1 key = HostKey(hostname, ipaddress, SourceType.HOST) assert key in mhs section = mhs[key] assert len(section.sections) == 1 # yapf: disable assert (section.sections[SectionName("section_name_%s" % hostname)] == len(sources) * [["section_content"]]) # shouldn't this be tested for a cluster? def test_multiple_sources_from_different_hosts(self, hostname, ipaddress, mode, config_cache, host_config): sources = [ ProgramSource.ds(hostname + "0", ipaddress, mode=mode, template=""), TCPSource(hostname + "1", ipaddress, mode=mode), TCPSource(hostname + "2", ipaddress, mode=mode), ] nodes = make_nodes( config_cache, host_config, ipaddress, mode=mode, sources=sources, ) mhs = MultiHostSections() update_host_sections( mhs, nodes, max_cachefile_age=0, host_config=host_config, fetcher_messages=[ FetcherMessage.from_raw_data( result.OK(source.default_raw_data), Snapshot.null(), source.fetcher_type, ) for _h, _i, sources in nodes for source in sources ], selected_sections=NO_SELECTION, ) assert len(mhs) == 1 key = HostKey(hostname, ipaddress, SourceType.HOST) assert key in mhs section = mhs[key] assert len(section.sections) == len(sources) for source in sources: # yapf: disable assert ( section.sections[SectionName("section_name_%s" % source.hostname)] == [["section_content"]])