mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-08 11:46:35 +00:00
DEPLOYMENT_MODE was compared against a quoted literal 219 times across 110
files. IS_COMPOSE_MODE and IS_SWARM_MODE say the same thing once, and
test_deployment_mode_booleans keeps it that way: only the file that defines
them may still compare.
Both are real booleans under this ansible, measured before the swap because
the alternative is silent: a variable that templates to the string "False" is
truthy in Jinja, which is why IS_STACK_HOST is written `| bool` at every use.
These need no filter.
What had to learn the names:
* utils/annotations/task_gate.py, which several lints ask whether a task is
compose-only gated;
* four compose-template lints that recognise their gate by matching the
expression;
* the fixtures that stand in for the play's variables. They derive the
booleans from the mode now instead of carrying them, or a test that
overrides DEPLOYMENT_MODE keeps the old pair and asserts the wrong
branch.
Each replacement was checked rather than trusted: the 206 conditions that
changed in place were evaluated old against new through the Templar under
compose, swarm and host, with no disagreement, and no changed line turned out
to be anything but a condition swap. `!= 'swarm'` became `not IS_SWARM_MODE`
rather than the shorter IS_COMPOSE_MODE, because only the literal translation
stays correct should the mode ever be host.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
214 lines
7.4 KiB
Python
214 lines
7.4 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",
|
|
"IS_COMPOSE_MODE": True,
|
|
"IS_SWARM_MODE": False,
|
|
}
|
|
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=[]), "")
|
|
|
|
def _run_entries(self, **overrides):
|
|
variables = {
|
|
"application_id": "web-app-discourse",
|
|
"_sso_enabled": True,
|
|
"_provider_domain": ONION,
|
|
"DEPLOYMENT_MODE": "compose",
|
|
"IS_COMPOSE_MODE": True,
|
|
"IS_SWARM_MODE": False,
|
|
}
|
|
variables.update(overrides)
|
|
return self.lookup.run(None, variables=variables, entries_only=True)
|
|
|
|
def test_entries_only_returns_the_raw_mappings(self):
|
|
self.assertEqual(
|
|
self._run_entries(),
|
|
["host.docker.internal:host-gateway", f"{ONION}:host-gateway"],
|
|
)
|
|
|
|
def test_entries_only_follows_the_swarm_address(self):
|
|
self.assertEqual(
|
|
self._run_entries(
|
|
DEPLOYMENT_MODE="swarm",
|
|
ansible_facts={"default_ipv4": {"address": "10.0.0.7"}},
|
|
),
|
|
["host.docker.internal:host-gateway", f"{ONION}:10.0.0.7"],
|
|
)
|
|
|
|
def test_entries_only_is_empty_without_sso(self):
|
|
self.assertEqual(self._run_entries(_sso_enabled=False), [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|