2023-08-09 14:38:08 +00:00
|
|
|
import subprocess
|
|
|
|
|
2023-11-28 09:01:32 +00:00
|
|
|
from clan_cli.ssh import Host, HostGroup, parse_deployment_address
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_ipv6() -> None:
|
|
|
|
host = parse_deployment_address("foo", "[fe80::1%eth0]:2222")
|
|
|
|
assert host.host == "fe80::1%eth0"
|
|
|
|
assert host.port == 2222
|
2023-11-28 12:31:50 +00:00
|
|
|
host = parse_deployment_address("foo", "[fe80::1%eth0]")
|
|
|
|
assert host.host == "fe80::1%eth0"
|
|
|
|
assert host.port is None
|
2023-08-09 14:38:08 +00:00
|
|
|
|
|
|
|
|
2023-08-10 10:39:26 +00:00
|
|
|
def test_run(host_group: HostGroup) -> None:
|
|
|
|
proc = host_group.run("echo hello", stdout=subprocess.PIPE)
|
2023-08-09 14:38:08 +00:00
|
|
|
assert proc[0].result.stdout == "hello\n"
|
|
|
|
|
|
|
|
|
2023-08-10 10:39:26 +00:00
|
|
|
def test_run_environment(host_group: HostGroup) -> None:
|
|
|
|
p1 = host_group.run(
|
|
|
|
"echo $env_var", stdout=subprocess.PIPE, extra_env=dict(env_var="true")
|
|
|
|
)
|
2023-08-09 14:38:08 +00:00
|
|
|
assert p1[0].result.stdout == "true\n"
|
2023-08-10 10:39:26 +00:00
|
|
|
p2 = host_group.run(["env"], stdout=subprocess.PIPE, extra_env=dict(env_var="true"))
|
2023-08-09 14:38:08 +00:00
|
|
|
assert "env_var=true" in p2[0].result.stdout
|
|
|
|
|
|
|
|
|
2023-08-10 10:39:26 +00:00
|
|
|
def test_run_no_shell(host_group: HostGroup) -> None:
|
|
|
|
proc = host_group.run(["echo", "$hello"], stdout=subprocess.PIPE)
|
2023-08-09 14:38:08 +00:00
|
|
|
assert proc[0].result.stdout == "$hello\n"
|
|
|
|
|
|
|
|
|
2023-08-10 10:39:26 +00:00
|
|
|
def test_run_function(host_group: HostGroup) -> None:
|
2023-08-09 14:38:08 +00:00
|
|
|
def some_func(h: Host) -> bool:
|
|
|
|
p = h.run("echo hello", stdout=subprocess.PIPE)
|
|
|
|
return p.stdout == "hello\n"
|
|
|
|
|
2023-08-10 10:39:26 +00:00
|
|
|
res = host_group.run_function(some_func)
|
2023-08-09 14:38:08 +00:00
|
|
|
assert res[0].result
|
|
|
|
|
|
|
|
|
2023-08-10 10:39:26 +00:00
|
|
|
def test_timeout(host_group: HostGroup) -> None:
|
2023-08-09 14:38:08 +00:00
|
|
|
try:
|
2023-08-10 10:39:26 +00:00
|
|
|
host_group.run_local("sleep 10", timeout=0.01)
|
2023-08-09 14:38:08 +00:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
assert False, "should have raised TimeoutExpired"
|
|
|
|
|
|
|
|
|
2023-08-10 10:39:26 +00:00
|
|
|
def test_run_exception(host_group: HostGroup) -> None:
|
|
|
|
r = host_group.run("exit 1", check=False)
|
2023-08-09 14:38:08 +00:00
|
|
|
assert r[0].result.returncode == 1
|
|
|
|
|
|
|
|
try:
|
2023-08-10 10:39:26 +00:00
|
|
|
host_group.run("exit 1")
|
2023-08-09 14:38:08 +00:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
assert False, "should have raised Exception"
|
|
|
|
|
|
|
|
|
2023-08-10 10:39:26 +00:00
|
|
|
def test_run_function_exception(host_group: HostGroup) -> None:
|
2023-08-09 14:38:08 +00:00
|
|
|
def some_func(h: Host) -> subprocess.CompletedProcess[str]:
|
|
|
|
return h.run_local("exit 1")
|
|
|
|
|
|
|
|
try:
|
2023-08-10 10:39:26 +00:00
|
|
|
host_group.run_function(some_func)
|
2023-08-09 14:38:08 +00:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
assert False, "should have raised Exception"
|