Skip to content

Commit

Permalink
Register default network alias using ContainerDef (#7861)
Browse files Browse the repository at this point in the history
Changes in 1dba8d1 doesn't register a default network alias
when implementing the new `ContainerDef`. This impacts to
kafka and mongodb modules.

Fixes #7823
  • Loading branch information
eddumelendez authored Nov 21, 2023
1 parent 696481e commit 832d943
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,7 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
private ContainerDef containerDef;

ContainerDef createContainerDef() {
ContainerDef def = new ContainerDef();
def.addNetworkAlias("tc-" + Base58.randomString(8));
return def;
return new ContainerDef();
}

ContainerDef getContainerDef() {
Expand All @@ -242,6 +240,7 @@ public GenericContainer(@NonNull final DockerImageName dockerImageName) {
public GenericContainer(@NonNull final RemoteDockerImage image) {
this.image = image;
this.containerDef = createContainerDef();
this.containerDef.addNetworkAlias("tc-" + Base58.randomString(8));
this.containerDef.setImage(image);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.testcontainers.images.RemoteDockerImage;
import org.testcontainers.images.builder.ImageFromDockerfile;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;

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

@Test
public void shouldContainDefaultNetworkAlias() {
try (GenericContainer<?> container = new GenericContainer<>("testcontainers/helloworld:1.1.0")) {
container.start();
assertThat(container.getNetworkAliases()).hasSize(1);
}
}

@Test
public void shouldContainDefaultNetworkAliasWhenUsingGenericContainer() {
try (HelloWorldContainer container = new HelloWorldContainer("testcontainers/helloworld:1.1.0")) {
container.start();
assertThat(container.getNetworkAliases()).hasSize(1);
}
}

@Test
public void shouldContainDefaultNetworkAliasWhenUsingContainerDef() {
try (TcHelloWorldContainer container = new TcHelloWorldContainer("testcontainers/helloworld:1.1.0")) {
container.start();
assertThat(container.getNetworkAliases()).hasSize(1);
}
}

static class NoopStartupCheckStrategy extends StartupCheckStrategy {

@Override
Expand Down Expand Up @@ -267,4 +292,31 @@ protected void waitUntilReady() {
throw new IllegalStateException("Nope!");
}
}

static class HelloWorldContainer extends GenericContainer<HelloWorldContainer> {

public HelloWorldContainer(String image) {
super(DockerImageName.parse(image));
withExposedPorts(8080);
}
}

static class TcHelloWorldContainer extends GenericContainer<HelloWorldContainer> {

public TcHelloWorldContainer(String image) {
super(DockerImageName.parse(image));
}

@Override
ContainerDef createContainerDef() {
return new HelloWorldContainerDef();
}

class HelloWorldContainerDef extends ContainerDef {

HelloWorldContainerDef() {
addExposedTcpPort(8080);
}
}
}
}

0 comments on commit 832d943

Please sign in to comment.