Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()