126 lines
4.4 KiB
Python
126 lines
4.4 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)
|
|
monkeypatch.delenv("REPORT_LANGUAGE", 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.report_language == "fr"
|
|
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,
|
|
)
|
|
|
|
|
|
def test_load_config_accepts_report_language(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",
|
|
"REPORT_LANGUAGE=en",
|
|
]
|
|
),
|
|
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)
|
|
monkeypatch.delenv("REPORT_LANGUAGE", raising=False)
|
|
|
|
config = load_config(env_file)
|
|
|
|
assert config.report_language == "en"
|
|
|
|
|
|
def test_load_config_rejects_invalid_report_language(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",
|
|
"REPORT_LANGUAGE=de",
|
|
]
|
|
),
|
|
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)
|
|
monkeypatch.delenv("REPORT_LANGUAGE", raising=False)
|
|
|
|
with pytest.raises(ConfigError, match="REPORT_LANGUAGE doit valoir fr ou en"):
|
|
load_config(env_file)
|