mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 19:01:54 +00:00
Every script a role ships now lives under `files/<language>/`, and the unit
suite is split the same way: `tests/unit/python/` beside new sibling trees for
the other languages. `tests/lint/filesystem/test_role_files_layout.py` keeps it
that way; directories whose shape is dictated from outside carry a `.nocheck`
naming the constraint (Docker build contexts, WordPress mu-plugins, the Joomla
extension, Odoo addons, Playwright suites, the `files/test/test.sh` harness
that `test-e2e-cli` resolves generically).
This cannot be applied in halves. Moving a file and repointing what names it
are the same change: 14 references resolve only at deploy time, so the tree is
green either before or after, never between.
Those 14 were found by static resolution over every reference class, not by the
test suite -- a full `make quality-high` was green while all of them were
broken:
* 7 `src:` values built from a role variable holding a bare filename
(svc-bkp-remote-2-local, decidim, espocrm, friendica, joomla, pixelfed x2).
The variable doubles as the destination name inside the container, so the
language segment belongs at the call site, not in the variable.
* `sys-service/tasks/04_base.yml` resolved every consumer's systemd script
through `first_found(..., errors='strict')` over `files/script.{sh,py}`.
18 roles ship exactly that and have no template fallback: the play would
have died on each.
* `sys-front-inj-javascript` fell back to `<role>/files/javascript.js`; the
interface now reads `files/javascript/javascript.js`.
* `sys-front-inj-logout/templates/head_sub.j2` built the cache-busting mtime
path through `path_absolute(... 'files', ...)`.
* 4 `ansible.builtin.script:` invocations naming a bare command
(pull.py, prune.sh, set_postgres_superuser_password.sh, ganesha-serve.sh).
* `dev-python/files/shell/install.sh` pointed at a sibling role's script.
Depth-dependent paths inside the moved trees were bumped accordingly: 59
`parents[N]` computations, 12 relative climbs, and the per-role recover CLI,
whose path `cli/administration/recover/recoverers.py` still assembled from the
pre-move location.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
182 lines
6.3 KiB
Python
182 lines
6.3 KiB
Python
import unittest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from plugins.lookup.container_extra_hosts import LookupModule
|
|
|
|
ONION = "auth." + "b" * 56 + ".onion"
|
|
|
|
|
|
class TestContainerExtraHostsLookup(unittest.TestCase):
|
|
def setUp(self):
|
|
self.lookup = LookupModule()
|
|
|
|
def _get(name, *args, **kwargs):
|
|
plugin = MagicMock()
|
|
|
|
def _run(terms, variables=None, **_kwargs):
|
|
vars_ = variables or {}
|
|
if name == "config":
|
|
return [vars_.get("_sso_enabled", False)]
|
|
if name == "tls":
|
|
return [vars_.get("_provider_domain", "")]
|
|
raise AssertionError(f"unexpected lookup '{name}'")
|
|
|
|
plugin.run.side_effect = _run
|
|
return plugin
|
|
|
|
patcher = patch(
|
|
"plugins.lookup.container_extra_hosts.lookup_loader.get", side_effect=_get
|
|
)
|
|
patcher.start()
|
|
self.addCleanup(patcher.stop)
|
|
|
|
def _run(self, extra_hosts=None, **overrides):
|
|
variables = {
|
|
"application_id": "web-app-espocrm",
|
|
"_sso_enabled": True,
|
|
"_provider_domain": ONION,
|
|
"DEPLOYMENT_MODE": "compose",
|
|
}
|
|
variables.update(overrides)
|
|
kwargs = {} if extra_hosts is None else {"extra_hosts": extra_hosts}
|
|
return self.lookup.run(None, variables=variables, **kwargs)[0]
|
|
|
|
def test_compose_pins_the_provider_to_the_host_gateway(self):
|
|
self.assertEqual(
|
|
self._run(),
|
|
'extra_hosts:\n - "host.docker.internal:host-gateway"\n'
|
|
f' - "{ONION}:host-gateway"',
|
|
)
|
|
|
|
def test_swarm_pins_the_provider_to_the_node_address(self):
|
|
block = self._run(
|
|
DEPLOYMENT_MODE="swarm",
|
|
ansible_facts={"default_ipv4": {"address": "10.0.0.7"}},
|
|
)
|
|
self.assertIn(f' - "{ONION}:10.0.0.7"', block)
|
|
self.assertNotIn(f"{ONION}:host-gateway", block)
|
|
|
|
def test_docker_internal_alias_is_always_present(self):
|
|
self.assertIn(' - "host.docker.internal:host-gateway"', self._run())
|
|
|
|
def test_sso_disabled_emits_nothing(self):
|
|
self.assertEqual(self._run(_sso_enabled=False), "")
|
|
|
|
def test_clearnet_provider_emits_nothing(self):
|
|
self.assertEqual(self._run(_provider_domain="auth.example.org"), "")
|
|
|
|
def test_onion_substring_that_is_not_the_suffix_emits_nothing(self):
|
|
self.assertEqual(self._run(_provider_domain="auth.onion.example.org"), "")
|
|
|
|
def test_empty_provider_domain_emits_nothing(self):
|
|
self.assertEqual(self._run(_provider_domain=""), "")
|
|
|
|
def test_compose_mode_force_overrides_the_cluster_mode(self):
|
|
block = self._run(compose_mode_force="compose", DEPLOYMENT_MODE="swarm")
|
|
self.assertIn(f' - "{ONION}:host-gateway"', block)
|
|
|
|
def test_deployment_mode_is_used_without_an_override(self):
|
|
block = self._run(
|
|
compose_mode_force="",
|
|
DEPLOYMENT_MODE="swarm",
|
|
ansible_facts={"default_ipv4": {"address": "10.0.0.8"}},
|
|
)
|
|
self.assertIn(f' - "{ONION}:10.0.0.8"', block)
|
|
|
|
def test_absent_deployment_mode_defaults_to_compose(self):
|
|
variables = {
|
|
"application_id": "web-app-espocrm",
|
|
"_sso_enabled": True,
|
|
"_provider_domain": ONION,
|
|
}
|
|
block = self.lookup.run(None, variables=variables)[0]
|
|
self.assertIn(f' - "{ONION}:host-gateway"', block)
|
|
|
|
def test_swarm_without_facts_raises(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self._run(DEPLOYMENT_MODE="swarm")
|
|
|
|
def test_positional_terms_raise(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self.lookup.run(["web-app-espocrm"], variables={})
|
|
|
|
def test_missing_application_id_raises(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self.lookup.run(None, variables={})
|
|
|
|
def test_application_id_kwarg_overrides_the_play_var(self):
|
|
seen = []
|
|
|
|
def _get(name, *args, **kwargs):
|
|
plugin = MagicMock()
|
|
|
|
def _run(terms, variables=None, **_kwargs):
|
|
if name == "config":
|
|
seen.append(terms[0])
|
|
return [False]
|
|
return [""]
|
|
|
|
plugin.run.side_effect = _run
|
|
return plugin
|
|
|
|
with patch(
|
|
"plugins.lookup.container_extra_hosts.lookup_loader.get", side_effect=_get
|
|
):
|
|
self.lookup.run(
|
|
None,
|
|
variables={"application_id": "web-app-joomla"},
|
|
application_id="web-app-penpot",
|
|
)
|
|
self.assertEqual(seen, ["web-app-penpot"])
|
|
|
|
def test_caller_entries_alone_emit_a_block_without_sso(self):
|
|
self.assertEqual(
|
|
self._run(_sso_enabled=False, extra_hosts=["seaweedfs.example:1.2.3.4"]),
|
|
'extra_hosts:\n - "seaweedfs.example:1.2.3.4"',
|
|
)
|
|
|
|
def test_caller_entries_are_appended_after_the_sso_pins(self):
|
|
block = self._run(extra_hosts=["db.example:10.0.0.9"])
|
|
self.assertEqual(
|
|
block.splitlines(),
|
|
[
|
|
"extra_hosts:",
|
|
' - "host.docker.internal:host-gateway"',
|
|
f' - "{ONION}:host-gateway"',
|
|
' - "db.example:10.0.0.9"',
|
|
],
|
|
)
|
|
|
|
def test_a_bare_string_is_accepted(self):
|
|
self.assertIn(
|
|
' - "one.example:192.0.2.1"',
|
|
self._run(_sso_enabled=False, extra_hosts="one.example:192.0.2.1"),
|
|
)
|
|
|
|
def test_duplicates_collapse_in_first_seen_order(self):
|
|
block = self._run(
|
|
extra_hosts=["host.docker.internal:host-gateway", "db.example:10.0.0.9"]
|
|
)
|
|
self.assertEqual(block.count("host.docker.internal"), 1)
|
|
self.assertEqual(
|
|
block.splitlines()[1], ' - "host.docker.internal:host-gateway"'
|
|
)
|
|
|
|
def test_empty_entries_are_dropped(self):
|
|
self.assertEqual(
|
|
self._run(_sso_enabled=False, extra_hosts=["", None, " "]), ""
|
|
)
|
|
|
|
def test_entry_without_a_separator_raises(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self._run(_sso_enabled=False, extra_hosts=["no-address-here"])
|
|
|
|
def test_no_sources_at_all_emit_nothing(self):
|
|
self.assertEqual(self._run(_sso_enabled=False, extra_hosts=[]), "")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|