From: Daniele Varrazzo Date: Sat, 6 Jan 2024 16:10:58 +0000 (+0100) Subject: refactor(test): make resolution monkeypatching common fixtures X-Git-Tag: 3.1.17~3^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=76b9b4028cf3bf18d54e4e1b8e209184ac7a42a4;p=thirdparty%2Fpsycopg.git refactor(test): make resolution monkeypatching common fixtures --- diff --git a/tests/conftest.py b/tests/conftest.py index 6647cfd9e..db49ffdc8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,6 +8,7 @@ import pytest pytest_plugins = ( "tests.fix_db", "tests.fix_pq", + "tests.fix_dns", "tests.fix_mypy", "tests.fix_faker", "tests.fix_proxy", diff --git a/tests/fix_dns.py b/tests/fix_dns.py new file mode 100644 index 000000000..4a538e5d6 --- /dev/null +++ b/tests/fix_dns.py @@ -0,0 +1,61 @@ +import asyncio +import socket + +import pytest + + +@pytest.fixture +def fake_resolve(monkeypatch): + """ + Fixture to return known name from name resolution. + """ + fake_hosts = { + "localhost": ["127.0.0.1"], + "foo.com": ["1.1.1.1"], + "qux.com": ["2.2.2.2"], + "dup.com": ["3.3.3.3", "3.3.3.4"], + "alot.com": [f"4.4.4.{n}" for n in range(10, 30)], + } + + def family(host): + return socket.AF_INET6 if ":" in host else socket.AF_INET + + def fake_getaddrinfo(host, port, *args, **kwargs): + assert isinstance(port, int) or (isinstance(port, str) and port.isdigit()) + try: + addrs = fake_hosts[host] + except KeyError: + raise OSError(f"unknown test host: {host}") + else: + return [ + (family(addr), socket.SOCK_STREAM, 6, "", (addr, port)) + for addr in addrs + ] + + _patch_gai(monkeypatch, fake_getaddrinfo) + + +@pytest.fixture +def fail_resolve(monkeypatch): + """ + Fixture to fail any name resolution. + """ + + def fail_getaddrinfo(host, port, **kwargs): + pytest.fail(f"shouldn't try to resolve {host}") + + _patch_gai(monkeypatch, fail_getaddrinfo) + + +def _patch_gai(monkeypatch, f): + monkeypatch.setattr(socket, "getaddrinfo", f) + try: + loop = asyncio.get_running_loop() + except RuntimeError: + pass + else: + + async def af(*args, **kwargs): + return f(*args, **kwargs) + + monkeypatch.setattr(loop, "getaddrinfo", af) diff --git a/tests/test_connection_async.py b/tests/test_connection_async.py index 86ccfe10b..ced5db583 100644 --- a/tests/test_connection_async.py +++ b/tests/test_connection_async.py @@ -15,7 +15,6 @@ from .test_connection import tx_params, tx_params_isolation, tx_values_map from .test_connection import conninfo_params_timeout from .test_connection import testctx # noqa: F401 # fixture from .test_adapt import make_bin_dumper, make_dumper -from .test_conninfo import fake_resolve # noqa: F401 pytestmark = pytest.mark.anyio @@ -804,7 +803,7 @@ async def test_cancel_closed(aconn): aconn.cancel() -async def test_resolve_hostaddr_conn(monkeypatch, fake_resolve): # noqa: F811 +async def test_resolve_hostaddr_conn(monkeypatch, fake_resolve): got = [] def fake_connect_gen(conninfo, **kwargs): diff --git a/tests/test_conninfo.py b/tests/test_conninfo.py index ae892ed3c..2e8c44822 100644 --- a/tests/test_conninfo.py +++ b/tests/test_conninfo.py @@ -349,39 +349,3 @@ def test_timeout(setpgenv, conninfo, want, env): params = conninfo_to_dict(conninfo) timeout = timeout_from_conninfo(params) assert timeout == want - - -@pytest.fixture -async def fake_resolve(monkeypatch): - fake_hosts = { - "localhost": ["127.0.0.1"], - "foo.com": ["1.1.1.1"], - "qux.com": ["2.2.2.2"], - "dup.com": ["3.3.3.3", "3.3.3.4"], - "alot.com": [f"4.4.4.{n}" for n in range(10, 30)], - } - - def family(host): - return socket.AF_INET6 if ":" in host else socket.AF_INET - - async def fake_getaddrinfo(host, port, **kwargs): - assert isinstance(port, int) or (isinstance(port, str) and port.isdigit()) - try: - addrs = fake_hosts[host] - except KeyError: - raise OSError(f"unknown test host: {host}") - else: - return [ - (family(addr), socket.SOCK_STREAM, 6, "", (addr, port)) - for addr in addrs - ] - - monkeypatch.setattr(asyncio.get_running_loop(), "getaddrinfo", fake_getaddrinfo) - - -@pytest.fixture -async def fail_resolve(monkeypatch): - async def fail_getaddrinfo(host, port, **kwargs): - pytest.fail(f"shouldn't try to resolve {host}") - - monkeypatch.setattr(asyncio.get_running_loop(), "getaddrinfo", fail_getaddrinfo) diff --git a/tests/test_dns.py b/tests/test_dns.py index a83aaeb66..7ea89ad68 100644 --- a/tests/test_dns.py +++ b/tests/test_dns.py @@ -3,8 +3,6 @@ import pytest import psycopg from psycopg.conninfo import conninfo_to_dict -from .test_conninfo import fake_resolve # noqa: F401 # fixture - @pytest.mark.usefixtures("fake_resolve") async def test_resolve_hostaddr_conn(aconn_cls, monkeypatch):