Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez committed Nov 5, 2024
1 parent deb2e3f commit b4a4e0f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,7 @@ private void tryStart() {
copyToTransferableContainerPathMap.forEach(this::copyFileToContainer);
}

if (!reused) {
connectToPortForwardingNetwork(createCommand.getNetworkMode());
}
connectToPortForwardingNetwork(createCommand.getNetworkMode());

if (!reused) {
containerIsCreated(containerId);
Expand Down Expand Up @@ -624,7 +622,14 @@ private void connectToPortForwardingNetwork(String networkMode) {
.map(ContainerNetwork::getNetworkID)
.ifPresent(networkId -> {
if (!Arrays.asList(networkId, "none", "host").contains(networkMode)) {
dockerClient.connectToNetworkCmd().withContainerId(containerId).withNetworkId(networkId).exec();
com.github.dockerjava.api.model.Network network =
this.dockerClient.inspectNetworkCmd().withNetworkId(networkId).exec();
if (!network.getContainers().containsKey(this.containerId)) {
this.dockerClient.connectToNetworkCmd()
.withContainerId(this.containerId)
.withNetworkId(networkId)
.exec();
}
}
});
}
Expand Down Expand Up @@ -828,7 +833,7 @@ private void applyConfiguration(CreateContainerCmd createCommand) {
withExtraHost(INTERNAL_HOST_HOSTNAME, it.getIpAddress());
});

String[] extraHostsArray = extraHosts.stream().toArray(String[]::new);
String[] extraHostsArray = extraHosts.stream().distinct().toArray(String[]::new);
createCommand.withExtraHosts(extraHostsArray);

if (workingDirectory != null) {
Expand Down
118 changes: 46 additions & 72 deletions core/src/test/java/org/testcontainers/containers/ExposedHostTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -94,59 +95,49 @@ public void testExposedHostPortOnFixedInternalPorts() {
public void testExposedHostWithReusableContainerAndFixedNetworkName() throws IOException, InterruptedException {
Testcontainers.exposeHostPorts(server.getAddress().getPort());

try (
GenericContainer<?> container = new GenericContainer<>(tinyContainerDef())
.withReuse(true)
.withNetwork(network)
) {
container.start();
GenericContainer<?> container = new GenericContainer<>(tinyContainerDef()).withReuse(true).withNetwork(network);
container.start();

assertHttpResponseFromHost(container, server.getAddress().getPort());
assertHttpResponseFromHost(container, server.getAddress().getPort());

PortForwardingContainer.INSTANCE.reset();
Testcontainers.exposeHostPorts(server.getAddress().getPort());
PortForwardingContainer.INSTANCE.reset();
Testcontainers.exposeHostPorts(server.getAddress().getPort());

try (
GenericContainer<?> reusedContainer = new GenericContainer<>(tinyContainerDef())
.withReuse(true)
.withNetwork(network)
) {
reusedContainer.start();
GenericContainer<?> reusedContainer = new GenericContainer<>(tinyContainerDef())
.withReuse(true)
.withNetwork(network);
reusedContainer.start();

assertThat(reusedContainer.getContainerId()).isEqualTo(container.getContainerId());
assertHttpResponseFromHost(reusedContainer, server.getAddress().getPort());
}
}
assertThat(reusedContainer.getContainerId()).isEqualTo(container.getContainerId());
assertHttpResponseFromHost(reusedContainer, server.getAddress().getPort());

container.stop();
reusedContainer.stop();
}

@Test
public void testExposedHostOnFixedInternalPortsWithReusableContainerAndFixedNetworkName()
throws IOException, InterruptedException {
Testcontainers.exposeHostPorts(ImmutableMap.of(server.getAddress().getPort(), 1234));

try (
GenericContainer<?> container = new GenericContainer<>(tinyContainerDef())
.withReuse(true)
.withNetwork(network)
) {
container.start();
GenericContainer<?> container = new GenericContainer<>(tinyContainerDef()).withReuse(true).withNetwork(network);
container.start();

assertHttpResponseFromHost(container, 1234);
assertHttpResponseFromHost(container, 1234);

PortForwardingContainer.INSTANCE.reset();
Testcontainers.exposeHostPorts(ImmutableMap.of(server.getAddress().getPort(), 1234));
PortForwardingContainer.INSTANCE.reset();
Testcontainers.exposeHostPorts(ImmutableMap.of(server.getAddress().getPort(), 1234));

try (
GenericContainer<?> reusedContainer = new GenericContainer<>(tinyContainerDef())
.withReuse(true)
.withNetwork(network)
) {
reusedContainer.start();
GenericContainer<?> reusedContainer = new GenericContainer<>(tinyContainerDef())
.withReuse(true)
.withNetwork(network);
reusedContainer.start();

assertThat(reusedContainer.getContainerId()).isEqualTo(container.getContainerId());
assertHttpResponseFromHost(reusedContainer, 1234);
}
}
assertThat(reusedContainer.getContainerId()).isEqualTo(container.getContainerId());
assertHttpResponseFromHost(reusedContainer, 1234);

container.stop();
reusedContainer.stop();
}

@SneakyThrows
Expand Down Expand Up @@ -185,47 +176,30 @@ private void assertHttpResponseFromHost(GenericContainer<?> container, int port)
}

private static Network createReusableNetwork(UUID name) {
String id = DockerClientFactory
.instance()
.client()
.listNetworksCmd()
.exec()
.stream()
.filter(network -> {
return (
network.getName().equals(name.toString()) &&
network.getLabels().equals(DockerClientFactory.DEFAULT_LABELS)
);
})
.map(com.github.dockerjava.api.model.Network::getId)
.findFirst()
.orElseGet(() -> {
return DockerClientFactory
.instance()
.client()
.createNetworkCmd()
.withName(name.toString())
.withCheckDuplicate(true)
.withLabels(DockerClientFactory.DEFAULT_LABELS)
.exec()
.getId();
});

return new Network() {
String networkName = name.toString();
Network network = new Network() {
@Override
public Statement apply(Statement base, Description description) {
return base;
public String getId() {
return networkName;
}

@Override
public String getId() {
return id;
}
public void close() {}

@Override
public void close() {
// never close
public Statement apply(Statement base, Description description) {
return null;
}
};

List<com.github.dockerjava.api.model.Network> networks = DockerClientFactory
.lazyClient()
.listNetworksCmd()
.withNameFilter(networkName)
.exec();
if (networks.isEmpty()) {
Network.builder().createNetworkCmdModifier(cmd -> cmd.withName(networkName)).build().getId();
}
return network;
}
}

0 comments on commit b4a4e0f

Please sign in to comment.