test: add unit tests for core, parser and operations
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from sshctl.config.resolver import ResolvedHost
|
||||
from sshctl.export.exporter import export_csv, export_csv_to_file
|
||||
|
||||
|
||||
def test_export_csv_with_hosts() -> None:
|
||||
hosts = [
|
||||
ResolvedHost(
|
||||
alias="server1",
|
||||
hostname="10.0.0.1",
|
||||
user="admin",
|
||||
port=22,
|
||||
proxy_jump="gw",
|
||||
identity_file="~/.ssh/id_rsa",
|
||||
options={},
|
||||
),
|
||||
ResolvedHost(
|
||||
alias="server2",
|
||||
hostname="10.0.0.2",
|
||||
user="root",
|
||||
port=2222,
|
||||
proxy_jump="",
|
||||
identity_file="",
|
||||
options={},
|
||||
),
|
||||
]
|
||||
|
||||
csv_content = export_csv(hosts)
|
||||
lines = csv_content.strip().splitlines()
|
||||
assert len(lines) == 3
|
||||
assert lines[0] == "Host,HostName,User,Port,ProxyJump,IdentityFile"
|
||||
assert "server1" in lines[1]
|
||||
assert "server2" in lines[2]
|
||||
assert "10.0.0.1" in lines[1]
|
||||
assert "gw" in lines[1]
|
||||
|
||||
|
||||
def test_export_csv_to_file(tmp_path: Path) -> None:
|
||||
hosts = [
|
||||
ResolvedHost(
|
||||
alias="test",
|
||||
hostname="10.0.0.1",
|
||||
user="root",
|
||||
port=22,
|
||||
proxy_jump="",
|
||||
identity_file="",
|
||||
options={},
|
||||
),
|
||||
]
|
||||
|
||||
path = export_csv_to_file(tmp_path / "output.csv", hosts)
|
||||
assert path.exists()
|
||||
content = path.read_text()
|
||||
assert "test" in content
|
||||
assert "10.0.0.1" in content
|
||||
|
||||
|
||||
def test_export_csv_requires_args() -> None:
|
||||
raised = False
|
||||
try:
|
||||
export_csv()
|
||||
except ValueError:
|
||||
raised = True
|
||||
assert raised, "Expected ValueError"
|
||||
Reference in New Issue
Block a user