diff --git a/roles/web-app-nextcloud/tasks/01_fullstack.yml b/roles/web-app-nextcloud/tasks/01_fullstack.yml index 67e84706eb..81267fe7e6 100644 --- a/roles/web-app-nextcloud/tasks/01_fullstack.yml +++ b/roles/web-app-nextcloud/tasks/01_fullstack.yml @@ -18,6 +18,7 @@ on this host, so Talk would hand out no STUN or TURN server and calls behind NAT would carry no media. Deploy {{ NEXTCLOUD_HPB_TURN_STANDALONE_ROLE }} or set services.talk.enabled to false. quiet: true + when: IS_STACK_HOST | bool - name: "🧩 load docker, db and proxy for {{ application_id }}" include_role: diff --git a/tests/unit/python/roles/web-app-nextcloud/test_talk_turn_assert.py b/tests/unit/python/roles/web-app-nextcloud/test_talk_turn_assert.py new file mode 100644 index 0000000000..87df3de2d3 --- /dev/null +++ b/tests/unit/python/roles/web-app-nextcloud/test_talk_turn_assert.py @@ -0,0 +1,65 @@ +import unittest +from pathlib import Path + +from jinja2 import Environment + +from utils import PROJECT_ROOT +from utils.cache.yaml import load_yaml_any + +FULLSTACK = ( + Path(PROJECT_ROOT) / "roles" / "web-app-nextcloud" / "tasks" / "01_fullstack.yml" +) +ASSERT_NAME = "🛑 Assert Talk has the TURN server its calls relay through" + + +def _turn_assert() -> tuple[dict, list[str]]: + for task in load_yaml_any(str(FULLSTACK)): + for inner in task.get("block", []): + if inner.get("name") == ASSERT_NAME: + outer = task.get("when", []) + return inner, outer if isinstance(outer, list) else [outer] + raise AssertionError(f"{ASSERT_NAME!r} not found in {FULLSTACK}") + + +def _runs_and_holds(*, stack_host: bool, coturn_here: bool) -> tuple[bool, bool]: + task, guards = _turn_assert() + inner = task.get("when", []) + guards = guards + (inner if isinstance(inner, list) else [inner]) + env = Environment(autoescape=True) + env.filters["bool"] = bool + context = { + "NEXTCLOUD_HPB_SIGNALING_ENABLED": True, + "IS_STACK_HOST": stack_host, + "NEXTCLOUD_HPB_TURN_ACTIVE_SERVER": "turn.example.org:3478" + if coturn_here + else "", + } + runs = all(bool(env.compile_expression(guard)(**context)) for guard in guards) + holds = bool( + env.compile_expression(task["ansible.builtin.assert"]["that"])(**context) + ) + return runs, holds + + +class TestTalkTurnAssert(unittest.TestCase): + def test_a_swarm_worker_without_coturn_skips_the_assert(self) -> None: + runs, _ = _runs_and_holds(stack_host=False, coturn_here=False) + self.assertFalse( + runs, + "a swarm worker never runs Talk and is not in web-svc-coturn's group, " + "so the assert must not judge it", + ) + + def test_the_stack_host_with_coturn_passes(self) -> None: + self.assertEqual( + _runs_and_holds(stack_host=True, coturn_here=True), (True, True) + ) + + def test_the_stack_host_without_coturn_is_refused(self) -> None: + self.assertEqual( + _runs_and_holds(stack_host=True, coturn_here=False), (True, False) + ) + + +if __name__ == "__main__": + unittest.main()