Example #1
0
    def test_update_empty_file(self, tmp_path: Path) -> None:
        cache_id = HostName("host1"), socket.AF_INET
        ip_lookup_cache = ip_lookup.IPLookupCache({})
        ip_lookup_cache[cache_id] = "127.0.0.1"

        new_cache_instance = ip_lookup.IPLookupCache({})
        new_cache_instance.load_persisted()
        assert new_cache_instance[cache_id] == "127.0.0.1"
Example #2
0
    def test_update_without_persistence(self, tmp_path: Path) -> None:
        cache_id1 = HostName("host1"), socket.AF_INET

        ip_lookup_cache = ip_lookup.IPLookupCache({})
        ip_lookup_cache[cache_id1] = "0.0.0.0"

        with ip_lookup_cache.persisting_disabled():
            ip_lookup_cache[cache_id1] = "127.0.0.1"

        assert ip_lookup_cache[cache_id1] == "127.0.0.1"

        new_cache_instance = ip_lookup.IPLookupCache({})
        new_cache_instance.load_persisted()
        assert new_cache_instance[cache_id1] == "0.0.0.0"
Example #3
0
    def test_update_existing_entry(self, tmp_path: Path) -> None:
        cache_id1 = HostName("host1"), socket.AF_INET
        cache_id2 = HostName("host2"), socket.AF_INET

        ip_lookup_cache = ip_lookup.IPLookupCache({
            cache_id1: "1",
            cache_id2: "2",
        })
        ip_lookup_cache.save_persisted()

        ip_lookup_cache[cache_id1] = "127.0.0.1"

        new_cache_instance = ip_lookup.IPLookupCache({})
        new_cache_instance.load_persisted()
        assert new_cache_instance[cache_id1] == "127.0.0.1"
        assert new_cache_instance[cache_id2] == "2"
Example #4
0
    def test_load_invalid_syntax(self, tmp_path: Path) -> None:
        with ip_lookup.IPLookupCache.PATH.open(mode="w",
                                               encoding="utf-8") as f:
            f.write("{...")

        cache = ip_lookup.IPLookupCache({})
        cache.load_persisted()
        assert not cache
Example #5
0
    def test_clear(self, tmp_path: Path) -> None:
        ip_lookup.IPLookupCache({
            (HostName("host1"), socket.AF_INET):
            "127.0.0.1"
        }).save_persisted()

        ip_lookup_cache = ip_lookup.IPLookupCache({})
        ip_lookup_cache.load_persisted()
        assert ip_lookup_cache[(HostName("host1"),
                                socket.AF_INET)] == "127.0.0.1"

        ip_lookup_cache.clear()

        assert not ip_lookup_cache

        ip_lookup_cache = ip_lookup.IPLookupCache({})
        ip_lookup_cache.load_persisted()
        assert not ip_lookup_cache
Example #6
0
    def test_load_legacy(self, tmp_path: Path) -> None:
        cache_id1 = HostName("host1"), socket.AF_INET
        cache_id2 = HostName("host2"), socket.AF_INET

        with ip_lookup.IPLookupCache.PATH.open("w", encoding="utf-8") as f:
            f.write(repr({"host1": "127.0.0.1", "host2": "127.0.0.2"}))

        cache = ip_lookup.IPLookupCache({})
        cache.load_persisted()
        assert cache[cache_id1] == "127.0.0.1"
        assert cache[cache_id2] == "127.0.0.2"
Example #7
0
def test_update_dns_cache(monkeypatch: MonkeyPatch) -> None:
    def _getaddrinfo(host,
                     port,
                     family=None,
                     socktype=None,
                     proto=None,
                     flags=None):
        # Needs to return [(family, type, proto, canonname, sockaddr)] but only
        # caring about the address
        return {
            ("blub", socket.AF_INET):
            [(family, None, None, None, ("127.0.0.13", 1337))],
            ("bla", socket.AF_INET):
            [(family, None, None, None, ("127.0.0.37", 1337))],
            ("dual", socket.AF_INET):
            [(family, None, None, None, ("127.0.0.42", 1337))],
        }[(host, family)]

    monkeypatch.setattr(socket, "getaddrinfo", _getaddrinfo)

    ts = Scenario()
    ts.add_host(HostName("blub"), tags={"criticality": "offline"})
    ts.add_host(HostName("bla"))
    ts.add_host(HostName("dual"), tags={"address_family": "ip-v4v6"})
    ts.apply(monkeypatch)

    config_cache = config.get_config_cache()
    assert (ip_lookup.update_dns_cache(
        host_configs=(config_cache.get_host_config(hn)
                      for hn in config_cache.all_active_hosts()),
        configured_ipv4_addresses={},
        configured_ipv6_addresses={},
        simulation_mode=False,
        override_dns=None,
    ) == (3, ["dual"]))

    # Check persisted data
    cache = ip_lookup.IPLookupCache({})
    cache.load_persisted()
    assert cache[(HostName("blub"), socket.AF_INET)] == "127.0.0.13"
    assert cache.get((HostName("dual"), socket.AF_INET6)) is None
Example #8
0
 def test_repr(self) -> None:
     assert isinstance(repr(ip_lookup.IPLookupCache({})), str)