From 1999ab9ea31f2f5a8934361c2b00cf48102b9148 Mon Sep 17 00:00:00 2001
From: Salomon Popp <salomon.popp@bakdata.com>
Date: Mon, 4 Mar 2024 12:29:24 +0000
Subject: [PATCH] Delay Keycloak admin access until testcontainer is fully
 ready (#9)

* Retry Keycloak admin access with delay if testcontainer not fully ready

* Replace custom retry handler with `wait_for_logs`
---
 tests/test_oauth.py | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tests/test_oauth.py b/tests/test_oauth.py
index c6b5f6c..adc172d 100644
--- a/tests/test_oauth.py
+++ b/tests/test_oauth.py
@@ -6,6 +6,7 @@
 from starlette.middleware.sessions import SessionMiddleware
 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
@@ -30,7 +31,9 @@ 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()
+        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(
             json.loads(Path(self.RESOURCES_PATH / "realm.json").read_bytes())