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>
156 lines
5.4 KiB
Python
156 lines
5.4 KiB
Python
"""Unit tests for plugins/lookup/container_networks.py.
|
|
|
|
Mocks the Ansible-side machinery and asserts that the plugin glue
|
|
resolves variables and chains the underlying ``render_container_networks``
|
|
function correctly. Rendering correctness itself is covered by
|
|
tests.unit.utils.networks.test_render and the integration snapshot test.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
import unittest.mock as mock
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from plugins.lookup.container_networks import LookupModule
|
|
|
|
|
|
class _Templar:
|
|
def __init__(self, vars_=None):
|
|
self.available_variables = vars_ or {}
|
|
|
|
def template(self, value, **_):
|
|
return value
|
|
|
|
|
|
def _make_lookup(vars_=None):
|
|
lm = LookupModule()
|
|
lm._templar = _Templar(vars_ or {})
|
|
lm._loader = mock.MagicMock()
|
|
return lm
|
|
|
|
|
|
class TestContainerNetworksLookup(unittest.TestCase):
|
|
def test_raises_when_positional_terms_supplied(self):
|
|
lm = _make_lookup({"application_id": "x", "DEPLOYMENT_MODE": "swarm"})
|
|
with self.assertRaises(AnsibleError):
|
|
lm.run(["unexpected"], variables=lm._templar.available_variables)
|
|
|
|
def test_raises_when_application_id_missing(self):
|
|
lm = _make_lookup({"DEPLOYMENT_MODE": "swarm"})
|
|
with self.assertRaises(AnsibleError):
|
|
lm.run([], variables={})
|
|
|
|
def test_chains_render_with_resolved_vars(self):
|
|
vars_ = {
|
|
"application_id": "web-app-baserow",
|
|
"DEPLOYMENT_MODE": "compose",
|
|
"IS_COMPOSE_MODE": True,
|
|
"IS_SWARM_MODE": False,
|
|
}
|
|
lm = _make_lookup(vars_)
|
|
|
|
with (
|
|
mock.patch(
|
|
"utils.networks.lookup_context.build_service_registry_from_applications",
|
|
return_value={},
|
|
),
|
|
mock.patch("utils.networks.lookup_context.lookup_loader") as loader_mock,
|
|
mock.patch(
|
|
"plugins.lookup.container_networks.render_container_networks",
|
|
return_value="RENDERED",
|
|
) as render_mock,
|
|
):
|
|
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [{}])
|
|
|
|
result = lm.run([], variables=vars_)
|
|
|
|
self.assertEqual(result, ["RENDERED"])
|
|
kwargs = render_mock.call_args.kwargs
|
|
self.assertEqual(kwargs["application_id"], "web-app-baserow")
|
|
self.assertEqual(kwargs["deployment_mode"], "compose")
|
|
|
|
def test_falls_back_to_compose_when_deployment_mode_unset(self):
|
|
vars_ = {"application_id": "web-app-x"}
|
|
lm = _make_lookup(vars_)
|
|
|
|
with (
|
|
mock.patch(
|
|
"utils.networks.lookup_context.build_service_registry_from_applications",
|
|
return_value={},
|
|
),
|
|
mock.patch("utils.networks.lookup_context.lookup_loader") as loader_mock,
|
|
mock.patch(
|
|
"plugins.lookup.container_networks.render_container_networks",
|
|
return_value="RENDERED",
|
|
) as render_mock,
|
|
):
|
|
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [{}])
|
|
lm.run([], variables=vars_)
|
|
|
|
self.assertEqual(render_mock.call_args.kwargs["deployment_mode"], "compose")
|
|
|
|
def test_lookup_closures_pass_arguments_in_expected_order(self):
|
|
"""config_lookup.run([app, path, default]) and
|
|
database_lookup.run([app, key]), both with variables=vars_."""
|
|
vars_ = {
|
|
"application_id": "web-app-x",
|
|
"DEPLOYMENT_MODE": "swarm",
|
|
"IS_COMPOSE_MODE": False,
|
|
"IS_SWARM_MODE": True,
|
|
}
|
|
lm = _make_lookup(vars_)
|
|
|
|
config_calls: list[tuple] = []
|
|
database_calls: list[tuple] = []
|
|
|
|
def _config_run(args, **kwargs):
|
|
config_calls.append((tuple(args), kwargs))
|
|
return [args[2] if len(args) > 2 else None]
|
|
|
|
def _database_run(args, **kwargs):
|
|
database_calls.append((tuple(args), kwargs))
|
|
return [""]
|
|
|
|
def _exercise(*_, lookup_config, lookup_database, **__):
|
|
lookup_config("web-app-x", "services.ldap.enabled", False)
|
|
lookup_database("web-app-x", "shared")
|
|
return "RENDERED"
|
|
|
|
with (
|
|
mock.patch(
|
|
"utils.networks.lookup_context.build_service_registry_from_applications",
|
|
return_value={},
|
|
),
|
|
mock.patch("utils.networks.lookup_context.lookup_loader") as loader_mock,
|
|
mock.patch(
|
|
"plugins.lookup.container_networks.render_container_networks",
|
|
side_effect=_exercise,
|
|
),
|
|
):
|
|
|
|
def _get(name, **_):
|
|
if name == "applications":
|
|
return mock.MagicMock(run=lambda *_a, **_k: [{}])
|
|
if name == "config":
|
|
return mock.MagicMock(run=_config_run)
|
|
return mock.MagicMock(run=_database_run)
|
|
|
|
loader_mock.get.side_effect = _get
|
|
lm.run([], variables=vars_)
|
|
|
|
self.assertEqual(len(config_calls), 1)
|
|
args, kwargs = config_calls[0]
|
|
self.assertEqual(args, ("web-app-x", "services.ldap.enabled", False))
|
|
self.assertEqual(kwargs.get("variables"), vars_)
|
|
|
|
self.assertEqual(len(database_calls), 1)
|
|
args, kwargs = database_calls[0]
|
|
self.assertEqual(args, ("web-app-x", "shared"))
|
|
self.assertEqual(kwargs.get("variables"), vars_)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|