Files
computer-playbook/tests/unit/python/plugins/lookup/test_compose_networks.py
T
kevinveenbirkenbachandClaude Opus 5 53a20bc4c9 refactor(vars): ask the deployment mode by name, not by string comparison
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>
2026-09-26 02:51:58 +02:00

166 lines
5.7 KiB
Python

"""Unit tests for plugins/lookup/compose_networks.py.
Mocks the Ansible-side machinery (loader, lookup_loader, templar,
build_service_registry_from_applications) and
asserts that the plugin glue resolves variables and chains the underlying
``render_compose_networks`` function correctly.
The 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.compose_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 TestComposeNetworksLookup(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": "swarm",
"IS_COMPOSE_MODE": False,
"IS_SWARM_MODE": True,
"swarm": {"network": {"encryption": 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.compose_networks.render_compose_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"], "swarm")
self.assertFalse(kwargs["swarm_encrypted"])
def test_swarm_encrypted_defaults_to_true_when_unset(self):
vars_ = {
"application_id": "web-app-x",
"DEPLOYMENT_MODE": "swarm",
"IS_COMPOSE_MODE": False,
"IS_SWARM_MODE": True,
}
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.compose_networks.render_compose_networks",
return_value="RENDERED",
) as render_mock,
):
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [""])
lm.run([], variables=vars_)
self.assertTrue(render_mock.call_args.kwargs["swarm_encrypted"])
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.sso.enabled", False)
lookup_database("web-app-x", "id")
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.compose_networks.render_compose_networks",
side_effect=_exercise,
),
):
def _get(name, **_):
if name == "config":
return mock.MagicMock(run=_config_run)
if name == "applications":
return mock.MagicMock(run=lambda *_a, **_k: [{}])
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.sso.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", "id"))
self.assertEqual(kwargs.get("variables"), vars_)
if __name__ == "__main__":
unittest.main()