Skip to content

Commit b4a4e0f

Browse files
committed
Polish
1 parent deb2e3f commit b4a4e0f

File tree

2 files changed

+56
-77
lines changed

2 files changed

+56
-77
lines changed

core/src/main/java/org/testcontainers/containers/GenericContainer.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,7 @@ private void tryStart() {
435435
copyToTransferableContainerPathMap.forEach(this::copyFileToContainer);
436436
}
437437

438-
if (!reused) {
439-
connectToPortForwardingNetwork(createCommand.getNetworkMode());
440-
}
438+
connectToPortForwardingNetwork(createCommand.getNetworkMode());
441439

442440
if (!reused) {
443441
containerIsCreated(containerId);
@@ -624,7 +622,14 @@ private void connectToPortForwardingNetwork(String networkMode) {
624622
.map(ContainerNetwork::getNetworkID)
625623
.ifPresent(networkId -> {
626624
if (!Arrays.asList(networkId, "none", "host").contains(networkMode)) {
627-
dockerClient.connectToNetworkCmd().withContainerId(containerId).withNetworkId(networkId).exec();
625+
com.github.dockerjava.api.model.Network network =
626+
this.dockerClient.inspectNetworkCmd().withNetworkId(networkId).exec();
627+
if (!network.getContainers().containsKey(this.containerId)) {
628+
this.dockerClient.connectToNetworkCmd()
629+
.withContainerId(this.containerId)
630+
.withNetworkId(networkId)
631+
.exec();
632+
}
628633
}
629634
});
630635
}
@@ -828,7 +833,7 @@ private void applyConfiguration(CreateContainerCmd createCommand) {
828833
withExtraHost(INTERNAL_HOST_HOSTNAME, it.getIpAddress());
829834
});
830835

831-
String[] extraHostsArray = extraHosts.stream().toArray(String[]::new);
836+
String[] extraHostsArray = extraHosts.stream().distinct().toArray(String[]::new);
832837
createCommand.withExtraHosts(extraHostsArray);
833838

834839
if (workingDirectory != null) {

core/src/test/java/org/testcontainers/containers/ExposedHostTest.java

Lines changed: 46 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.IOException;
1717
import java.io.OutputStream;
1818
import java.net.InetSocketAddress;
19+
import java.util.List;
1920
import java.util.UUID;
2021

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

97-
try (
98-
GenericContainer<?> container = new GenericContainer<>(tinyContainerDef())
99-
.withReuse(true)
100-
.withNetwork(network)
101-
) {
102-
container.start();
98+
GenericContainer<?> container = new GenericContainer<>(tinyContainerDef()).withReuse(true).withNetwork(network);
99+
container.start();
103100

104-
assertHttpResponseFromHost(container, server.getAddress().getPort());
101+
assertHttpResponseFromHost(container, server.getAddress().getPort());
105102

106-
PortForwardingContainer.INSTANCE.reset();
107-
Testcontainers.exposeHostPorts(server.getAddress().getPort());
103+
PortForwardingContainer.INSTANCE.reset();
104+
Testcontainers.exposeHostPorts(server.getAddress().getPort());
108105

109-
try (
110-
GenericContainer<?> reusedContainer = new GenericContainer<>(tinyContainerDef())
111-
.withReuse(true)
112-
.withNetwork(network)
113-
) {
114-
reusedContainer.start();
106+
GenericContainer<?> reusedContainer = new GenericContainer<>(tinyContainerDef())
107+
.withReuse(true)
108+
.withNetwork(network);
109+
reusedContainer.start();
115110

116-
assertThat(reusedContainer.getContainerId()).isEqualTo(container.getContainerId());
117-
assertHttpResponseFromHost(reusedContainer, server.getAddress().getPort());
118-
}
119-
}
111+
assertThat(reusedContainer.getContainerId()).isEqualTo(container.getContainerId());
112+
assertHttpResponseFromHost(reusedContainer, server.getAddress().getPort());
113+
114+
container.stop();
115+
reusedContainer.stop();
120116
}
121117

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

127-
try (
128-
GenericContainer<?> container = new GenericContainer<>(tinyContainerDef())
129-
.withReuse(true)
130-
.withNetwork(network)
131-
) {
132-
container.start();
123+
GenericContainer<?> container = new GenericContainer<>(tinyContainerDef()).withReuse(true).withNetwork(network);
124+
container.start();
133125

134-
assertHttpResponseFromHost(container, 1234);
126+
assertHttpResponseFromHost(container, 1234);
135127

136-
PortForwardingContainer.INSTANCE.reset();
137-
Testcontainers.exposeHostPorts(ImmutableMap.of(server.getAddress().getPort(), 1234));
128+
PortForwardingContainer.INSTANCE.reset();
129+
Testcontainers.exposeHostPorts(ImmutableMap.of(server.getAddress().getPort(), 1234));
138130

139-
try (
140-
GenericContainer<?> reusedContainer = new GenericContainer<>(tinyContainerDef())
141-
.withReuse(true)
142-
.withNetwork(network)
143-
) {
144-
reusedContainer.start();
131+
GenericContainer<?> reusedContainer = new GenericContainer<>(tinyContainerDef())
132+
.withReuse(true)
133+
.withNetwork(network);
134+
reusedContainer.start();
145135

146-
assertThat(reusedContainer.getContainerId()).isEqualTo(container.getContainerId());
147-
assertHttpResponseFromHost(reusedContainer, 1234);
148-
}
149-
}
136+
assertThat(reusedContainer.getContainerId()).isEqualTo(container.getContainerId());
137+
assertHttpResponseFromHost(reusedContainer, 1234);
138+
139+
container.stop();
140+
reusedContainer.stop();
150141
}
151142

152143
@SneakyThrows
@@ -185,47 +176,30 @@ private void assertHttpResponseFromHost(GenericContainer<?> container, int port)
185176
}
186177

187178
private static Network createReusableNetwork(UUID name) {
188-
String id = DockerClientFactory
189-
.instance()
190-
.client()
191-
.listNetworksCmd()
192-
.exec()
193-
.stream()
194-
.filter(network -> {
195-
return (
196-
network.getName().equals(name.toString()) &&
197-
network.getLabels().equals(DockerClientFactory.DEFAULT_LABELS)
198-
);
199-
})
200-
.map(com.github.dockerjava.api.model.Network::getId)
201-
.findFirst()
202-
.orElseGet(() -> {
203-
return DockerClientFactory
204-
.instance()
205-
.client()
206-
.createNetworkCmd()
207-
.withName(name.toString())
208-
.withCheckDuplicate(true)
209-
.withLabels(DockerClientFactory.DEFAULT_LABELS)
210-
.exec()
211-
.getId();
212-
});
213-
214-
return new Network() {
179+
String networkName = name.toString();
180+
Network network = new Network() {
215181
@Override
216-
public Statement apply(Statement base, Description description) {
217-
return base;
182+
public String getId() {
183+
return networkName;
218184
}
219185

220186
@Override
221-
public String getId() {
222-
return id;
223-
}
187+
public void close() {}
224188

225189
@Override
226-
public void close() {
227-
// never close
190+
public Statement apply(Statement base, Description description) {
191+
return null;
228192
}
229193
};
194+
195+
List<com.github.dockerjava.api.model.Network> networks = DockerClientFactory
196+
.lazyClient()
197+
.listNetworksCmd()
198+
.withNameFilter(networkName)
199+
.exec();
200+
if (networks.isEmpty()) {
201+
Network.builder().createNetworkCmdModifier(cmd -> cmd.withName(networkName)).build().getId();
202+
}
203+
return network;
230204
}
231205
}

0 commit comments

Comments
 (0)