Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set memory limit for Keycloak container #1351

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@

String[] command() default {};

long memoryLimitMiB() default 1000;
fedinskiy marked this conversation as resolved.
Show resolved Hide resolved

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));
}
}