diff --git a/cli/meta/roles/applications/complexity/model.py b/cli/meta/roles/applications/complexity/model.py index 5ce71b6783..f7d05c27f7 100644 --- a/cli/meta/roles/applications/complexity/model.py +++ b/cli/meta/roles/applications/complexity/model.py @@ -9,10 +9,10 @@ from typing import TYPE_CHECKING, Any, NamedTuple from utils.cache.applications import get_variants from utils.env.parser import env_setting +from utils.roles.deploy import role_deploy_modes from utils.roles.lifecycle import tested_lifecycles from utils.roles.meta_lookup import ( MetaServicesShapeError, - get_role_mode_enabled, get_role_test_skips, ) from utils.roles.validation.invokable import list_invokables_by_type @@ -291,13 +291,10 @@ def compute_complexity_rows( rows = [] for name in names: stack = role_has_stack(roles_dir / name) - compose = name in compose_apps - swarm = name in swarm_apps and stack - host = ( - name in host_apps - and not stack - and get_role_mode_enabled(roles_dir / name, mode="host", role_name=name) - ) + offered = role_deploy_modes(roles_dir / name, role_name=name) + compose = name in compose_apps and offered.get("compose", False) + swarm = name in swarm_apps and offered.get("swarm", False) + host = name in host_apps and offered.get("host", False) skips = _role_test_skips(roles_dir, name) rows.append( _build_row(name, forward, reverse, max_level)._replace( @@ -354,14 +351,11 @@ def compute_variant_complexity_rows( continue name = role_dir.name lifecycle = _role_lifecycle(variants.get(name)) - compose = name in compose_apps - swarm_role = name in swarm_apps stack = role_has_stack(role_dir) - host = ( - name in host_apps - and not stack - and get_role_mode_enabled(role_dir, mode="host", role_name=name) - ) + offered = role_deploy_modes(role_dir, role_name=name) + compose = name in compose_apps and offered.get("compose", False) + swarm_role = name in swarm_apps and offered.get("swarm", False) + host = name in host_apps and offered.get("host", False) skips = _role_test_skips(roles_dir, name) for index, variant_config in enumerate(variants.get(name) or []): providers = direct_dep_roles( @@ -375,11 +369,11 @@ def compute_variant_complexity_rows( )._replace( lifecycle=lifecycle, compose=compose, - swarm=swarm_role and stack, + swarm=swarm_role, stack=stack, host=host, test_compose=compose and "compose" not in skips, - test_swarm=swarm_role and stack and "swarm" not in skips, + test_swarm=swarm_role and "swarm" not in skips, test_host=host and "host" not in skips, ) ) diff --git a/roles/web-svc-legal/README.md b/roles/web-svc-legal/README.md index 92bc5fbab6..bb07ea912e 100644 --- a/roles/web-svc-legal/README.md +++ b/roles/web-svc-legal/README.md @@ -19,7 +19,7 @@ flowchart LR dep_web_app_prometheus["web-app-prometheus 🐳🐝"] dep_web_svc_html["web-svc-html 💻"] end - subgraph role [web-svc-legal] + subgraph role [web-svc-legal 🐳🐝] svc_legal["legal ❌"] svc_html["html"] svc_prometheus["prometheus"] diff --git a/roles/web-svc-legal/meta/services.yml b/roles/web-svc-legal/meta/services.yml index ad0c8e2d41..1ae841bec2 100644 --- a/roles/web-svc-legal/meta/services.yml +++ b/roles/web-svc-legal/meta/services.yml @@ -2,6 +2,10 @@ # nocheck: dynamic-flag legal: modes: + compose: + enabled: true + swarm: + enabled: true host: enabled: false enabled: false diff --git a/tests/unit/python/utils/roles/test_deploy.py b/tests/unit/python/utils/roles/test_deploy.py new file mode 100644 index 0000000000..ef47824976 --- /dev/null +++ b/tests/unit/python/utils/roles/test_deploy.py @@ -0,0 +1,109 @@ +"""Unit tests for utils.roles.deploy.role_deploy_modes. + +Covers: + * a declared mode set wins over the role's container shape; + * an injector without a stack can still be offered compose and swarm; + * a role that declares nothing falls back on its shape; + * the ``enabled`` value of each offered mode is carried through. +""" + +from __future__ import annotations + +import textwrap +import unittest +from pathlib import Path +from tempfile import TemporaryDirectory + +from utils.roles.deploy import role_deploy_modes +from utils.roles.mapping import ROLE_FILE_META_SERVICES + + +class _RoleFixture: + def __init__(self, root: Path): + self.root = root + + def write(self, role_name: str, body: str, *, stack: bool) -> Path: + role_dir = self.root / role_name + (role_dir / "meta").mkdir(parents=True, exist_ok=True) + (role_dir / ROLE_FILE_META_SERVICES).write_text( + textwrap.dedent(body).lstrip("\n"), encoding="utf-8" + ) + if stack: + (role_dir / "templates").mkdir(parents=True, exist_ok=True) + (role_dir / "templates" / "docker-compose.yml.j2").write_text( + "services: {}\n", encoding="utf-8" + ) + return role_dir + + +class TestRoleDeployModes(unittest.TestCase): + def setUp(self) -> None: + self._tmp = TemporaryDirectory() + self.addCleanup(self._tmp.cleanup) + self.fx = _RoleFixture(Path(self._tmp.name)) + + def test_declaration_outranks_a_missing_stack(self) -> None: + role = self.fx.write( + "web-svc-probe", + """ + probe: + modes: + compose: + enabled: true + swarm: + enabled: true + host: + enabled: false + """, + stack=False, + ) + self.assertEqual( + role_deploy_modes(role, "web-svc-probe"), + {"compose": True, "swarm": True, "host": False}, + ) + + def test_declaration_outranks_a_present_stack(self) -> None: + role = self.fx.write( + "web-svc-probe", + """ + probe: + modes: + host: + enabled: true + """, + stack=True, + ) + self.assertEqual(role_deploy_modes(role, "web-svc-probe"), {"host": True}) + + def test_a_stack_role_without_a_declaration_falls_back_on_its_shape(self) -> None: + role = self.fx.write("web-svc-probe", "probe:\n enabled: true\n", stack=True) + self.assertEqual( + role_deploy_modes(role, "web-svc-probe"), + {"compose": True, "swarm": True}, + ) + + def test_a_stackless_role_without_a_declaration_is_host_only(self) -> None: + role = self.fx.write("web-svc-probe", "probe:\n enabled: true\n", stack=False) + self.assertEqual(role_deploy_modes(role, "web-svc-probe"), {"host": True}) + + def test_a_disabled_declared_mode_is_offered_as_false(self) -> None: + role = self.fx.write( + "web-svc-probe", + """ + probe: + modes: + compose: + enabled: true + swarm: + enabled: false + """, + stack=True, + ) + self.assertEqual( + role_deploy_modes(role, "web-svc-probe"), + {"compose": True, "swarm": False}, + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/utils/roles/deploy.py b/utils/roles/deploy.py index b9664137ea..8ff9f28b21 100644 --- a/utils/roles/deploy.py +++ b/utils/roles/deploy.py @@ -9,7 +9,7 @@ from __future__ import annotations from pathlib import Path -from utils.roles.meta_lookup import get_role_mode_enabled +from utils.roles.meta_lookup import get_role_declared_modes, get_role_mode_enabled def role_has_stack(role_dir: Path) -> bool: @@ -26,12 +26,18 @@ def role_deploy_modes(role_dir: Path, role_name: str | None = None) -> dict[str, """Return the deploy modes a role supports, keyed by mode with its ``modes..enabled`` SPOT value. - A stack role (ships a compose template) is offered ``compose`` and - ``swarm``; a host-only role is offered ``host``. The two are mutually - exclusive: swarm needs a stack, host configures the machine instead. + What the role declares wins. A role that names its modes gets exactly + those, which is how an injector without a stack of its own can still be + deployed into compose and swarm alongside the partner that carries one. + + Only a role that declares nothing falls back on its shape: a stack role + (ships a compose template) is offered ``compose`` and ``swarm``, anything + else is offered ``host``. """ role_dir = Path(role_dir) - modes = ("compose", "swarm") if role_has_stack(role_dir) else ("host",) + modes = get_role_declared_modes(role_dir, role_name=role_name) or ( + ("compose", "swarm") if role_has_stack(role_dir) else ("host",) + ) return { mode: get_role_mode_enabled(role_dir, mode=mode, role_name=role_name) for mode in modes diff --git a/utils/roles/meta_lookup.py b/utils/roles/meta_lookup.py index 536d30393c..039a0ebf99 100644 --- a/utils/roles/meta_lookup.py +++ b/utils/roles/meta_lookup.py @@ -149,6 +149,32 @@ DEPLOY_MODES: tuple[str, ...] = ("compose", "swarm") """The stack deploy modes the CI test-deploy matrix (get_role_skip) covers.""" +def get_role_declared_modes( + role: PathLike, *, role_name: str | None = None +) -> tuple[str, ...]: + """Return the deploy modes the role names under ``modes``, in ``MODES`` + order, regardless of their ``enabled`` value. + + Empty when the role declares no ``modes`` block, which leaves the caller + on its own default. A role that names a mode its stack shape would not + imply (an injector asking for compose) is stating an intent, so the + declaration outranks the shape. + """ + role_dir, name = _resolve_role(role, role_name) + primary = _primary_entry(name, _read_meta_services(role_dir)) + if primary is None: + return () + modes = primary.get("modes") + if modes is None: + return () + if not isinstance(modes, dict): + raise MetaServicesShapeError( + f"Invalid modes type in meta/services.yml for role '{name}': " + f"expected mapping, got {type(modes).__name__}." + ) + return tuple(mode for mode in MODES if mode in modes) + + def get_role_mode_enabled( role: PathLike, *, mode: str, role_name: str | None = None ) -> bool: