Files
PVE-Backup-Report/tests/test_config.py
T
2026-05-13 16:04:17 +02:00

79 lines
2.8 KiB
Python

import os
import pytest
from pve_backup_report.config import ConfigError, load_config, parse_pbs_servers
def test_load_config_from_env_file(tmp_path, monkeypatch) -> None:
env_file = tmp_path / ".env"
env_file.write_text(
"\n".join(
[
"PVE_API_URL=https://pve.example.invalid:8006",
"PVE_API_TOKEN_ID=backup-report@pve!report",
"PVE_API_TOKEN_SECRET=secret",
"PVE_VERIFY_TLS=false",
"PVE_TIMEOUT_SECONDS=10",
"PBS_HOSTNAMES=192.0.2.10=backup-a,192.0.2.11=backup-b",
]
),
encoding="utf-8",
)
monkeypatch.delenv("PVE_API_URL", raising=False)
monkeypatch.delenv("PVE_API_TOKEN_ID", raising=False)
monkeypatch.delenv("PVE_API_TOKEN_SECRET", raising=False)
for key in list(os.environ):
if key.startswith("PBS") and key != "PBS_HOSTNAMES":
monkeypatch.delenv(key, raising=False)
config = load_config(env_file)
assert config.pve_api_url == "https://pve.example.invalid:8006"
assert config.pve_verify_tls is False
assert config.pve_timeout_seconds == 10
assert config.pve_backup_jobs_endpoint == "/cluster/backup"
assert config.pve_task_history_limit == 500
assert config.pve_task_log_limit == 5000
assert config.configured_pbs_servers == ()
assert config.pbs_hostnames == {
"192.0.2.10": "backup-a",
"192.0.2.11": "backup-b",
}
def test_parse_pbs_servers_detects_unbounded_numeric_prefixes() -> None:
servers = parse_pbs_servers(
{
"PBS10_NAME": "PBS10",
"PBS10_API_URL": "https://backup-j.example.invalid:8007",
"PBS10_API_TOKEN_ID": "backup-report@pbs!report",
"PBS10_API_TOKEN_SECRET": "secret10",
"PBS02_NAME": "PBS02",
"PBS02_API_URL": "https://backup-b.example.invalid:8007",
"PBS02_API_TOKEN_ID": "backup-report@pbs!report",
"PBS02_API_TOKEN_SECRET": "secret2",
},
pve_verify_tls=True,
pve_timeout_seconds=30,
)
assert [server.prefix for server in servers] == ["PBS02", "PBS10"]
assert [server.name for server in servers] == ["PBS02", "PBS10"]
assert [server.api_url for server in servers] == [
"https://backup-b.example.invalid:8007",
"https://backup-j.example.invalid:8007",
]
def test_parse_pbs_servers_rejects_incomplete_api_block() -> None:
with pytest.raises(ConfigError, match="configuration PBS04 incomplete"):
parse_pbs_servers(
{
"PBS04_API_URL": "https://backup-d.example.invalid:8007",
"PBS04_API_TOKEN_ID": "backup-report@pbs!report",
},
pve_verify_tls=True,
pve_timeout_seconds=30,
)