Files
computer-playbook/tests/unit/plugins/lookup/test_prometheus_integration_active.py
T
kevinveenbirkenbachandClaude Opus 4.8 238bc285c7 refactor(lookups): reach merged config via the lookup-loader SPOT
Consumer lookup plugins now reach the merged config through the Ansible
lookup loader instead of importing get_merged_applications /
get_merged_domains from utils.cache. The merge stays one cached
implementation surfaced via the applications/config/domains/users lookups;
consumers call lookup_loader.get(...).run() (the compose_networks idiom).

- get_merged_applications walks the whole meta/ tree recursively
  (meta/**/*.yml to applications.<app>.<path>, addons and any depth) and
  folds the raw meta/volumes.yml subtree onto applications.<app>.volumes
  after the templar render so the Jinja source: strings stay unrendered.
- 31 consumer lookups rerouted; providers (applications/config/
  applications_current_play/users/domains) and the raw-volume accessors
  (volume/nginx) keep the cache import behind # nocheck: lookup-cache-import.
- New lint test_no_cache_imports_in_lookups enforces the contract.
- nfs_flavor is now the flavor SPOT (svc-storage-nfs-server + nfs_client_src
  resolve via lookup('nfs_flavor')); removed the unused STORAGE_NFS_FLAVOR
  group_var. Extracted extract_default_credentials to
  utils/cache/schema_credentials.py to keep applications.py under the cap.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-28 18:35:06 +02:00

141 lines
5.1 KiB
Python

from __future__ import annotations
import unittest
from unittest import mock
from plugins.lookup.prometheus_integration_active import LookupModule
def _make_applications(
*app_ids: str,
prometheus_deps: tuple = (),
) -> dict:
"""Build a minimal applications dict.
Apps listed in *prometheus_deps* get services.prometheus.enabled: true.
"""
apps = {}
for app_id in app_ids:
if app_id in prometheus_deps:
apps[app_id] = {"services": {"prometheus": {"enabled": True}}}
else:
apps[app_id] = {}
return apps
def _run(applications: dict, application_id: str, group_names: list) -> bool:
lm = LookupModule()
lm._loader = mock.MagicMock()
with mock.patch(
"plugins.lookup.prometheus_integration_active.lookup_loader"
) as loader_mock:
loader_mock.get.return_value = mock.MagicMock(
run=lambda *_a, **_k: [applications]
)
return lm.run(
[],
variables={
"applications": applications,
"application_id": application_id,
"group_names": group_names,
},
)[0]
def _run_explicit(applications: dict, application_id: str, group_names: list) -> bool:
"""Invoke with application_id as explicit term 0 — the template usage pattern."""
lm = LookupModule()
lm._loader = mock.MagicMock()
with mock.patch(
"plugins.lookup.prometheus_integration_active.lookup_loader"
) as loader_mock:
loader_mock.get.return_value = mock.MagicMock(
run=lambda *_a, **_k: [applications]
)
return lm.run(
[application_id],
variables={
"applications": applications,
"group_names": group_names,
},
)[0]
class TestPrometheusIntegrationActiveDeploymentCheck(unittest.TestCase):
"""group_names gate — web-app-prometheus must be on this host."""
def test_false_when_prometheus_not_in_group_names(self):
apps = _make_applications("web-app-gitea", prometheus_deps=("web-app-gitea",))
result = _run(apps, "web-app-gitea", [])
self.assertFalse(result)
def test_false_when_prometheus_not_deployed_even_with_dep(self):
apps = _make_applications("web-app-gitea", prometheus_deps=("web-app-gitea",))
result = _run(apps, "web-app-gitea", ["web-app-gitea"])
self.assertFalse(result)
class TestPrometheusIntegrationActivePrometheusVhost(unittest.TestCase):
"""When the current app IS web-app-prometheus the result is always True."""
def test_true_for_prometheus_vhost_itself(self):
apps = _make_applications("web-app-prometheus")
result = _run(apps, "web-app-prometheus", ["web-app-prometheus"])
self.assertTrue(result)
def test_true_for_prometheus_vhost_even_without_service_dep(self):
# web-app-prometheus doesn't need its own service dep — it IS the host.
apps = {"web-app-prometheus": {}}
result = _run(apps, "web-app-prometheus", ["web-app-prometheus"])
self.assertTrue(result)
class TestPrometheusIntegrationActiveServiceDep(unittest.TestCase):
"""services.prometheus.enabled gate for non-prometheus vhosts."""
def test_true_when_app_declares_prometheus_dep(self):
apps = _make_applications("web-app-gitea", prometheus_deps=("web-app-gitea",))
result = _run(apps, "web-app-gitea", ["web-app-prometheus", "web-app-gitea"])
self.assertTrue(result)
def test_false_when_app_has_no_prometheus_dep(self):
apps = _make_applications("web-app-gitea")
result = _run(apps, "web-app-gitea", ["web-app-prometheus", "web-app-gitea"])
self.assertFalse(result)
def test_false_when_prometheus_dep_disabled(self):
apps = {"web-app-gitea": {"services": {"prometheus": {"enabled": False}}}}
result = _run(apps, "web-app-gitea", ["web-app-prometheus", "web-app-gitea"])
self.assertFalse(result)
class TestPrometheusIntegrationActiveExplicitTerm(unittest.TestCase):
"""application_id passed as explicit term 0 — matches template invocation pattern.
Templates call lookup('prometheus_integration_active', application_id).
'applications' is read from available_variables.
"""
def test_true_when_app_declares_prometheus_dep_explicit(self):
apps = _make_applications("web-app-gitea", prometheus_deps=("web-app-gitea",))
result = _run_explicit(
apps, "web-app-gitea", ["web-app-prometheus", "web-app-gitea"]
)
self.assertTrue(result)
def test_false_when_app_has_no_prometheus_dep_explicit(self):
apps = _make_applications("web-app-gitea")
result = _run_explicit(
apps, "web-app-gitea", ["web-app-prometheus", "web-app-gitea"]
)
self.assertFalse(result)
def test_true_for_prometheus_vhost_explicit(self):
apps = _make_applications("web-app-prometheus")
result = _run_explicit(apps, "web-app-prometheus", ["web-app-prometheus"])
self.assertTrue(result)
if __name__ == "__main__":
unittest.main()