diff --git a/cli/administration/deploy/development/inventory/planner.py b/cli/administration/deploy/development/inventory/planner.py index 8b57c862f9..d175c694bf 100644 --- a/cli/administration/deploy/development/inventory/planner.py +++ b/cli/administration/deploy/development/inventory/planner.py @@ -16,6 +16,7 @@ from __future__ import annotations from typing import TYPE_CHECKING from utils.cache.applications import get_variants +from utils.roles.applications.variants import active_variants_for_round from . import legacy_resolver @@ -123,12 +124,15 @@ def plan_dev_inventory_matrix( primary_round_variants = primary_round_variants_per_round[round_index] round_include = per_variant_includes[round_index] - round_variants = dict(primary_round_variants) - for dep, dep_variants in variants_per_app.items(): - if dep in round_variants: - continue - dep_count = max(1, len(dep_variants or [{}])) - round_variants[dep] = round_index if round_index < dep_count else 0 + round_variants = { + **active_variants_for_round( + roles_dir=roles_dir, + round_index=round_index, + primary_app_variants=primary_round_variants, + variants_per_app=variants_per_app, + ), + **primary_round_variants, + } inv_dir = f"{base}-{round_index}" if total_rounds > 1 else base plan.append((round_index, inv_dir, round_variants, round_include, purge_set)) diff --git a/tests/unit/python/cli/administration/deploy/development/inventory/test_planner.py b/tests/unit/python/cli/administration/deploy/development/inventory/test_planner.py index 66895568d7..47d7e02ffc 100644 --- a/tests/unit/python/cli/administration/deploy/development/inventory/test_planner.py +++ b/tests/unit/python/cli/administration/deploy/development/inventory/test_planner.py @@ -255,5 +255,44 @@ class TestFilterPlanToVariants(unittest.TestCase): filter_plan_to_variants(self.PLAN, [0, 7]) +class TestPlanBakesPinnedVariant(unittest.TestCase): + @patch( + "utils.roles.applications.variants.resolve_pins", + autospec=True, + return_value={"web-app-bar": 1}, + ) + @patch( + "cli.administration.deploy.development.inventory.legacy_resolver._resolve_round_include", + autospec=True, + return_value=("web-app-bar", "web-app-foo"), + ) + @patch( + "cli.administration.deploy.development.inventory.legacy_resolver._build_services_overrides_for_round", + autospec=True, + return_value={}, + ) + @patch( + "cli.administration.deploy.development.inventory.planner.get_variants", + autospec=True, + ) + def test_round_variants_carry_the_pinned_index( + self, + get_variants_mock: MagicMock, + _overrides_mock: MagicMock, + _include_mock: MagicMock, + _pins_mock: MagicMock, + ) -> None: + get_variants_mock.return_value = { + "web-app-foo": [{}], + "web-app-bar": [{}, {}], + } + plan = plan_dev_inventory_matrix( + roles_dir="/roles", + primary_apps=["web-app-foo"], + base_inventory_dir="/srv/inv", + ) + self.assertEqual(plan[0][2], {"web-app-foo": 0, "web-app-bar": 1}) + + if __name__ == "__main__": # pragma: no cover unittest.main() diff --git a/utils/roles/applications/variants.py b/utils/roles/applications/variants.py index 24d0b4279c..ab1a62a0cb 100644 --- a/utils/roles/applications/variants.py +++ b/utils/roles/applications/variants.py @@ -32,6 +32,7 @@ from utils.roles.mapping import ROLE_FILE_META_SERVICES __all__ = [ "NestedOverrideConflictError", + "active_variants_for_round", "collect_provider_overrides", "services_overrides_for_round", ] @@ -123,6 +124,49 @@ def _apply_nested_service_maps(overrides: dict[str, dict], *, roles_dir: str) -> overrides[provider] = apply_topic(overrides.get(provider), topics["services"]) +def active_variants_for_round( + *, + roles_dir: str, + round_index: int, + primary_app_variants: Mapping[str, int], + variants_per_app: Mapping[str, list], +) -> dict[str, int]: + """Return ``{role: variant index}`` one round runs every role with variants in. + + Args: + roles_dir: roles directory the ``# variant-pin:`` markers are read from. + round_index: the round of the sweep. + primary_app_variants: the already clamped index of every primary app; + no pin overrides it. + variants_per_app: parsed variants per role. + + Returns: + The clamped round index per role, replaced by the index a pin in + another role's active variant forces. + """ + + def _round_index_for(role_name: str, variant_list: list) -> int: + variant_count = max(1, len(variant_list)) + if role_name in primary_app_variants: + idx = primary_app_variants[role_name] + else: + idx = round_index if round_index < variant_count else 0 + return idx if 0 <= idx < len(variant_list) else 0 + + active_index = { + role_name: _round_index_for(role_name, variant_list) + for role_name, variant_list in variants_per_app.items() + if variant_list + } + pinned = resolve_pins( + active_index, roles_dir=roles_dir, variants_per_app=variants_per_app + ) + for role_name, index in pinned.items(): + if role_name not in primary_app_variants and role_name in active_index: + active_index[role_name] = index + return active_index + + def services_overrides_for_round( *, roles_dir: str, @@ -150,26 +194,12 @@ def services_overrides_for_round( variants_per_app = get_variants(roles_dir=roles_dir, readonly=True) overrides: dict[str, dict] = {} roles_path = Path(roles_dir) - - def _round_index_for(role_name: str, variant_list: list) -> int: - variant_count = max(1, len(variant_list)) - if role_name in primary_app_variants: - idx = primary_app_variants[role_name] - else: - idx = round_index if round_index < variant_count else 0 - return idx if 0 <= idx < len(variant_list) else 0 - - active_index = { - role_name: _round_index_for(role_name, variant_list) - for role_name, variant_list in variants_per_app.items() - if variant_list - } - pinned = resolve_pins( - active_index, roles_dir=roles_dir, variants_per_app=variants_per_app + active_index = active_variants_for_round( + roles_dir=roles_dir, + round_index=round_index, + primary_app_variants=primary_app_variants, + variants_per_app=variants_per_app, ) - for role_name, index in pinned.items(): - if role_name not in primary_app_variants and role_name in active_index: - active_index[role_name] = index for role_name, variant_list in variants_per_app.items(): if not variant_list: