Skip to content

Commit

Permalink
add custom command parts
Browse files Browse the repository at this point in the history
this allows to set custom config cli arguments and add/overwrite default settings from the testcontainer assumptions
  • Loading branch information
dasniko committed Apr 30, 2024
1 parent 82542f5 commit 08f0fa6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@ KeycloakContainer keycloak = new KeycloakContainer()
.withFeaturesDisabled("authorization", "impersonation", "...");
```

## Custom CLI Config arguments

All default configurations in this Testcontainer is done through environment variables.
You can overwrite and/or add config settings on command-line-level (cli args) with this method:

```java
@Container
KeycloakContainer keycloak = new KeycloakContainer()
.withCustomCommand("--hostname=keycloak.local");
```

A warning will be printed to the log output when custom command parts are being used, so that you are aware that you are responsible on your own for proper execution of this container.

## Testing Custom Extensions

To ease extension testing, you can tell the Keycloak Testcontainer to detect extensions in a given classpath folder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public abstract class ExtendableKeycloakContainer<SELF extends ExtendableKeycloa

private String providerClassLocation;
private List<File> providerLibsLocations;
private List<String> customCommandParts;

/**
* Create a KeycloakContainer with default image and version tag
Expand Down Expand Up @@ -222,17 +223,33 @@ protected void configure() {
}
}

if (customCommandParts != null) {
logger().warn("You are using custom command parts. " +
"Container behavior and configuration may be corrupted. " +
"You are self responsible for proper behavior and functionality!\n" +
"CustomCommandParts: {}", customCommandParts);
commandParts.addAll(customCommandParts);
}

setCommand(commandParts.toArray(new String[0]));
}

@Override
public SELF withCommand(String cmd) {
throw new IllegalStateException("You are trying to set custom container commands, which is currently not supported by this Testcontainer.");
throw new IllegalStateException("You are trying to set custom container commands, which is not supported by this Testcontainer. Try using the withCustomCommand() method.");
}

@Override
public SELF withCommand(String... commandParts) {
throw new IllegalStateException("You are trying to set custom container commands, which is currently not supported by this Testcontainer.");
throw new IllegalStateException("You are trying to set custom container commands, which is not supported by this Testcontainer. Try using the withCustomCommand() method.");
}

public SELF withCustomCommand(String cmd) {
if (customCommandParts == null) {
customCommandParts = new ArrayList<>();
}
customCommandParts.add(cmd);
return self();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dasniko.testcontainers.keycloak;

import io.restassured.response.ValidatableResponse;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -16,6 +17,7 @@

import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -118,9 +120,7 @@ public void shouldRunOnDifferentContextPath() {
String authServerUrl = keycloak.getAuthServerUrl();
assertThat(authServerUrl, endsWith(contextPath));

given().when().get(authServerUrl + "/realms/master/.well-known/openid-configuration")
.then().statusCode(200);

getWellKnownOpenidConfigurationResponse(authServerUrl);
checkKeycloakContainerInternals(keycloak);
}
}
Expand Down Expand Up @@ -172,6 +172,20 @@ void shouldOpenFixedDebugPort() throws IOException {
}
}

@Test
void shouldUseCustomCommandPart() {
try (KeycloakContainer keycloak = new KeycloakContainer().withCustomCommand("--hostname=keycloak.local")) {
keycloak.start();
String issuer = getWellKnownOpenidConfigurationResponse(keycloak.getAuthServerUrl()).extract().path("issuer");
assertThat(issuer, containsString("keycloak.local"));
}
}

private static ValidatableResponse getWellKnownOpenidConfigurationResponse(String authServerUrl) {
return given().when().get(authServerUrl + "/realms/master/.well-known/openid-configuration")
.then().statusCode(200);
}

private void checkKeycloakContainerInternals(KeycloakContainer keycloak) {
Keycloak keycloakAdminClient = keycloak.getKeycloakAdminClient();
ServerInfoRepresentation serverInfo = keycloakAdminClient.serverInfo().getInfo();
Expand Down

0 comments on commit 08f0fa6

Please sign in to comment.