|
1 | 1 | package kafka_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "context"
|
5 | 6 | "fmt"
|
| 7 | + "io" |
6 | 8 | "log"
|
| 9 | + "strings" |
7 | 10 |
|
| 11 | + "github.com/IBM/sarama" |
| 12 | + "github.com/docker/docker/api/types/container" |
8 | 13 | "github.com/testcontainers/testcontainers-go"
|
9 | 14 | "github.com/testcontainers/testcontainers-go/modules/kafka"
|
| 15 | + "github.com/testcontainers/testcontainers-go/network" |
| 16 | + "github.com/testcontainers/testcontainers-go/wait" |
10 | 17 | )
|
11 | 18 |
|
12 | 19 | func ExampleRun() {
|
@@ -41,3 +48,299 @@ func ExampleRun() {
|
41 | 48 | // test-cluster
|
42 | 49 | // true
|
43 | 50 | }
|
| 51 | + |
| 52 | +func ExampleKafkaContainer_BrokersByHostDockerInternal() { |
| 53 | + ctx := context.Background() |
| 54 | + |
| 55 | + kafkaContainer, err := kafka.Run(ctx, |
| 56 | + "confluentinc/confluent-local:7.5.0", |
| 57 | + kafka.WithClusterID("test-cluster"), |
| 58 | + ) |
| 59 | + if err != nil { |
| 60 | + log.Fatalf("failed to start container: %s", err) |
| 61 | + } |
| 62 | + |
| 63 | + // Clean up the container after |
| 64 | + defer func() { |
| 65 | + if err := kafkaContainer.Terminate(ctx); err != nil { |
| 66 | + log.Fatalf("failed to terminate container: %s", err) |
| 67 | + } |
| 68 | + }() |
| 69 | + |
| 70 | + { |
| 71 | + state, err := kafkaContainer.State(ctx) |
| 72 | + if err != nil { |
| 73 | + log.Fatalf("failed to get container state: %s", err) // nolint:gocritic |
| 74 | + } |
| 75 | + |
| 76 | + fmt.Println(kafkaContainer.ClusterID) |
| 77 | + fmt.Println(state.Running) |
| 78 | + } |
| 79 | + |
| 80 | + const topic = "example-topic" |
| 81 | + |
| 82 | + // Produce a message from the host that will be read by a consumer in another docker container |
| 83 | + { |
| 84 | + brokers, err := kafkaContainer.Brokers(ctx) |
| 85 | + |
| 86 | + config := sarama.NewConfig() |
| 87 | + config.Producer.Return.Successes = true |
| 88 | + producer, err := sarama.NewSyncProducer(brokers, config) |
| 89 | + if err != nil { |
| 90 | + log.Fatal(err) |
| 91 | + } |
| 92 | + |
| 93 | + if _, _, err := producer.SendMessage(&sarama.ProducerMessage{ |
| 94 | + Topic: topic, |
| 95 | + Key: sarama.StringEncoder("key"), |
| 96 | + Value: sarama.StringEncoder("example_message_value"), |
| 97 | + }); err != nil { |
| 98 | + log.Fatal(err) |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + // getBrokersByHostDockerInternal { |
| 104 | + brokers, err := kafkaContainer.BrokersByHostDockerInternal(ctx) |
| 105 | + if err != nil { |
| 106 | + log.Fatal(err) |
| 107 | + } |
| 108 | + |
| 109 | + // Run another container that can connect to the kafka container via hostname "host.docker.internal" |
| 110 | + kcat, err := testcontainers.GenericContainer( |
| 111 | + ctx, |
| 112 | + testcontainers.GenericContainerRequest{ |
| 113 | + ContainerRequest: testcontainers.ContainerRequest{ |
| 114 | + Image: "confluentinc/cp-kafkacat", |
| 115 | + Entrypoint: []string{"kafkacat"}, |
| 116 | + Cmd: []string{"-b", strings.Join(brokers, ","), "-C", "-t", topic, "-c", "1"}, |
| 117 | + WaitingFor: wait.ForExit(), |
| 118 | + |
| 119 | + // Add host.docker.internal to the consumer container so it can contact the kafka borkers |
| 120 | + HostConfigModifier: func(hc *container.HostConfig) { |
| 121 | + hc.ExtraHosts = append(hc.ExtraHosts, "host.docker.internal:host-gateway") |
| 122 | + }, |
| 123 | + }, |
| 124 | + Started: true, |
| 125 | + }, |
| 126 | + ) |
| 127 | + if err != nil { |
| 128 | + log.Fatalf("kafkacat error: %v", err) |
| 129 | + } |
| 130 | + |
| 131 | + lr, err := kcat.Logs(ctx) |
| 132 | + if err != nil { |
| 133 | + log.Fatalf("kafkacat logs error: %v", err) |
| 134 | + } |
| 135 | + |
| 136 | + logs, err := io.ReadAll(lr) |
| 137 | + if err != nil { |
| 138 | + log.Fatalf("kafkacat logs read error: %v", err) |
| 139 | + } |
| 140 | + |
| 141 | + fmt.Println("read message:", string(bytes.TrimSpace(logs))) |
| 142 | + // } |
| 143 | + |
| 144 | + // Output: |
| 145 | + // test-cluster |
| 146 | + // true |
| 147 | + // read message: example_message_value |
| 148 | +} |
| 149 | + |
| 150 | +func ExampleKafkaContainer_BrokersByContainerName() { |
| 151 | + ctx := context.Background() |
| 152 | + |
| 153 | + // getBrokersByContainerName_Kafka { |
| 154 | + net, err := network.New(ctx) |
| 155 | + if err != nil { |
| 156 | + log.Fatalf("failed to create network: %s", err) |
| 157 | + } |
| 158 | + |
| 159 | + kafkaContainer, err := kafka.Run(ctx, |
| 160 | + "confluentinc/confluent-local:7.5.0", |
| 161 | + kafka.WithClusterID("test-cluster"), |
| 162 | + network.WithNetwork(nil, net), // Run kafka test container in a new docker network |
| 163 | + ) |
| 164 | + if err != nil { |
| 165 | + log.Fatalf("failed to start container: %s", err) |
| 166 | + } |
| 167 | + // } |
| 168 | + |
| 169 | + // Clean up the container after |
| 170 | + defer func() { |
| 171 | + if err := kafkaContainer.Terminate(ctx); err != nil { |
| 172 | + log.Fatalf("failed to terminate container: %s", err) |
| 173 | + } |
| 174 | + }() |
| 175 | + |
| 176 | + { |
| 177 | + state, err := kafkaContainer.State(ctx) |
| 178 | + if err != nil { |
| 179 | + log.Fatalf("failed to get container state: %s", err) // nolint:gocritic |
| 180 | + } |
| 181 | + |
| 182 | + fmt.Println(kafkaContainer.ClusterID) |
| 183 | + fmt.Println(state.Running) |
| 184 | + } |
| 185 | + |
| 186 | + const topic = "example-topic" |
| 187 | + |
| 188 | + // Produce a message from the host that will be read by a consumer in another docker container |
| 189 | + { |
| 190 | + brokers, err := kafkaContainer.Brokers(ctx) |
| 191 | + |
| 192 | + config := sarama.NewConfig() |
| 193 | + config.Producer.Return.Successes = true |
| 194 | + producer, err := sarama.NewSyncProducer(brokers, config) |
| 195 | + if err != nil { |
| 196 | + log.Fatal(err) |
| 197 | + } |
| 198 | + |
| 199 | + if _, _, err := producer.SendMessage(&sarama.ProducerMessage{ |
| 200 | + Topic: topic, |
| 201 | + Key: sarama.StringEncoder("key"), |
| 202 | + Value: sarama.StringEncoder("example_message_value"), |
| 203 | + }); err != nil { |
| 204 | + log.Fatal(err) |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + // getBrokersByContainerName_Kcat { |
| 209 | + brokers, err := kafkaContainer.BrokersByContainerName(ctx) |
| 210 | + if err != nil { |
| 211 | + log.Fatal(err) |
| 212 | + } |
| 213 | + |
| 214 | + // Run another container that can connect to the kafka container via the kafka containers name |
| 215 | + kcat, err := testcontainers.GenericContainer( |
| 216 | + ctx, |
| 217 | + testcontainers.GenericContainerRequest{ |
| 218 | + ContainerRequest: testcontainers.ContainerRequest{ |
| 219 | + Image: "confluentinc/cp-kafkacat", |
| 220 | + Entrypoint: []string{"kafkacat"}, |
| 221 | + Cmd: []string{"-b", strings.Join(brokers, ","), "-C", "-t", topic, "-c", "1"}, |
| 222 | + WaitingFor: wait.ForExit(), |
| 223 | + Networks: []string{net.Name}, // Run kafkacat in the same docker network as the testcontainer |
| 224 | + }, |
| 225 | + Started: true, |
| 226 | + }, |
| 227 | + ) |
| 228 | + if err != nil { |
| 229 | + log.Fatalf("kafkacat error: %v", err) |
| 230 | + } |
| 231 | + |
| 232 | + lr, err := kcat.Logs(ctx) |
| 233 | + if err != nil { |
| 234 | + log.Fatalf("kafkacat logs error: %v", err) |
| 235 | + } |
| 236 | + |
| 237 | + logs, err := io.ReadAll(lr) |
| 238 | + if err != nil { |
| 239 | + log.Fatalf("kafkacat logs read error: %v", err) |
| 240 | + } |
| 241 | + |
| 242 | + fmt.Println("read message:", string(bytes.TrimSpace(logs))) |
| 243 | + // } |
| 244 | + |
| 245 | + // Output: |
| 246 | + // test-cluster |
| 247 | + // true |
| 248 | + // read message: example_message_value |
| 249 | +} |
| 250 | + |
| 251 | +func ExampleKafkaContainer_BrokersByContainerId() { |
| 252 | + ctx := context.Background() |
| 253 | + |
| 254 | + net, err := network.New(ctx) |
| 255 | + if err != nil { |
| 256 | + log.Fatalf("failed to create network: %s", err) |
| 257 | + } |
| 258 | + |
| 259 | + kafkaContainer, err := kafka.Run(ctx, |
| 260 | + "confluentinc/confluent-local:7.5.0", |
| 261 | + kafka.WithClusterID("test-cluster"), |
| 262 | + network.WithNetwork(nil, net), // Run kafka test container in a new docker network |
| 263 | + ) |
| 264 | + if err != nil { |
| 265 | + log.Fatalf("failed to start container: %s", err) |
| 266 | + } |
| 267 | + |
| 268 | + // Clean up the container after |
| 269 | + defer func() { |
| 270 | + if err := kafkaContainer.Terminate(ctx); err != nil { |
| 271 | + log.Fatalf("failed to terminate container: %s", err) |
| 272 | + } |
| 273 | + }() |
| 274 | + |
| 275 | + { |
| 276 | + state, err := kafkaContainer.State(ctx) |
| 277 | + if err != nil { |
| 278 | + log.Fatalf("failed to get container state: %s", err) // nolint:gocritic |
| 279 | + } |
| 280 | + |
| 281 | + fmt.Println(kafkaContainer.ClusterID) |
| 282 | + fmt.Println(state.Running) |
| 283 | + } |
| 284 | + |
| 285 | + const topic = "example-topic" |
| 286 | + |
| 287 | + // Produce a message from the host that will be read by a consumer in another docker container |
| 288 | + { |
| 289 | + brokers, err := kafkaContainer.Brokers(ctx) |
| 290 | + |
| 291 | + config := sarama.NewConfig() |
| 292 | + config.Producer.Return.Successes = true |
| 293 | + producer, err := sarama.NewSyncProducer(brokers, config) |
| 294 | + if err != nil { |
| 295 | + log.Fatal(err) |
| 296 | + } |
| 297 | + |
| 298 | + if _, _, err := producer.SendMessage(&sarama.ProducerMessage{ |
| 299 | + Topic: topic, |
| 300 | + Key: sarama.StringEncoder("key"), |
| 301 | + Value: sarama.StringEncoder("example_message_value"), |
| 302 | + }); err != nil { |
| 303 | + log.Fatal(err) |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + brokers, err := kafkaContainer.BrokersByContainerId(ctx) |
| 308 | + if err != nil { |
| 309 | + log.Fatal(err) |
| 310 | + } |
| 311 | + |
| 312 | + // Run another container that can connect to the kafka container via the kafka containers ContainerID |
| 313 | + kcat, err := testcontainers.GenericContainer( |
| 314 | + ctx, |
| 315 | + testcontainers.GenericContainerRequest{ |
| 316 | + ContainerRequest: testcontainers.ContainerRequest{ |
| 317 | + Image: "confluentinc/cp-kafkacat", |
| 318 | + Entrypoint: []string{"kafkacat"}, |
| 319 | + Cmd: []string{"-b", strings.Join(brokers, ","), "-C", "-t", topic, "-c", "1"}, |
| 320 | + WaitingFor: wait.ForExit(), |
| 321 | + Networks: []string{net.Name}, // Run kafkacat in the same docker network as the testcontainer |
| 322 | + }, |
| 323 | + Started: true, |
| 324 | + }, |
| 325 | + ) |
| 326 | + if err != nil { |
| 327 | + log.Fatalf("kafkacat error: %v", err) |
| 328 | + } |
| 329 | + |
| 330 | + lr, err := kcat.Logs(ctx) |
| 331 | + if err != nil { |
| 332 | + log.Fatalf("kafkacat logs error: %v", err) |
| 333 | + } |
| 334 | + |
| 335 | + logs, err := io.ReadAll(lr) |
| 336 | + if err != nil { |
| 337 | + log.Fatalf("kafkacat logs read error: %v", err) |
| 338 | + } |
| 339 | + |
| 340 | + fmt.Println("read message:", string(bytes.TrimSpace(logs))) |
| 341 | + |
| 342 | + // Output: |
| 343 | + // test-cluster |
| 344 | + // true |
| 345 | + // read message: example_message_value |
| 346 | +} |
0 commit comments