示例#1
0
    def test_check_intervals_updates_persisted(self, logger, monkeypatch):
        monkeypatch.setattr(time, "time", lambda c=itertools.count(1000, 50): next(c))

        section_store = MockStore(
            "/dev/null",
            PersistedSections[SNMPRawDataSection](
                {
                    SectionName("section"): (0, 0, [["old"]]),
                }
            ),
            logger=logger,
        )
        _new: Sequence[SNMPRawDataSection] = [["new"]]  # For the type checker only
        raw_data: SNMPRawData = {SectionName("section"): _new}
        parser = SNMPParser(
            HostName("testhost"),
            section_store,
            check_intervals={SectionName("section"): 42},
            keep_outdated=True,
            logger=logger,
        )
        shs = parser.parse(raw_data, selection=NO_SELECTION)
        assert shs.sections == {SectionName("section"): [["new"]]}
        assert shs.cache_info == {}
        assert shs.piggybacked_raw_data == {}
        assert section_store.load() == PersistedSections[SNMPRawDataSection](
            {
                SectionName("section"): (1000, 1042, [["new"]]),
            }
        )
示例#2
0
    def test_keep_outdated_true(self, logger, monkeypatch):
        monkeypatch.setattr(time, "time", lambda c=itertools.count(1000, 50): next(c))

        section_store = MockStore(
            "/dev/null",
            PersistedSections[SNMPRawDataSection](
                {
                    SectionName("section"): (500, 600, [["old"]]),
                }
            ),
            logger=logger,
        )
        parser = SNMPParser(
            HostName("testhost"),
            section_store,
            check_intervals={SectionName("section"): 42},
            keep_outdated=True,
            logger=logger,
        )
        shs = parser.parse({}, selection=NO_SELECTION)
        assert shs.sections == {SectionName("section"): [["old"]]}
        assert shs.cache_info == {
            SectionName("section"): (500, 100),
        }
        assert shs.piggybacked_raw_data == {}
        assert not section_store.load() == PersistedSections[SNMPRawDataSection](
            {
                SectionName("section"): (1000, 1042, [["old"]]),
            }
        )
示例#3
0
    def test_update_with_persisted_and_store(self, logger):
        section_store = MockStore(
            "/dev/null",
            PersistedSections[SNMPRawDataSection](
                {
                    SectionName("stored"): (0, 0, [["old"]]),
                }
            ),
            logger=logger,
        )
        _new: Sequence[SNMPRawDataSection] = [["new"]]  # For the type checker only
        raw_data: SNMPRawData = {SectionName("fresh"): _new}
        parser = SNMPParser(
            HostName("testhost"),
            section_store,
            check_intervals={},
            keep_outdated=True,
            logger=logger,
        )

        shs = parser.parse(raw_data, selection=NO_SELECTION)
        assert shs.sections == {
            SectionName("stored"): [["old"]],
            SectionName("fresh"): [["new"]],
        }
        assert shs.cache_info == {SectionName("stored"): (0, 0)}
        assert shs.piggybacked_raw_data == {}
        assert section_store.load() == {
            SectionName("stored"): (0, 0, [["old"]]),
        }
示例#4
0
    def test_update_with_empty_persisted(self, logger):
        section_store = MockStore(
            "/dev/null",
            PersistedSections[SNMPRawDataSection](
                {
                    SectionName("stored"): (0, 0, [["old"]]),
                }
            ),
            logger=logger,
        )
        raw_data: SNMPRawData = {}
        parser = SNMPParser(
            HostName("testhost"),
            section_store,
            check_intervals={},
            keep_outdated=True,
            logger=logger,
        )

        shs = parser.parse(raw_data, selection=NO_SELECTION)
        assert shs.sections == {SectionName("stored"): [["old"]]}
        assert shs.cache_info == {SectionName("stored"): (0, 0)}
        assert shs.piggybacked_raw_data == {}
        assert section_store.load() == {
            SectionName("stored"): (0, 0, [["old"]]),
        }
示例#5
0
    def test_update_with_empty_store_and_persisted(self):
        section_store = MockStore(PersistedSections[SNMPRawDataSection]({}))
        raw_data: SNMPRawData = {}
        parser = SNMPParser(
            "testhost",
            section_store,
            check_intervals={},
            keep_outdated=True,
            logger=logging.getLogger("test"),
        )

        shs = parser.parse(raw_data, selection=NO_SELECTION)
        assert shs.sections == {}
        assert shs.cache_info == {}
        assert shs.piggybacked_raw_data == {}
        assert section_store.load() == {}
示例#6
0
    def test_update_with_empty_store(self):
        section_store = MockStore(PersistedSections[SNMPRawDataSection]({}))
        _new: SNMPRawDataSection = [["new"]]  # For the type checker only
        raw_data: SNMPRawData = {SectionName("fresh"): _new}
        parser = SNMPParser(
            "testhost",
            section_store,
            check_intervals={},
            keep_outdated=True,
            logger=logging.getLogger("test"),
        )

        shs = parser.parse(raw_data, selection=NO_SELECTION)
        assert shs.sections == {SectionName("fresh"): [["new"]]}
        assert shs.cache_info == {}
        assert shs.piggybacked_raw_data == {}
        assert section_store.load() == {}
示例#7
0
 def parser(self, hostname):
     return SNMPParser(
         hostname,
         SectionStore(
             "/tmp/store",
             logger=logging.Logger("test"),
         ),
         check_intervals={},
         keep_outdated=True,
         logger=logging.Logger("test"),
     )
示例#8
0
    def test_keep_outdated_false(self, monkeypatch):
        monkeypatch.setattr(time,
                            "time",
                            lambda c=itertools.count(1000, 50): next(c))

        section_store = MockStore(PersistedSections[SNMPRawDataSection]({
            SectionName("section"): (500, 600, [["old"]]),
        }))
        parser = SNMPParser(
            "testhost",
            section_store,
            check_intervals={SectionName("section"): 42},
            keep_outdated=False,
            logger=logging.getLogger("test"),
        )
        shs = parser.parse({}, selection=NO_SELECTION)
        assert shs.sections == {}
        assert shs.cache_info == {}
        assert shs.piggybacked_raw_data == {}
        assert section_store.load() == {}
示例#9
0
 def _make_parser(self) -> SNMPParser:
     host_config = config.HostConfig.make_host_config(self.hostname)
     return SNMPParser(
         self.hostname,
         SectionStore[SNMPRawDataSection](
             self.persisted_sections_file_path,
             logger=self._logger,
         ),
         check_intervals={
             section_name: host_config.snmp_fetch_interval(section_name)
             for section_name in self._make_checking_sections()
         },
         keep_outdated=self.use_outdated_persisted_sections,
         logger=self._logger,
     )