diff --git a/tests/unit/python/utils/github/variant/test_instructions.py b/tests/unit/python/utils/github/variant/test_instructions.py new file mode 100644 index 0000000000..0b9b1bc462 --- /dev/null +++ b/tests/unit/python/utils/github/variant/test_instructions.py @@ -0,0 +1,102 @@ +"""The README replay rides on a row that runs in the guide's own mode. + +The replay is a second deploy on the same runner. On a swarm row that runner +would have to bring a compose stack up beside a swarm one, so the marker has to +land on a compose row for a role that ships a stack, and on a host row for one +installed onto the machine. + +The trap this guards is quiet: marking the wrong row still produces a 📖 in a +job title, so a run looks like it replayed the instructions either way. +""" + +from __future__ import annotations + +import unittest +from unittest.mock import patch + +from utils.github.variant import instructions + + +def _entry(app: str, variant: str, mode: str, **overrides: str) -> dict[str, str]: + entry = { + "apps": app, + "variant": variant, + "mode": mode, + "priority": "false", + "clone": "false", + "covered": "0", + "instructions": "", + "label": f"{app}#{variant}", + } + entry.update(overrides) + return entry + + +_VARIANTS = { + "web-app-x": [ + {"services": {"a": {"enabled": True}, "b": {"enabled": True}}}, + {"services": {"a": {"enabled": True}}}, + ] +} + + +def _mark(entries, mode="compose", variants=None): + """Mark with the guide's mode forced, leaving the variant search real. + + Both bindings are patched: ``mark`` resolved the name at import, while + ``guide_variant`` calls its own module's copy. + """ + with ( + patch.object(instructions, "guide_deployable", return_value=mode), + patch("utils.roles.guide.guide_deployable", return_value=mode), + ): + return instructions.mark( + entries, variants if variants is not None else _VARIANTS + ) + + +class TestInstructionsMode(unittest.TestCase): + def test_the_compose_row_of_the_smallest_variant_carries_it(self) -> None: + marked = _mark([_entry("web-app-x", "1", "compose")]) + self.assertEqual("compose", marked[0]["instructions"]) + + def test_a_swarm_row_never_carries_a_compose_replay(self) -> None: + marked = _mark([_entry("web-app-x", "1", "swarm")]) + self.assertEqual("", marked[0]["instructions"]) + + def test_the_swarm_row_is_skipped_and_the_compose_row_takes_it(self) -> None: + marked = _mark( + [_entry("web-app-x", "1", "swarm"), _entry("web-app-x", "1", "compose")] + ) + self.assertEqual(["", "compose"], [e["instructions"] for e in marked]) + + def test_a_variant_deployed_only_in_swarm_does_not_win_the_search(self) -> None: + marked = _mark( + [_entry("web-app-x", "1", "swarm"), _entry("web-app-x", "0", "compose")] + ) + self.assertEqual(["", "compose"], [e["instructions"] for e in marked]) + + def test_a_host_role_carries_it_on_its_host_row(self) -> None: + marked = _mark([_entry("web-app-x", "1", "host")], mode="host") + self.assertEqual("host", marked[0]["instructions"]) + + def test_a_redundant_row_carries_nothing(self) -> None: + marked = _mark([_entry("web-app-x", "1", "compose", covered="7")]) + self.assertEqual("", marked[0]["instructions"]) + + def test_only_one_row_per_role_is_marked(self) -> None: + marked = _mark( + [_entry("web-app-x", "1", "compose"), _entry("web-app-x", "1", "compose")] + ) + self.assertEqual(["compose", ""], [e["instructions"] for e in marked]) + + def test_the_label_gains_the_glyph_only_where_it_is_marked(self) -> None: + marked = _mark( + [_entry("web-app-x", "1", "swarm"), _entry("web-app-x", "1", "compose")] + ) + self.assertEqual("web-app-x#1", marked[0]["label"]) + self.assertNotEqual("web-app-x#1", marked[1]["label"]) + + +if __name__ == "__main__": + unittest.main() diff --git a/utils/github/variant/instructions.py b/utils/github/variant/instructions.py index 5b274a404b..89adaaa508 100644 --- a/utils/github/variant/instructions.py +++ b/utils/github/variant/instructions.py @@ -11,7 +11,7 @@ from __future__ import annotations from typing import TYPE_CHECKING -from utils.roles.guide import guide_variant +from utils.roles.guide import guide_deployable, guide_variant from utils.symbol_glossary import to_emoji if TYPE_CHECKING: @@ -37,19 +37,26 @@ def mark( them proves nothing the first replay did not. The glyph goes between the variant number and the ⭐ of a priority row, with no space between the two: they are one marker block, not two axes. + + The row has to run in the guide's own mode. The replay is a second deploy + on the same runner, so hanging it on a swarm row would ask that runner to + bring a compose stack up beside a swarm one. Only rows in that mode count + as deployed here either, so the variant search cannot settle on one that + this sweep runs in swarm alone. """ + star = f" {to_emoji('priority')}" deployed: dict[str, set[str]] = {} for entry in entries: - if not redundant(entry): - deployed.setdefault(entry["apps"], set()).add(entry["variant"]) - star = f" {to_emoji('priority')}" + app = entry["apps"] + if not redundant(entry) and entry["mode"] == guide_deployable(app): + deployed.setdefault(app, set()).add(entry["variant"]) marked: set[str] = set() for entry in entries: app = entry["apps"] if app in marked or redundant(entry): continue variant, mode = guide_variant(app, variants_per_app, deployed.get(app, set())) - if not mode or entry["variant"] != variant: + if not mode or entry["variant"] != variant or entry["mode"] != mode: continue entry["instructions"] = mode entry["label"] = (