diff --git a/cli/administration/deploy/development/deploy/run.py b/cli/administration/deploy/development/deploy/run.py index 57d2a82a32..7e1f27c78f 100644 --- a/cli/administration/deploy/development/deploy/run.py +++ b/cli/administration/deploy/development/deploy/run.py @@ -17,6 +17,7 @@ if TYPE_CHECKING: from collections.abc import Mapping _FORWARDED_ENV_KEYS = ( + "APP_ID", "GH_TOKEN", "GITHUB_TOKEN", "GITHUB_REPOSITORY_OWNER", diff --git a/docs/contributing/tools/github/actions/instructions.md b/docs/contributing/tools/github/actions/instructions.md index 8dc2235e4f..c4fc0c4bba 100644 --- a/docs/contributing/tools/github/actions/instructions.md +++ b/docs/contributing/tools/github/actions/instructions.md @@ -18,6 +18,8 @@ The marked row carries `instructions: ` in the matrix JSON and a 📖 in i The marker adds `web-app-docs` to that row's `apps`, so the documentation is deployed beside the role under test. Its own CLI test then reads the role's page, takes the Production block out of the rendered HTML, and runs it. +One role per deploy, never a list. [guide_role.py](../../../../../plugins/lookup/guide_role.py) names it from `APP_ID`, the row's own app, which [run.py](../../../../../cli/administration/deploy/development/deploy/run.py) forwards into the infinito container where Ansible runs. A deploy that carries no `APP_ID`, or one whose app publishes no Production block, draws an invokable peer of the round at random instead. + Reading the block off the page rather than out of the tree is the point: a reader copies from the site, so that is what has to work. Nothing in the repository is consulted at replay time. ## The machine 🐳 @@ -41,5 +43,5 @@ The CLI test prints the block it fetched and the output of the replay. The role' To reproduce one locally, deploy the role together with the documentation and let the CLI test run: ```bash -make compose-deploy mode=reinstall apps=,web-app-docs full_cycle=false +APP_ID= make compose-deploy mode=reinstall apps=,web-app-docs full_cycle=false ``` diff --git a/plugins/lookup/guide_role.py b/plugins/lookup/guide_role.py index a27531c390..c3ef01ef10 100644 --- a/plugins/lookup/guide_role.py +++ b/plugins/lookup/guide_role.py @@ -1,13 +1,23 @@ """Which deployed role's published guide a replay should read. -``lookup('guide_role', application_id)`` picks the first invokable app the round -deployed beside ``application_id``. Invokability comes from +``lookup('guide_role', application_id)`` names exactly one role, never a list: +the replay brings up a machine per role, so a round that deployed twenty of +them still replays one guide. + +The role comes from ``APP_ID``, which the deploy workflow sets to the row's +app and :mod:`cli.administration.deploy.development.deploy.run` forwards into +the container. A round that carries no such name, or one naming a role that +publishes no guide, draws an invokable peer at random instead, so a local +round covers a different guide each time rather than always the first one the +inventory happens to list. Invokability comes from :func:`utils.roles.validation.invokable.list_invokable_app_ids`, so no caller re-derives it from role-name prefixes. """ from __future__ import annotations +import os +import secrets from typing import Any from ansible.plugins.lookup import LookupBase @@ -15,18 +25,30 @@ from ansible.plugins.lookup import LookupBase from plugins.lookup.deployment import running_apps from utils.roles.validation.invokable import list_invokable_app_ids +ENV_VAR = "APP_ID" -def guide_role(application_id: str, running: list[str], invokable: list[str]) -> str: + +def guide_role( + application_id: str, + running: list[str], + invokable: list[str], + pinned: str = "", +) -> str: """Return the app whose guide is replayed, ``application_id`` when it is alone. Args: application_id: the role asking, which is skipped as its own peer. running: the apps this round deploys. invokable: every app that publishes a guide. + pinned: the app the round was started for, used when it publishes a + guide and ignored when it does not, because ``APP_ID`` is set on + every deploy row and not every row deploys a documented role. """ known = set(invokable) + if pinned in known: + return pinned peers = [app for app in running if app != application_id and app in known] - return peers[0] if peers else application_id + return secrets.choice(peers) if peers else application_id class LookupModule(LookupBase): @@ -41,5 +63,10 @@ class LookupModule(LookupBase): str(terms[0]) if terms else str(vars_.get("application_id", "")) ) return [ - guide_role(application_id, running_apps(vars_), list_invokable_app_ids()) + guide_role( + application_id, + running_apps(vars_), + list_invokable_app_ids(), + os.environ.get(ENV_VAR, "").strip(), + ) ] diff --git a/scripts/tests/deploy/local/deploy/apps/update/selection.sh b/scripts/tests/deploy/local/deploy/apps/update/selection.sh index d35ab13c08..2de6d081be 100755 --- a/scripts/tests/deploy/local/deploy/apps/update/selection.sh +++ b/scripts/tests/deploy/local/deploy/apps/update/selection.sh @@ -7,6 +7,8 @@ set -euo pipefail # INFINITO_CONTAINER e.g. infinito_nexus_arch # INFINITO_DEBUG true|false # INFINITO_INVENTORY_DIR e.g. /etc/inventories/local-full-server +# Optional: +# APP_ID role whose published guide the docs CLI test replays : "${apps:?apps is not set (e.g. apps=web-app-nextcloud)}" : "${INFINITO_CONTAINER:?INFINITO_CONTAINER is not set (e.g. infinito_nexus_arch)}" @@ -40,6 +42,7 @@ echo "inventory_dir=${INFINITO_INVENTORY_DIR}" deploy_with_cache_retry "update-${apps//[^A-Za-z0-9._-]/-}" -- \ docker exec \ -e disable="${disable:-}" \ + -e APP_ID="${APP_ID:-}" \ -e INFINITO_INVENTORY_FILE="${INFINITO_INVENTORY_FILE}" \ -e apps="${apps}" \ -e INFINITO_DEBUG="${INFINITO_DEBUG}" \ diff --git a/tests/unit/python/cli/administration/deploy/development/test_forwarded_env.py b/tests/unit/python/cli/administration/deploy/development/test_forwarded_env.py new file mode 100644 index 0000000000..fff5086c64 --- /dev/null +++ b/tests/unit/python/cli/administration/deploy/development/test_forwarded_env.py @@ -0,0 +1,56 @@ +"""What a deploy pass carries from the host into the infinito container. + +Ansible runs inside that container, so a variable the host exports reaches a +lookup only when ``_run_deploy`` forwards it explicitly. ``APP_ID`` is the +deploy workflow's name for the row's app, and +:mod:`plugins.lookup.guide_role` reads it to pin the documentation replay. +""" + +from __future__ import annotations + +import os +import unittest +from types import SimpleNamespace +from unittest.mock import patch + +from cli.administration.deploy.development.deploy.run import _run_deploy + + +class _Compose: + def __init__(self) -> None: + self.extra_env: dict[str, str] = {} + + def exec(self, cmd, *, check, live, extra_env): + self.extra_env = extra_env + return SimpleNamespace(returncode=0) + + +def _deploy() -> dict[str, str]: + compose = _Compose() + _run_deploy( + compose, + deploy_ids=["web-app-docs"], + debug=False, + passthrough=[], + inventory_dir="/srv/inv", + container_name="infinito", + ) + return compose.extra_env + + +class TestForwardedEnv(unittest.TestCase): + @patch.dict(os.environ, {"APP_ID": "web-app-nextcloud"}, clear=True) + def test_app_id_reaches_the_container(self): + self.assertEqual(_deploy().get("APP_ID"), "web-app-nextcloud") + + @patch.dict(os.environ, {}, clear=True) + def test_an_unset_app_id_is_not_invented(self): + self.assertNotIn("APP_ID", _deploy()) + + @patch.dict(os.environ, {"APP_ID": ""}, clear=True) + def test_an_empty_app_id_is_not_forwarded(self): + self.assertNotIn("APP_ID", _deploy()) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/unit/python/plugins/lookup/test_guide_role.py b/tests/unit/python/plugins/lookup/test_guide_role.py index 149e136ea2..313571345b 100644 --- a/tests/unit/python/plugins/lookup/test_guide_role.py +++ b/tests/unit/python/plugins/lookup/test_guide_role.py @@ -1,13 +1,16 @@ +import os import unittest from unittest.mock import patch -from plugins.lookup.guide_role import LookupModule, guide_role +from plugins.lookup.guide_role import ENV_VAR, LookupModule, guide_role INVOKABLE = ["web-app-docs", "web-app-nextcloud", "svc-db-postgres", "dsk-gnt-claude"] +NO_PIN = {ENV_VAR: ""} + class TestGuideRole(unittest.TestCase): - def test_the_first_invokable_peer_wins(self): + def test_the_only_invokable_peer_wins(self): self.assertEqual( guide_role( "web-app-docs", ["web-app-docs", "web-app-nextcloud"], INVOKABLE @@ -29,26 +32,52 @@ class TestGuideRole(unittest.TestCase): def test_an_empty_round_names_the_asking_role(self): self.assertEqual(guide_role("web-app-docs", [], INVOKABLE), "web-app-docs") - def test_the_round_order_decides_between_two_peers(self): - self.assertEqual( - guide_role( - "web-app-docs", ["svc-db-postgres", "dsk-gnt-claude"], INVOKABLE - ), - "svc-db-postgres", - ) + def test_one_of_the_peers_is_drawn(self): + peers = ["svc-db-postgres", "dsk-gnt-claude"] + self.assertIn(guide_role("web-app-docs", peers, INVOKABLE), peers) - def test_a_peer_outside_the_invokable_set_is_skipped_for_a_later_one(self): - self.assertEqual( + def test_every_peer_can_be_drawn(self): + peers = ["svc-db-postgres", "dsk-gnt-claude"] + drawn = {guide_role("web-app-docs", peers, INVOKABLE) for _ in range(200)} + self.assertEqual(drawn, set(peers)) + + def test_a_peer_outside_the_invokable_set_is_never_drawn(self): + drawn = { guide_role( "web-app-docs", ["sys-svc-proxy", "user-administrator", "dsk-gnt-claude"], INVOKABLE, + ) + for _ in range(200) + } + self.assertEqual(drawn, {"dsk-gnt-claude"}) + + def test_the_pinned_role_outranks_the_draw(self): + drawn = { + guide_role( + "web-app-docs", + ["svc-db-postgres", "dsk-gnt-claude"], + INVOKABLE, + pinned="web-app-nextcloud", + ) + for _ in range(200) + } + self.assertEqual(drawn, {"web-app-nextcloud"}) + + def test_a_pin_without_a_guide_leaves_the_draw_alone(self): + self.assertEqual( + guide_role( + "web-app-docs", + ["web-app-nextcloud"], + INVOKABLE, + pinned="sys-svc-proxy", ), - "dsk-gnt-claude", + "web-app-nextcloud", ) class TestGuideRoleLookup(unittest.TestCase): + @patch.dict(os.environ, NO_PIN) @patch("plugins.lookup.guide_role.list_invokable_app_ids") def test_the_whitelist_outranks_the_round_groups(self, invokable): invokable.return_value = INVOKABLE @@ -61,6 +90,7 @@ class TestGuideRoleLookup(unittest.TestCase): ) self.assertEqual(result, ["svc-db-postgres"]) + @patch.dict(os.environ, NO_PIN) @patch("plugins.lookup.guide_role.list_invokable_app_ids") def test_without_a_whitelist_the_round_groups_decide(self, invokable): invokable.return_value = INVOKABLE @@ -73,6 +103,7 @@ class TestGuideRoleLookup(unittest.TestCase): ) self.assertEqual(result, ["web-app-nextcloud"]) + @patch.dict(os.environ, NO_PIN) @patch("plugins.lookup.guide_role.list_invokable_app_ids") def test_the_asking_role_falls_back_to_the_variable(self, invokable): invokable.return_value = INVOKABLE @@ -85,6 +116,16 @@ class TestGuideRoleLookup(unittest.TestCase): ) self.assertEqual(result, ["web-app-docs"]) + @patch.dict(os.environ, {ENV_VAR: " dsk-gnt-claude "}) + @patch("plugins.lookup.guide_role.list_invokable_app_ids") + def test_the_environment_pins_the_role(self, invokable): + invokable.return_value = INVOKABLE + result = LookupModule().run( + ["web-app-docs"], + variables={"group_names": ["web-app-docs", "web-app-nextcloud"]}, + ) + self.assertEqual(result, ["dsk-gnt-claude"]) + if __name__ == "__main__": unittest.main()