Refactor Proxmox settings validation and enhance dotenv loading logic; update README for clarity on configuration requirements

This commit is contained in:
Philip Henning 2026-03-08 19:25:45 +01:00
parent 9a7bd81d17
commit 376e6f5631
10 changed files with 135 additions and 25 deletions

View file

@ -16,9 +16,6 @@ def test_factory_returns_live_service_when_live_env_is_present() -> None:
settings = AppSettings.from_env(
{
"PROXMOX_URL": "https://proxmox.example.invalid:8006",
"PROXMOX_USER": "root",
"PROXMOX_PASSWORD": "secret",
"PROXMOX_REALM": "pam",
},
load_dotenv_file=False,
)

View file

@ -1,3 +1,5 @@
from pathlib import Path
import pytest
from pve_vm_setup.errors import SettingsError
@ -57,6 +59,29 @@ def test_settings_allow_create_by_default_when_prevent_flag_is_unset() -> None:
assert settings.safety_policy.allow_create is True
def test_settings_treat_url_only_as_live_capable_for_interactive_login() -> None:
settings = AppSettings.from_env(
{
"PROXMOX_URL": "https://proxmox.example.invalid:8006",
},
load_dotenv_file=False,
)
assert settings.is_live_configured is True
def test_settings_validate_live_requirements_still_needs_login_defaults_for_doctor() -> None:
settings = AppSettings.from_env(
{
"PROXMOX_URL": "https://proxmox.example.invalid:8006",
},
load_dotenv_file=False,
)
with pytest.raises(SettingsError):
settings.validate_live_requirements()
def test_settings_reject_invalid_default_iso_regex_selector() -> None:
with pytest.raises(SettingsError):
AppSettings.from_env(
@ -65,3 +90,65 @@ def test_settings_reject_invalid_default_iso_regex_selector() -> None:
},
load_dotenv_file=False,
)
def test_settings_loads_current_directory_dotenv_when_present(tmp_path: Path, monkeypatch) -> None:
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("HOME", str(tmp_path / "home"))
(tmp_path / ".env").write_text(
"PROXMOX_URL=https://cwd.example.invalid:8006\n",
encoding="utf-8",
)
settings = AppSettings.from_env({}, load_dotenv_file=True)
assert settings.proxmox_url == "https://cwd.example.invalid:8006"
def test_settings_prefers_config_dotenv_over_current_directory_dotenv(
tmp_path: Path, monkeypatch
) -> None:
home = tmp_path / "home"
config_dir = home / ".config" / "pve-vm-setup"
config_dir.mkdir(parents=True)
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("HOME", str(home))
(tmp_path / ".env").write_text(
"PROXMOX_URL=https://cwd.example.invalid:8006\n",
encoding="utf-8",
)
(config_dir / ".env").write_text(
"PROXMOX_URL=https://config.example.invalid:8006\n",
encoding="utf-8",
)
settings = AppSettings.from_env({}, load_dotenv_file=True)
assert settings.proxmox_url == "https://config.example.invalid:8006"
def test_settings_prefers_environment_over_config_and_current_directory_dotenv(
tmp_path: Path, monkeypatch
) -> None:
home = tmp_path / "home"
config_dir = home / ".config" / "pve-vm-setup"
config_dir.mkdir(parents=True)
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("HOME", str(home))
(tmp_path / ".env").write_text(
"PROXMOX_URL=https://cwd.example.invalid:8006\n",
encoding="utf-8",
)
(config_dir / ".env").write_text(
"PROXMOX_URL=https://config.example.invalid:8006\n",
encoding="utf-8",
)
settings = AppSettings.from_env(
{
"PROXMOX_URL": "https://env.example.invalid:8006",
},
load_dotenv_file=True,
)
assert settings.proxmox_url == "https://env.example.invalid:8006"