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

hostPort != containerPort #1334

Merged
merged 13 commits into from
Mar 29, 2019
10 changes: 10 additions & 0 deletions core/src/main/java/org/testcontainers/Testcontainers.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.testcontainers;

import java.util.Map;
import java.util.Map.Entry;

import lombok.experimental.UtilityClass;
bsideup marked this conversation as resolved.
Show resolved Hide resolved

import org.testcontainers.containers.PortForwardingContainer;

@UtilityClass
Expand All @@ -11,4 +15,10 @@ public void exposeHostPorts(int... ports) {
PortForwardingContainer.INSTANCE.exposeHostPort(port);
}
}

public void exposeHostPorts(Map<Integer, Integer> ports) {
for(Entry<Integer, Integer> entry : ports.entrySet()) {
PortForwardingContainer.INSTANCE.exposeHostPort(entry.getKey(), entry.getValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
import org.testcontainers.utility.TestcontainersConfiguration;

import java.time.Duration;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

public enum PortForwardingContainer {
INSTANCE;

private GenericContainer container;

private final Set<Integer> exposedPorts = Collections.newSetFromMap(new ConcurrentHashMap<>());
private final Set<Entry<Integer, Integer>> exposedPorts = Collections.newSetFromMap(new ConcurrentHashMap<>());

@Getter(value = AccessLevel.PRIVATE, lazy = true)
private final Connection sshConnection = createSSHSession();
Expand Down Expand Up @@ -56,8 +58,13 @@ private Connection createSSHSession() {

@SneakyThrows
public void exposeHostPort(int port) {
if (exposedPorts.add(port)) {
getSshConnection().requestRemotePortForwarding("", port, "localhost", port);
exposeHostPort(port, port);
}

@SneakyThrows
public void exposeHostPort(int hostPort, int containerPort) {
bsideup marked this conversation as resolved.
Show resolved Hide resolved
if (exposedPorts.add(new AbstractMap.SimpleEntry<>(hostPort, containerPort))) {
getSshConnection().requestRemotePortForwarding("", containerPort, "localhost", hostPort);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;

import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;

Expand All @@ -29,7 +31,10 @@ public static void setUpClass() throws Exception {
});

server.start();
Testcontainers.exposeHostPorts(server.getAddress().getPort());

Map<Integer, Integer> portMapping = new HashMap<>();
portMapping.put(server.getAddress().getPort(), 80);
bopi- marked this conversation as resolved.
Show resolved Hide resolved
Testcontainers.exposeHostPorts(portMapping);
}

@AfterClass
Expand All @@ -54,7 +59,7 @@ protected void assertResponse(GenericContainer container) {
try {
container.start();

String response = container.execInContainer("wget", "-O", "-", "http://host.testcontainers.internal:" + server.getAddress().getPort()).getStdout();
String response = container.execInContainer("wget", "-O", "-", "http://host.testcontainers.internal").getStdout();
bopi- marked this conversation as resolved.
Show resolved Hide resolved

assertEquals("received response", "Hello World!", response);
} finally {
Expand Down