okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge main 6c642df75 into f-own-int (round 16: network waits, robustness, Windows kill-on-close job start, GitHub and transfer fixes)cd9745050

main
3.0 KiB73 linesraw
1"""Three ways a feed host can be unreachable, for the callers that must say NOTHING at a launch (lane NET; the owner's rule: every online check skips
2silently when this computer is offline or the server is not there).
3
4    for kind in net_dead.KINDS:
5        with net_dead.dead_feed(kind) as source:
6            ...call the code with `source` as the update feed...
7
8* "offline": no name resolves (a computer with no network at all): the lookup fails at once.
9* "refused": a local port nobody listens on (a firewall that refuses, a proxy that is down).
10* "never answers": a server that accepts the connection and never sends a byte; the rule's windows (12 s, then 30 s) are scaled down so the test
11  takes a second or two, and every call that gives its own window gets the same scaled one.
12"""
13from __future__ import annotations
14
15import contextlib
16import socket
17from unittest import mock
18
19from strata_control import netpolicy
20from tests.terminal.test_netpolicy import BlackHole, refused_port
21
22KINDS = ("offline", "refused", "never answers")
23WINDOW = (0.3, 0.6)
24
25
26@contextlib.contextmanager
27def no_name_resolution():
28    """A computer with no name service: every lookup of a name other than this computer fails at once, as with no network (loopback addresses and
29    "localhost" still resolve). For the tests that stand for an offline PC: they never ask a real resolver (the test guard in conftest.py)."""
30    import ipaddress
31    real = socket.getaddrinfo
32
33    def lookup(host, *args, **kwargs):
34        text = str(host).strip("[]").lower() if host is not None else ""
35        try:
36            local = text in ("", "localhost") or ipaddress.ip_address(text).is_loopback
37        except ValueError:
38            local = False
39        if local:
40            return real(host, *args, **kwargs)
41        raise socket.gaierror(socket.EAI_NONAME, "Name or service not known")
42
43    with mock.patch.object(socket, "getaddrinfo", lookup):
44        yield
45
46
47@contextlib.contextmanager
48def dead_feed(kind: str):
49    """Yield a feed address that is unreachable in the way `kind` says."""
50    assert kind in KINDS, kind
51    real_open = netpolicy.open_url
52
53    def scaled(open_fn, request, **kw):
54        kw["limits"] = WINDOW
55        kw["gap"] = 0.05
56        return real_open(open_fn, request, **kw)
57
58    with contextlib.ExitStack() as stack:
59        stack.enter_context(mock.patch.object(netpolicy, "open_url", scaled))
60        stack.enter_context(mock.patch.object(netpolicy, "CONNECT_LIMITS", WINDOW))
61        netpolicy.HOSTS.forget()
62        if kind == "offline":
63            stack.enter_context(mock.patch.object(socket, "getaddrinfo", side_effect=socket.gaierror(socket.EAI_NONAME, "Name or service not known")))
64            yield "https://strata-feed.example.invalid/strata-feed.json"
65        elif kind == "refused":
66            yield f"http://127.0.0.1:{refused_port()}/strata-feed.json"
67        else:
68            hole = BlackHole()
69            try:
70                yield f"http://127.0.0.1:{hole.port}/strata-feed.json"
71            finally:
72                hole.close()
73        netpolicy.HOSTS.forget()