Skip to content

Commit

Permalink
Set memory limit for Keycloak container
Browse files Browse the repository at this point in the history
Closes #1350

Signed-off-by: Martin Bartoš <[email protected]>
  • Loading branch information
mabartos authored and michalvavrik committed Oct 2, 2024
1 parent 374d3db commit 2b907f1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@

String[] command() default {};

long memoryLimitMiB() default 1000;

Class<? extends ManagedResourceBuilder> builder() default KeycloakContainerManagedResourceBuilder.class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class KeycloakContainerManagedResourceBuilder extends ContainerManagedRes
private int restPort;
private String[] command;
private String expectedLog;
private long memoryLimitMiB;

@Override
protected String getImage() {
Expand All @@ -44,13 +45,18 @@ protected String getExpectedLog() {
return expectedLog;
}

protected Long getMemoryLimitMiB() {
return memoryLimitMiB;
}

@Override
public void init(Annotation annotation) {
KeycloakContainer metadata = (KeycloakContainer) annotation;
this.image = PropertiesUtils.resolveProperty(metadata.image());
this.restPort = metadata.port();
this.expectedLog = PropertiesUtils.resolveProperty(metadata.expectedLog());
this.command = metadata.command();
this.memoryLimitMiB = metadata.memoryLimitMiB();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.test.services.containers;

import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
Expand Down Expand Up @@ -38,6 +40,8 @@ protected GenericContainer<?> initContainer() {
}

container.withCreateContainerCmdModifier(cmd -> cmd.withName(DockerUtils.generateDockerContainerName()));
container.withCreateContainerCmdModifier(cmd -> Optional.ofNullable(cmd.getHostConfig())
.ifPresent(config -> config.withMemory(convertMiBtoBytes(model.getMemoryLimitMiB()))));

if (isReusable()) {
Log.info(model.getContext().getOwner(), "Running container on Reusable mode");
Expand Down Expand Up @@ -66,4 +70,9 @@ protected boolean isReusable() {
private boolean isPrivileged() {
return model.getContext().getOwner().getConfiguration().isTrue(PRIVILEGED_MODE);
}

static long convertMiBtoBytes(long valueInMiB) {
final var exponentMiB = 20;
return (long) (valueInMiB * Math.pow(2, exponentMiB));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.quarkus.test.services.containers;

import static io.quarkus.test.services.containers.KeycloakGenericDockerContainerManagedResource.convertMiBtoBytes;
import static org.hamcrest.MatcherAssert.assertThat;

import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;

public class MemoryLimitTest {

@Test
void convertMiBToBytes() {
assertThat(convertMiBtoBytes(0), CoreMatchers.is(0L));
assertThat(convertMiBtoBytes(-0), CoreMatchers.is(0L));

assertThat(convertMiBtoBytes(1000L), CoreMatchers.is(1048576000L));
assertThat(convertMiBtoBytes(1000), CoreMatchers.is(1048576000L));
assertThat(convertMiBtoBytes(100), CoreMatchers.is(104857600L));
assertThat(convertMiBtoBytes(10), CoreMatchers.is(10485760L));
assertThat(convertMiBtoBytes(1), CoreMatchers.is(1048576L));

assertThat(convertMiBtoBytes(-1000L), CoreMatchers.is(-1048576000L));
assertThat(convertMiBtoBytes(-1000), CoreMatchers.is(-1048576000L));
assertThat(convertMiBtoBytes(-100), CoreMatchers.is(-104857600L));
assertThat(convertMiBtoBytes(-10), CoreMatchers.is(-10485760L));
assertThat(convertMiBtoBytes(-1), CoreMatchers.is(-1048576L));

assertThat(convertMiBtoBytes(2000), CoreMatchers.is(2097152000L));
assertThat(convertMiBtoBytes(20000), CoreMatchers.is(20971520000L));
assertThat(convertMiBtoBytes(200000), CoreMatchers.is(209715200000L));

assertThat(convertMiBtoBytes(999999999999999999L), CoreMatchers.is(Long.MAX_VALUE));

assertThat(convertMiBtoBytes(Integer.MAX_VALUE), CoreMatchers.is((long) Integer.MAX_VALUE << 20));
assertThat(convertMiBtoBytes(Integer.MIN_VALUE), CoreMatchers.is((long) Integer.MIN_VALUE << 20));

assertThat(convertMiBtoBytes(Long.MAX_VALUE), CoreMatchers.is(Long.MAX_VALUE));
assertThat(convertMiBtoBytes(Long.MIN_VALUE), CoreMatchers.is(Long.MIN_VALUE));
}
}

0 comments on commit 2b907f1

Please sign in to comment.