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

Register default network alias using ContainerDef #7861

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}
Loading