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_write_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 = source._make_file_cache() table: SNMPTable = [] raw_data: SNMPRawData = {SectionName("X"): table} # End of setup. source.disable_data_source_cache() assert source.is_agent_cache_disabled() # Another one bites the dust. file_cache = source._make_file_cache() file_cache.write(raw_data) assert not file_cache.path.exists() assert file_cache.read() is None
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_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_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()