Skip to content

Commit

Permalink
Fix use of SHM for Selenium containers on Windows (#1948)
Browse files Browse the repository at this point in the history
* Change assertion used for SHM testing to support Windows

* Make test resilient to future changes in default mounts
by only checking counts of SHM volumes

* WIP - experimental

* Tweak assertion to cope with /dev/shm/

* Switch to directly adding SHM bind rather than using addFileSystemBind

* Simplify assertion code
  • Loading branch information
rnorth committed Oct 6, 2019
1 parent 52420d8 commit 94c903d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
Expand Up @@ -167,7 +167,9 @@ private static String unencodeResourceURIToFilePath(@NotNull final String resour
private String resolvePath() {
String result = getResourcePath();

// Special case for Windows
if (SystemUtils.IS_OS_WINDOWS && result.startsWith("/")) {
// Remove leading /
result = result.substring(1);
}

Expand Down
@@ -1,5 +1,8 @@
package org.testcontainers.containers;

import com.github.dockerjava.api.model.AccessMode;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.Volume;
import com.google.common.collect.ImmutableSet;

import com.github.dockerjava.api.command.InspectContainerResponse;
Expand Down Expand Up @@ -166,7 +169,7 @@ protected void configure() {
setCommand("/opt/bin/entry_point.sh");

if (getShmSize() == null) {
addFileSystemBind("/dev/shm", "/dev/shm", BindMode.READ_WRITE);
this.getBinds().add(new Bind("/dev/shm", new Volume("/dev/shm"), AccessMode.rw));
}

/*
Expand Down
Expand Up @@ -4,13 +4,14 @@
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testcontainers.containers.BrowserWebDriverContainer;

import java.util.List;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;

public class BrowserWebDriverContainerTest {

Expand Down Expand Up @@ -51,34 +52,40 @@ public void provideDefaultNoProxyEnvironmentIfNotSet() {
public void createContainerWithShmVolume() {
try (
BrowserWebDriverContainer webDriverContainer = new BrowserWebDriverContainer()
.withCapabilities(new FirefoxOptions())
) {
webDriverContainer.start();

final List<InspectContainerResponse.Mount> mounts = webDriverContainer.getContainerInfo().getMounts();
assertEquals("Shm mounts present", mounts.size(), 1);
final List<InspectContainerResponse.Mount> shmVolumes = shmVolumes(webDriverContainer);

final InspectContainerResponse.Mount shmMount = mounts.get(0);
assertEquals("Shm mount source is correct", "/dev/shm", shmMount.getSource());
assertEquals("Shm mount destination is correct", "/dev/shm", shmMount.getDestination().getPath());
assertEquals("Shm mount mode is correct", shmMount.getMode(), "rw");
assertEquals("Only one shm mount present", 1, shmVolumes.size());
assertEquals("Shm mount source is correct", "/dev/shm", shmVolumes.get(0).getSource());
assertEquals("Shm mount mode is correct", "rw", shmVolumes.get(0).getMode());
}
}

@Test
public void createContainerWithoutShmVolume() {
try (
BrowserWebDriverContainer webDriverContainer = new BrowserWebDriverContainer<>()
.withSharedMemorySize(512 * FileUtils.ONE_MB)
BrowserWebDriverContainer webDriverContainer = new BrowserWebDriverContainer<>()
.withSharedMemorySize(512 * FileUtils.ONE_MB)
.withCapabilities(new FirefoxOptions())
) {
webDriverContainer.start();
assertEquals("Shared memory size is configured", 512 * FileUtils.ONE_MB, webDriverContainer.getShmSize());

final long shmMountCount = webDriverContainer.getContainerInfo().getMounts()
.stream()
.filter(m -> "/dev/shm".equals(m.getSource()))
.count();
assertEquals("No shm mounts present", shmMountCount, 0L);
assertEquals("Shared memory size is configured",
512 * FileUtils.ONE_MB,
webDriverContainer.getShmSize());

assertEquals("No shm mounts present", emptyList(), shmVolumes(webDriverContainer));
}
}

private List<InspectContainerResponse.Mount> shmVolumes(final BrowserWebDriverContainer container) {
return container.getContainerInfo().getMounts()
.stream()
// destination path is always /dev/shm
.filter(m -> m.getDestination().getPath().equals("/dev/shm"))
.collect(toList());
}
}

0 comments on commit 94c903d

Please sign in to comment.