Skip to content

Commit 1ef2f84

Browse files
committed
feat(kafka_native): new module
1 parent c100f88 commit 1ef2f84

File tree

11 files changed

+843
-0
lines changed

11 files changed

+843
-0
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ updates:
4242
- /modules/k3s
4343
- /modules/k6
4444
- /modules/kafka
45+
- /modules/kafka_native
4546
- /modules/localstack
4647
- /modules/mariadb
4748
- /modules/meilisearch

docs/modules/kafka_native.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Kafka Native
2+
3+
Since <a href="https://github.com/testcontainers/testcontainers-go/releases/tag/v0.39.0"><span class="tc-version">:material-tag: v0.39.0</span></a>
4+
5+
## Introduction
6+
7+
The Testcontainers module for [Apache Kafka Native](https://hub.docker.com/r/apache/kafka-native).
8+
9+
## Adding this module to your project dependencies
10+
11+
Please run the following command to add the Kafka module to your Go dependencies:
12+
13+
```
14+
go get github.com/testcontainers/testcontainers-go/modules/kafka_native
15+
```
16+
17+
## Usage example
18+
19+
<!--codeinclude-->
20+
[Creating a Kafka container](../../modules/kafka_native/examples_test.go) inside_block:runKafkaContainer
21+
<!--/codeinclude-->
22+
23+
## Module Reference
24+
25+
### Run function
26+
27+
The Kafka module exposes one entrypoint function to create the Kafka container, and this function receives three parameters:
28+
29+
```golang
30+
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*KafkaContainer, error)
31+
```
32+
33+
- `context.Context`, the Go context.
34+
- `string`, the Docker image to use.
35+
- `testcontainers.ContainerCustomizer`, a variadic argument for passing options.
36+
37+
#### Image
38+
39+
Use the second argument in the `Run` function to set a valid Docker image.
40+
In example: `Run(context.Background(), "apache/kafka-native:3.9.1")`.
41+
42+
#### Environment variables
43+
44+
The environment variables that are already set by default are:
45+
46+
<!--codeinclude-->
47+
[Environment variables](../../modules/kafka_native/kafka.go) inside_block:envVars
48+
<!--/codeinclude-->
49+
50+
And also KAFKA_ADVERTISED_LISTENERS that is defined dynamically based on the container's hostname.
51+
52+
#### Init script
53+
54+
The Kafka container will be started using a custom shell script:
55+
56+
<!--codeinclude-->
57+
[Init script](../../modules/kafka_native/kafka.go) inside_block:starterScriptContentText
58+
<!--/codeinclude-->
59+
60+
### Container Options
61+
62+
When starting the Kafka container, you can pass options in a variadic way to configure it.
63+
64+
{% include "../features/common_functional_options_list.md" %}
65+
66+
### Container Methods
67+
68+
The Kafka container exposes the following methods:
69+
70+
#### Brokers
71+
72+
The `Brokers(ctx)` method returns the Kafka brokers as a string slice, containing the host and the random port defined by Kafka's public port (`9093/tcp`).
73+
74+
<!--codeinclude-->
75+
[Get Kafka brokers](../../modules/kafka_native/kafka_test.go) inside_block:getBrokers
76+
<!--/codeinclude-->

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ nav:
9393
- modules/k3s.md
9494
- modules/k6.md
9595
- modules/kafka.md
96+
- modules/kafka_native.md
9697
- modules/localstack.md
9798
- modules/mariadb.md
9899
- modules/meilisearch.md

modules/kafka_native/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include ../../commons-test.mk
2+
3+
.PHONY: test
4+
test:
5+
$(MAKE) test-kafka-native
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package kafka_native_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/IBM/sarama"
7+
)
8+
9+
// TestKafkaConsumer is a test consumer for Kafka
10+
type TestKafkaConsumer struct {
11+
t *testing.T
12+
ready chan bool
13+
done chan bool
14+
cancel chan bool
15+
message *sarama.ConsumerMessage
16+
}
17+
18+
func NewTestKafkaConsumer(t *testing.T) (*TestKafkaConsumer, <-chan bool, <-chan bool, func()) {
19+
t.Helper()
20+
kc := &TestKafkaConsumer{
21+
t: t,
22+
ready: make(chan bool, 1),
23+
done: make(chan bool, 1),
24+
cancel: make(chan bool, 1),
25+
}
26+
return kc, kc.ready, kc.done, func() {
27+
kc.cancel <- true
28+
}
29+
}
30+
31+
func (k *TestKafkaConsumer) Setup(_ sarama.ConsumerGroupSession) error {
32+
return nil
33+
}
34+
35+
func (k *TestKafkaConsumer) Cleanup(_ sarama.ConsumerGroupSession) error {
36+
return nil
37+
}
38+
39+
// ConsumeClaim is called by the Kafka client library when a message is received
40+
func (k *TestKafkaConsumer) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
41+
k.ready <- true
42+
for {
43+
select {
44+
case message := <-claim.Messages():
45+
k.message = message
46+
session.MarkMessage(message, "")
47+
k.done <- true
48+
49+
case <-k.cancel:
50+
return nil
51+
52+
case <-session.Context().Done():
53+
return nil
54+
}
55+
}
56+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package kafka_native_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
8+
"github.com/testcontainers/testcontainers-go"
9+
"github.com/testcontainers/testcontainers-go/modules/kafka_native"
10+
)
11+
12+
func ExampleRun() {
13+
// runKafkaContainer {
14+
ctx := context.Background()
15+
16+
kafkaContainer, err := kafka_native.Run(ctx,
17+
"apache/kafka-native:3.9.1",
18+
kafka_native.WithClusterID("test-cluster"),
19+
)
20+
defer func() {
21+
if err := testcontainers.TerminateContainer(kafkaContainer); err != nil {
22+
log.Printf("failed to terminate container: %s", err)
23+
}
24+
}()
25+
if err != nil {
26+
log.Printf("failed to start container: %s", err)
27+
return
28+
}
29+
// }
30+
31+
state, err := kafkaContainer.State(ctx)
32+
if err != nil {
33+
log.Printf("failed to get container state: %s", err)
34+
return
35+
}
36+
37+
fmt.Println(kafkaContainer.ClusterID)
38+
fmt.Println(state.Running)
39+
40+
// Output:
41+
// test-cluster
42+
// true
43+
}

modules/kafka_native/go.mod

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
module github.com/testcontainers/testcontainers-go/modules/kafka_native
2+
3+
go 1.23.0
4+
5+
toolchain go1.23.6
6+
7+
require (
8+
github.com/IBM/sarama v1.42.1
9+
github.com/docker/go-connections v0.5.0
10+
github.com/stretchr/testify v1.10.0
11+
github.com/testcontainers/testcontainers-go v0.38.0
12+
golang.org/x/mod v0.16.0
13+
)
14+
15+
require (
16+
dario.cat/mergo v1.0.1 // indirect
17+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
18+
github.com/Microsoft/go-winio v0.6.2 // indirect
19+
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
20+
github.com/containerd/errdefs v1.0.0 // indirect
21+
github.com/containerd/errdefs/pkg v0.3.0 // indirect
22+
github.com/containerd/log v0.1.0 // indirect
23+
github.com/containerd/platforms v0.2.1 // indirect
24+
github.com/cpuguy83/dockercfg v0.3.2 // indirect
25+
github.com/davecgh/go-spew v1.1.1 // indirect
26+
github.com/distribution/reference v0.6.0 // indirect
27+
github.com/docker/docker v28.2.2+incompatible // indirect
28+
github.com/docker/go-units v0.5.0 // indirect
29+
github.com/eapache/go-resiliency v1.4.0 // indirect
30+
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 // indirect
31+
github.com/eapache/queue v1.1.0 // indirect
32+
github.com/ebitengine/purego v0.8.4 // indirect
33+
github.com/felixge/httpsnoop v1.0.4 // indirect
34+
github.com/go-logr/logr v1.4.2 // indirect
35+
github.com/go-logr/stdr v1.2.2 // indirect
36+
github.com/go-ole/go-ole v1.2.6 // indirect
37+
github.com/gogo/protobuf v1.3.2 // indirect
38+
github.com/golang/snappy v0.0.4 // indirect
39+
github.com/google/uuid v1.6.0 // indirect
40+
github.com/hashicorp/errwrap v1.1.0 // indirect
41+
github.com/hashicorp/go-multierror v1.1.1 // indirect
42+
github.com/hashicorp/go-uuid v1.0.3 // indirect
43+
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
44+
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
45+
github.com/jcmturner/gofork v1.7.6 // indirect
46+
github.com/jcmturner/gokrb5/v8 v8.4.4 // indirect
47+
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
48+
github.com/klauspost/compress v1.18.0 // indirect
49+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
50+
github.com/magiconair/properties v1.8.10 // indirect
51+
github.com/moby/docker-image-spec v1.3.1 // indirect
52+
github.com/moby/go-archive v0.1.0 // indirect
53+
github.com/moby/patternmatcher v0.6.0 // indirect
54+
github.com/moby/sys/sequential v0.6.0 // indirect
55+
github.com/moby/sys/user v0.4.0 // indirect
56+
github.com/moby/sys/userns v0.1.0 // indirect
57+
github.com/moby/term v0.5.0 // indirect
58+
github.com/morikuni/aec v1.0.0 // indirect
59+
github.com/opencontainers/go-digest v1.0.0 // indirect
60+
github.com/opencontainers/image-spec v1.1.1 // indirect
61+
github.com/pierrec/lz4/v4 v4.1.18 // indirect
62+
github.com/pkg/errors v0.9.1 // indirect
63+
github.com/pmezard/go-difflib v1.0.0 // indirect
64+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
65+
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
66+
github.com/shirou/gopsutil/v4 v4.25.5 // indirect
67+
github.com/sirupsen/logrus v1.9.3 // indirect
68+
github.com/tklauser/go-sysconf v0.3.12 // indirect
69+
github.com/tklauser/numcpus v0.6.1 // indirect
70+
github.com/yusufpapurcu/wmi v1.2.4 // indirect
71+
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
72+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
73+
go.opentelemetry.io/otel v1.35.0 // indirect
74+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 // indirect
75+
go.opentelemetry.io/otel/metric v1.35.0 // indirect
76+
go.opentelemetry.io/otel/trace v1.35.0 // indirect
77+
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
78+
golang.org/x/crypto v0.37.0 // indirect
79+
golang.org/x/net v0.38.0 // indirect
80+
golang.org/x/sync v0.8.0 // indirect
81+
golang.org/x/sys v0.32.0 // indirect
82+
gopkg.in/yaml.v3 v3.0.1 // indirect
83+
)
84+
85+
replace github.com/testcontainers/testcontainers-go => ../..

0 commit comments

Comments
 (0)