test: add unit tests for core, parser and operations
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from sshctl.core.models import HostBlock, SshConfig, ZonePath
|
||||
|
||||
|
||||
def test_host_block_defaults() -> None:
|
||||
host = HostBlock(alias="test")
|
||||
assert host.alias == "test"
|
||||
assert host.options == {}
|
||||
assert host.hostname is None
|
||||
assert host.user is None
|
||||
assert host.port is None
|
||||
assert host.proxy_jump is None
|
||||
|
||||
|
||||
def test_host_block_set_get() -> None:
|
||||
host = HostBlock(alias="test")
|
||||
host.set("HostName", "10.0.0.1")
|
||||
host.set("User", "admin")
|
||||
assert host.hostname == "10.0.0.1"
|
||||
assert host.user == "admin"
|
||||
|
||||
|
||||
def test_host_block_port() -> None:
|
||||
host = HostBlock(alias="test")
|
||||
host.set("Port", "2222")
|
||||
assert host.port == 2222
|
||||
|
||||
|
||||
def test_host_block_port_none() -> None:
|
||||
host = HostBlock(alias="test")
|
||||
assert host.port is None
|
||||
|
||||
|
||||
def test_host_block_default_get() -> None:
|
||||
host = HostBlock(alias="test")
|
||||
assert host.get("Nonexistent") is None
|
||||
assert host.get("Nonexistent", "default") == "default"
|
||||
|
||||
|
||||
def test_ssh_config_find() -> None:
|
||||
config = SshConfig()
|
||||
config.blocks.append(HostBlock(alias="server1"))
|
||||
config.blocks.append(HostBlock(alias="server2"))
|
||||
|
||||
found = config.find("server1")
|
||||
assert found is not None
|
||||
assert found.alias == "server1"
|
||||
assert config.find("nonexistent") is None
|
||||
|
||||
|
||||
def test_ssh_config_aliases() -> None:
|
||||
config = SshConfig()
|
||||
config.blocks.append(HostBlock(alias="*"))
|
||||
config.blocks.append(HostBlock(alias="server1"))
|
||||
config.blocks.append(HostBlock(alias="server2"))
|
||||
|
||||
assert config.aliases == ["server1", "server2"]
|
||||
|
||||
|
||||
def test_ssh_config_aliases_exclude_glob_patterns() -> None:
|
||||
config = SshConfig()
|
||||
config.blocks.append(HostBlock(alias="web01"))
|
||||
config.blocks.append(HostBlock(alias="192.168.*"))
|
||||
config.blocks.append(HostBlock(alias="10.*"))
|
||||
config.blocks.append(HostBlock(alias="203.0.113.* 198.51.100.*"))
|
||||
config.blocks.append(HostBlock(alias="web[0-9]"))
|
||||
config.blocks.append(HostBlock(alias="db?"))
|
||||
config.blocks.append(HostBlock(alias="*"))
|
||||
|
||||
assert config.aliases == ["web01"]
|
||||
|
||||
|
||||
def test_ssh_config_find_all() -> None:
|
||||
config = SshConfig()
|
||||
config.blocks.append(HostBlock(alias="web01"))
|
||||
config.blocks.append(HostBlock(alias="web02"))
|
||||
config.blocks.append(HostBlock(alias="db01"))
|
||||
|
||||
result = config.find_all("web*")
|
||||
assert len(result) == 2
|
||||
assert result[0].alias == "web01"
|
||||
assert result[1].alias == "web02"
|
||||
|
||||
|
||||
def test_zone_path_internal() -> None:
|
||||
zp = ZonePath(
|
||||
universe="internal",
|
||||
sector="AZ",
|
||||
cluster="cluster1",
|
||||
zone="8xx-serverfarm",
|
||||
vlan="82-k8s",
|
||||
)
|
||||
expected = Path("conf.d/internal/AZ/cluster1/8xx-serverfarm/82-k8s")
|
||||
assert zp.to_rel_path() == expected
|
||||
|
||||
|
||||
def test_zone_path_external() -> None:
|
||||
zp = ZonePath(universe="external")
|
||||
assert zp.to_rel_path() == Path("conf.d/external")
|
||||
|
||||
|
||||
def test_zone_path_from_rel_path() -> None:
|
||||
zp = ZonePath.from_rel_path(Path("conf.d/internal/AZ/cluster1"))
|
||||
assert zp.universe == "internal"
|
||||
assert zp.sector == "AZ"
|
||||
assert zp.cluster == "cluster1"
|
||||
assert zp.zone is None
|
||||
|
||||
|
||||
def test_zone_path_invalid() -> None:
|
||||
raised = False
|
||||
try:
|
||||
ZonePath.from_rel_path(Path("other/file"))
|
||||
except ValueError:
|
||||
raised = True
|
||||
assert raised, "Expected ValueError"
|
||||
|
||||
|
||||
def test_zone_path_unknown_universe() -> None:
|
||||
raised = False
|
||||
try:
|
||||
ZonePath.from_rel_path(Path("conf.d/unknown"))
|
||||
except ValueError:
|
||||
raised = True
|
||||
assert raised, "Expected ValueError"
|
||||
Reference in New Issue
Block a user