Refactor Proxmox settings validation and enhance dotenv loading logic; update README for clarity on configuration requirements
This commit is contained in:
parent
9a7bd81d17
commit
376e6f5631
10 changed files with 135 additions and 25 deletions
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue