Skip to content

Commit 2e27225

Browse files
authoredMar 24, 2024··
fix(keycloak): container should use dedicated API endpoints to determine container readiness (#490)
As decided to support v18.0 image or newer, we should use these dedicated API endpoints to determine container readiness. These endpoints became introduced with v18.0.
1 parent 909107b commit 2e27225

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed
 

‎modules/keycloak/testcontainers/keycloak/__init__.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ def __init__(
5050
def _configure(self) -> None:
5151
self.with_env("KEYCLOAK_ADMIN", self.username)
5252
self.with_env("KEYCLOAK_ADMIN_PASSWORD", self.password)
53+
# Enable health checks
54+
# see: https://www.keycloak.org/server/health#_relevant_options
55+
self.with_env("KC_HEALTH_ENABLED", "true")
56+
# Starting Keycloak in development mode
57+
# see: https://www.keycloak.org/server/configuration#_starting_keycloak_in_development_mode
5358
self.with_command("start-dev")
5459

5560
def get_url(self) -> str:
@@ -58,14 +63,15 @@ def get_url(self) -> str:
5863
return f"http://{host}:{port}"
5964

6065
@wait_container_is_ready(requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout)
61-
def _connect(self) -> None:
62-
response = requests.get(self.get_url(), timeout=1)
66+
def _readiness_probe(self) -> None:
67+
# Keycloak provides an REST API endpoints for health checks: https://www.keycloak.org/server/health
68+
response = requests.get(f"{self.get_url()}/health/ready", timeout=1)
6369
response.raise_for_status()
6470

6571
def start(self) -> "KeycloakContainer":
6672
self._configure()
6773
super().start()
68-
self._connect()
74+
self._readiness_probe()
6975
return self
7076

7177
def get_client(self, **kwargs) -> KeycloakAdmin:
+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import pytest
12
from testcontainers.keycloak import KeycloakContainer
23

34

4-
def test_docker_run_keycloak():
5-
with KeycloakContainer("quay.io/keycloak/keycloak:24.0.1") as keycloak_admin:
5+
@pytest.mark.parametrize("image_version", ["24.0.1", "18.0"])
6+
def test_docker_run_keycloak(image_version: str):
7+
with KeycloakContainer(f"quay.io/keycloak/keycloak:{image_version}") as keycloak_admin:
68
keycloak_admin.get_client().users_count()

0 commit comments

Comments
 (0)
Please sign in to comment.