| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import shutil |
| 4 |
import sys |
| 5 |
from functools import lru_cache |
| 6 |
from typing import Callable |
| 7 |
from typing import Generator |
| 8 |
from typing import Iterator |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
import execnet |
| 13 |
from execnet.gateway import Gateway |
| 14 |
from execnet.gateway_base import ExecModel |
| 15 |
from execnet.gateway_base import WorkerPool |
| 16 |
from execnet.gateway_base import get_execmodel |
| 17 |
|
| 18 |
collect_ignore = ["build", "doc/_build"] |
| 19 |
|
| 20 |
rsyncdirs = ["conftest.py", "execnet", "testing", "doc"] |
| 21 |
|
| 22 |
|
| 23 |
@pytest.hookimpl(hookwrapper=True) |
| 24 |
def pytest_runtest_setup(item: pytest.Item) -> Generator[None, None, None]: |
| 25 |
if item.fspath.purebasename in ("test_group", "test_info"): |
| 26 |
getspecssh(item.config) |
| 27 |
yield |
| 28 |
if "pypy" in item.keywords and not item.config.option.pypy: |
| 29 |
pytest.skip("pypy tests skipped, use --pypy to run them.") |
| 30 |
|
| 31 |
|
| 32 |
@pytest.fixture |
| 33 |
def group_function() -> Iterator[execnet.Group]: |
| 34 |
group = execnet.Group() |
| 35 |
yield group |
| 36 |
group.terminate(0.5) |
| 37 |
|
| 38 |
|
| 39 |
@pytest.fixture |
| 40 |
def makegateway(group_function: execnet.Group) -> Callable[[str], Gateway]: |
| 41 |
return group_function.makegateway |
| 42 |
|
| 43 |
|
| 44 |
pytest_plugins = ["pytester", "doctest"] |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
def pytest_addoption(parser: pytest.Parser) -> None: |
| 49 |
group = parser.getgroup("execnet", "execnet testing options") |
| 50 |
group.addoption( |
| 51 |
"--gx", |
| 52 |
action="append", |
| 53 |
dest="gspecs", |
| 54 |
default=None, |
| 55 |
help="add a global test environment, XSpec-syntax. ", |
| 56 |
) |
| 57 |
group.addoption( |
| 58 |
"--pypy", |
| 59 |
action="store_true", |
| 60 |
dest="pypy", |
| 61 |
help="run some tests also against pypy", |
| 62 |
) |
| 63 |
group.addoption( |
| 64 |
"--broken-isp", |
| 65 |
action="store_true", |
| 66 |
dest="broken_isp", |
| 67 |
help=( |
| 68 |
"Skips tests that assume your ISP doesn't put up a landing " |
| 69 |
"page on invalid addresses" |
| 70 |
), |
| 71 |
) |
| 72 |
|
| 73 |
|
| 74 |
@pytest.fixture |
| 75 |
def specssh(request: pytest.FixtureRequest) -> execnet.XSpec: |
| 76 |
return getspecssh(request.config) |
| 77 |
|
| 78 |
|
| 79 |
@pytest.fixture |
| 80 |
def specsocket(request: pytest.FixtureRequest) -> execnet.XSpec: |
| 81 |
return getsocketspec(request.config) |
| 82 |
|
| 83 |
|
| 84 |
def getgspecs(config: pytest.Config) -> list[execnet.XSpec]: |
| 85 |
return [execnet.XSpec(gspec) for gspec in config.getvalueorskip("gspecs")] |
| 86 |
|
| 87 |
|
| 88 |
def getspecssh(config: pytest.Config) -> execnet.XSpec: |
| 89 |
xspecs = getgspecs(config) |
| 90 |
for spec in xspecs: |
| 91 |
if spec.ssh: |
| 92 |
if not shutil.which("ssh"): |
| 93 |
pytest.skip("command not found: ssh") |
| 94 |
return spec |
| 95 |
pytest.skip("need '--gx ssh=...'") |
| 96 |
|
| 97 |
|
| 98 |
def getsocketspec(config: pytest.Config) -> execnet.XSpec: |
| 99 |
xspecs = getgspecs(config) |
| 100 |
for spec in xspecs: |
| 101 |
if spec.socket: |
| 102 |
return spec |
| 103 |
pytest.skip("need '--gx socket=...'") |
| 104 |
|
| 105 |
|
| 106 |
def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: |
| 107 |
if "gw" in metafunc.fixturenames: |
| 108 |
assert "anypython" not in metafunc.fixturenames, "need combine?" |
| 109 |
if hasattr(metafunc.function, "gwtypes"): |
| 110 |
gwtypes = metafunc.function.gwtypes |
| 111 |
elif hasattr(metafunc.cls, "gwtype"): |
| 112 |
gwtypes = [metafunc.cls.gwtype] |
| 113 |
else: |
| 114 |
gwtypes = ["popen", "socket", "ssh", "proxy"] |
| 115 |
metafunc.parametrize("gw", gwtypes, indirect=True) |
| 116 |
|
| 117 |
|
| 118 |
@lru_cache |
| 119 |
def getexecutable(name: str) -> str | None: |
| 120 |
if name == "sys.executable": |
| 121 |
return sys.executable |
| 122 |
return shutil.which(name) |
| 123 |
|
| 124 |
|
| 125 |
@pytest.fixture(params=("sys.executable", "pypy3")) |
| 126 |
def anypython(request: pytest.FixtureRequest) -> str: |
| 127 |
name = request.param |
| 128 |
executable = getexecutable(name) |
| 129 |
if executable is None: |
| 130 |
pytest.skip(f"no {name} found") |
| 131 |
if "execmodel" in request.fixturenames and name != "sys.executable": |
| 132 |
backend = request.getfixturevalue("execmodel").backend |
| 133 |
if backend not in ("thread", "main_thread_only"): |
| 134 |
pytest.xfail(f"cannot run {backend!r} execmodel with bare {name}") |
| 135 |
return executable |
| 136 |
|
| 137 |
|
| 138 |
@pytest.fixture(scope="session") |
| 139 |
def group() -> Iterator[execnet.Group]: |
| 140 |
g = execnet.Group() |
| 141 |
yield g |
| 142 |
g.terminate(timeout=1) |
| 143 |
|
| 144 |
|
| 145 |
@pytest.fixture |
| 146 |
def gw( |
| 147 |
request: pytest.FixtureRequest, |
| 148 |
execmodel: ExecModel, |
| 149 |
group: execnet.Group, |
| 150 |
) -> Gateway: |
| 151 |
try: |
| 152 |
return group[request.param] |
| 153 |
except KeyError: |
| 154 |
if request.param == "popen": |
| 155 |
gw = group.makegateway("popen//id=popen//execmodel=%s" % execmodel.backend) |
| 156 |
elif request.param == "socket": |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
pname = "sproxy1" |
| 161 |
if pname not in group: |
| 162 |
proxygw = group.makegateway("popen//id=%s" % pname) |
| 163 |
|
| 164 |
gw = group.makegateway( |
| 165 |
f"socket//id=socket//installvia={pname}//execmodel={execmodel.backend}" |
| 166 |
) |
| 167 |
|
| 168 |
gw.proxygw = proxygw |
| 169 |
assert pname in group |
| 170 |
elif request.param == "ssh": |
| 171 |
sshhost = request.getfixturevalue("specssh").ssh |
| 172 |
|
| 173 |
|
| 174 |
gw = group.makegateway(f"ssh={sshhost}//id=ssh") |
| 175 |
elif request.param == "proxy": |
| 176 |
group.makegateway("popen//id=proxy-transport") |
| 177 |
gw = group.makegateway( |
| 178 |
"popen//via=proxy-transport//id=proxy//execmodel=%s" % execmodel.backend |
| 179 |
) |
| 180 |
else: |
| 181 |
assert 0, f"unknown execmodel: {request.param}" |
| 182 |
return gw |
| 183 |
|
| 184 |
|
| 185 |
@pytest.fixture( |
| 186 |
params=["thread", "main_thread_only", "eventlet", "gevent"], scope="session" |
| 187 |
) |
| 188 |
def execmodel(request: pytest.FixtureRequest) -> ExecModel: |
| 189 |
if request.param not in ("thread", "main_thread_only"): |
| 190 |
pytest.importorskip(request.param) |
| 191 |
if request.param in ("eventlet", "gevent") and sys.platform == "win32": |
| 192 |
pytest.xfail(request.param + " does not work on win32") |
| 193 |
return get_execmodel(request.param) |
| 194 |
|
| 195 |
|
| 196 |
@pytest.fixture |
| 197 |
def pool(execmodel: ExecModel) -> WorkerPool: |
| 198 |
return WorkerPool(execmodel=execmodel) |