From 1c82d17fbfa50ead35dcaf780d6b95aacfcae59d Mon Sep 17 00:00:00 2001 From: Gael Goinvic Date: Tue, 6 Oct 2026 08:38:13 +0000 Subject: [PATCH] New ingress_ready_for fixture --- tests/integration/fixtures/__init__.py | 3 ++- tests/integration/fixtures/helm.py | 11 ++++++++++ tests/integration/lib/utils.py | 28 +------------------------ tests/integration/test_element_admin.py | 14 ++++++------- tests/integration/test_element_web.py | 14 ++++++------- 5 files changed, 28 insertions(+), 42 deletions(-) diff --git a/tests/integration/fixtures/__init__.py b/tests/integration/fixtures/__init__.py index af117fc8..f2088c25 100644 --- a/tests/integration/fixtures/__init__.py +++ b/tests/integration/fixtures/__init__.py @@ -6,7 +6,7 @@ from .ca import delegated_ca, root_ca, ssl_context from .cluster import cert_manager, cluster, ess_namespace, helm_client, ingress, kube_client, prometheus_operator_crds from .data import ESSData, generated_data -from .helm import helm_prerequisites, ingress_ready, matrix_stack, secrets_generated +from .helm import helm_prerequisites, ingress_ready, ingress_ready_for, matrix_stack, secrets_generated from .matrix_tools import build_matrix_tools, loaded_matrix_tools from .users import User, users @@ -21,6 +21,7 @@ __all__ = [ "helm_client", "helm_prerequisites", "ingress_ready", + "ingress_ready_for", "ingress", "kube_client", "loaded_matrix_tools", diff --git a/tests/integration/fixtures/helm.py b/tests/integration/fixtures/helm.py index ade7a03f..1541e6c2 100644 --- a/tests/integration/fixtures/helm.py +++ b/tests/integration/fixtures/helm.py @@ -259,6 +259,17 @@ def ingress_ready(cluster, kube_client: AsyncClient, matrix_stack, generated_dat return _ingress_ready +@pytest.fixture +async def ingress_ready_for(request, ingress_ready): + """Wait for the ingress named by the indirect parameter to be ready, on the host. + + docker_playwright tests use this instead of waiting inside their body: their fixtures + are resolved on the host, which can check the ingress readiness against the kube API, + while their body runs in the playwright container, which cannot. + """ + await ingress_ready(request.param) + + @pytest.fixture(scope="session") def secrets_generated(cluster, kube_client: AsyncClient, matrix_stack, generated_data: ESSData): async def _secrets_generated(secret_key) -> str: diff --git a/tests/integration/lib/utils.py b/tests/integration/lib/utils.py index 73fad57d..ed9ac8f4 100644 --- a/tests/integration/lib/utils.py +++ b/tests/integration/lib/utils.py @@ -13,7 +13,7 @@ from contextlib import asynccontextmanager from dataclasses import dataclass from datetime import datetime, timedelta from pathlib import Path -from ssl import SSLContext, create_default_context +from ssl import SSLContext from typing import Any from urllib.parse import urlparse @@ -175,32 +175,6 @@ async def aiohttp_put_json(url: str, data: dict, headers: dict, ssl_context: SSL return {} -async def wait_for_https_endpoint(hostname: str, ca_pem: str, timeout: int = 120) -> None: - """Wait until the ingress serving hostname answers HTTPS with a certificate signed by ca_pem. - - The k3d ingress proxy listens on the loopback interface of the host running pytest, and of the - test container which shares the host network. The hostname is passed with the Host header and - SNI, like aiohttp_get_json does. - - Args: - hostname (str): The hostname served by the ingress - ca_pem (str): The PEM of the CA that signed the ingress certificate - timeout (int): How many seconds to wait for the ingress - """ - ssl_context = create_default_context() - ssl_context.load_verify_locations(cadata=ca_pem) - - async def probe(): - async with ( - aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, - session.get("https://127.0.0.1/", headers={"Host": hostname}, server_hostname=hostname) as response, - ): - if response.status >= 500: - raise RuntimeError(f"Ingress for {hostname} is not ready yet: {response.status}") - - await async_retry_with_timeout(probe, max_retries=60, timeout_seconds=timeout, delay=2) - - def merge(a: dict, b: dict, path=None): if not path: path = [] diff --git a/tests/integration/test_element_admin.py b/tests/integration/test_element_admin.py index e065677b..060a69bb 100644 --- a/tests/integration/test_element_admin.py +++ b/tests/integration/test_element_admin.py @@ -9,7 +9,7 @@ import pytest from .artifacts import CertKey from .fixtures import ESSData, User -from .lib.utils import aiohttp_client, value_file_has, wait_for_https_endpoint +from .lib.utils import aiohttp_client, value_file_has @pytest.mark.skipif(value_file_has("elementAdmin.enabled", False), reason="elementAdmin not deployed") @@ -32,16 +32,15 @@ async def test_element_admin_can_access_root(ingress_ready, generated_data: ESSD @pytest.mark.skipif(value_file_has("elementAdmin.enabled", False), reason="elementAdmin not deployed") @pytest.mark.skipif(value_file_has("matrixAuthenticationService.enabled", False), reason="MAS not deployed") @pytest.mark.parametrize("users", [[User("browser-admin-user", admin=True)]], indirect=True) +@pytest.mark.parametrize("ingress_ready_for", ["element-admin"], indirect=True) @pytest.mark.asyncio_cooperative @pytest.mark.docker_playwright -async def test_element_admin_login(generated_data: ESSData, users: list[User], root_ca: CertKey): +async def test_element_admin_login(generated_data: ESSData, users: list[User], root_ca: CertKey, ingress_ready_for): # Imported here: playwright is only installed inside the test container from playwright.async_api import expect from .lib.browser import browser_page, login_on_mas_page - await wait_for_https_endpoint(f"admin.{generated_data.server_name}", root_ca.cert_bundle_as_pem()) - async with browser_page(generated_data._root_ca.cert_as_pem()) as page: await page.goto(f"https://admin.{generated_data.server_name}/") await page.get_by_role("button", name="Get started").click() @@ -58,16 +57,17 @@ async def test_element_admin_login(generated_data: ESSData, users: list[User], r @pytest.mark.skipif(value_file_has("elementAdmin.enabled", False), reason="elementAdmin not deployed") @pytest.mark.skipif(value_file_has("matrixAuthenticationService.enabled", False), reason="MAS not deployed") @pytest.mark.parametrize("users", [[User("browser-non-admin-user")]], indirect=True) +@pytest.mark.parametrize("ingress_ready_for", ["element-admin"], indirect=True) @pytest.mark.asyncio_cooperative @pytest.mark.docker_playwright -async def test_element_admin_login_rejects_non_admin(generated_data: ESSData, users: list[User], root_ca: CertKey): +async def test_element_admin_login_rejects_non_admin( + generated_data: ESSData, users: list[User], root_ca: CertKey, ingress_ready_for +): # Imported here: playwright is only installed inside the test container from playwright.async_api import expect from .lib.browser import browser_page, login_on_mas_page - await wait_for_https_endpoint(f"admin.{generated_data.server_name}", root_ca.cert_bundle_as_pem()) - async with browser_page(root_ca.cert_bundle_as_pem()) as page: await page.goto(f"https://admin.{generated_data.server_name}/") await page.get_by_role("button", name="Get started").click() diff --git a/tests/integration/test_element_web.py b/tests/integration/test_element_web.py index 7d17d778..fe8d993a 100644 --- a/tests/integration/test_element_web.py +++ b/tests/integration/test_element_web.py @@ -9,7 +9,7 @@ import pytest from .artifacts import CertKey from .fixtures import ESSData, User -from .lib.utils import aiohttp_get_json, value_file_has, wait_for_https_endpoint +from .lib.utils import aiohttp_get_json, value_file_has @pytest.mark.skipif(value_file_has("elementWeb.enabled", False), reason="ElementWeb not deployed") @@ -24,16 +24,15 @@ async def test_element_web_can_access_config_json(ingress_ready, generated_data: # Runs inside the playwright container, as the host may not be able to run a browser (e.g. Alpine) @pytest.mark.skipif(value_file_has("elementWeb.enabled", False), reason="ElementWeb not deployed") +@pytest.mark.parametrize("ingress_ready_for", ["element-web"], indirect=True) @pytest.mark.asyncio_cooperative @pytest.mark.docker_playwright -async def test_element_web_loads_in_browser(generated_data: ESSData, root_ca: CertKey): +async def test_element_web_loads_in_browser(generated_data: ESSData, root_ca: CertKey, ingress_ready_for): # Imported here: playwright is only installed inside the test container from playwright.async_api import expect from .lib.browser import browser_page - await wait_for_https_endpoint(f"element.{generated_data.server_name}", root_ca.cert_bundle_as_pem()) - async with browser_page(generated_data._root_ca.cert_as_pem()) as page: await page.goto(f"https://element.{generated_data.server_name}/") @@ -44,16 +43,17 @@ async def test_element_web_loads_in_browser(generated_data: ESSData, root_ca: Ce @pytest.mark.skipif(value_file_has("elementWeb.enabled", False), reason="ElementWeb not deployed") @pytest.mark.skipif(value_file_has("matrixAuthenticationService.enabled", False), reason="MAS not deployed") @pytest.mark.parametrize("users", [[User("browser-element-web-user")]], indirect=True) +@pytest.mark.parametrize("ingress_ready_for", ["element-web"], indirect=True) @pytest.mark.asyncio_cooperative @pytest.mark.docker_playwright -async def test_element_web_login_via_mas(generated_data: ESSData, users: list[User], root_ca: CertKey): +async def test_element_web_login_via_mas( + generated_data: ESSData, users: list[User], root_ca: CertKey, ingress_ready_for +): # Imported here: playwright is only installed inside the test container from playwright.async_api import expect from .lib.browser import browser_page, login_on_mas_page - await wait_for_https_endpoint(f"element.{generated_data.server_name}", root_ca.cert_bundle_as_pem()) - async with browser_page(root_ca.cert_bundle_as_pem()) as page: await page.goto(f"https://element.{generated_data.server_name}/") await page.get_by_role("button", name="Continue").click()