def test_multiple_sources_from_different_hosts(self, hostname, ipaddress, config_cache, host_config):
        sources = [
            ProgramSource.ds(hostname + "0", ipaddress, template=""),
            TCPSource(hostname + "1", ipaddress),
            TCPSource(hostname + "2", ipaddress),
        ]

        nodes = make_nodes(config_cache, host_config, ipaddress, sources=sources)

        host_sections = _collect_host_sections(
            nodes=nodes,
            file_cache_max_age=file_cache.MaxAge.none(),
            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,
        )[0]
        assert len(host_sections) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in host_sections

        section = host_sections[key]

        assert len(section.sections) == len(sources)
        for source in sources:
            # yapf: disable
            assert (
                section.sections[SectionName("section_name_%s" % source.hostname)]
                == [["section_content"]])
Example #2
0
    def test_multiple_sources_from_different_hosts(
        self, hostname, ipaddress, config_cache, host_config
    ):
        sources = [
            ProgramSource.ds(HostName(f"{hostname}0"), ipaddress, template=""),
            TCPSource(HostName(f"{hostname}1"), ipaddress),
            TCPSource(HostName(f"{hostname}2"), ipaddress),
        ]

        host_sections = _collect_host_sections(
            fetched=[
                (
                    source,
                    FetcherMessage.from_raw_data(
                        result.OK(source.default_raw_data),
                        Snapshot.null(),
                        source.fetcher_type,
                    ),
                )
                for source in sources
            ],
            file_cache_max_age=file_cache.MaxAge.none(),
            selected_sections=NO_SELECTION,
        )[0]

        assert set(host_sections) == {
            HostKey(HostName(f"{hostname}0"), ipaddress, SourceType.HOST),
            HostKey(HostName(f"{hostname}1"), ipaddress, SourceType.HOST),
            HostKey(HostName(f"{hostname}2"), ipaddress, SourceType.HOST),
        }

        for source in sources:
            assert host_sections[
                HostKey(source.hostname, source.ipaddress, SourceType.HOST)
            ].sections[SectionName(f"section_name_{source.hostname}")] == [["section_content"]]
    def test_multiple_sources_from_the_same_host(
        self,
        hostname,
        ipaddress,
        config_cache,
        host_config,
    ):
        sources = [
            ProgramSource.ds(hostname, ipaddress, template=""),
            TCPSource(hostname, ipaddress),
        ]

        host_sections = _collect_host_sections(
            sources=sources,
            file_cache_max_age=file_cache.MaxAge.none(),
            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,
        )[0]
        assert len(host_sections) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in host_sections

        section = host_sections[key]

        assert len(section.sections) == 1
        assert section.sections[SectionName(
            "section_name_%s" %
            hostname)] == len(sources) * [["section_content"]]
Example #4
0
    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,
            ),
        ]

        broker = ParsedSectionsBroker()
        update_host_sections(
            broker,
            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(broker) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in broker

        section = broker[key]

        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, config_cache, host_config):
        host_sections = _collect_host_sections(
            nodes=make_nodes(
                config_cache,
                host_config,
                ipaddress,
                sources=(),
            ),
            file_cache_max_age=file_cache.MaxAge.none(),
            fetcher_messages=(),
            selected_sections=NO_SELECTION,
        )[0]
        # The length is not zero because the function always sets,
        # at least, a piggy back section.
        assert len(host_sections) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in host_sections

        section = host_sections[key]

        # Public attributes from HostSections:
        assert not section.sections

    def test_one_snmp_source(self, hostname, ipaddress, config_cache,
                             host_config):
        host_sections = _collect_host_sections(
            nodes=make_nodes(
                config_cache,
                host_config,
                ipaddress,
                sources=[
                    SNMPSource.snmp(
                        hostname,
                        ipaddress,
                        selected_sections=NO_SELECTION,
                        force_cache_refresh=False,
                        on_scan_error=OnError.RAISE,
                    ),
                ],
            ),
            file_cache_max_age=file_cache.MaxAge.none(),
            fetcher_messages=[
                FetcherMessage.from_raw_data(
                    result.OK({}),
                    Snapshot.null(),
                    FetcherType.SNMP,
                ),
            ],
            selected_sections=NO_SELECTION,
        )[0]
        assert len(host_sections) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in host_sections

        section = host_sections[key]

        assert len(section.sections) == 1
        assert section.sections[SectionName("section_name_%s" %
                                            hostname)] == [["section_content"]]

    @pytest.mark.parametrize(
        "source",
        [
            PiggybackSource,
            lambda hostname, ipaddress: ProgramSource.ds(
                hostname,
                ipaddress,
                template="",
            ),
            TCPSource,
        ],
    )
    def test_one_nonsnmp_source(self, hostname, ipaddress, config_cache,
                                host_config, source):
        source = source(hostname, ipaddress)
        assert source.source_type is SourceType.HOST

        host_sections = _collect_host_sections(
            nodes=make_nodes(
                config_cache,
                host_config,
                ipaddress,
                sources=[source],
            ),
            file_cache_max_age=file_cache.MaxAge.none(),
            fetcher_messages=[
                FetcherMessage.from_raw_data(
                    result.OK(source.default_raw_data),
                    Snapshot.null(),
                    source.fetcher_type,
                ),
            ],
            selected_sections=NO_SELECTION,
        )[0]
        assert len(host_sections) == 1

        key = HostKey(hostname, ipaddress, source.source_type)
        assert key in host_sections

        section = host_sections[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,
        config_cache,
        host_config,
    ):
        sources = [
            ProgramSource.ds(hostname, ipaddress, template=""),
            TCPSource(hostname, ipaddress),
        ]

        host_sections = _collect_host_sections(
            nodes=make_nodes(config_cache,
                             host_config,
                             ipaddress,
                             sources=sources),
            file_cache_max_age=file_cache.MaxAge.none(),
            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,
        )[0]
        assert len(host_sections) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in host_sections

        section = host_sections[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, config_cache, host_config):
        sources = [
            ProgramSource.ds(hostname + "0", ipaddress, template=""),
            TCPSource(hostname + "1", ipaddress),
            TCPSource(hostname + "2", ipaddress),
        ]

        nodes = make_nodes(config_cache, host_config, ipaddress, sources=sources)

        host_sections = _collect_host_sections(
            nodes=nodes,
            file_cache_max_age=file_cache.MaxAge.none(),
            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,
        )[0]
        assert len(host_sections) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in host_sections

        section = host_sections[key]

        assert len(section.sections) == len(sources)
        for source in sources:
            # yapf: disable
            assert (
                section.sections[SectionName("section_name_%s" % source.hostname)]
                == [["section_content"]])
Example #6
0
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()
        ts.add_host(hostname)
        return ts.apply(monkeypatch)

    def test_no_sources(self, hostname, ipaddress, config_cache, host_config):
        host_sections = _collect_host_sections(
            fetched=(),
            file_cache_max_age=file_cache.MaxAge.none(),
            selected_sections=NO_SELECTION,
        )[0]
        assert not host_sections

    def test_one_snmp_source(self, hostname, ipaddress, config_cache, host_config):
        raw_data: SNMPRawData = {}
        host_sections = _collect_host_sections(
            fetched=[
                (
                    SNMPSource.snmp(
                        hostname,
                        ipaddress,
                        selected_sections=NO_SELECTION,
                        force_cache_refresh=False,
                        on_scan_error=OnError.RAISE,
                    ),
                    FetcherMessage.from_raw_data(
                        result.OK(raw_data),
                        Snapshot.null(),
                        FetcherType.SNMP,
                    ),
                )
            ],
            file_cache_max_age=file_cache.MaxAge.none(),
            selected_sections=NO_SELECTION,
        )[0]
        assert len(host_sections) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in host_sections

        section = host_sections[key]

        assert len(section.sections) == 1
        assert section.sections[SectionName("section_name_%s" % hostname)] == [["section_content"]]

    @pytest.mark.parametrize(
        "source",
        [
            PiggybackSource,
            lambda hostname, ipaddress: ProgramSource.ds(
                hostname,
                ipaddress,
                template="",
            ),
            TCPSource,
        ],
    )
    def test_one_nonsnmp_source(self, hostname, ipaddress, config_cache, host_config, source):
        source = source(hostname, ipaddress)
        assert source.source_type is SourceType.HOST

        host_sections = _collect_host_sections(
            fetched=[
                (
                    source,
                    FetcherMessage.from_raw_data(
                        result.OK(source.default_raw_data),
                        Snapshot.null(),
                        source.fetcher_type,
                    ),
                )
            ],
            file_cache_max_age=file_cache.MaxAge.none(),
            selected_sections=NO_SELECTION,
        )[0]
        assert len(host_sections) == 1

        key = HostKey(hostname, ipaddress, source.source_type)
        assert key in host_sections

        section = host_sections[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,
        config_cache,
        host_config,
    ):
        sources = [
            ProgramSource.ds(hostname, ipaddress, template=""),
            TCPSource(hostname, ipaddress),
        ]

        host_sections = _collect_host_sections(
            fetched=[
                (
                    source,
                    FetcherMessage.from_raw_data(
                        result.OK(source.default_raw_data),
                        Snapshot.null(),
                        source.fetcher_type,
                    ),
                )
                for source in sources
            ],
            file_cache_max_age=file_cache.MaxAge.none(),
            selected_sections=NO_SELECTION,
        )[0]
        assert len(host_sections) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in host_sections

        section = host_sections[key]

        assert len(section.sections) == 1
        assert section.sections[SectionName("section_name_%s" % hostname)] == len(sources) * [
            ["section_content"]
        ]

    def test_multiple_sources_from_different_hosts(
        self, hostname, ipaddress, config_cache, host_config
    ):
        sources = [
            ProgramSource.ds(HostName(f"{hostname}0"), ipaddress, template=""),
            TCPSource(HostName(f"{hostname}1"), ipaddress),
            TCPSource(HostName(f"{hostname}2"), ipaddress),
        ]

        host_sections = _collect_host_sections(
            fetched=[
                (
                    source,
                    FetcherMessage.from_raw_data(
                        result.OK(source.default_raw_data),
                        Snapshot.null(),
                        source.fetcher_type,
                    ),
                )
                for source in sources
            ],
            file_cache_max_age=file_cache.MaxAge.none(),
            selected_sections=NO_SELECTION,
        )[0]

        assert set(host_sections) == {
            HostKey(HostName(f"{hostname}0"), ipaddress, SourceType.HOST),
            HostKey(HostName(f"{hostname}1"), ipaddress, SourceType.HOST),
            HostKey(HostName(f"{hostname}2"), ipaddress, SourceType.HOST),
        }

        for source in sources:
            assert host_sections[
                HostKey(source.hostname, source.ipaddress, SourceType.HOST)
            ].sections[SectionName(f"section_name_{source.hostname}")] == [["section_content"]]