mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-30 14:56:11 +00:00
Merge remote-tracking branch 'upstream/main' into feature/role-stalwart-mail-swarm
This commit is contained in:
2209 files changed
+71797
-5913
No files matched your search
+91
@@ -0,0 +1,91 @@
|
||||
"""Unit tests for `cli.administration.deploy.development.inventory.nested_overrides`.
|
||||
|
||||
The service registry is stubbed: what matters is which role a claim is
|
||||
attributed to, not how a service key resolves to its provider.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from cli.administration.deploy.development.inventory.nested_overrides import (
|
||||
NestedOverrideConflictError,
|
||||
collect_provider_overrides,
|
||||
)
|
||||
|
||||
_REGISTRY = {
|
||||
"openwebui": {"role": "web-app-openwebui"},
|
||||
"gitea": {"role": "web-app-gitea"},
|
||||
}
|
||||
|
||||
_PATCH = (
|
||||
"cli.administration.deploy.development.inventory.nested_overrides"
|
||||
".build_service_registry_from_roles_dir"
|
||||
)
|
||||
|
||||
|
||||
class TestCollectProviderOverrides(unittest.TestCase):
|
||||
def _collect(self, payloads, registry=None):
|
||||
with patch(_PATCH, autospec=True) as build:
|
||||
build.return_value = _REGISTRY if registry is None else registry
|
||||
return collect_provider_overrides(payloads, roles_dir="/roles")
|
||||
|
||||
def test_a_named_topic_reaches_its_provider(self) -> None:
|
||||
payloads = {
|
||||
"web-app-moodle": {"services": {"openwebui": {"services": None}}},
|
||||
}
|
||||
self.assertEqual(
|
||||
{"web-app-openwebui": {"services": None}}, self._collect(payloads)
|
||||
)
|
||||
|
||||
def test_several_topics_of_one_provider_are_collected_together(self) -> None:
|
||||
payloads = {
|
||||
"web-app-moodle": {
|
||||
"services": {
|
||||
"openwebui": {"services": None, "addons": {"whisper": False}},
|
||||
},
|
||||
},
|
||||
}
|
||||
self.assertEqual(
|
||||
{"web-app-openwebui": {"services": None, "addons": {"whisper": False}}},
|
||||
self._collect(payloads),
|
||||
)
|
||||
|
||||
def test_a_roles_own_entry_is_not_an_override(self) -> None:
|
||||
payloads = {"web-app-gitea": {"services": {"gitea": {"users": {"a": 1}}}}}
|
||||
self.assertEqual({}, self._collect(payloads))
|
||||
|
||||
def test_an_unregistered_service_key_is_skipped(self) -> None:
|
||||
payloads = {"web-app-moodle": {"services": {"nowhere": {"services": None}}}}
|
||||
self.assertEqual({}, self._collect(payloads))
|
||||
|
||||
def test_two_roles_agreeing_is_not_a_conflict(self) -> None:
|
||||
payloads = {
|
||||
"web-app-moodle": {"services": {"openwebui": {"services": None}}},
|
||||
"web-app-gitea": {"services": {"openwebui": {"services": None}}},
|
||||
}
|
||||
self.assertEqual(
|
||||
{"web-app-openwebui": {"services": None}}, self._collect(payloads)
|
||||
)
|
||||
|
||||
def test_two_roles_demanding_different_values_raises(self) -> None:
|
||||
payloads = {
|
||||
"web-app-moodle": {"services": {"openwebui": {"services": None}}},
|
||||
"web-app-gitea": {"services": {"openwebui": {"services": {"a": 1}}}},
|
||||
}
|
||||
with self.assertRaises(NestedOverrideConflictError):
|
||||
self._collect(payloads)
|
||||
|
||||
def test_a_round_without_overrides_never_builds_the_registry(self) -> None:
|
||||
payloads = {"web-app-moodle": {"services": {"openwebui": {"enabled": True}}}}
|
||||
with patch(_PATCH, autospec=True) as build:
|
||||
build.return_value = _REGISTRY
|
||||
self.assertEqual(
|
||||
{}, collect_provider_overrides(payloads, roles_dir="/roles")
|
||||
)
|
||||
build.assert_not_called()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,65 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from cli.administration.deploy.development.coredns import CoreDNSCorefileRenderer
|
||||
from utils.cache.files import PROJECT_ROOT, read_text
|
||||
|
||||
TEMPLATE = "compose/coredns/Corefile.tmpl"
|
||||
|
||||
|
||||
def _render() -> str:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
(root / ".env").write_text(
|
||||
'INFINITO_DOMAIN=infinito.test\nINFINITO_IP4="172.30.0.10"\n',
|
||||
encoding="utf-8",
|
||||
)
|
||||
template = root / TEMPLATE
|
||||
template.parent.mkdir(parents=True)
|
||||
template.write_text(read_text(str(PROJECT_ROOT / TEMPLATE)), encoding="utf-8")
|
||||
out = CoreDNSCorefileRenderer(repo_root=root).render(show_preview=False)
|
||||
return read_text(str(out))
|
||||
|
||||
|
||||
class TestCoreDNSCorefileRenderer(unittest.TestCase):
|
||||
def test_the_domain_regex_is_derived_from_the_domain_escaped_once(self) -> None:
|
||||
corefile = _render()
|
||||
self.assertIn("match ^infinito\\.test\\.$", corefile)
|
||||
self.assertIn("match ^(.*)\\.infinito\\.test\\.$", corefile)
|
||||
self.assertNotIn("\\\\", corefile)
|
||||
self.assertNotIn("${", corefile)
|
||||
|
||||
def test_the_deployment_zone_answers_every_type_itself(self) -> None:
|
||||
corefile = _render()
|
||||
catch_all = "template IN ANY infinito.test {"
|
||||
self.assertIn(catch_all, corefile)
|
||||
self.assertIn("IN SOA ns.infinito.test. hostmaster.infinito.test.", corefile)
|
||||
lines = corefile.splitlines()
|
||||
self.assertLess(
|
||||
max(
|
||||
index
|
||||
for index, line in enumerate(lines)
|
||||
if line.strip() == "template IN A {"
|
||||
),
|
||||
next(
|
||||
index for index, line in enumerate(lines) if line.strip() == catch_all
|
||||
),
|
||||
"the zone-wide template must come after the A templates, or it "
|
||||
"answers their names with NODATA and the deployment loses its own "
|
||||
"addresses",
|
||||
)
|
||||
self.assertLess(
|
||||
lines.index(f" {catch_all}"),
|
||||
next(index for index, line in enumerate(lines) if "forward ." in line),
|
||||
"a type the zone does not answer must not reach the public "
|
||||
"resolvers: they return NXDOMAIN for the reserved TLD, and a "
|
||||
"caching resolver in front then denies every type of that name, "
|
||||
"including the A record the deployment depends on",
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user