81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
import pytest
|
|
import click
|
|
from autossh_mgr.config import (
|
|
TunnelConfig, ensure_dirs, load_tunnels, save_tunnels, get_tunnel
|
|
) # TunnelConfig kept for test_save_preserves_non_default_values
|
|
|
|
|
|
def test_ensure_dirs_creates_structure(tmp_path):
|
|
ensure_dirs(tmp_path)
|
|
assert (tmp_path / "tunnels.yaml").exists()
|
|
assert (tmp_path / "pids").is_dir()
|
|
assert (tmp_path / "logs").is_dir()
|
|
|
|
|
|
def test_ensure_dirs_is_idempotent(tmp_path):
|
|
ensure_dirs(tmp_path)
|
|
ensure_dirs(tmp_path) # must not raise
|
|
|
|
|
|
def test_load_tunnels_empty(config_dir):
|
|
assert load_tunnels(config_dir) == []
|
|
|
|
|
|
def test_save_and_load_roundtrip(config_dir, sample_tunnel):
|
|
save_tunnels(config_dir, [sample_tunnel])
|
|
loaded = load_tunnels(config_dir)
|
|
assert len(loaded) == 1
|
|
t = loaded[0]
|
|
assert t.name == "web-service"
|
|
assert t.host == "relay.example.com"
|
|
assert t.user == "deploy"
|
|
assert t.local_port == 8080
|
|
assert t.remote_port == 18080
|
|
assert t.port == 22
|
|
assert t.identity_file == "~/.ssh/id_ed25519"
|
|
assert t.local_host == "127.0.0.1"
|
|
assert t.remote_host == "0.0.0.0"
|
|
assert t.ssh_options == ""
|
|
|
|
|
|
def test_save_atomic_write(config_dir, sample_tunnel):
|
|
save_tunnels(config_dir, [sample_tunnel])
|
|
# No .tmp file left behind
|
|
assert not (config_dir / "tunnels.yaml.tmp").exists()
|
|
|
|
|
|
def test_get_tunnel_found(config_dir, sample_tunnel):
|
|
save_tunnels(config_dir, [sample_tunnel])
|
|
t = get_tunnel(config_dir, "web-service")
|
|
assert t.name == "web-service"
|
|
|
|
|
|
def test_get_tunnel_not_found(config_dir):
|
|
with pytest.raises(click.ClickException, match="No tunnel named 'missing'"):
|
|
get_tunnel(config_dir, "missing")
|
|
|
|
|
|
def test_get_config_dir_env_override(monkeypatch, tmp_path):
|
|
monkeypatch.setenv("AUTOSSH_MGR_CONFIG_DIR", str(tmp_path))
|
|
from autossh_mgr.config import get_config_dir
|
|
assert get_config_dir() == tmp_path
|
|
|
|
|
|
def test_save_preserves_non_default_values(config_dir):
|
|
tunnel = TunnelConfig(
|
|
name="custom", host="h.com", user="u",
|
|
local_port=80, remote_port=8080,
|
|
port=2222,
|
|
identity_file="~/.ssh/custom_key",
|
|
local_host="0.0.0.0",
|
|
remote_host="127.0.0.1",
|
|
ssh_options="-v",
|
|
)
|
|
save_tunnels(config_dir, [tunnel])
|
|
loaded = load_tunnels(config_dir)[0]
|
|
assert loaded.port == 2222
|
|
assert loaded.identity_file == "~/.ssh/custom_key"
|
|
assert loaded.local_host == "0.0.0.0"
|
|
assert loaded.remote_host == "127.0.0.1"
|
|
assert loaded.ssh_options == "-v"
|