-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Kafka module * Replace AmbassadorContainer with SocatContainer in DockerComposeContainer * make it possible to use KafkaContainer with external Zookeeper * fix typo * fix Kafka tests * Add to CHANGELOG.md, name SocatContainer, add listeners explanation comment to KafkaContainer * listen on alias * rename myNetworkAlias -> networkAlias
- Loading branch information
Showing
12 changed files
with
343 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
core/src/main/java/org/testcontainers/containers/SocatContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.testcontainers.containers; | ||
|
||
import org.testcontainers.utility.Base58; | ||
import org.testcontainers.utility.TestcontainersConfiguration; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* A socat container is used as a TCP proxy, enabling any TCP port of another container to be exposed | ||
* publicly, even if that container does not make the port public itself. | ||
*/ | ||
public class SocatContainer extends GenericContainer<SocatContainer> { | ||
|
||
private final Map<Integer, String> targets = new HashMap<>(); | ||
|
||
public SocatContainer() { | ||
super(TestcontainersConfiguration.getInstance().getSocatContainerImage()); | ||
withCreateContainerCmdModifier(it -> it.withEntrypoint("/bin/sh")); | ||
withCreateContainerCmdModifier(it -> it.withName("testcontainers-socat-" + Base58.randomString(8))); | ||
} | ||
|
||
public SocatContainer withTarget(int exposedPort, String host) { | ||
return withTarget(exposedPort, host, exposedPort); | ||
} | ||
|
||
public SocatContainer withTarget(int exposedPort, String host, int internalPort) { | ||
addExposedPort(exposedPort); | ||
targets.put(exposedPort, String.format("%s:%s", host, internalPort)); | ||
return self(); | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
withCommand("-c", | ||
targets.entrySet().stream() | ||
.map(entry -> "socat TCP-LISTEN:" + entry.getKey() + ",fork,reuseaddr TCP:" + entry.getValue()) | ||
.collect(Collectors.joining(" & ")) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>testcontainers-parent</artifactId> | ||
<version>0-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>kafka</artifactId> | ||
<name>TestContainers :: Apache Kafka</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>testcontainers</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.kafka</groupId> | ||
<artifactId>kafka-clients</artifactId> | ||
<version>1.0.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>3.8.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>23.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
</project> |
82 changes: 82 additions & 0 deletions
82
modules/kafka/src/main/java/org.testcontainers.containers/KafkaContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.testcontainers.containers; | ||
|
||
import org.testcontainers.utility.Base58; | ||
|
||
import java.util.stream.Stream; | ||
|
||
public class KafkaContainer extends GenericContainer<KafkaContainer> { | ||
|
||
public static final int KAFKA_PORT = 9092; | ||
|
||
public static final int ZOOKEEPER_PORT = 2181; | ||
|
||
protected String externalZookeeperConnect = null; | ||
|
||
protected SocatContainer proxy; | ||
|
||
public KafkaContainer() { | ||
this("4.0.0"); | ||
} | ||
|
||
public KafkaContainer(String confluentPlatformVersion) { | ||
super("confluentinc/cp-kafka:" + confluentPlatformVersion); | ||
|
||
withNetwork(Network.newNetwork()); | ||
String networkAlias = "kafka-" + Base58.randomString(6); | ||
withNetworkAliases(networkAlias); | ||
withExposedPorts(KAFKA_PORT); | ||
|
||
// Use two listeners with different names, it will force Kafka to communicate with itself via internal | ||
// listener when KAFKA_INTER_BROKER_LISTENER_NAME is set, otherwise Kafka will try to use the advertised listener | ||
withEnv("KAFKA_LISTENERS", "PLAINTEXT://0.0.0.0:9092,BROKER://" + networkAlias + ":9093"); | ||
withEnv("KAFKA_LISTENER_SECURITY_PROTOCOL_MAP", "BROKER:PLAINTEXT,PLAINTEXT:PLAINTEXT"); | ||
withEnv("KAFKA_INTER_BROKER_LISTENER_NAME", "BROKER"); | ||
|
||
withEnv("KAFKA_BROKER_ID", "1"); | ||
withEnv("KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR", "1"); | ||
withEnv("KAFKA_OFFSETS_TOPIC_NUM_PARTITIONS", "1"); | ||
withEnv("KAFKA_LOG_FLUSH_INTERVAL_MESSAGES", Long.MAX_VALUE + ""); | ||
} | ||
|
||
public KafkaContainer withEmbeddedZookeeper() { | ||
externalZookeeperConnect = null; | ||
return self(); | ||
} | ||
|
||
public KafkaContainer withExternalZookeeper(String connectString) { | ||
externalZookeeperConnect = connectString; | ||
return self(); | ||
} | ||
|
||
public String getBootstrapServers() { | ||
return String.format("PLAINTEXT://%s:%s", proxy.getContainerIpAddress(), proxy.getFirstMappedPort()); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
String networkAlias = getNetworkAliases().get(0); | ||
proxy = new SocatContainer() | ||
.withNetwork(getNetwork()) | ||
.withTarget(9092, networkAlias) | ||
.withTarget(2181, networkAlias); | ||
|
||
proxy.start(); | ||
withEnv("KAFKA_ADVERTISED_LISTENERS", "BROKER://" + networkAlias + ":9093,PLAINTEXT://" + proxy.getContainerIpAddress() + ":" + proxy.getFirstMappedPort()); | ||
|
||
if (externalZookeeperConnect != null) { | ||
withEnv("KAFKA_ZOOKEEPER_CONNECT", externalZookeeperConnect); | ||
} else { | ||
addExposedPort(ZOOKEEPER_PORT); | ||
withEnv("KAFKA_ZOOKEEPER_CONNECT", "localhost:2181"); | ||
withClasspathResourceMapping("tc-zookeeper.properties", "/zookeeper.properties", BindMode.READ_ONLY); | ||
withCommand("sh", "-c", "zookeeper-server-start /zookeeper.properties & /etc/confluent/docker/run"); | ||
} | ||
|
||
super.start(); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
Stream.<Runnable>of(super::stop, proxy::stop).parallel().forEach(Runnable::run); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
clientPort=2181 | ||
dataDir=/var/lib/zookeeper/data | ||
dataLogDir=/var/lib/zookeeper/log |
Oops, something went wrong.