93 lines
2.9 KiB
Python
93 lines
2.9 KiB
Python
"""Tests for remote domain."""
|
|
|
|
import pytest
|
|
|
|
from flow.core.config import TargetConfig
|
|
from flow.core.errors import FlowError
|
|
from flow.domain.remote.models import SSHCommand, Target
|
|
from flow.domain.remote.resolution import (
|
|
build_ssh_command,
|
|
list_targets,
|
|
parse_target,
|
|
resolve_target,
|
|
terminfo_fix_command,
|
|
)
|
|
|
|
|
|
class TestParseTarget:
|
|
def test_valid_spec(self):
|
|
user, ns, plat = parse_target("personal@orb")
|
|
assert user is None
|
|
assert ns == "personal"
|
|
assert plat == "orb"
|
|
|
|
def test_valid_spec_with_user(self):
|
|
user, ns, plat = parse_target("alice@personal@orb")
|
|
assert user == "alice"
|
|
assert ns == "personal"
|
|
assert plat == "orb"
|
|
|
|
def test_missing_at_raises(self):
|
|
with pytest.raises(FlowError):
|
|
parse_target("invalid")
|
|
|
|
def test_empty_parts_raises(self):
|
|
with pytest.raises(FlowError):
|
|
parse_target("@orb")
|
|
|
|
|
|
class TestResolveTarget:
|
|
def test_found(self):
|
|
targets = [TargetConfig(namespace="personal", platform="orb", host="personal.orb")]
|
|
result = resolve_target("personal@orb", targets, default_user="tomas")
|
|
assert result.host == "personal.orb"
|
|
assert result.label == "personal@orb"
|
|
assert result.user == "tomas"
|
|
|
|
def test_not_found(self):
|
|
with pytest.raises(FlowError, match="Unknown target"):
|
|
resolve_target("missing@host", [], default_user="tomas")
|
|
|
|
def test_falls_back_to_host_template(self):
|
|
result = resolve_target("personal@orb", [], default_user="tomas")
|
|
assert result.host == "personal.orb"
|
|
|
|
|
|
class TestBuildSSHCommand:
|
|
def test_basic(self):
|
|
target = Target(namespace="personal", platform="orb", host="personal.orb", user="tomas")
|
|
cmd = build_ssh_command(target)
|
|
assert "ssh" in cmd.argv
|
|
assert cmd.destination == "tomas@personal.orb"
|
|
assert cmd.env["DF_NAMESPACE"] == "personal"
|
|
assert "tmux" in cmd.argv
|
|
|
|
def test_with_identity(self):
|
|
target = Target(namespace="work", platform="ec2", host="work.ec2", identity="~/.ssh/id_work", user="tomas")
|
|
cmd = build_ssh_command(target)
|
|
assert "-i" in cmd.argv
|
|
assert "~/.ssh/id_work" in cmd.argv
|
|
|
|
def test_without_tmux(self):
|
|
target = Target(namespace="p", platform="o", host="h", user="tomas")
|
|
cmd = build_ssh_command(target, no_tmux=True)
|
|
assert "tmux" not in cmd.argv
|
|
assert cmd.destination == "tomas@h"
|
|
|
|
|
|
class TestListTargets:
|
|
def test_converts_configs(self):
|
|
configs = [
|
|
TargetConfig(namespace="a", platform="b", host="a.b"),
|
|
TargetConfig(namespace="c", platform="d", host="c.d"),
|
|
]
|
|
targets = list_targets(configs)
|
|
assert len(targets) == 2
|
|
assert targets[0].label == "a@b"
|
|
|
|
|
|
class TestTerminfoFix:
|
|
def test_returns_command(self):
|
|
cmd = terminfo_fix_command()
|
|
assert "infocmp" in cmd
|