diff --git a/modules/keycloak/testcontainers/keycloak/__init__.py b/modules/keycloak/testcontainers/keycloak/__init__.py index ff7a64c2..843283d6 100644 --- a/modules/keycloak/testcontainers/keycloak/__init__.py +++ b/modules/keycloak/testcontainers/keycloak/__init__.py @@ -50,6 +50,11 @@ def __init__( def _configure(self) -> None: self.with_env("KEYCLOAK_ADMIN", self.username) self.with_env("KEYCLOAK_ADMIN_PASSWORD", self.password) + # Enable health checks + # see: https://www.keycloak.org/server/health#_relevant_options + self.with_env("KC_HEALTH_ENABLED", "true") + # Starting Keycloak in development mode + # see: https://www.keycloak.org/server/configuration#_starting_keycloak_in_development_mode self.with_command("start-dev") def get_url(self) -> str: @@ -58,14 +63,15 @@ def get_url(self) -> str: return f"http://{host}:{port}" @wait_container_is_ready(requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout) - def _connect(self) -> None: - response = requests.get(self.get_url(), timeout=1) + def _readiness_probe(self) -> None: + # Keycloak provides an REST API endpoints for health checks: https://www.keycloak.org/server/health + response = requests.get(f"{self.get_url()}/health/ready", timeout=1) response.raise_for_status() def start(self) -> "KeycloakContainer": self._configure() super().start() - self._connect() + self._readiness_probe() return self def get_client(self, **kwargs) -> KeycloakAdmin: diff --git a/modules/keycloak/tests/test_keycloak.py b/modules/keycloak/tests/test_keycloak.py index f6d29a4e..6eac4215 100644 --- a/modules/keycloak/tests/test_keycloak.py +++ b/modules/keycloak/tests/test_keycloak.py @@ -1,6 +1,8 @@ +import pytest from testcontainers.keycloak import KeycloakContainer -def test_docker_run_keycloak(): - with KeycloakContainer("quay.io/keycloak/keycloak:24.0.1") as keycloak_admin: +@pytest.mark.parametrize("image_version", ["24.0.1", "18.0"]) +def test_docker_run_keycloak(image_version: str): + with KeycloakContainer(f"quay.io/keycloak/keycloak:{image_version}") as keycloak_admin: keycloak_admin.get_client().users_count()