Skip to content

Commit

Permalink
allow to set initial and max RAM percentage usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dasniko committed Mar 4, 2024
1 parent 07de68d commit 5d4c089
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@ KeycloakContainer keycloak = new KeycloakContainer()
.withContextPath("/auth");
```

### Memory Settings

As of Keycloak 24 the container doesn't use an absolute amount of memory, but a relative percentage of the overall available memory to the container, [see also here](https://www.keycloak.org/server/containers#_specifying_different_memory_settings).

This testcontainer has an initial memory setting of

JAVA_OPTS_KC_HEAP="-XX:InitialRAMPercentage=1 -XX:MaxRAMPercentage=5"

to not overload your environment.
You can override this settng with the `withRamPercentage(initial, max)` method:

```java
@Container
KeycloakContainer keycloak = new KeycloakContainer()
.withRamPercentage(50, 70);
```

## TLS (SSL) Usage

You have three options to use HTTPS/TLS secured communication with your Keycloak Testcontainer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public abstract class ExtendableKeycloakContainer<SELF extends ExtendableKeycloa
private static final int KEYCLOAK_PORT_HTTPS = 8443;
private static final int KEYCLOAK_PORT_DEBUG = 8787;
private static final Duration DEFAULT_STARTUP_TIMEOUT = Duration.ofMinutes(2);
private static final int DEFAULT_INITIAL_RAM_PERCENTAGE = 1;
private static final int DEFAULT_MAX_RAM_PERCENTAGE = 5;

private static final String KEYCLOAK_ADMIN_USER = "admin";
private static final String KEYCLOAK_ADMIN_PASSWORD = "admin";
Expand All @@ -79,6 +81,8 @@ public abstract class ExtendableKeycloakContainer<SELF extends ExtendableKeycloa
private String adminUsername = KEYCLOAK_ADMIN_USER;
private String adminPassword = KEYCLOAK_ADMIN_PASSWORD;
private String contextPath = KEYCLOAK_CONTEXT_PATH;
private int initialRamPercentage = DEFAULT_INITIAL_RAM_PERCENTAGE;
private int maxRamPercentage = DEFAULT_MAX_RAM_PERCENTAGE;

private final Set<String> importFiles;
private String tlsCertificateFilename;
Expand Down Expand Up @@ -151,6 +155,7 @@ protected void configure() {

withEnv("KEYCLOAK_ADMIN", adminUsername);
withEnv("KEYCLOAK_ADMIN_PASSWORD", adminPassword);
withEnv("JAVA_OPTS_KC_HEAP", "-XX:InitialRAMPercentage=%d -XX:MaxRAMPercentage=%d".formatted(initialRamPercentage, maxRamPercentage));

if (useTls && isNotBlank(tlsCertificateFilename)) {
String tlsCertFilePath = "/opt/keycloak/conf/tls.crt";
Expand Down Expand Up @@ -303,6 +308,12 @@ public SELF withContextPath(String contextPath) {
return self();
}

public SELF withRamPercentage(int initialRamPercentage, int maxRamPercentage) {
this.initialRamPercentage = initialRamPercentage;
this.maxRamPercentage = maxRamPercentage;
return self();
}

/**
* Exposes the given classes location as an exploded providers.jar.
*
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/junit-platform.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.enabled = false
junit.jupiter.execution.parallel.config.fixed.parallelism = 5
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.mode.classes.default = same_thread

0 comments on commit 5d4c089

Please sign in to comment.