Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(keycloak): Add support for Keycloak version >=25 #694

Merged
merged 1 commit into from
Sep 8, 2024
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
18 changes: 15 additions & 3 deletions modules/keycloak/testcontainers/keycloak/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class KeycloakContainer(DockerContainer):

>>> from testcontainers.keycloak import KeycloakContainer

>>> with KeycloakContainer(f"quay.io/keycloak/keycloak:24.0.1") as keycloak:
>>> with KeycloakContainer(f"quay.io/keycloak/keycloak:25.0.4") as keycloak:
... keycloak.get_client().users_count()
1
"""
Expand All @@ -45,13 +45,15 @@ def __init__(
username: Optional[str] = None,
password: Optional[str] = None,
port: int = 8080,
management_port: int = 9000,
cmd: Optional[str] = _DEFAULT_DEV_COMMAND,
) -> None:
super().__init__(image=image)
self.username = username or os.environ.get("KEYCLOAK_ADMIN", "test")
self.password = password or os.environ.get("KEYCLOAK_ADMIN_PASSWORD", "test")
self.port = port
self.with_exposed_ports(self.port)
self.management_port = management_port
self.with_exposed_ports(self.port, self.management_port)
self.cmd = cmd

def _configure(self) -> None:
Expand All @@ -71,10 +73,20 @@ def get_url(self) -> str:
port = self.get_exposed_port(self.port)
return f"http://{host}:{port}"

def get_management_url(self) -> str:
host = self.get_container_host_ip()
port = self.get_exposed_port(self.management_port)
return f"http://{host}:{port}"

@wait_container_is_ready(requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout)
def _readiness_probe(self) -> None:
# Keycloak provides REST API endpoints for health checks: https://www.keycloak.org/server/health
response = requests.get(f"{self.get_url()}/health/ready", timeout=1)
try:
# Try the new health endpoint for keycloak 25.0.0 and above
# See https://www.keycloak.org/docs/25.0.0/release_notes/#management-port-for-metrics-and-health-endpoints
response = requests.get(f"{self.get_management_url()}/health/ready", timeout=1)
except requests.exceptions.ConnectionError:
response = requests.get(f"{self.get_url()}/health/ready", timeout=1)
response.raise_for_status()
if _DEFAULT_DEV_COMMAND in self._command:
wait_for_logs(self, "Added user .* to realm .*")
Expand Down
2 changes: 1 addition & 1 deletion modules/keycloak/tests/test_keycloak.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from testcontainers.keycloak import KeycloakContainer


@pytest.mark.parametrize("image_version", ["24.0.1", "18.0"])
@pytest.mark.parametrize("image_version", ["25.0", "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:
assert keycloak_admin.get_client().users_count() == 1