Replies: 1 comment
-
|
Approach 1 — Multiple compose override files, one container per test classCreate a base // Test class A
@Testcontainers
class RealmATest {
@Container
static ComposeContainer env = new ComposeContainer(
new File("src/test/resources/docker-compose.yml"),
new File("src/test/resources/docker-compose-realm-a.yml"))
.withExposedService("keycloak", 8080, Wait.forHttp("/health/ready").forStatusCode(200));
// all tests in this class share the same container
}
// Test class B uses a separate container with its own config
@Testcontainers
class RealmBTest {
@Container
static ComposeContainer env = new ComposeContainer(
new File("src/test/resources/docker-compose.yml"),
new File("src/test/resources/docker-compose-realm-b.yml"))
.withExposedService("keycloak", 8080, Wait.forHttp("/health/ready").forStatusCode(200));
}Approach 2 — Apply Keycloak config via REST API between tests (no restart needed)Instead of remounting files and restarting keycloak-config-cli, push realm configuration directly through Keycloak's Admin REST API after the container is up. This is faster and avoids any restart: @BeforeEach
void applyRealmConfig() throws Exception {
String adminToken = getAdminToken(); // POST /realms/master/protocol/openid-connect/token
// Import realm JSON via Admin API
String realmJson = Files.readString(Path.of("src/test/resources/realm-test.json"));
HttpClient.newHttpClient().send(
HttpRequest.newBuilder()
.uri(URI.create(keycloakUrl + "/admin/realms"))
.header("Authorization", "Bearer " + adminToken)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(realmJson))
.build(),
HttpResponse.BodyHandlers.discarding()
);
}
@AfterEach
void cleanupRealm() throws Exception {
// DELETE /admin/realms/{realmName} to reset between tests
String adminToken = getAdminToken();
HttpClient.newHttpClient().send(
HttpRequest.newBuilder()
.uri(URI.create(keycloakUrl + "/admin/realms/test-realm"))
.header("Authorization", "Bearer " + adminToken)
.DELETE()
.build(),
HttpResponse.BodyHandlers.discarding()
);
}This approach keeps Keycloak running continuously and only changes realm state — no container restart, no volume remounting. Why Approach 2 is preferred
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I'm using
ComposeContainerto spin up someapplication-container(e.g. Keycloak) and then configure it by let's sayinit-container(keycloak-config-cli). Wheninit-containerfinishes it works, it just stops with correct exit code.example of
docker-compose.ymlI'd like to reuse the initial state of
application-containerand just do some changes ininit-container(e.g. mount different config files) and then start it again to do a re-configuration stuff forapplication-container. Is that possible with current implementation? Any hints how to do that?thanks
Beta Was this translation helpful? Give feedback.
All reactions