mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-08 11:46:35 +00:00
The hardcoded-primary-domain lint exempted tests/ and scripts/tests/, so test fixtures kept their own copy of the domain and would drift from INFINITO_DOMAIN the moment it changes. The lint now scans both trees, and a unit test pins its scope: only default.env, Markdown, inventories/, .claude/ and build/ may name the domain. Python tests take the value from default_domain_primary(), and the workspace helper builds its dashboard and Matomo URLs from INFINITO_DOMAIN once the repo .env is loaded. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
164 lines
5.4 KiB
Python
164 lines
5.4 KiB
Python
import importlib
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from utils.cache.yaml import dump_yaml_str
|
|
from utils.domains.default_primary import default_domain_primary
|
|
from utils.roles.mapping import (
|
|
ROLE_FILE_META_DOMAINS,
|
|
ROLE_FILE_META_SERVER,
|
|
ROLE_FILE_VARS_MAIN,
|
|
)
|
|
|
|
domain_list = importlib.import_module("utils.domains.list")
|
|
DOMAIN = default_domain_primary()
|
|
|
|
|
|
class TestDomainList(unittest.TestCase):
|
|
def write_role(self, roles_dir: Path, role_name: str, app_id: str, config: dict):
|
|
role_dir = roles_dir / role_name
|
|
(role_dir / "vars").mkdir(parents=True, exist_ok=True)
|
|
(role_dir / "meta").mkdir(parents=True, exist_ok=True)
|
|
(role_dir / ROLE_FILE_VARS_MAIN).write_text(
|
|
dump_yaml_str({"application_id": app_id}),
|
|
encoding="utf-8",
|
|
)
|
|
server_payload = (
|
|
dict(config.get("server", {})) if isinstance(config, dict) else {}
|
|
)
|
|
domains_payload = server_payload.pop("domains", {})
|
|
(role_dir / ROLE_FILE_META_SERVER).write_text(
|
|
dump_yaml_str(server_payload),
|
|
encoding="utf-8",
|
|
)
|
|
(role_dir / ROLE_FILE_META_DOMAINS).write_text(
|
|
dump_yaml_str(domains_payload),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
def test_list_application_domains_renders_and_flattens_supported_shapes(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
roles_dir = Path(tmp) / "roles"
|
|
roles_dir.mkdir()
|
|
|
|
self.write_role(
|
|
roles_dir,
|
|
"web-app-dashboard",
|
|
"web-app-dashboard",
|
|
{
|
|
"server": {
|
|
"domains": {
|
|
"canonical": ["dashboard.{{ DOMAIN_PRIMARY }}"],
|
|
"aliases": ["www.dashboard.{{ DOMAIN_PRIMARY }}"],
|
|
}
|
|
}
|
|
},
|
|
)
|
|
self.write_role(
|
|
roles_dir,
|
|
"web-app-minio",
|
|
"web-app-minio",
|
|
{
|
|
"server": {
|
|
"domains": {
|
|
"canonical": {
|
|
"api": "api.s3.{{ DOMAIN_PRIMARY }}",
|
|
"console": "console.s3.{{ DOMAIN_PRIMARY }}",
|
|
},
|
|
"aliases": [],
|
|
}
|
|
}
|
|
},
|
|
)
|
|
|
|
with patch.object(domain_list, "ROLES_DIR", roles_dir):
|
|
domains = domain_list.list_application_domains(DOMAIN)
|
|
|
|
self.assertEqual(
|
|
domains,
|
|
sorted(
|
|
[
|
|
f"api.s3.{DOMAIN}",
|
|
f"console.s3.{DOMAIN}",
|
|
f"dashboard.{DOMAIN}",
|
|
f"test.{DOMAIN}",
|
|
]
|
|
),
|
|
)
|
|
|
|
def test_list_application_domains_can_include_aliases_and_www_variants(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
roles_dir = Path(tmp) / "roles"
|
|
roles_dir.mkdir()
|
|
|
|
self.write_role(
|
|
roles_dir,
|
|
"web-app-dashboard",
|
|
"web-app-dashboard",
|
|
{
|
|
"server": {
|
|
"domains": {
|
|
"canonical": ["dashboard.{{ DOMAIN_PRIMARY }}"],
|
|
"aliases": ["www.dashboard.{{ DOMAIN_PRIMARY }}"],
|
|
}
|
|
}
|
|
},
|
|
)
|
|
|
|
with patch.object(domain_list, "ROLES_DIR", roles_dir):
|
|
domains = domain_list.list_application_domains(
|
|
DOMAIN,
|
|
include_aliases=True,
|
|
include_www=True,
|
|
)
|
|
|
|
self.assertEqual(
|
|
domains,
|
|
[
|
|
f"dashboard.{DOMAIN}",
|
|
f"test.{DOMAIN}",
|
|
f"www.dashboard.{DOMAIN}",
|
|
f"www.test.{DOMAIN}",
|
|
],
|
|
)
|
|
|
|
def test_list_application_domains_includes_derived_test_domain_without_roles(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
roles_dir = Path(tmp) / "roles"
|
|
roles_dir.mkdir()
|
|
|
|
with patch.object(domain_list, "ROLES_DIR", roles_dir):
|
|
domains = domain_list.list_application_domains(DOMAIN)
|
|
|
|
self.assertEqual(domains, [f"test.{DOMAIN}"])
|
|
|
|
def test_list_application_domains_raises_on_collisions(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
roles_dir = Path(tmp) / "roles"
|
|
roles_dir.mkdir()
|
|
|
|
shared_config = {
|
|
"server": {
|
|
"domains": {
|
|
"canonical": ["same.{{ DOMAIN_PRIMARY }}"],
|
|
"aliases": [],
|
|
}
|
|
}
|
|
}
|
|
self.write_role(roles_dir, "app-a", "app-a", shared_config)
|
|
self.write_role(roles_dir, "app-b", "app-b", shared_config)
|
|
|
|
with (
|
|
patch.object(domain_list, "ROLES_DIR", roles_dir),
|
|
self.assertRaises(AnsibleError),
|
|
):
|
|
domain_list.list_application_domains(DOMAIN)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|