예제 #1
0
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
예제 #2
0
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