Skip to content

fix(keycloak): Fixed Keycloak testcontainer for latest version v26.1.0 #766

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

Merged
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
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