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

Convert Docs for Kafka Module #1158 #2925

Merged
merged 5 commits into from
Jun 29, 2020
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
36 changes: 20 additions & 16 deletions docs/modules/kafka.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,45 @@ More precisely Testcontainers uses the official Docker images for [Confluent OSS
## Example

The following field in your JUnit test class will prepare a container running Kafka:
```java
@Rule
public KafkaContainer kafka = new KafkaContainer();
```
<!--codeinclude-->
[JUnit Rule](../../modules/kafka/src/test/java/org/testcontainers/containers/KafkaContainerTest.java) inside_block:junitRule
<!--/codeinclude-->

Now your tests or any other process running on your machine can get access to running Kafka broker by using the following bootstrap server location:
```java
kafka.getBootstrapServers()
```

<!--codeinclude-->
[Bootstrap Servers](../../modules/kafka/src/test/java/org/testcontainers/containers/KafkaContainerTest.java) inside_block:getBootstrapServers
<!--/codeinclude-->


## Options

### Selecting Kafka version

You can select a version of Confluent Platform by passing it to the container's constructor:
```java
new KafkaContainer("4.1.2")
```
<!--codeinclude-->
[Version Constructor](../../modules/kafka/src/test/java/org/testcontainers/containers/KafkaContainerTest.java) inside_block:constructorWithVersion
<!--/codeinclude-->


The correspondence between Confluent Platform versions and Kafka versions can be seen [in Confluent documentation](https://docs.confluent.io/current/installation/versions-interoperability.html#cp-and-apache-kafka-compatibility)

### <a name="zookeeper"></a> Using external Zookeeper

If for some reason you want to use an externally running Zookeeper, then just pass its location during construction:
```java
new KafkaContainer().withExternalZookeeper("localhost:2181")
```
<!--codeinclude-->
[External Zookeeper](../../modules/kafka/src/test/java/org/testcontainers/containers/KafkaContainerTest.java) inside_block:withExternalZookeeper
<!--/codeinclude-->


## Multi-container usage

If your test needs to run some other Docker container which needs access to the Kafka, do the following:

* Run your other container on the same network as Kafka container, e.g.:
```java
new GenericContainer("myImage").withNetwork(kafka.getNetwork())
```
<!--codeinclude-->
[Network](../../modules/kafka/src/test/java/org/testcontainers/containers/KafkaContainerTest.java) inside_block:withKafkaNetwork
<!--/codeinclude-->
* Use `kafka.getNetworkAliases().get(0)+":9092"` as bootstrap server location.
Or just give your Kafka container a network alias of your liking.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.junit.Rule;
import org.junit.Test;
import org.rnorth.ducttape.unreliables.Unreliables;

Expand All @@ -23,6 +24,11 @@

public class KafkaContainerTest {

// junitRule {
@Rule
public KafkaContainer kafka = new KafkaContainer();
// }

@Test
public void testUsage() throws Exception {
try (KafkaContainer kafka = new KafkaContainer()) {
Expand All @@ -31,22 +37,48 @@ public void testUsage() throws Exception {
}
}


@Test
public void testUsageWithVersion() throws Exception {
try (
// constructorWithVersion {
KafkaContainer kafka = new KafkaContainer("4.1.2")
// }
) {
kafka.start();
testKafkaFunctionality(
// getBootstrapServers {
kafka.getBootstrapServers()
// }
);
}
}

@Test
public void testExternalZookeeperWithExternalNetwork() throws Exception {
try (
Network network = Network.newNetwork();

// withExternalZookeeper {
KafkaContainer kafka = new KafkaContainer()
.withNetwork(network)
.withExternalZookeeper("zookeeper:2181");
// }

GenericContainer zookeeper = new GenericContainer("confluentinc/cp-zookeeper:4.0.0")
.withNetwork(network)
.withNetworkAliases("zookeeper")
.withEnv("ZOOKEEPER_CLIENT_PORT", "2181");

// withKafkaNetwork {
GenericContainer application = new GenericContainer("alpine").withNetwork(kafka.getNetwork())
// }
.withNetworkAliases("dummy")
.withCommand("sleep 10000")
) {
zookeeper.start();
kafka.start();
application.start();

testKafkaFunctionality(kafka.getBootstrapServers());
}
Expand Down