-
-
Notifications
You must be signed in to change notification settings - Fork 251
Add support for Confluent Kafka 8 #1053
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| FROM confluentinc/cp-kafka:7.9.1 | ||
| FROM confluentinc/cp-kafka:8.0.0 |
This file contains hidden or 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
148 changes: 148 additions & 0 deletions
148
packages/modules/kafka/src/kafka-container-latest.test.ts
This file contains hidden or 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,148 @@ | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
| import { GenericContainer, Network } from "testcontainers"; | ||
| import { getImage } from "../../../testcontainers/src/utils/test-helper"; | ||
| import { KafkaContainer, SaslSslListenerOptions } from "./kafka-container"; | ||
| import { testPubSub } from "./test-helper"; | ||
|
|
||
| const IMAGE = getImage(__dirname); | ||
|
|
||
| describe("KafkaContainer", { timeout: 240_000 }, () => { | ||
| const certificatesDir = path.resolve(__dirname, "..", "test-certs"); | ||
|
|
||
| // connect { | ||
| it("should connect", async () => { | ||
| const kafkaContainer = await new KafkaContainer(IMAGE).withExposedPorts(9093).start(); | ||
|
|
||
| await testPubSub(kafkaContainer); | ||
|
|
||
| await kafkaContainer.stop(); | ||
| }); | ||
| // } | ||
|
|
||
| it("should connect with custom network", async () => { | ||
| const network = await new Network().start(); | ||
| const kafkaContainer = await new KafkaContainer(IMAGE).withNetwork(network).withExposedPorts(9093).start(); | ||
|
|
||
| await testPubSub(kafkaContainer); | ||
|
|
||
| await kafkaContainer.stop(); | ||
| await network.stop(); | ||
| }); | ||
|
|
||
| it("should be reusable", async () => { | ||
| const originalKafkaContainer = await new KafkaContainer(IMAGE).withReuse().start(); | ||
| const newKafkaContainer = await new KafkaContainer(IMAGE).withReuse().start(); | ||
|
|
||
| expect(newKafkaContainer.getId()).toBe(originalKafkaContainer.getId()); | ||
|
|
||
| await originalKafkaContainer.stop(); | ||
| }); | ||
|
|
||
| // ssl { | ||
| it(`should connect with SASL`, async () => { | ||
| const saslConfig: SaslSslListenerOptions = { | ||
| port: 9096, | ||
| sasl: { | ||
| mechanism: "SCRAM-SHA-512", | ||
| user: { | ||
| name: "app-user", | ||
| password: "userPassword", | ||
| }, | ||
| }, | ||
| keystore: { | ||
| content: fs.readFileSync(path.resolve(certificatesDir, "kafka.server.keystore.pfx")), | ||
| passphrase: "serverKeystorePassword", | ||
| }, | ||
| truststore: { | ||
| content: fs.readFileSync(path.resolve(certificatesDir, "kafka.server.truststore.pfx")), | ||
| passphrase: "serverTruststorePassword", | ||
| }, | ||
| }; | ||
|
|
||
| const kafkaContainer = new KafkaContainer("confluentinc/cp-kafka:7.5.0").withSaslSslListener(saslConfig); | ||
| const startedKafkaContainer = await kafkaContainer.start(); | ||
|
|
||
| await testPubSub(startedKafkaContainer, { | ||
| brokers: [`${startedKafkaContainer.getHost()}:${startedKafkaContainer.getMappedPort(9096)}`], | ||
| sasl: { | ||
| username: "app-user", | ||
| password: "userPassword", | ||
| mechanism: "scram-sha-512", | ||
| }, | ||
| ssl: { | ||
| ca: [fs.readFileSync(path.resolve(certificatesDir, "kafka.client.truststore.pem"))], | ||
| }, | ||
| }); | ||
| await startedKafkaContainer.stop(); | ||
| }); | ||
| // } | ||
|
|
||
| it(`should connect with SASL in custom network`, async () => { | ||
| const network = await new Network().start(); | ||
|
|
||
| const saslConfig: SaslSslListenerOptions = { | ||
| port: 9096, | ||
| sasl: { | ||
| mechanism: "SCRAM-SHA-512", | ||
| user: { | ||
| name: "app-user", | ||
| password: "userPassword", | ||
| }, | ||
| }, | ||
| keystore: { | ||
| content: fs.readFileSync(path.resolve(certificatesDir, "kafka.server.keystore.pfx")), | ||
| passphrase: "serverKeystorePassword", | ||
| }, | ||
| truststore: { | ||
| content: fs.readFileSync(path.resolve(certificatesDir, "kafka.server.truststore.pfx")), | ||
| passphrase: "serverTruststorePassword", | ||
| }, | ||
| }; | ||
|
|
||
| const kafkaContainer = await new KafkaContainer(IMAGE) | ||
| .withNetwork(network) | ||
| .withNetworkAliases("kafka") | ||
| .withSaslSslListener(saslConfig) | ||
| .start(); | ||
|
|
||
| const kafkaCliContainer = await new GenericContainer(IMAGE) | ||
| .withNetwork(network) | ||
| .withCommand(["bash", "-c", "sleep infinity"]) | ||
| .withCopyFilesToContainer([ | ||
| { | ||
| source: path.resolve(certificatesDir, "kafka.client.truststore.pem"), | ||
| target: "/truststore.pem", | ||
| }, | ||
| ]) | ||
| .withCopyContentToContainer([ | ||
| { | ||
| content: ` | ||
| security.protocol=SASL_SSL | ||
| ssl.truststore.location=/truststore.pem | ||
| ssl.truststore.type=PEM | ||
| ssl.endpoint.identification.algorithm= | ||
| sasl.mechanism=SCRAM-SHA-512 | ||
| sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required \\ | ||
| username="app-user" \\ | ||
| password="userPassword"; | ||
| `, | ||
| target: "/etc/kafka/consumer.properties", | ||
| }, | ||
| ]) | ||
| .start(); | ||
|
|
||
| await kafkaCliContainer.exec( | ||
| "kafka-topics --create --topic test-topic --bootstrap-server kafka:9096 --command-config /etc/kafka/consumer.properties" | ||
| ); | ||
| const { output, exitCode } = await kafkaCliContainer.exec( | ||
| "kafka-topics --list --bootstrap-server kafka:9096 --command-config /etc/kafka/consumer.properties" | ||
| ); | ||
|
|
||
| expect(exitCode).toBe(0); | ||
| expect(output).toContain("test-topic"); | ||
|
|
||
| await kafkaCliContainer.stop(); | ||
| await kafkaContainer.stop(); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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,34 @@ | ||
| import { Kafka, KafkaConfig, logLevel } from "kafkajs"; | ||
| import { StartedTestContainer } from "testcontainers"; | ||
|
|
||
| export async function testPubSub(kafkaContainer: StartedTestContainer, additionalConfig: Partial<KafkaConfig> = {}) { | ||
| const kafka = new Kafka({ | ||
| logLevel: logLevel.NOTHING, | ||
| brokers: [`${kafkaContainer.getHost()}:${kafkaContainer.getMappedPort(9093)}`], | ||
| ...additionalConfig, | ||
| }); | ||
|
|
||
| const producer = kafka.producer(); | ||
| await producer.connect(); | ||
|
|
||
| const consumer = kafka.consumer({ groupId: "test-group" }); | ||
| await consumer.connect(); | ||
|
|
||
| await producer.send({ | ||
| topic: "test-topic", | ||
| messages: [{ value: "test message" }], | ||
| }); | ||
|
|
||
| await consumer.subscribe({ topic: "test-topic", fromBeginning: true }); | ||
|
|
||
| const consumedMessage = await new Promise((resolve) => { | ||
| consumer.run({ | ||
| eachMessage: async ({ message }) => resolve(message.value?.toString()), | ||
| }); | ||
| }); | ||
|
|
||
| expect(consumedMessage).toBe("test message"); | ||
|
|
||
| await consumer.disconnect(); | ||
| await producer.disconnect(); | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.