def test_fetcher_deserialization_snmpv3_credentials(self, fetcher: SNMPFetcher) -> None: # snmp_config is Final, but for testing... fetcher.snmp_config = fetcher.snmp_config._replace( # type: ignore[misc] credentials=("authNoPriv", "md5", "md5", "abc"), ) other = type(fetcher).from_json(json_identity(fetcher.to_json())) assert other.snmp_config.credentials == fetcher.snmp_config.credentials
def test_fetch_from_io_non_empty(self, monkeypatch: MonkeyPatch, fetcher: SNMPFetcher) -> None: table = [["1"]] monkeypatch.setattr( snmp_table, "get_snmp_table", lambda *_, **__: table, ) section_name = SectionName("pim") monkeypatch.setattr( fetcher, "sections", { section_name: SectionMeta( checking=True, disabled=False, redetect=False, fetch_interval=None, ), }, ) assert fetcher.fetch(Mode.INVENTORY) == result.OK({}) # 'pim' is not an inventory section assert fetcher.fetch(Mode.CHECKING) == result.OK({section_name: [table]}) monkeypatch.setattr( snmp, "gather_available_raw_section_names", lambda *_, **__: {SectionName("pim")}, ) assert fetcher.fetch(Mode.DISCOVERY) == result.OK({section_name: [table]})
def test_mode_checking_not_do_status_data_inventory( self, monkeypatch: MonkeyPatch, fetcher: SNMPFetcher) -> None: monkeypatch.setattr(fetcher, "do_status_data_inventory", False) monkeypatch.setattr( snmp, "gather_available_raw_section_names", lambda *_, **__: fetcher._get_detected_sections(Mode.CHECKING), ) assert fetcher.fetch(Mode.CHECKING) == result.OK({})
def test_mode_checking_do_status_data_inventory( self, set_sections: List[List[str]], monkeypatch: MonkeyPatch, fetcher: SNMPFetcher ) -> None: table = set_sections monkeypatch.setattr(fetcher, "do_status_data_inventory", True) monkeypatch.setattr( snmp, "gather_available_raw_section_names", lambda *_, **__: fetcher._get_detected_sections(Mode.CHECKING), ) assert fetcher.fetch(Mode.CHECKING) == result.OK({SectionName("pim"): [table]})
def test_fetch_from_io_partially_empty( self, monkeypatch: MonkeyPatch, fetcher: SNMPFetcher ) -> None: section_name = SectionName("pum") monkeypatch.setattr( fetcher, "sections", { section_name: SectionMeta( checking=True, disabled=False, redetect=False, fetch_interval=None, ), }, ) table = [["1"]] monkeypatch.setattr( snmp_table, "get_snmp_table", lambda tree, **__: table if tree.base == fetcher.plugin_store[section_name].trees[0].base else [], ) assert fetcher.fetch(Mode.CHECKING) == result.OK({section_name: [table, []]})
def fetcher_pysnmp(self, file_cache: SNMPFileCache) -> SNMPFetcher: return SNMPFetcher( file_cache, sections={}, on_error=OnError.RAISE, missing_sys_description=False, do_status_data_inventory=False, section_store_path="/tmp/db", snmp_config=SNMPHostConfig( is_ipv6_primary=False, hostname=HostName("bob"), ipaddress="1.2.3.4", credentials="public", port=42, is_bulkwalk_host=False, is_snmpv2or3_without_bulkwalk_host=False, bulk_walk_size_of=0, timing={}, oid_range_limits={}, snmpv3_contexts=[], character_encoding=None, is_usewalk_host=False, snmp_backend=SNMPBackendEnum.PYSNMP if not cmk_version.is_raw_edition() else SNMPBackendEnum.CLASSIC, ), )
def fetcher(self, file_cache): return SNMPFetcher( file_cache, sections={}, on_error="raise", missing_sys_description=False, do_status_data_inventory=False, section_store_path="/tmp/db", snmp_config=SNMPHostConfig( is_ipv6_primary=False, hostname="bob", ipaddress="1.2.3.4", credentials="public", port=42, is_bulkwalk_host=False, is_snmpv2or3_without_bulkwalk_host=False, bulk_walk_size_of=0, timing={}, oid_range_limits=[], snmpv3_contexts=[], character_encoding=None, is_usewalk_host=False, snmp_backend=SNMPBackendEnum.CLASSIC, ), )
def test_fetcher_pysnmp_backend_deserialization( self, fetcher_pysnmp: SNMPFetcher) -> None: other = type(fetcher_pysnmp).from_json( json_identity(fetcher_pysnmp.to_json())) assert other.snmp_config.snmp_backend == ( SNMPBackendEnum.PYSNMP if not cmk_version.is_raw_edition() else SNMPBackendEnum.CLASSIC)
def test_fetcher_deserialization(self, fetcher: SNMPFetcher) -> None: other = type(fetcher).from_json(json_identity(fetcher.to_json())) assert isinstance(other, SNMPFetcher) assert other.plugin_store == fetcher.plugin_store assert other.checking_sections == fetcher.checking_sections assert other.on_error == fetcher.on_error assert other.missing_sys_description == fetcher.missing_sys_description assert other.snmp_config == fetcher.snmp_config assert other.snmp_config.snmp_backend == SNMPBackendEnum.CLASSIC
def test_fetch_from_io_empty(self, monkeypatch: MonkeyPatch, fetcher: SNMPFetcher) -> None: monkeypatch.setattr( snmp_table, "get_snmp_table", lambda *_, **__: [], ) monkeypatch.setattr( snmp, "gather_available_raw_section_names", lambda *_, **__: {SectionName("pam")}, ) assert fetcher.fetch(Mode.DISCOVERY) == result.OK({SectionName("pam"): [[]]})
def test_fetch_reading_cache_in_discovery_mode(self, fetcher: SNMPFetcher) -> None: assert isinstance(fetcher.file_cache, StubFileCache) # For mypy assert fetcher.file_cache.cache == b"cached_section" assert fetcher.fetch(Mode.DISCOVERY) == result.OK(b"cached_section")