Skip to content

Commit b1642e9

Browse files
fix(keycloak): Fixed Keycloak testcontainer for latest version v26.1.0 (#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]>
1 parent f0bb0f5 commit b1642e9

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

modules/keycloak/testcontainers/keycloak/__init__.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs
2121

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

2428

2529
class KeycloakContainer(DockerContainer):
@@ -57,6 +61,9 @@ def __init__(
5761
self.cmd = cmd
5862

5963
def _configure(self) -> None:
64+
self.with_env(ADMIN_USERNAME_ENVIRONMENT_VARIABLE, self.username)
65+
self.with_env(ADMIN_PASSWORD_ENVIRONMENT_VARIABLE, self.password)
66+
# legacy env vars (<= 26.0.0)
6067
self.with_env("KEYCLOAK_ADMIN", self.username)
6168
self.with_env("KEYCLOAK_ADMIN_PASSWORD", self.password)
6269
# Enable health checks
@@ -89,7 +96,8 @@ def _readiness_probe(self) -> None:
8996
response = requests.get(f"{self.get_url()}/health/ready", timeout=1)
9097
response.raise_for_status()
9198
if _DEFAULT_DEV_COMMAND in self._command:
92-
wait_for_logs(self, "Added user .* to realm .*")
99+
wait_for_logs(self, "started in \\d+\\.\\d+s")
100+
wait_for_logs(self, "Created temporary admin user|Added user '")
93101

94102
def start(self) -> "KeycloakContainer":
95103
super().start()

modules/keycloak/tests/test_keycloak.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from testcontainers.keycloak import KeycloakContainer
33

44

5-
@pytest.mark.parametrize("image_version", ["25.0", "24.0.1", "18.0"])
5+
@pytest.mark.parametrize("image_version", ["26.0.0", "25.0", "24.0.1", "18.0"])
66
def test_docker_run_keycloak(image_version: str):
77
with KeycloakContainer(f"quay.io/keycloak/keycloak:{image_version}") as keycloak_admin:
88
assert keycloak_admin.get_client().users_count() == 1

0 commit comments

Comments
 (0)