okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
cd9745050
main
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 4for kind in net_dead.KINDS: 5with 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 11takes 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).""" 30import ipaddress 31real = socket .getaddrinfo 32 33def lookup (host ,* args ,** kwargs ): 34text = str (host ).strip ("[]" ).lower ()if host is not None else "" 35try : 36local = text in ("" ,"localhost" )or ipaddress .ip_address (text ).is_loopback 37except ValueError : 38local = False 39if local : 40return real (host ,* args ,** kwargs ) 41raise socket .gaierror (socket .EAI_NONAME ,"Name or service not known" ) 42 43with mock .patch .object (socket ,"getaddrinfo" ,lookup ): 44yield 45 46 47@ contextlib . contextmanager 48def dead_feed (kind :str ): 49"""Yield a feed address that is unreachable in the way `kind` says.""" 50assert kind in KINDS ,kind 51real_open = netpolicy .open_url 52 53def scaled (open_fn ,request ,** kw ): 54kw ["limits" ]= WINDOW 55kw ["gap" ]= 0.05 56return real_open (open_fn ,request ,** kw ) 57 58with contextlib .ExitStack ()as stack : 59stack .enter_context (mock .patch .object (netpolicy ,"open_url" ,scaled )) 60stack .enter_context (mock .patch .object (netpolicy ,"CONNECT_LIMITS" ,WINDOW )) 61netpolicy .HOSTS .forget () 62if kind == "offline" : 63stack .enter_context (mock .patch .object (socket ,"getaddrinfo" ,side_effect = socket .gaierror (socket .EAI_NONAME ,"Name or service not known" ))) 64yield "https://strata-feed.example.invalid/strata-feed.json" 65elif kind == "refused" : 66yield f"http://127.0.0.1: { refused_port () } /strata-feed.json" 67else : 68hole = BlackHole () 69try : 70yield f"http://127.0.0.1: { hole . port } /strata-feed.json" 71finally : 72hole .close () 73netpolicy .HOSTS .forget ()