add host_group fixture
This commit is contained in:
@@ -11,4 +11,5 @@ pytest_plugins = [
|
|||||||
"sshd",
|
"sshd",
|
||||||
"command",
|
"command",
|
||||||
"ports",
|
"ports",
|
||||||
|
"host_group",
|
||||||
]
|
]
|
||||||
|
|||||||
23
pkgs/clan-cli/tests/host_group.py
Normal file
23
pkgs/clan-cli/tests/host_group.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import os
|
||||||
|
import pwd
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from sshd import Sshd
|
||||||
|
|
||||||
|
from clan_cli.ssh import Host, HostGroup, HostKeyCheck
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def host_group(sshd: Sshd) -> HostGroup:
|
||||||
|
login = pwd.getpwuid(os.getuid()).pw_name
|
||||||
|
return HostGroup(
|
||||||
|
[
|
||||||
|
Host(
|
||||||
|
"127.0.0.1",
|
||||||
|
port=sshd.port,
|
||||||
|
user=login,
|
||||||
|
key=sshd.key,
|
||||||
|
host_key_check=HostKeyCheck.NONE,
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
@@ -1,89 +1,63 @@
|
|||||||
import os
|
|
||||||
import pwd
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from sshd import Sshd
|
from clan_cli.ssh import Host, HostGroup
|
||||||
|
|
||||||
from clan_cli.ssh import Host, HostGroup, HostKeyCheck
|
|
||||||
|
|
||||||
|
|
||||||
def deploy_group(sshd: Sshd) -> HostGroup:
|
def test_run(host_group: HostGroup) -> None:
|
||||||
login = pwd.getpwuid(os.getuid()).pw_name
|
proc = host_group.run("echo hello", stdout=subprocess.PIPE)
|
||||||
return HostGroup(
|
|
||||||
[
|
|
||||||
Host(
|
|
||||||
"127.0.0.1",
|
|
||||||
port=sshd.port,
|
|
||||||
user=login,
|
|
||||||
key=sshd.key,
|
|
||||||
host_key_check=HostKeyCheck.NONE,
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_run(sshd: Sshd) -> None:
|
|
||||||
g = deploy_group(sshd)
|
|
||||||
proc = g.run("echo hello", stdout=subprocess.PIPE)
|
|
||||||
assert proc[0].result.stdout == "hello\n"
|
assert proc[0].result.stdout == "hello\n"
|
||||||
|
|
||||||
|
|
||||||
def test_run_environment(sshd: Sshd) -> None:
|
def test_run_environment(host_group: HostGroup) -> None:
|
||||||
g = deploy_group(sshd)
|
p1 = host_group.run(
|
||||||
p1 = g.run("echo $env_var", stdout=subprocess.PIPE, extra_env=dict(env_var="true"))
|
"echo $env_var", stdout=subprocess.PIPE, extra_env=dict(env_var="true")
|
||||||
|
)
|
||||||
assert p1[0].result.stdout == "true\n"
|
assert p1[0].result.stdout == "true\n"
|
||||||
p2 = g.run(["env"], stdout=subprocess.PIPE, extra_env=dict(env_var="true"))
|
p2 = host_group.run(["env"], stdout=subprocess.PIPE, extra_env=dict(env_var="true"))
|
||||||
assert "env_var=true" in p2[0].result.stdout
|
assert "env_var=true" in p2[0].result.stdout
|
||||||
|
|
||||||
|
|
||||||
def test_run_no_shell(sshd: Sshd) -> None:
|
def test_run_no_shell(host_group: HostGroup) -> None:
|
||||||
g = deploy_group(sshd)
|
proc = host_group.run(["echo", "$hello"], stdout=subprocess.PIPE)
|
||||||
proc = g.run(["echo", "$hello"], stdout=subprocess.PIPE)
|
|
||||||
assert proc[0].result.stdout == "$hello\n"
|
assert proc[0].result.stdout == "$hello\n"
|
||||||
|
|
||||||
|
|
||||||
def test_run_function(sshd: Sshd) -> None:
|
def test_run_function(host_group: HostGroup) -> None:
|
||||||
def some_func(h: Host) -> bool:
|
def some_func(h: Host) -> bool:
|
||||||
p = h.run("echo hello", stdout=subprocess.PIPE)
|
p = h.run("echo hello", stdout=subprocess.PIPE)
|
||||||
return p.stdout == "hello\n"
|
return p.stdout == "hello\n"
|
||||||
|
|
||||||
g = deploy_group(sshd)
|
res = host_group.run_function(some_func)
|
||||||
res = g.run_function(some_func)
|
|
||||||
assert res[0].result
|
assert res[0].result
|
||||||
|
|
||||||
|
|
||||||
def test_timeout(sshd: Sshd) -> None:
|
def test_timeout(host_group: HostGroup) -> None:
|
||||||
g = deploy_group(sshd)
|
|
||||||
try:
|
try:
|
||||||
g.run_local("sleep 10", timeout=0.01)
|
host_group.run_local("sleep 10", timeout=0.01)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
assert False, "should have raised TimeoutExpired"
|
assert False, "should have raised TimeoutExpired"
|
||||||
|
|
||||||
|
|
||||||
def test_run_exception(sshd: Sshd) -> None:
|
def test_run_exception(host_group: HostGroup) -> None:
|
||||||
g = deploy_group(sshd)
|
r = host_group.run("exit 1", check=False)
|
||||||
|
|
||||||
r = g.run("exit 1", check=False)
|
|
||||||
assert r[0].result.returncode == 1
|
assert r[0].result.returncode == 1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
g.run("exit 1")
|
host_group.run("exit 1")
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
assert False, "should have raised Exception"
|
assert False, "should have raised Exception"
|
||||||
|
|
||||||
|
|
||||||
def test_run_function_exception(sshd: Sshd) -> None:
|
def test_run_function_exception(host_group: HostGroup) -> None:
|
||||||
def some_func(h: Host) -> subprocess.CompletedProcess[str]:
|
def some_func(h: Host) -> subprocess.CompletedProcess[str]:
|
||||||
return h.run_local("exit 1")
|
return h.run_local("exit 1")
|
||||||
|
|
||||||
g = deploy_group(sshd)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
g.run_function(some_func)
|
host_group.run_function(some_func)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user