Skip to content

Commit

Permalink
Allow to set Shared Memory size (shmSize) for GenericContainer (#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
fzakaria authored and kiview committed Nov 26, 2018
1 parent cd35993 commit 58a6c69
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Info;
import com.github.dockerjava.api.model.Link;
import com.github.dockerjava.api.model.PortBinding;
Expand Down Expand Up @@ -153,6 +154,13 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
@Nullable
private String workingDirectory = null;

/**
* The shared memory size to use when starting the container.
* This value is in bytes.
*/
@Nullable
private Long shmSize;

private Map<MountableFile, String> copyToFileContainerPathMap = new HashMap<>();

/*
Expand Down Expand Up @@ -318,6 +326,17 @@ private void tryStart(Profiler profiler) {
}
}

/**
* Set any custom settings for the create command such as shared memory size.
*/
private HostConfig buildHostConfig() {
HostConfig config = new HostConfig();
if (shmSize != null) {
config.withShmSize(shmSize);
}
return config;
}

private void connectToPortForwardingNetwork(String networkMode) {
PortForwardingContainer.INSTANCE.getNetwork().map(ContainerNetwork::getNetworkID).ifPresent(networkId -> {
if (!Arrays.asList(networkId, "none", "host").contains(networkMode)) {
Expand Down Expand Up @@ -434,7 +453,9 @@ public Set<Integer> getLivenessCheckPortNumbers() {
}

private void applyConfiguration(CreateContainerCmd createCommand) {

HostConfig hostConfig = buildHostConfig();
createCommand.withHostConfig(hostConfig);

// Set up exposed ports (where there are no host port bindings defined)
ExposedPort[] portArray = exposedPorts.stream()
.map(ExposedPort::new)
Expand Down Expand Up @@ -1157,6 +1178,16 @@ public SELF withCreateContainerCmdModifier(Consumer<CreateContainerCmd> modifier
return self();
}

/**
* Size of /dev/shm
* @param bytes The number of megabytes to assign the shared memory. If null, it will apply the Docker default which is 64 MB.
* @return this
*/
public SELF withSharedMemorySize(Long bytes) {
this.shmSize = bytes;
return self();
}

/**
* Convenience class with access to non-public members of GenericContainer.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.testcontainers.junit;

import com.github.dockerjava.api.model.HostConfig;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.Uninterruptibles;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.rabbitmq.client.*;
import org.apache.commons.io.FileUtils;
import org.bson.Document;
import org.junit.*;
import org.rnorth.ducttape.RetryCountExceededException;
Expand Down Expand Up @@ -375,4 +377,18 @@ public void addExposedPortAfterWithExposedPortsTest() {
assertTrue("withExposedPort should be exposed", redis.getExposedPorts().contains(REDIS_PORT));
assertTrue("addExposedPort should be exposed", redis.getExposedPorts().contains(8987));
}

@Test
public void sharedMemorySetTest() {
try (GenericContainer containerWithSharedMemory = new GenericContainer("busybox:1.29")
.withSharedMemorySize(1024L * FileUtils.ONE_MB)) {

containerWithSharedMemory.start();

HostConfig hostConfig =
containerWithSharedMemory.getDockerClient().inspectContainerCmd(containerWithSharedMemory.getContainerId())
.exec().getHostConfig();
assertEquals("Shared memory not set on container", hostConfig.getShmSize(), 1024 * FileUtils.ONE_MB);
}
}
}

0 comments on commit 58a6c69

Please sign in to comment.