diff --git a/tests/unit/python/utils/github/variant/test_axes.py b/tests/unit/python/utils/github/variant/test_axes.py index 1344f6f736..38f4ce0ac2 100644 --- a/tests/unit/python/utils/github/variant/test_axes.py +++ b/tests/unit/python/utils/github/variant/test_axes.py @@ -5,6 +5,11 @@ import unittest.mock as mock from utils.github.variant import axes, network, pins, pools from utils.roles.display import display_names +from utils.roles.mapping import ( + ROLE_FILE_META_DOMAINS, + ROLE_FILE_META_NETWORKS, + ROLE_FILE_META_SERVICES, +) from utils.symbol_glossary import to_emoji _REACTIVE = "{{ 'svc-net-tor' in group_names }}" @@ -38,6 +43,19 @@ _VARIANTS = { _ALL = ("clearnet", "tor", "multi") +_PARTNER_META = { + ("web-svc-relay", ROLE_FILE_META_DOMAINS): {"canonical": ["relay.example"]}, + ("web-svc-relay", ROLE_FILE_META_SERVICES): {"tor": {"enabled": False}}, + ("svc-engine", ROLE_FILE_META_SERVICES): {"tor": {"enabled": False}}, + ("web-svc-dark", ROLE_FILE_META_DOMAINS): {"canonical": ["dark.example"]}, + ("web-svc-dark", ROLE_FILE_META_SERVICES): {"tor": {"enabled": _REACTIVE}}, + ("web-svc-dark", ROLE_FILE_META_NETWORKS): {"reachability": {"modes": ["tor"]}}, +} + + +def _partner_meta(app: str, filename: str) -> dict: + return _PARTNER_META.get((app, filename), {}) + def _row(name: str, variant: int, modes: tuple[str, ...], **extra) -> dict: return {"name": name, "variant": variant, "modes": modes, **extra} @@ -264,6 +282,23 @@ class TestNetworkStates(unittest.TestCase): self.assertEqual(network.disabled_services("tor"), "") self.assertEqual(network.disabled_services("multi"), "") + @mock.patch.object(network, "_meta", side_effect=_partner_meta) + def test_a_partner_the_node_serves_no_network_of_is_disabled(self, _meta) -> None: + deployed = ["web-svc-relay", "web-svc-dark", "svc-engine"] + self.assertEqual(network.disabled_services("tor", deployed), "web-svc-relay") + self.assertEqual( + network.disabled_services("clearnet", deployed), "tor web-svc-dark" + ) + self.assertEqual(network.disabled_services("multi", deployed), "") + + (entry,) = _assign( + [_row("web-app-a", 0, ("compose",), pin_network="tor", services=deployed)], + sweep=0, + network_input="auto", + variants_per_app=_VARIANTS, + ) + self.assertEqual(entry["disable"], "web-svc-relay") + class TestCombinations(unittest.TestCase): def test_two_modes_on_the_network_axis_yield_six_runs(self) -> None: diff --git a/utils/github/variant/axes.py b/utils/github/variant/axes.py index 0a97273af9..2e06b28eab 100644 --- a/utils/github/variant/axes.py +++ b/utils/github/variant/axes.py @@ -317,7 +317,7 @@ def assign( "enforce_filesystem": "true" if pin_filesystem is not None or len(filesystems) == 1 else "false", - "disable": disabled_services(state), + "disable": disabled_services(state, row.get("services", ())), "priority": "true" if priority else "false", "instructions": "", "id": str(row.get("id", 0)), diff --git a/utils/github/variant/network.py b/utils/github/variant/network.py index 1acf3d11a2..a130eab88f 100644 --- a/utils/github/variant/network.py +++ b/utils/github/variant/network.py @@ -25,7 +25,11 @@ from utils.networks.reachability import ( from utils.roles.applications.services.registry import ( build_service_registry_from_roles_dir, ) -from utils.roles.mapping import ROLE_FILE_META_NETWORKS, ROLE_FILE_META_SERVICES +from utils.roles.mapping import ( + ROLE_FILE_META_DOMAINS, + ROLE_FILE_META_NETWORKS, + ROLE_FILE_META_SERVICES, +) if TYPE_CHECKING: from collections.abc import Mapping, Sequence @@ -102,6 +106,28 @@ def row_pulls_tor(row: Mapping[str, Any]) -> bool: return tor_provider() in (row.get("services") or ()) +def _partner_networks(app: str) -> tuple[str, ...] | None: + """The networks a deployed partner may be served on. + + Args: + app: the partner's application id. + + Returns: + ``None`` for a partner without domains or without a Tor bond, which the + deploy's domain map never refuses; else its ``reachability.modes``, + without ``tor`` when its Tor gate is a literal false. + """ + if not _meta(app, ROLE_FILE_META_DOMAINS): + return None + flag = _tor_flag({"services": _meta(app, ROLE_FILE_META_SERVICES)}) + if flag is None: + return None + modes, _ = role_reachability( + {"networks": _meta(app, ROLE_FILE_META_NETWORKS)}, app=app + ) + return tuple(name for name in modes or NETWORKS if flag is not False or name != TOR) + + def row_states( app: str, variant: int | None = None, @@ -202,7 +228,21 @@ def combinations( ] -def disabled_services(state: str) -> str: - """The ``disable`` tokens a row deploys with: Tor goes when the node - serves no network that needs it.""" - return "" if TOR in NODE_MODES[state] else TOR_SERVICE +def disabled_services(state: str, services: Sequence[str] = ()) -> str: + """The ``disable`` tokens a row deploys with. + + Args: + state: the node network mode of the row. + services: the roles the row's variant deploys. + + Returns: + Tor when the node serves no network that needs it, and every deployed + partner (:func:`_partner_networks`) the node serves none of the + networks of, since the deploy refuses that partner there. + """ + tokens = [] if TOR in NODE_MODES[state] else [TOR_SERVICE] + for name in services: + networks = _partner_networks(name) + if networks is not None and not set(NODE_MODES[state]) & set(networks): + tokens.append(name) + return " ".join(tokens)