first commit
This commit is contained in:
commit
dd50a0d05e
31 files changed
+5299
No files matched your search
Executable
+165
@@ -0,0 +1,165 @@
|
||||
"""Configuration parsing and validation."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from proxdomain.config import ConfigError, load_config, parse_config
|
||||
from tests.support import make_config
|
||||
|
||||
|
||||
def raw_config() -> dict:
|
||||
return {
|
||||
"general": {"base_domain": "example.com"},
|
||||
"proxmox": {
|
||||
"api_url": "https://pve.example.com:8006/api2/json",
|
||||
"token_id": "automation@pve!proxdomain",
|
||||
"token_secret": "secret",
|
||||
},
|
||||
"cloudflare": {"api_token": "cf-token"},
|
||||
"webserver": {"kind": "nginx", "manage_dir": "/etc/nginx/conf.d/proxdomain.d"},
|
||||
}
|
||||
|
||||
|
||||
class ConfigTests(unittest.TestCase):
|
||||
def test_defaults(self) -> None:
|
||||
config = make_config()
|
||||
self.assertEqual(config.general.default_port, 80)
|
||||
self.assertEqual(config.general.domain_template, "{hostname}.{base_domain}")
|
||||
self.assertEqual(config.webserver.test_command, ("nginx", "-t"))
|
||||
self.assertEqual(config.webserver.reload_command, ("systemctl", "reload", "nginx"))
|
||||
self.assertEqual(config.webserver.tls.mode, "none")
|
||||
self.assertEqual(config.proxmox.ip_preference, "ipv4")
|
||||
|
||||
def test_requires_base_domain(self) -> None:
|
||||
raw = raw_config()
|
||||
del raw["general"]["base_domain"]
|
||||
with self.assertRaises(ConfigError) as ctx:
|
||||
parse_config(raw)
|
||||
self.assertIn("general.base_domain", str(ctx.exception))
|
||||
|
||||
def test_base_domain_must_have_a_dot(self) -> None:
|
||||
raw = raw_config()
|
||||
raw["general"]["base_domain"] = "localhost"
|
||||
with self.assertRaises(ConfigError):
|
||||
parse_config(raw)
|
||||
|
||||
def test_template_needs_a_placeholder(self) -> None:
|
||||
raw = raw_config()
|
||||
raw["general"]["domain_template"] = "static.example.com"
|
||||
with self.assertRaises(ConfigError) as ctx:
|
||||
parse_config(raw)
|
||||
self.assertIn("domain_template", str(ctx.exception))
|
||||
|
||||
def test_token_id_shape(self) -> None:
|
||||
raw = raw_config()
|
||||
raw["proxmox"]["token_id"] = "automation@pve"
|
||||
with self.assertRaises(ConfigError) as ctx:
|
||||
parse_config(raw)
|
||||
self.assertIn("token_id", str(ctx.exception))
|
||||
|
||||
def test_certbot_requires_email(self) -> None:
|
||||
raw = raw_config()
|
||||
raw["webserver"]["tls"] = {"mode": "certbot"}
|
||||
with self.assertRaises(ConfigError) as ctx:
|
||||
parse_config(raw)
|
||||
self.assertIn("email", str(ctx.exception))
|
||||
|
||||
def test_certbot_only_for_nginx(self) -> None:
|
||||
raw = raw_config()
|
||||
raw["webserver"]["kind"] = "caddy"
|
||||
raw["webserver"]["tls"] = {"mode": "certbot", "email": "a@example.com"}
|
||||
with self.assertRaises(ConfigError):
|
||||
parse_config(raw)
|
||||
|
||||
def test_traefik_files_tls_rejected(self) -> None:
|
||||
raw = raw_config()
|
||||
raw["webserver"]["kind"] = "traefik"
|
||||
raw["webserver"]["tls"] = {"mode": "files"}
|
||||
with self.assertRaises(ConfigError):
|
||||
parse_config(raw)
|
||||
|
||||
def test_ssh_mode_requires_host(self) -> None:
|
||||
raw = raw_config()
|
||||
raw["webserver"]["mode"] = "ssh"
|
||||
with self.assertRaises(ConfigError) as ctx:
|
||||
parse_config(raw)
|
||||
self.assertIn("ssh_host", str(ctx.exception))
|
||||
|
||||
def test_invalid_choice(self) -> None:
|
||||
raw = raw_config()
|
||||
raw["webserver"]["kind"] = "apache"
|
||||
with self.assertRaises(ConfigError) as ctx:
|
||||
parse_config(raw)
|
||||
self.assertIn("nginx, caddy, traefik", str(ctx.exception))
|
||||
|
||||
def test_secret_from_environment(self) -> None:
|
||||
raw = raw_config()
|
||||
raw["proxmox"] = {
|
||||
"api_url": "https://pve:8006/api2/json",
|
||||
"token_id": "a@pve!t",
|
||||
"token_secret_env": "PROX_TEST_PVE_SECRET",
|
||||
}
|
||||
raw["cloudflare"] = {"api_token_env": "PROX_TEST_CF_TOKEN"}
|
||||
with mock.patch.dict(
|
||||
os.environ, {"PROX_TEST_PVE_SECRET": "s3cret", "PROX_TEST_CF_TOKEN": "cf"}
|
||||
):
|
||||
config = parse_config(raw)
|
||||
self.assertEqual(config.proxmox.resolve_secret(), "s3cret")
|
||||
self.assertEqual(config.cloudflare.resolve_token(), "cf")
|
||||
with mock.patch.dict(os.environ, {}, clear=True):
|
||||
with self.assertRaises(ConfigError):
|
||||
config.proxmox.resolve_secret()
|
||||
|
||||
def test_secret_from_file(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
secret = Path(tmp) / "pve.secret"
|
||||
secret.write_text("from-file\n", encoding="utf-8")
|
||||
raw = raw_config()
|
||||
raw["proxmox"].pop("token_secret")
|
||||
raw["proxmox"]["token_secret_file"] = str(secret)
|
||||
config = parse_config(raw)
|
||||
self.assertEqual(config.proxmox.resolve_secret(), "from-file")
|
||||
|
||||
def test_load_from_disk_and_env(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "proxdomain.toml"
|
||||
path.write_text(
|
||||
"[general]\nbase_domain = \"example.com\"\n"
|
||||
"[proxmox]\napi_url = \"https://pve:8006/api2/json\"\n"
|
||||
"token_id = \"a@pve!t\"\ntoken_secret = \"x\"\n"
|
||||
"[cloudflare]\napi_token = \"y\"\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
config = load_config(path)
|
||||
self.assertEqual(config.general.base_domain, "example.com")
|
||||
self.assertEqual(config.source, path)
|
||||
with mock.patch.dict(os.environ, {"PROXDOMAIN_CONFIG": str(path)}):
|
||||
self.assertEqual(load_config().general.base_domain, "example.com")
|
||||
|
||||
def test_missing_config_file(self) -> None:
|
||||
with self.assertRaises(ConfigError) as ctx:
|
||||
load_config("/nonexistent/proxdomain.toml")
|
||||
self.assertIn("no configuration file found", str(ctx.exception))
|
||||
|
||||
def test_empty_test_command_disables_validation(self) -> None:
|
||||
raw = raw_config()
|
||||
raw["webserver"]["test_command"] = []
|
||||
config = parse_config(raw)
|
||||
self.assertEqual(config.webserver.test_command, ())
|
||||
|
||||
def test_bad_toml(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "broken.toml"
|
||||
path.write_text("general = [", encoding="utf-8")
|
||||
with self.assertRaises(ConfigError) as ctx:
|
||||
load_config(path)
|
||||
self.assertIn("not valid TOML", str(ctx.exception))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user