perf(test-e2e-playwright): order Playwright passes by meta/tests.yml run_after

A role's playwright.run_after names the roles its pass runs after; the new
playwright_order lookup reads it through role_tests (which now renders
Jinja with the queried role as application_id) and orders the round with
a stable topological sort, so only constrained roles move. web-app-docs
runs after every other invokable role: in CI run 41206003651 (Discourse#2)
its pass waited 3126 s for the docs server's Sphinx build while stalwart,
bigbluebutton, jitsi and prometheus (~400 s) queued behind it. Cycles,
self references, unknown roles and non-list values fail loudly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-10-31 21:41:55 +01:00
1 parent 104c140a0b
commit 89063972a6
11 files changed
+361 -8

No files matched your search

@@ -12,11 +12,15 @@ import unittest
from pathlib import Path
from ansible.errors import AnsibleError
from ansible.parsing.dataloader import DataLoader
from ansible.template import Templar
from plugins.lookup.role_tests import LookupModule
from utils.cache.yaml import dump_yaml
from utils.roles.mapping import ROLE_FILE_META_TESTS
RUN_AFTER_EXPRESSION = "{{ invokables | difference([application_id]) }}"
class TestRoleTestsLookup(unittest.TestCase):
@classmethod
@@ -26,7 +30,33 @@ class TestRoleTestsLookup(unittest.TestCase):
cls.root = Path(cls._tmp.name)
tests_yml = cls.root / "roles" / "svc-runner" / ROLE_FILE_META_TESTS
tests_yml.parent.mkdir(parents=True)
dump_yaml(tests_yml, {"cli": {"timeout": 9000}})
dump_yaml(
tests_yml,
{
"cli": {"timeout": 9000},
"playwright": {"run_after": RUN_AFTER_EXPRESSION},
},
)
def test_a_jinja_value_renders_with_the_queried_role_as_application_id(self):
variables = {
"playbook_dir": str(self.root),
"application_id": "web-app-caller",
"invokables": ["svc-runner", "web-app-caller", "web-app-x"],
}
lookup = LookupModule(
loader=DataLoader(),
templar=Templar(loader=DataLoader(), variables=variables),
)
[rendered] = lookup.run(
["svc-runner", "playwright.run_after"], variables=variables
)
self.assertEqual(sorted(rendered), ["web-app-caller", "web-app-x"])
def test_a_jinja_value_stays_raw_without_a_templar(self):
self.assertEqual(
self._run(["svc-runner", "playwright.run_after"]), [RUN_AFTER_EXPRESSION]
)
def _run(self, terms, **kwargs):
return LookupModule().run(
@@ -0,0 +1,5 @@
from pathlib import Path
PROJECT_ROOT: Path = Path(__file__).resolve().parents[6]
__all__ = ["PROJECT_ROOT"]
@@ -0,0 +1,119 @@
import importlib.util
import sys
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from ansible.errors import AnsibleError
from ansible.parsing.dataloader import DataLoader
from ansible.template import Templar
from plugins.lookup.role_tests import LookupModule as RoleTestsLookup
from utils.cache.yaml import dump_yaml
from utils.roles.mapping import ROLE_FILE_META_TESTS
from . import PROJECT_ROOT
ROUND = [
"web-app-keycloak",
"web-app-matomo",
"web-app-docs",
"web-app-stalwart",
"web-app-prometheus",
]
def _load_module():
path = (
PROJECT_ROOT
/ "roles"
/ "test-e2e-playwright"
/ "lookup_plugins"
/ "playwright_order.py"
)
spec = importlib.util.spec_from_file_location("playwright_order_lookup", path)
module = importlib.util.module_from_spec(spec)
assert spec and spec.loader
sys.modules["playwright_order_lookup"] = module
spec.loader.exec_module(module)
return module
_plugin = _load_module()
class TestPlaywrightOrder(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory(prefix="playwright_order_")
self.addCleanup(self.tmp.cleanup)
self.playbook_dir = Path(self.tmp.name)
for role in [*ROUND, "web-app-outside"]:
(self.playbook_dir / "roles" / role / "meta").mkdir(parents=True)
self.variables = {
"playbook_dir": str(self.playbook_dir),
"invokables": [*ROUND, "web-app-outside"],
}
def _declare_run_after(self, role: str, run_after) -> None:
dump_yaml(
self.playbook_dir / "roles" / role / ROLE_FILE_META_TESTS,
{"playwright": {"timeout": 9000, "run_after": run_after}},
)
def _order(self, roles: list[str]) -> list[str]:
templar = Templar(loader=DataLoader(), variables=self.variables)
lookup = _plugin.LookupModule(loader=DataLoader(), templar=templar)
role_tests = RoleTestsLookup(loader=DataLoader(), templar=templar)
with patch.object(_plugin, "lookup_loader") as loader:
loader.get.return_value = role_tests
return lookup.run([roles], variables=self.variables)[0]
def test_without_run_after_the_order_is_kept(self):
self.assertEqual(self._order(list(ROUND)), ROUND)
def test_every_invokable_but_itself_puts_docs_behind_all_others(self):
self._declare_run_after(
"web-app-docs", "{{ invokables | difference([application_id]) }}"
)
others = [role for role in ROUND if role != "web-app-docs"]
self.assertEqual(self._order(list(ROUND)), [*others, "web-app-docs"])
def test_run_after_only_constrains_roles_in_the_round(self):
self._declare_run_after("web-app-matomo", ["web-app-outside"])
self._declare_run_after("web-app-docs", ["web-app-stalwart"])
self.assertEqual(
self._order(list(ROUND)),
[
"web-app-keycloak",
"web-app-matomo",
"web-app-stalwart",
"web-app-docs",
"web-app-prometheus",
],
)
def test_a_role_naming_itself_raises(self):
self._declare_run_after("web-app-docs", ["web-app-docs"])
with self.assertRaises(AnsibleError):
self._order(list(ROUND))
def test_a_cycle_raises(self):
self._declare_run_after("web-app-docs", ["web-app-prometheus"])
self._declare_run_after("web-app-prometheus", ["web-app-docs"])
with self.assertRaises(AnsibleError):
self._order(list(ROUND))
def test_an_unknown_role_raises(self):
self._declare_run_after("web-app-docs", ["web-app-typo"])
with self.assertRaises(AnsibleError):
self._order(list(ROUND))
def test_a_value_that_is_no_list_raises(self):
self._declare_run_after("web-app-docs", "web-app-stalwart")
with self.assertRaises(AnsibleError):
self._order(list(ROUND))
if __name__ == "__main__":
unittest.main()
@@ -13,6 +13,7 @@ from utils.roles.order import (
build_dependency_graph,
find_cycle,
ordered_roles,
stable_order_after,
topological_sort,
)
@@ -82,5 +83,37 @@ class TestRoleOrder(unittest.TestCase):
ordered_roles(self.temp_dir, "grp")
class TestStableOrderAfter(unittest.TestCase):
NAMES = ("keycloak", "matomo", "docs", "stalwart", "jitsi")
def test_no_constraint_keeps_the_order(self):
self.assertEqual(stable_order_after(list(self.NAMES), {}), list(self.NAMES))
def test_a_name_after_all_others_moves_last_and_nothing_else_moves(self):
others = [name for name in self.NAMES if name != "docs"]
result = stable_order_after(list(self.NAMES), {"docs": others})
self.assertEqual(result, [*others, "docs"])
def test_a_satisfied_constraint_moves_nothing(self):
result = stable_order_after(list(self.NAMES), {"stalwart": ["matomo"]})
self.assertEqual(result, list(self.NAMES))
def test_names_outside_the_list_constrain_nothing(self):
result = stable_order_after(
list(self.NAMES), {"matomo": ["absent"], "docs": ["jitsi", "gone"]}
)
self.assertEqual(result, ["keycloak", "matomo", "stalwart", "jitsi", "docs"])
def test_a_name_listing_itself_raises(self):
with self.assertRaises(RuntimeError):
stable_order_after(list(self.NAMES), {"docs": ["docs"]})
def test_a_cycle_raises_and_names_its_members(self):
with self.assertRaises(RuntimeError) as ctx:
stable_order_after(list(self.NAMES), {"docs": ["jitsi"], "jitsi": ["docs"]})
self.assertIn("docs", str(ctx.exception))
self.assertIn("jitsi", str(ctx.exception))
if __name__ == "__main__":
unittest.main()