Files
computer-playbook/tests/unit/python/plugins/lookup/test_addon_env_flags.py
T
kevinveenbirkenbachandClaude Opus 5 7162b7bf4c Merge fork/feature/svc-net-tor into feature/ai-agent-employees
834 commits from the Tor onion-service branch against 299 of ours, from merge
base 7b646bdece: 127 conflicted files, 3506 files changed.

The two sides are largely orthogonal. Theirs adds svc-net-tor and a `tor` service
across the role tree, plus two sweeping refactors: shipped scripts sorted by
language (files/ruby/, files/python/, files/shell/, scripts/lint/*.sh) and the
unit suite moved under tests/unit/python/. Ours is requirement 036, the native AI
gateway, plus the MCP role work. Most conflicts were both sides appending to the
same service map, resolved as unions.

Resolutions that were not unions:

Credentials moved namespace. Theirs renamed meta/schema.yml to meta/secrets.yml
and re-addressed credentials as secrets.credentials.<name>. Git carried our
litellm_api_key blocks through the rename, but every consumer we added still read
'credentials.<name>', which no longer resolves. All 45 call sites are migrated.

plugins/filter/container_volumes.py is deleted by theirs and relocated to
plugins/lookup/container_volumes.py, where it carries the same tmpfs defect our
f85996b43 had just fixed: a hard-coded {"size": ...} that silently drops a per
mount `mode`. The fix is re-applied in the relocated file, as that commit's merge
note anticipated.

compose.yml keeps only INFINITO_PLAYWRIGHT_REPORTS_BASE_DIR. Theirs also forwards
INFINITO_PLAYWRIGHT_STAGE_BASE_DIR; that line is deliberately dropped, per the
analysis recorded in 45804e9ad -- STAGE has a drift-proof role-vars fallback,
while forwarding it promotes a setdefault-pinnable .env value above that SPOT.

tasks/stages/01_constructor.yml takes their include, ./tasks/utils/setup/group.yml,
because their refactor removed tasks/groups/*-roles.yml, leaving our include
target nonexistent. Our task name is kept, since svc-virt is in the loop.

Makefile swarm-zombie is a union: their validate plus teardown, then our
lab_subnet cleanup, which their side dropped while leaving lab_subnet.sh as the
only orphaned reference in the file.

01_bootstrap.sh keeps our deterministic --ip pinning, which distinguishes an
already-attached node from a real failure, over their `|| true`.

Their language-sorted layout orphaned references our side still held: zammad's
two scripts/apply/*.rb, jenkins' java-with-ca-trust.sh, and the notify topics
their handlers renamed to hyphenated form. All corrected.

roles/sys-svc-compose/files/shell/swarm/stack_ready.sh gains our latched-update
block: paused, rollback_paused, rollback_started and rollback_completed exit 2
rather than polling a state that cannot resolve without another deploy.

config_from_run is unioned to (title, logged, jobs); the production caller in
trigger/__main__.py now passes jobs, without which our suite suppression would
have been inert.

READMEs are regenerated rather than hand-merged: 15 LiteLLM rows and 91 Tor rows
now derive from the merged metadata, which is itself evidence the meta merge is
coherent.

Verified: 5161 unit tests pass. The single failure, test_query.py TestSortSpec,
also fails on fork/feature/svc-net-tor at b51d2f9919, measured in a baseline
worktree -- so this merge introduces no new unit-test failure. Two modules are
excluded from that run because `baudolo`, a dependency their branch introduces,
is not installed on this host.
Not verified: no deploy has run against the merged tree, and make quality has not
been run end to end here.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-24 16:32:30 +02:00

263 lines
9.8 KiB
Python

import unittest
import unittest.mock as mock
from unittest.mock import patch
from ansible.errors import AnsibleError
from plugins.lookup.addon_env_flags import LookupModule, env_key
def _applications_loader(apps):
def _get(name, *a, **k):
if name == "applications":
return mock.MagicMock(run=lambda *_a, **_k: [apps])
return mock.MagicMock(run=lambda *_a, **_k: [None])
loader_mock = mock.MagicMock()
loader_mock.get.side_effect = _get
return loader_mock
class TestEnvKey(unittest.TestCase):
"""env_key() must match addon-gating.js envKey() exactly."""
def test_simple(self):
self.assertEqual(env_key("collectives"), "COLLECTIVES_ADDON_ENABLED")
def test_underscores_preserved(self):
self.assertEqual(env_key("files_bpm"), "FILES_BPM_ADDON_ENABLED")
self.assertEqual(
env_key("integration_gitlab"), "INTEGRATION_GITLAB_ADDON_ENABLED"
)
def test_non_alnum_runs_collapse(self):
self.assertEqual(
env_key("ldap-authenticator"), "LDAP_AUTHENTICATOR_ADDON_ENABLED"
)
self.assertEqual(env_key("a.b-c"), "A_B_C_ADDON_ENABLED")
class TestAddonEnvFlagsLookup(unittest.TestCase):
def setUp(self):
self.lookup = LookupModule()
self.lookup._loader = mock.MagicMock()
self.addons = {
"collectives": {"enabled": True, "required": True},
"contacts": {"enabled": True, "required": True},
"optional_on": {"enabled": True, "required": False},
"required_off": {"enabled": False, "required": True},
"off_optional": {"enabled": False, "required": False},
"str_flags": {"enabled": "true", "required": "true"},
"no_required": {"enabled": True},
}
self._patchers = [
patch(
"plugins.lookup.addon_env_flags.lookup_loader",
_applications_loader({}),
),
patch("plugins.lookup.addon_env_flags.get", return_value=self.addons),
patch(
"plugins.lookup.addon_env_flags._render_with_templar",
side_effect=lambda v, **k: v,
),
]
for p in self._patchers:
p.start()
self.addCleanup(lambda: [p.stop() for p in self._patchers])
def _flags(self):
out = self.lookup.run(["web-app-x"], variables={})[0]
return dict(line.split("=", 1) for line in out.splitlines())
def test_enabled_and_required_true(self):
f = self._flags()
self.assertEqual(f["COLLECTIVES_ADDON_ENABLED"], "true")
self.assertEqual(f["CONTACTS_ADDON_ENABLED"], "true")
def test_required_false_skipped_even_if_enabled(self):
self.assertEqual(self._flags()["OPTIONAL_ON_ADDON_ENABLED"], "false")
def test_disabled_is_false(self):
f = self._flags()
self.assertEqual(f["REQUIRED_OFF_ADDON_ENABLED"], "false")
self.assertEqual(f["OFF_OPTIONAL_ADDON_ENABLED"], "false")
def test_missing_required_defaults_false(self):
self.assertEqual(self._flags()["NO_REQUIRED_ADDON_ENABLED"], "false")
def test_string_flags_coerced(self):
self.assertEqual(self._flags()["STR_FLAGS_ADDON_ENABLED"], "true")
def test_one_line_per_addon_sorted_by_key(self):
lines = self.lookup.run(["web-app-x"], variables={})[0].splitlines()
self.assertEqual(len(lines), len(self.addons))
keys = [ln.split("=", 1)[0] for ln in lines]
self.assertEqual(keys, sorted(keys))
def test_no_terms_raises(self):
with self.assertRaises(AnsibleError):
self.lookup.run([], variables={})
def test_too_many_terms_raises(self):
with self.assertRaises(AnsibleError):
self.lookup.run(["a", "b"], variables={})
class TestNoAddons(unittest.TestCase):
def test_empty_addons_yields_empty_string(self):
lookup = LookupModule()
lookup._loader = mock.MagicMock()
with (
patch(
"plugins.lookup.addon_env_flags.lookup_loader",
_applications_loader({}),
),
patch("plugins.lookup.addon_env_flags.get", return_value={}),
patch(
"plugins.lookup.addon_env_flags._render_with_templar",
side_effect=lambda v, **k: v,
),
):
self.assertEqual(lookup.run(["web-app-x"], variables={}), [""])
class TestBridgeDeploymentGating(unittest.TestCase):
"""A bridged addon's flag is true only when one of its partner roles is in the
round's deployed closure (the inventory groups). When the set cannot be
resolved the gating is skipped (flag falls back to enabled AND required)."""
def setUp(self):
self.lookup = LookupModule()
self.lookup._loader = mock.MagicMock()
self.addons = {
"gl_bridge": {"enabled": True, "required": True, "bridges": ["gitlab"]},
"masto_bridge": {
"enabled": True,
"required": True,
"bridges": ["mastodon"],
},
"multi_bridge": {
"enabled": True,
"required": True,
"bridges": ["openwebui", "flowise"],
},
"litellm_bridge": {
"enabled": True,
"required": True,
"bridges": ["litellm"],
},
"no_bridge": {"enabled": True, "required": True},
"disabled_bridge": {
"enabled": False,
"required": True,
"bridges": ["gitlab"],
},
}
self._patchers = [
patch(
"plugins.lookup.addon_env_flags.lookup_loader",
_applications_loader({}),
),
patch("plugins.lookup.addon_env_flags.get", return_value=self.addons),
patch(
"plugins.lookup.addon_env_flags._render_with_templar",
side_effect=lambda v, **k: v,
),
]
for p in self._patchers:
p.start()
self.addCleanup(lambda: [p.stop() for p in self._patchers])
def _flags(self, variables):
out = self.lookup.run(["web-app-x"], variables=variables)[0]
return dict(line.split("=", 1) for line in out.splitlines())
def test_partner_deployed_keeps_true(self):
f = self._flags({"group_names": ["web-app-gitlab", "web-app-nextcloud"]})
self.assertEqual(f["GL_BRIDGE_ADDON_ENABLED"], "true")
def test_partner_not_deployed_gated_false(self):
f = self._flags({"group_names": ["web-app-gitlab", "web-app-nextcloud"]})
self.assertEqual(f["MASTO_BRIDGE_ADDON_ENABLED"], "false")
def test_any_partner_of_multi_bridge_counts(self):
f = self._flags({"group_names": ["svc-ai-flowise"]})
self.assertEqual(f["MULTI_BRIDGE_ADDON_ENABLED"], "true")
def test_provider_role_without_playwright_suite_counts(self):
f = self._flags({"group_names": ["web-app-mediawiki", "svc-ai-litellm"]})
self.assertEqual(f["LITELLM_BRIDGE_ADDON_ENABLED"], "true")
def test_provider_role_absent_from_closure_gated_false(self):
f = self._flags({"group_names": ["web-app-mediawiki"]})
self.assertEqual(f["LITELLM_BRIDGE_ADDON_ENABLED"], "false")
def test_no_bridge_addon_unaffected(self):
f = self._flags({"group_names": ["web-app-nextcloud"]})
self.assertEqual(f["NO_BRIDGE_ADDON_ENABLED"], "true")
def test_disabled_addon_stays_false(self):
f = self._flags({"group_names": ["web-app-gitlab"]})
self.assertEqual(f["DISABLED_BRIDGE_ADDON_ENABLED"], "false")
def test_no_deployed_set_falls_back_no_gating(self):
f = self._flags({})
self.assertEqual(f["MASTO_BRIDGE_ADDON_ENABLED"], "true")
self.assertEqual(f["GL_BRIDGE_ADDON_ENABLED"], "true")
def test_deployed_set_as_string_is_split(self):
f = self._flags({"group_names": "web-app-gitlab web-app-nextcloud"})
self.assertEqual(f["GL_BRIDGE_ADDON_ENABLED"], "true")
self.assertEqual(f["MASTO_BRIDGE_ADDON_ENABLED"], "false")
class TestBridgeResolvesProviderKey(unittest.TestCase):
"""A bridge may name the service key a role registers, not only its entity.
'sso' is provided by web-app-keycloak, whose id ends in neither '-sso' nor
'sso', so the entity-suffix rule alone can never match it."""
def setUp(self):
self.lookup = LookupModule()
self.lookup._loader = mock.MagicMock()
self.applications = {
"web-app-keycloak": {
"services": {
"keycloak": {"enabled": True, "shared": True, "provides": "sso"}
}
},
"web-app-wordpress": {"services": {}},
}
self.addons = {
"ca_trust": {"enabled": True, "required": True, "bridges": ["sso"]},
}
self._patchers = [
patch(
"plugins.lookup.addon_env_flags.lookup_loader",
_applications_loader(self.applications),
),
patch("plugins.lookup.addon_env_flags.get", return_value=self.addons),
patch(
"plugins.lookup.addon_env_flags._render_with_templar",
side_effect=lambda v, **k: v,
),
]
for p in self._patchers:
p.start()
self.addCleanup(lambda: [p.stop() for p in self._patchers])
def _flag(self, apps):
out = self.lookup.run(["web-app-wordpress"], variables={"group_names": apps})[0]
return dict(line.split("=", 1) for line in out.splitlines())[
"CA_TRUST_ADDON_ENABLED"
]
def test_provider_role_deployed_keeps_true(self):
self.assertEqual(self._flag(["web-app-keycloak", "web-app-wordpress"]), "true")
def test_provider_role_absent_gates_false(self):
self.assertEqual(self._flag(["web-app-wordpress"]), "false")
if __name__ == "__main__":
unittest.main()