def test_disable_read_to_file_cache(monkeypatch, fs): # Beginning of setup. Scenario().add_host("hostname").apply(monkeypatch) source = SNMPDataSource("hostname", "ipaddress") source.set_max_cachefile_age(999) source.set_may_use_cache_file() # Patch I/O: It is good enough to patch `store.save_file()` # as pyfakefs takes care of the rest. monkeypatch.setattr( store, "save_file", lambda path, contents: fs.create_file(path, contents=contents)) file_cache = FileCache.from_source(source) table = [] # type: SNMPTable raw_data = {"X": table} # type: RawSNMPData # End of setup. assert not source.is_agent_cache_disabled() file_cache = FileCache.from_source(source) file_cache.write(raw_data) assert file_cache.path.exists() assert file_cache.read() == raw_data source.disable_data_source_cache() assert source.is_agent_cache_disabled() file_cache = FileCache.from_source(source) assert file_cache.read() is None
def test_disable_data_source_cache_no_write(mocker, monkeypatch): Scenario().add_host("hostname").apply(monkeypatch) source = SNMPDataSource("hostname", "ipaddress") source.disable_data_source_cache() disabled_checker = mocker.patch.object(source, "is_agent_cache_disabled") assert source._write_cache_file("X") is None disabled_checker.assert_called_once()
def test_describe_with_ipaddress(monkeypatch): hostname = "testhost" ipaddress = "127.0.0.1" Scenario().add_host(hostname).apply(monkeypatch) source = SNMPDataSource(hostname, ipaddress) default = "SNMP (Community: 'public', Bulk walk: no, Port: 161, Inline: no)" assert source.describe() == default
def test_describe_without_ipaddress_raises_exception(monkeypatch): hostname = "testhost" ipaddress = None Scenario().add_host(hostname).apply(monkeypatch) source = SNMPDataSource(hostname, ipaddress) # TODO: This does not seem to be the expected exception. with pytest.raises(NotImplementedError): source.describe()
def test_write_and_read_file_cache(monkeypatch, fs): # Beginning of setup. Scenario().add_host("hostname").apply(monkeypatch) source = SNMPDataSource("hostname", "ipaddress") source.set_max_cachefile_age(999) source.set_may_use_cache_file() # Patch I/O: It is good enough to patch `store.save_file()` # as pyfakefs takes care of the rest. monkeypatch.setattr( store, "save_file", lambda path, contents: fs.create_file(path, contents=contents)) file_cache = source._make_file_cache() table: SNMPTable = [] raw_data: SNMPRawData = {SectionName("X"): table} # End of setup. assert not source.is_agent_cache_disabled() assert not file_cache.path.exists() file_cache.write(raw_data) assert file_cache.path.exists() assert file_cache.read() == raw_data # Another one bites the dust. file_cache = source._make_file_cache() assert file_cache.read() == raw_data
def test_one_snmp_source(self, hostname, ipaddress, mode, config_cache, host_config): mhs = make_host_sections( config_cache, host_config, ipaddress, mode=mode, sources=[ SNMPDataSource(configurator=SNMPConfigurator.snmp( hostname, ipaddress, mode=mode, ),), ], 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"]]
def test_attribute_defaults(monkeypatch, ipaddress): hostname = "testhost" Scenario().add_host(hostname).apply(monkeypatch) source = SNMPDataSource(hostname, ipaddress) assert source._hostname == hostname assert source._ipaddress == ipaddress assert source.id() == "snmp" assert source.title() == "SNMP" assert source._cpu_tracking_id() == "snmp" assert source.get_do_snmp_scan() is False # From the base class assert source.name() == ("snmp:%s:%s" % (hostname, ipaddress if ipaddress else "")) assert source.is_agent_cache_disabled() is True assert source.get_may_use_cache_file() is False assert source.exception() is None
def source(self, hostname, mode): return SNMPDataSource(configurator=SNMPConfigurator( hostname, "1.2.3.4", mode=mode, source_type=SourceType.HOST, id_="snmp_id", cpu_tracking_id="snmp_cpu_id", title="snmp title", ))
def test_get_check_plugin_names_requires_type_filter_function_and_ipaddress(monkeypatch, ipaddress): hostname = "testhost" Scenario().add_host(hostname).apply(monkeypatch) source = SNMPDataSource(hostname, ipaddress) with pytest.raises(Exception): source.get_check_plugin_names() # One filter function is defined in cmk.base.inventory and another one in snmp_scan. def dummy_filter_func(host_config, on_error, do_snmp_scan, for_mgmt_board=False): return set() source.set_check_plugin_name_filter(dummy_filter_func) if ipaddress is None: with pytest.raises(NotImplementedError): source.get_check_plugin_names() else: assert source.get_check_plugin_names() == set()
def test_disable_data_source_cache_no_read(mocker, monkeypatch): Scenario().add_host("hostname").apply(monkeypatch) source = SNMPDataSource("hostname", "ipaddress") source.set_max_cachefile_age(999) source.disable_data_source_cache() mocker.patch.object(os.path, "exists", return_value=True) disabled_checker = mocker.patch.object(source, "is_agent_cache_disabled") assert source._read_cache_file() is None disabled_checker.assert_called_once()
def test_disable_data_source_cache(monkeypatch): Scenario().add_host("hostname").apply(monkeypatch) source = SNMPDataSource("hostname", "ipaddress") assert not source.is_agent_cache_disabled() source.disable_data_source_cache() assert source.is_agent_cache_disabled()
def test_detector_requires_type_filter_function_and_ipaddress( monkeypatch, ipaddress): hostname = "testhost" Scenario().add_host(hostname).apply(monkeypatch) source = SNMPDataSource(hostname, ipaddress) with pytest.raises(Exception): source._get_raw_section_names_to_process() def dummy_filter_func(sections, on_error, do_snmp_scan, *, binary_host, backend): return set() source.set_check_plugin_name_filter(dummy_filter_func, inventory=False) if ipaddress is None: with pytest.raises(NotImplementedError): source._get_raw_section_names_to_process() else: assert source._get_raw_section_names_to_process() == set()
def test_execute_with_canned_responses(monkeypatch): # Beginning of setup. tree = SNMPTree( base='.1.3.6.1.4.1.13595.2.2.3.1', oids=[ OIDEnd(), snmp_utils.OIDBytes("16"), ], ) name = "acme_agent_sessions" # Must be in config.registered_snmp_sections # Replace I/O with canned responses. monkeypatch.setattr(SNMPDataSource, "get_check_plugin_names", lambda *args, **kwargs: {name}) monkeypatch.setattr(snmp, "get_snmp_table_cached", lambda *args: tree) hostname = "testhost" ipaddress = "127.0.0.1" Scenario().add_host(hostname).apply(monkeypatch) source = SNMPDataSource(hostname, ipaddress) # End of setup. assert source._execute() == {name: [tree]}
def source_fixture(scenario, configurator): return SNMPDataSource(configurator)
def test_source_requires_ipaddress(monkeypatch): hostname = "testhost" Scenario().add_host(hostname).apply(monkeypatch) with pytest.raises(TypeError): SNMPDataSource(hostname, None)