Files
computer-playbook/tests/unit/python/utils/domains/test_list.py
T
kevinveenbirkenbachandClaude Opus 5 af605b049f refactor(domains): give local testing a .test domain, leave .example to docs
RFC 6761 reserves .test for testing and .example for documentation, and the
project's own domain was doing both jobs under the documentation name. It is
what default.env hands every local deploy and what the test fixtures assert
against, so it is a testing name.

Rename it wherever it stands for the running machine: the environment
defaults, the development inventory, the workspace and trust scripts, the test
suites, and the four documents that describe real behaviour - the DNS mapping
to loopback, the browser CA trust, WSL2 resolution. Those documents would have
started lying the moment default.env changed.

The sandbox allowlist moves with them, since egress is matched per pattern and
nothing resolves the old name any more, and the sandbox document that mirrors
that list follows it.

Two places keep .example on purpose: the link-style sample in the
documentation guide, which is documentation about documentation, and every
generic fixture name elsewhere in the suite - a.example, foo.example and their
kin. Those are a larger sweep of their own and are not touched here.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-02 13:10:34 +02:00

162 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.roles.mapping import (
ROLE_FILE_META_DOMAINS,
ROLE_FILE_META_SERVER,
ROLE_FILE_VARS_MAIN,
)
domain_list = importlib.import_module("utils.domains.list")
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("infinito.test")
self.assertEqual(
domains,
sorted(
[
"api.s3.infinito.test",
"console.s3.infinito.test",
"dashboard.infinito.test",
"test.infinito.test",
]
),
)
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(
"infinito.test",
include_aliases=True,
include_www=True,
)
self.assertEqual(
domains,
[
"dashboard.infinito.test",
"test.infinito.test",
"www.dashboard.infinito.test",
"www.test.infinito.test",
],
)
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("infinito.test")
self.assertEqual(domains, ["test.infinito.test"])
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("infinito.test")
if __name__ == "__main__":
unittest.main()