Skip to content

Commit

Permalink
fix(keycloak): container should use dedicated API endpoints to determ…
Browse files Browse the repository at this point in the history
…ine 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.
  • Loading branch information
max-pfeiffer committed Mar 24, 2024
1 parent 909107b commit 2e27225
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 9 additions & 3 deletions modules/keycloak/testcontainers/keycloak/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions modules/keycloak/tests/test_keycloak.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 2e27225

Please sign in to comment.