From 36e99dd2acfe349778888ae87796b88a063ca52e Mon Sep 17 00:00:00 2001 From: Salomon Popp Date: Mon, 4 Mar 2024 11:56:10 +0000 Subject: [PATCH 1/2] Retry Keycloak admin access with delay if testcontainer not fully ready --- tests/test_oauth.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/test_oauth.py b/tests/test_oauth.py index c6b5f6c..843108d 100644 --- a/tests/test_oauth.py +++ b/tests/test_oauth.py @@ -1,10 +1,11 @@ import json from pathlib import Path +from time import sleep from typing import Annotated, Generator from fastapi import Depends, FastAPI, Request, status import httpx from starlette.middleware.sessions import SessionMiddleware -from keycloak import KeycloakAdmin +from keycloak import KeycloakAdmin, KeycloakAuthenticationError import pytest from keycloak_oauth import KeycloakOAuth2, User from testcontainers.keycloak import KeycloakContainer @@ -30,7 +31,15 @@ def keycloak(self) -> Generator[KeycloakAdmin, None, None]: container, KeycloakContainer ) # HACK: wrong type annotation in testcontainers `with_command` container.with_bind_ports(container.port, container.port).start() - keycloak = container.get_client() + + while True: + try: + keycloak = container.get_client() + except KeycloakAuthenticationError: + sleep(0.1) + continue + break + assert keycloak.connection.base_url == container.get_base_api_url() + "/" keycloak.import_realm( json.loads(Path(self.RESOURCES_PATH / "realm.json").read_bytes()) From 4788fe4695bf18ce54dd64155a2eaa620af6f97d Mon Sep 17 00:00:00 2001 From: Salomon Popp Date: Mon, 4 Mar 2024 12:20:59 +0000 Subject: [PATCH 2/2] Replace custom retry handler with `wait_for_logs` --- tests/test_oauth.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/test_oauth.py b/tests/test_oauth.py index 843108d..adc172d 100644 --- a/tests/test_oauth.py +++ b/tests/test_oauth.py @@ -1,12 +1,12 @@ import json from pathlib import Path -from time import sleep from typing import Annotated, Generator from fastapi import Depends, FastAPI, Request, status import httpx from starlette.middleware.sessions import SessionMiddleware -from keycloak import KeycloakAdmin, KeycloakAuthenticationError +from keycloak import KeycloakAdmin import pytest +from testcontainers.core.waiting_utils import wait_for_logs from keycloak_oauth import KeycloakOAuth2, User from testcontainers.keycloak import KeycloakContainer from fastapi.testclient import TestClient @@ -31,14 +31,8 @@ def keycloak(self) -> Generator[KeycloakAdmin, None, None]: container, KeycloakContainer ) # HACK: wrong type annotation in testcontainers `with_command` container.with_bind_ports(container.port, container.port).start() - - while True: - try: - keycloak = container.get_client() - except KeycloakAuthenticationError: - sleep(0.1) - continue - break + wait_for_logs(container, "Running the server in development mode.") + keycloak = container.get_client() assert keycloak.connection.base_url == container.get_base_api_url() + "/" keycloak.import_realm(