def test_update_ip_lookup_cache_empty_file(_cache_file): cache_id = "host1", 4 ip_lookup_cache = ip_lookup._get_ip_lookup_cache() ip_lookup_cache.update_cache(cache_id, "127.0.0.1") cache = ip_lookup._load_ip_lookup_cache(lock=False) assert cache[cache_id] == "127.0.0.1" cache = ip_lookup._load_ip_lookup_cache(lock=False) assert cache[cache_id] == "127.0.0.1"
def test_update_dns_cache(monkeypatch, _cache_file): 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("blub", tags={"criticality": "offline"}) ts.add_host("bla") ts.add_host("dual", tags={"address_family": "ip-v4v6"}) ts.apply(monkeypatch) assert ip_lookup.update_dns_cache() == (3, ["dual"]) # Check persisted data cache = ip_lookup._load_ip_lookup_cache(lock=False) assert cache[("blub", 4)] == "127.0.0.13" assert ("dual", 6) not in cache
def test_load_legacy_lookup_cache(_cache_file): cache_id1 = "host1", 4 cache_id2 = "host2", 4 with _cache_file.open("w", encoding="utf-8") as f: f.write(u"%r" % {"host1": "127.0.0.1", "host2": "127.0.0.2"}) cache = ip_lookup._load_ip_lookup_cache(lock=False) assert cache[cache_id1] == "127.0.0.1" assert cache[cache_id2] == "127.0.0.2"
def test_update_ip_lookup_cache_extend_existing_file(_cache_file): cache_id1 = "host1", 4 cache_id2 = "host2", 4 ip_lookup_cache = ip_lookup._get_ip_lookup_cache() ip_lookup_cache.update_cache(cache_id1, "127.0.0.1") ip_lookup_cache.update_cache(cache_id2, "127.0.0.2") cache = ip_lookup._load_ip_lookup_cache(lock=False) assert cache[cache_id1] == "127.0.0.1" assert cache[cache_id2] == "127.0.0.2"
def test_update_ip_lookup_cache_update_existing_entry(_cache_file): cache_id1 = "host1", 4 cache_id2 = "host2", 4 with _cache_file.open(mode="w", encoding="utf-8") as f: f.write(u"%r" % {cache_id1: "1", cache_id2: "2"}) ip_lookup_cache = ip_lookup._get_ip_lookup_cache() ip_lookup_cache.update_cache(cache_id1, "127.0.0.1") cache = ip_lookup._load_ip_lookup_cache(lock=False) assert cache[cache_id1] == "127.0.0.1" assert cache[cache_id2] == "2"
def test_update_ip_lookup_cache_update_existing_entry(_cache_file): cache_id1 = "host1", socket.AF_INET cache_id2 = "host2", socket.AF_INET with _cache_file.open(mode="w", encoding="utf-8") as f: f.write( u"%r" % { ip_lookup.serialize_cache_id(cache_id1): "1", ip_lookup.serialize_cache_id(cache_id2): "2", }) ip_lookup_cache = ip_lookup._get_ip_lookup_cache() ip_lookup_cache.update_cache(cache_id1, "127.0.0.1") cache = ip_lookup._load_ip_lookup_cache(lock=False) assert cache[cache_id1] == "127.0.0.1" assert cache[cache_id2] == "2"
def test_update_dns_cache(monkeypatch, _cache_file): 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("blub", tags={"criticality": "offline"}) ts.add_host("bla") ts.add_host("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._load_ip_lookup_cache(lock=False) assert cache[("blub", socket.AF_INET)] == "127.0.0.13" assert ("dual", socket.AF_INET6) not in cache