Example #1
0
    def test_multiple_sources_from_different_hosts(self, hostname, ipaddress, config_cache, host_config):
        sources = [
            DSProgramDataSource(
                hostname + "0", ipaddress,
                configurator=DSProgramConfigurator(hostname + "0", ipaddress,
                                                   template="",),),
            TCPDataSource(hostname + "1", ipaddress),
            TCPDataSource(hostname + "2", ipaddress),
        ]

        mhs = make_host_sections(
            config_cache,
            host_config,
            ipaddress,
            sources=sources,
            max_cachefile_age=0,
            selected_raw_sections=None,
        )
        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_template_translation(self, monkeypatch, ipaddress):
        template = "<NOTHING>x<IP>x<HOST>x<host>x<ip>x"
        hostname = "testhost"
        Scenario().add_host(hostname).apply(monkeypatch)
        source = DSProgramDataSource(hostname, ipaddress, template)

        assert source.source_cmdline == "<NOTHING>x%sx%sx<host>x<ip>x" % (
            ipaddress if ipaddress is not None else "", hostname)
    def test_attribute_defaults(self, monkeypatch, ipaddress):
        template = ""
        hostname = "testhost"
        Scenario().add_host(hostname).apply(monkeypatch)
        source = DSProgramDataSource(
            hostname,
            ipaddress,
            configurator=DSProgramConfigurator(
                hostname,
                ipaddress,
                template=template,
            ),
        )

        assert source.id == "agent"
        assert source.name() == ""
        # ProgramDataSource
        assert source._cpu_tracking_id == "ds"
        assert source.configurator.cmdline == ""
        assert source.configurator.stdin is None
        assert source.configurator.description == "Program: "
    def test_attribute_defaults(self, monkeypatch, ipaddress):
        template = ""
        hostname = "testhost"
        Scenario().add_host(hostname).apply(monkeypatch)
        source = DSProgramDataSource(hostname, ipaddress, template)

        assert source.id() == "agent"
        assert source.name() == ""
        # ProgramDataSource
        assert source._cpu_tracking_id() == "ds"
        assert source.source_cmdline == ""
        assert source.source_stdin is None
        assert source.describe() == "Program: "
Example #5
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(ABCHostSections):
            def _extend_section(self, section_name, section_content):
                pass

        monkeypatch.setattr(
            ABCDataSource, "run", lambda self, *, selected_raw_sections: DummyHostSection(
                sections={SectionName("section_name_%s" % self.hostname): [["section_content"]]},
                cache_info={},
                piggybacked_raw_data={},
                persisted_sections="",
            ))

    @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):
        mhs = make_host_sections(
            config_cache,
            host_config,
            ipaddress,
            sources=[],
            max_cachefile_age=0,
            selected_raw_sections=None,
        )
        # 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]
        assert isinstance(section, AgentHostSections)

        # Public attributes from ABCHostSections:
        assert not section.sections
        assert not section.cache_info
        assert not section.piggybacked_raw_data
        assert not section.persisted_sections

    def test_one_snmp_source(self, hostname, ipaddress, config_cache, host_config):
        mhs = make_host_sections(
            config_cache,
            host_config,
            ipaddress,
            sources=[SNMPDataSource(hostname, ipaddress)],
            max_cachefile_age=0,
            selected_raw_sections=None,
        )
        assert len(mhs) == 1

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

        section = mhs[key]
        assert isinstance(section, SNMPHostSections)

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

    @pytest.mark.parametrize("source", [
        PiggyBackDataSource,
        lambda hostname, ipaddress: DSProgramDataSource(
            hostname,
            ipaddress,
            configurator=DSProgramConfigurator(hostname, ipaddress, template=""),
        ),
        TCPDataSource,
    ])
    def test_one_nonsnmp_source(self, hostname, ipaddress, config_cache, host_config, source):
        source = source(hostname, ipaddress)
        assert source.source_type is SourceType.HOST

        mhs = make_host_sections(
            config_cache,
            host_config,
            ipaddress,
            sources=[source],
            max_cachefile_age=0,
            selected_raw_sections=None,
        )
        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_multiple_sources_from_the_same_host(
        self,
        hostname,
        ipaddress,
        config_cache,
        host_config,
    ):
        sources = [
            DSProgramDataSource(
                hostname,
                ipaddress,
                configurator=DSProgramConfigurator(hostname, ipaddress, template=""),
            ),
            TCPDataSource(hostname, ipaddress),
        ]

        mhs = make_host_sections(
            config_cache,
            host_config,
            ipaddress,
            sources=sources,
            max_cachefile_age=0,
            selected_raw_sections=None,
        )
        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_multiple_sources_from_different_hosts(self, hostname, ipaddress, config_cache, host_config):
        sources = [
            DSProgramDataSource(
                hostname + "0", ipaddress,
                configurator=DSProgramConfigurator(hostname + "0", ipaddress,
                                                   template="",),),
            TCPDataSource(hostname + "1", ipaddress),
            TCPDataSource(hostname + "2", ipaddress),
        ]

        mhs = make_host_sections(
            config_cache,
            host_config,
            ipaddress,
            sources=sources,
            max_cachefile_age=0,
            selected_raw_sections=None,
        )
        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"]])