diff --git a/tests/unit/utils/roles/applications/services/test_config.py b/tests/unit/utils/roles/applications/services/test_config.py new file mode 100644 index 0000000000..05fb321c6f --- /dev/null +++ b/tests/unit/utils/roles/applications/services/test_config.py @@ -0,0 +1,69 @@ +"""Unit tests for resolve_service_config (consumer-override -> provider-native).""" + +from __future__ import annotations + +import unittest + +from utils.roles.applications.services.config import resolve_service_config + +# Provider entity name must equal the service key for the registry to register +# it as the provider (see discover_role_services): svc-net-tor -> entity "tor". +APPS = { + "svc-net-tor": { + "services": { + "tor": { + "enabled": True, + "shared": True, + "exclusive": True, + "primary": True, + } + } + }, + "web-app-consumer": {"services": {"tor": {"enabled": True, "shared": True}}}, + "web-app-override": { + "services": {"tor": {"enabled": True, "shared": True, "exclusive": False}} + }, +} + + +class TestResolveServiceConfig(unittest.TestCase): + def test_falls_back_to_provider_native(self): + self.assertIs( + resolve_service_config(APPS, "web-app-consumer", "tor", "exclusive"), + True, + ) + self.assertIs( + resolve_service_config(APPS, "web-app-consumer", "tor", "primary"), + True, + ) + + def test_consumer_override_wins(self): + self.assertIs( + resolve_service_config(APPS, "web-app-override", "tor", "exclusive"), + False, + ) + + def test_default_when_neither_declares(self): + self.assertEqual( + resolve_service_config( + APPS, "web-app-consumer", "tor", "nonexistent", default="fb" + ), + "fb", + ) + + def test_provider_reads_its_own_native_value(self): + # The provider role itself resolves to its own declaration, not a + # fallback (provider == application_id short-circuits). + self.assertIs( + resolve_service_config(APPS, "svc-net-tor", "tor", "exclusive"), + True, + ) + + def test_unknown_service_key_returns_default(self): + self.assertIsNone( + resolve_service_config(APPS, "web-app-consumer", "no-such-svc", "x") + ) + + +if __name__ == "__main__": # pragma: no cover + unittest.main() diff --git a/utils/roles/applications/services/config.py b/utils/roles/applications/services/config.py new file mode 100644 index 0000000000..cd34fcf540 --- /dev/null +++ b/utils/roles/applications/services/config.py @@ -0,0 +1,73 @@ +"""Resolve a single ``services..`` with provider fallback. + +A consuming role MAY override a shared service's config field in its own +``meta/services.yml`` ``services.`` block. When it does not, the value +falls back to the *provider* role's native declaration — the role whose entity +name is ```` (e.g. ``svc-net-tor`` for ``tor``). This keeps provider +defaults (e.g. ``tor.exclusive`` / ``tor.primary``) in one place while every +consumer stays free to override the field per app. +""" + +from __future__ import annotations + +from typing import Any + +from utils.roles.applications.config import get +from utils.roles.applications.services.registry import ( + build_service_registry_from_applications, +) + +_MISSING = object() + + +def _service_entry( + applications: dict[str, Any], + application_id: str, + service_key: str, +) -> dict[str, Any]: + entry = get( + applications=applications, + application_id=application_id, + config_path=f"services.{service_key}", + strict=False, + default={}, + skip_missing_app=True, + ) + return entry if isinstance(entry, dict) else {} + + +def resolve_service_config( + applications: dict[str, Any], + application_id: str, + service_key: str, + config_key: str, + default: Any = None, + *, + service_registry: dict[str, Any] | None = None, +) -> Any: + """Return ``services..`` for ``application_id``. + + Resolution order: + 1. the consuming role's own override, when it declares ``config_key``; + 2. the provider role's native value (role whose entity is ``service_key``); + 3. ``default``. + + Pass ``service_registry`` to reuse a registry already built by the caller + (avoids rebuilding it once per lookup inside a loop). + """ + consumer = _service_entry(applications, application_id, service_key) + if config_key in consumer: + return consumer[config_key] + + registry = ( + service_registry + if service_registry is not None + else build_service_registry_from_applications(applications) + ) + entry = registry.get(service_key) or {} + provider = entry.get("role") + if isinstance(provider, str) and provider and provider != application_id: + provider_cfg = _service_entry(applications, provider, service_key) + if config_key in provider_cfg: + return provider_cfg[config_key] + return default