Skip to content

Commit

Permalink
Avoid setting container config on doStart method (#33)
Browse files Browse the repository at this point in the history
Signed-off-by: Ozan Gunalp <[email protected]>
  • Loading branch information
ozangunalp authored Mar 2, 2022
1 parent 436a534 commit ebc6037
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -87,6 +86,12 @@ public StrimziKafkaContainer(String dockerImageName) {
private StrimziKafkaContainer(CompletableFuture<String> imageName) {
super(imageName);
this.imageNameProvider = imageName;
// we need this shared network in case we deploy StrimziKafkaCluster which consist of `StrimziKafkaContainer`
// instances and by default each container has its own network, which results in `Unable to resolve address: zookeeper:2181`
super.setNetwork(Network.SHARED);
// exposing kafka port from the container
super.setExposedPorts(Collections.singletonList(KAFKA_PORT));
super.addEnv("LOG_DIR", "/tmp");
}

@Override
Expand All @@ -103,16 +108,10 @@ protected void doStart() {
throw new RuntimeException(e);
}
// exposing Kafka and port from the container
if (this.hasKraftOrExternalZooKeeperConfigured()) {
super.setExposedPorts(Collections.singletonList(KAFKA_PORT));
} else {
if (!this.hasKraftOrExternalZooKeeperConfigured()) {
// expose internal ZooKeeper internal port iff external ZooKeeper or KRaft is not specified/enabled
super.setExposedPorts(Arrays.asList(KAFKA_PORT, StrimziZookeeperContainer.ZOOKEEPER_PORT));
super.addExposedPort(StrimziZookeeperContainer.ZOOKEEPER_PORT);
}
// we need this shared network in case we deploy StrimziKafkaCluster which consist of `StrimziKafkaContainer`
// instances and by default each container has its own network, which results in `Unable to resolve address: zookeeper:2181`
super.setNetwork(Network.SHARED);
super.addEnv("LOG_DIR", "/tmp");
// we need it for the startZookeeper(); and startKafka(); to run container before...
super.setCommand("sh", "-c", runStarterScript());
super.doStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ public StrimziZookeeperContainer(String dockerImageName) {
private StrimziZookeeperContainer(CompletableFuture<String> imageName) {
super(imageName);
this.imageNameProvider = imageName;
}

@Override
protected void doStart() {
if (!imageNameProvider.isDone()) {
imageNameProvider.complete(KafkaVersionService.strimziTestContainerImageName(kafkaVersion));
}
// we need this shared network in case we deploy StrimziKafkaCluster, which consist `StrimziZookeeperContainer`
// instance and by default each container has its own network
super.setNetwork(Network.SHARED);
Expand All @@ -80,6 +73,13 @@ protected void doStart() {
super.addEnv("ZOOKEEPER_CLIENT_PORT", String.valueOf(ZOOKEEPER_PORT));
// env for readiness
super.addEnv("ZOO_4LW_COMMANDS_WHITELIST", "ruok");
}

@Override
protected void doStart() {
if (!imageNameProvider.isDone()) {
imageNameProvider.complete(KafkaVersionService.strimziTestContainerImageName(kafkaVersion));
}
// we need it for the startZookeeper(); and startKafka(); to run container before...
withCommand("sh", "-c", "while [ ! -f " + STARTER_SCRIPT + " ]; do sleep 0.1; done; " + STARTER_SCRIPT);
super.doStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.Container;
import org.testcontainers.containers.Network;
import org.testcontainers.utility.MountableFile;

import java.io.IOException;
Expand Down Expand Up @@ -171,6 +172,25 @@ void testStartContainerWithCustomImage() {
systemUnderTest.stop();
}

@Test
void testStartContainerWithCustomNetwork() {
assumeDocker();

Network network = Network.newNetwork();

systemUnderTest = new StrimziKafkaContainer()
.withNetwork(network)
.waitForRunning();

systemUnderTest.start();

assertThat(systemUnderTest.getBootstrapServers(), is("PLAINTEXT://"
+ systemUnderTest.getContainerIpAddress() + ":" + systemUnderTest.getMappedPort(9092)));

assertThat(systemUnderTest.getNetwork().getId(), is(network.getId()));
systemUnderTest.stop();
}

@Test
void testUnsupportedKafkaVersion() {
assumeDocker();
Expand Down

0 comments on commit ebc6037

Please sign in to comment.