Skip to content

Commit

Permalink
fix(keycloak): Fixed Keycloak testcontainer for latest version v26.1.0 (
Browse files Browse the repository at this point in the history
#766)

@alexanderankin We already discussed last year that we only want to
support the latest Keycloak version.

I added the `latest` tag to test parameterization so we get a better
feedback for future Keycloak updates.

Fixes #764

---------

Co-authored-by: David Ankin <[email protected]>
  • Loading branch information
max-pfeiffer and alexanderankin authored Feb 11, 2025
1 parent f0bb0f5 commit b1642e9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion modules/keycloak/testcontainers/keycloak/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs

_DEFAULT_DEV_COMMAND = "start-dev"
# Since Keycloak v26.0.0
# See: https://www.keycloak.org/server/all-config#category-bootstrap_admin
ADMIN_USERNAME_ENVIRONMENT_VARIABLE = "KC_BOOTSTRAP_ADMIN_USERNAME"
ADMIN_PASSWORD_ENVIRONMENT_VARIABLE = "KC_BOOTSTRAP_ADMIN_PASSWORD"


class KeycloakContainer(DockerContainer):
Expand Down Expand Up @@ -57,6 +61,9 @@ def __init__(
self.cmd = cmd

def _configure(self) -> None:
self.with_env(ADMIN_USERNAME_ENVIRONMENT_VARIABLE, self.username)
self.with_env(ADMIN_PASSWORD_ENVIRONMENT_VARIABLE, self.password)
# legacy env vars (<= 26.0.0)
self.with_env("KEYCLOAK_ADMIN", self.username)
self.with_env("KEYCLOAK_ADMIN_PASSWORD", self.password)
# Enable health checks
Expand Down Expand Up @@ -89,7 +96,8 @@ def _readiness_probe(self) -> None:
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 .*")
wait_for_logs(self, "started in \\d+\\.\\d+s")
wait_for_logs(self, "Created temporary admin user|Added user '")

def start(self) -> "KeycloakContainer":
super().start()
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", ["25.0", "24.0.1", "18.0"])
@pytest.mark.parametrize("image_version", ["26.0.0", "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

0 comments on commit b1642e9

Please sign in to comment.