| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import shutil |
| 5 |
import subprocess |
| 6 |
import sys |
| 7 |
from pathlib import Path |
| 8 |
from typing import Callable |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
import execnet |
| 13 |
from execnet import XSpec |
| 14 |
from execnet.gateway import Gateway |
| 15 |
from execnet.gateway_io import popen_args |
| 16 |
from execnet.gateway_io import ssh_args |
| 17 |
from execnet.gateway_io import vagrant_ssh_args |
| 18 |
|
| 19 |
skip_win_pypy = pytest.mark.xfail( |
| 20 |
condition=hasattr(sys, "pypy_version_info") and sys.platform.startswith("win"), |
| 21 |
reason="failing on Windows on PyPy (#63)", |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
class TestXSpec: |
| 26 |
def test_norm_attributes(self) -> None: |
| 27 |
spec = XSpec( |
| 28 |
r"socket=192.168.102.2:8888//python=c:/this/python3.8//chdir=d:\hello" |
| 29 |
) |
| 30 |
assert spec.socket == "192.168.102.2:8888" |
| 31 |
assert spec.python == "c:/this/python3.8" |
| 32 |
assert spec.chdir == r"d:\hello" |
| 33 |
assert spec.nice is None |
| 34 |
assert not hasattr(spec, "_xyz") |
| 35 |
|
| 36 |
with pytest.raises(AttributeError): |
| 37 |
spec._hello() |
| 38 |
|
| 39 |
spec = XSpec("socket=192.168.102.2:8888//python=python2.5//nice=3") |
| 40 |
assert spec.socket == "192.168.102.2:8888" |
| 41 |
assert spec.python == "python2.5" |
| 42 |
assert spec.chdir is None |
| 43 |
assert spec.nice == "3" |
| 44 |
|
| 45 |
spec = XSpec("ssh=user@host//chdir=/hello/this//python=/usr/bin/python2.5") |
| 46 |
assert spec.ssh == "user@host" |
| 47 |
assert spec.python == "/usr/bin/python2.5" |
| 48 |
assert spec.chdir == "/hello/this" |
| 49 |
|
| 50 |
spec = XSpec("popen") |
| 51 |
assert spec.popen is True |
| 52 |
|
| 53 |
def test_ssh_options(self) -> None: |
| 54 |
spec = XSpec("ssh=-p 22100 user@host//python=python3") |
| 55 |
assert spec.ssh == "-p 22100 user@host" |
| 56 |
assert spec.python == "python3" |
| 57 |
|
| 58 |
spec = XSpec( |
| 59 |
"ssh=-i ~/.ssh/id_rsa-passwordless_login -p 22100 user@host//python=python3" |
| 60 |
) |
| 61 |
assert spec.ssh == "-i ~/.ssh/id_rsa-passwordless_login -p 22100 user@host" |
| 62 |
assert spec.python == "python3" |
| 63 |
|
| 64 |
def test_execmodel(self) -> None: |
| 65 |
spec = XSpec("execmodel=thread") |
| 66 |
assert spec.execmodel == "thread" |
| 67 |
spec = XSpec("execmodel=eventlet") |
| 68 |
assert spec.execmodel == "eventlet" |
| 69 |
|
| 70 |
def test_ssh_options_and_config(self) -> None: |
| 71 |
spec = XSpec("ssh=-p 22100 user@host//python=python3") |
| 72 |
spec.ssh_config = "/home/user/ssh_config" |
| 73 |
assert ssh_args(spec)[:6] == ["ssh", "-C", "-F", spec.ssh_config, "-p", "22100"] |
| 74 |
|
| 75 |
def test_vagrant_options(self) -> None: |
| 76 |
spec = XSpec("vagrant_ssh=default//python=python3") |
| 77 |
assert vagrant_ssh_args(spec)[:-1] == ["vagrant", "ssh", "default", "--", "-C"] |
| 78 |
|
| 79 |
def test_popen_with_sudo_python(self) -> None: |
| 80 |
spec = XSpec("popen//python=sudo python3") |
| 81 |
assert popen_args(spec) == [ |
| 82 |
"sudo", |
| 83 |
"python3", |
| 84 |
"-u", |
| 85 |
"-c", |
| 86 |
"import sys;exec(eval(sys.stdin.readline()))", |
| 87 |
] |
| 88 |
|
| 89 |
def test_env(self) -> None: |
| 90 |
xspec = XSpec("popen//env:NAME=value1") |
| 91 |
assert xspec.env["NAME"] == "value1" |
| 92 |
|
| 93 |
def test__samefilesystem(self) -> None: |
| 94 |
assert XSpec("popen")._samefilesystem() |
| 95 |
assert XSpec("popen//python=123")._samefilesystem() |
| 96 |
assert not XSpec("popen//chdir=hello")._samefilesystem() |
| 97 |
|
| 98 |
def test__spec_spec(self) -> None: |
| 99 |
for x in ("popen", "popen//python=this"): |
| 100 |
assert XSpec(x)._spec == x |
| 101 |
|
| 102 |
def test_samekeyword_twice_raises(self) -> None: |
| 103 |
pytest.raises(ValueError, XSpec, "popen//popen") |
| 104 |
pytest.raises(ValueError, XSpec, "popen//popen=123") |
| 105 |
|
| 106 |
def test_unknown_keys_allowed(self) -> None: |
| 107 |
xspec = XSpec("hello=3") |
| 108 |
assert xspec.hello == "3" |
| 109 |
|
| 110 |
def test_repr_and_string(self) -> None: |
| 111 |
for x in ("popen", "popen//python=this"): |
| 112 |
assert repr(XSpec(x)).find("popen") != -1 |
| 113 |
assert str(XSpec(x)) == x |
| 114 |
|
| 115 |
def test_hash_equality(self) -> None: |
| 116 |
assert XSpec("popen") == XSpec("popen") |
| 117 |
assert hash(XSpec("popen")) == hash(XSpec("popen")) |
| 118 |
assert XSpec("popen//python=123") != XSpec("popen") |
| 119 |
assert hash(XSpec("socket=hello:8080")) != hash(XSpec("popen")) |
| 120 |
|
| 121 |
|
| 122 |
class TestMakegateway: |
| 123 |
def test_no_type(self, makegateway: Callable[[str], Gateway]) -> None: |
| 124 |
pytest.raises(ValueError, lambda: makegateway("hello")) |
| 125 |
|
| 126 |
@skip_win_pypy |
| 127 |
def test_popen_default(self, makegateway: Callable[[str], Gateway]) -> None: |
| 128 |
gw = makegateway("") |
| 129 |
assert gw.spec.popen |
| 130 |
assert gw.spec.python is None |
| 131 |
rinfo = gw._rinfo() |
| 132 |
|
| 133 |
assert rinfo.cwd == os.getcwd() |
| 134 |
assert rinfo.version_info == sys.version_info |
| 135 |
|
| 136 |
@pytest.mark.skipif("not hasattr(os, 'nice')") |
| 137 |
@pytest.mark.xfail(reason="fails due to timing problems on busy single-core VMs") |
| 138 |
def test_popen_nice(self, makegateway: Callable[[str], Gateway]) -> None: |
| 139 |
gw = makegateway("popen") |
| 140 |
|
| 141 |
def getnice(channel) -> None: |
| 142 |
import os |
| 143 |
|
| 144 |
if hasattr(os, "nice"): |
| 145 |
channel.send(os.nice(0)) |
| 146 |
else: |
| 147 |
channel.send(None) |
| 148 |
|
| 149 |
remotenice = gw.remote_exec(getnice).receive() |
| 150 |
assert isinstance(remotenice, int) |
| 151 |
gw.exit() |
| 152 |
if remotenice is not None: |
| 153 |
gw = makegateway("popen//nice=5") |
| 154 |
remotenice2 = gw.remote_exec(getnice).receive() |
| 155 |
assert remotenice2 == remotenice + 5 |
| 156 |
|
| 157 |
def test_popen_env(self, makegateway: Callable[[str], Gateway]) -> None: |
| 158 |
gw = makegateway("popen//env:NAME123=123") |
| 159 |
ch = gw.remote_exec( |
| 160 |
""" |
| 161 |
import os |
| 162 |
channel.send(os.environ['NAME123']) |
| 163 |
""" |
| 164 |
) |
| 165 |
value = ch.receive() |
| 166 |
assert value == "123" |
| 167 |
|
| 168 |
@skip_win_pypy |
| 169 |
def test_popen_explicit(self, makegateway: Callable[[str], Gateway]) -> None: |
| 170 |
gw = makegateway("popen//python=%s" % sys.executable) |
| 171 |
assert gw.spec.python == sys.executable |
| 172 |
rinfo = gw._rinfo() |
| 173 |
assert rinfo.executable == sys.executable |
| 174 |
assert rinfo.cwd == os.getcwd() |
| 175 |
assert rinfo.version_info == sys.version_info |
| 176 |
|
| 177 |
@skip_win_pypy |
| 178 |
def test_popen_chdir_absolute( |
| 179 |
self, tmp_path: Path, makegateway: Callable[[str], Gateway] |
| 180 |
) -> None: |
| 181 |
gw = makegateway("popen//chdir=%s" % tmp_path) |
| 182 |
rinfo = gw._rinfo() |
| 183 |
assert rinfo.cwd == str(tmp_path.resolve()) |
| 184 |
|
| 185 |
@skip_win_pypy |
| 186 |
def test_popen_chdir_newsub( |
| 187 |
self, |
| 188 |
monkeypatch: pytest.MonkeyPatch, |
| 189 |
tmp_path: Path, |
| 190 |
makegateway: Callable[[str], Gateway], |
| 191 |
) -> None: |
| 192 |
monkeypatch.chdir(tmp_path) |
| 193 |
gw = makegateway("popen//chdir=hello") |
| 194 |
rinfo = gw._rinfo() |
| 195 |
expected = str(tmp_path.joinpath("hello").resolve()).lower() |
| 196 |
assert rinfo.cwd.lower() == expected |
| 197 |
|
| 198 |
def test_ssh(self, specssh: XSpec, makegateway: Callable[[str], Gateway]) -> None: |
| 199 |
sshhost = specssh.ssh |
| 200 |
gw = makegateway("ssh=%s//id=ssh1" % sshhost) |
| 201 |
assert gw.id == "ssh1" |
| 202 |
|
| 203 |
def test_vagrant(self, makegateway: Callable[[str], Gateway]) -> None: |
| 204 |
vagrant_bin = shutil.which("vagrant") |
| 205 |
if vagrant_bin is None: |
| 206 |
pytest.skip("Vagrant binary not in PATH") |
| 207 |
res = subprocess.run( |
| 208 |
[vagrant_bin, "status", "default", "--machine-readable"], |
| 209 |
capture_output=True, |
| 210 |
encoding="utf-8", |
| 211 |
errors="replace", |
| 212 |
check=True, |
| 213 |
).stdout |
| 214 |
print(res) |
| 215 |
if ",default,state,shutoff\n" in res: |
| 216 |
pytest.xfail("vm shutoff, run `vagrant up` first") |
| 217 |
if ",default,state,not_created\n" in res: |
| 218 |
pytest.xfail("vm not created, run `vagrant up` first") |
| 219 |
if ",default,state,running\n" not in res: |
| 220 |
pytest.fail("unknown vm state") |
| 221 |
|
| 222 |
gw = makegateway("vagrant_ssh=default//python=python3") |
| 223 |
rinfo = gw._rinfo() |
| 224 |
assert rinfo.cwd == "/home/vagrant" |
| 225 |
assert rinfo.executable == "/usr/bin/python" |
| 226 |
|
| 227 |
def test_socket( |
| 228 |
self, specsocket: XSpec, makegateway: Callable[[str], Gateway] |
| 229 |
) -> None: |
| 230 |
gw = makegateway("socket=%s//id=sock1" % specsocket.socket) |
| 231 |
rinfo = gw._rinfo() |
| 232 |
assert rinfo.executable |
| 233 |
assert rinfo.cwd |
| 234 |
assert rinfo.version_info |
| 235 |
assert gw.id == "sock1" |
| 236 |
|
| 237 |
|
| 238 |
@pytest.mark.xfail(reason="we can't instantiate a second gateway") |
| 239 |
def test_socket_second( |
| 240 |
self, specsocket: XSpec, makegateway: Callable[[str], Gateway] |
| 241 |
) -> None: |
| 242 |
gw = makegateway("socket=%s//id=sock1" % specsocket.socket) |
| 243 |
gw2 = makegateway("socket=%s//id=sock1" % specsocket.socket) |
| 244 |
rinfo = gw._rinfo() |
| 245 |
rinfo2 = gw2._rinfo() |
| 246 |
assert rinfo.executable == rinfo2.executable |
| 247 |
assert rinfo.cwd == rinfo2.cwd |
| 248 |
assert rinfo.version_info == rinfo2.version_info |
| 249 |
|
| 250 |
def test_socket_installvia(self) -> None: |
| 251 |
group = execnet.Group() |
| 252 |
group.makegateway("popen//id=p1") |
| 253 |
gw = group.makegateway("socket//installvia=p1//id=s1") |
| 254 |
assert gw.id == "s1" |
| 255 |
assert gw.remote_status() |
| 256 |
group.terminate() |