Skip to content

Commit 832d943

Browse files
authored
Register default network alias using ContainerDef (#7861)
Changes in 1dba8d1 doesn't register a default network alias when implementing the new `ContainerDef`. This impacts to kafka and mongodb modules. Fixes #7823
1 parent 696481e commit 832d943

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,7 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
215215
private ContainerDef containerDef;
216216

217217
ContainerDef createContainerDef() {
218-
ContainerDef def = new ContainerDef();
219-
def.addNetworkAlias("tc-" + Base58.randomString(8));
220-
return def;
218+
return new ContainerDef();
221219
}
222220

223221
ContainerDef getContainerDef() {
@@ -242,6 +240,7 @@ public GenericContainer(@NonNull final DockerImageName dockerImageName) {
242240
public GenericContainer(@NonNull final RemoteDockerImage image) {
243241
this.image = image;
244242
this.containerDef = createContainerDef();
243+
this.containerDef.addNetworkAlias("tc-" + Base58.randomString(8));
245244
this.containerDef.setImage(image);
246245
}
247246

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.testcontainers.images.RemoteDockerImage;
2424
import org.testcontainers.images.builder.ImageFromDockerfile;
2525
import org.testcontainers.images.builder.Transferable;
26+
import org.testcontainers.utility.DockerImageName;
2627
import org.testcontainers.utility.MountableFile;
2728

2829
import java.util.Arrays;
@@ -231,6 +232,30 @@ public void shouldReturnTheProvidedImage() {
231232
assertThat(container.getImage().get()).isEqualTo("alpine:3.16");
232233
}
233234

235+
@Test
236+
public void shouldContainDefaultNetworkAlias() {
237+
try (GenericContainer<?> container = new GenericContainer<>("testcontainers/helloworld:1.1.0")) {
238+
container.start();
239+
assertThat(container.getNetworkAliases()).hasSize(1);
240+
}
241+
}
242+
243+
@Test
244+
public void shouldContainDefaultNetworkAliasWhenUsingGenericContainer() {
245+
try (HelloWorldContainer container = new HelloWorldContainer("testcontainers/helloworld:1.1.0")) {
246+
container.start();
247+
assertThat(container.getNetworkAliases()).hasSize(1);
248+
}
249+
}
250+
251+
@Test
252+
public void shouldContainDefaultNetworkAliasWhenUsingContainerDef() {
253+
try (TcHelloWorldContainer container = new TcHelloWorldContainer("testcontainers/helloworld:1.1.0")) {
254+
container.start();
255+
assertThat(container.getNetworkAliases()).hasSize(1);
256+
}
257+
}
258+
234259
static class NoopStartupCheckStrategy extends StartupCheckStrategy {
235260

236261
@Override
@@ -267,4 +292,31 @@ protected void waitUntilReady() {
267292
throw new IllegalStateException("Nope!");
268293
}
269294
}
295+
296+
static class HelloWorldContainer extends GenericContainer<HelloWorldContainer> {
297+
298+
public HelloWorldContainer(String image) {
299+
super(DockerImageName.parse(image));
300+
withExposedPorts(8080);
301+
}
302+
}
303+
304+
static class TcHelloWorldContainer extends GenericContainer<HelloWorldContainer> {
305+
306+
public TcHelloWorldContainer(String image) {
307+
super(DockerImageName.parse(image));
308+
}
309+
310+
@Override
311+
ContainerDef createContainerDef() {
312+
return new HelloWorldContainerDef();
313+
}
314+
315+
class HelloWorldContainerDef extends ContainerDef {
316+
317+
HelloWorldContainerDef() {
318+
addExposedTcpPort(8080);
319+
}
320+
}
321+
}
270322
}

0 commit comments

Comments
 (0)