Repository navigation
adding kafka test container support #127
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 all commits
Commits
Show all changes
3 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
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
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,84 @@ | ||
| import tarfile | ||
| import time | ||
| from io import BytesIO | ||
| from textwrap import dedent | ||
|
|
||
| from kafka import KafkaConsumer | ||
| from kafka.errors import KafkaError | ||
|
|
||
| from testcontainers.core.container import DockerContainer | ||
| from testcontainers.core.waiting_utils import wait_container_is_ready | ||
|
|
||
|
|
||
| class KafkaContainer(DockerContainer): | ||
| KAFKA_PORT = 9093 | ||
| TC_START_SCRIPT = '/tc-start.sh' | ||
|
|
||
| def __init__(self, image="confluentinc/cp-kafka:5.4.3", port_to_expose=KAFKA_PORT): | ||
| super(KafkaContainer, self).__init__(image) | ||
| self.port_to_expose = port_to_expose | ||
| self.with_exposed_ports(self.port_to_expose) | ||
| listeners = 'PLAINTEXT://0.0.0.0:{},BROKER://0.0.0.0:9092'.format(port_to_expose) | ||
| self.with_env('KAFKA_LISTENERS', listeners) | ||
| self.with_env('KAFKA_LISTENER_SECURITY_PROTOCOL_MAP', | ||
| 'BROKER:PLAINTEXT,PLAINTEXT:PLAINTEXT') | ||
| self.with_env('KAFKA_INTER_BROKER_LISTENER_NAME', 'BROKER') | ||
|
|
||
| self.with_env('KAFKA_BROKER_ID', '1') | ||
| self.with_env('KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR', '1') | ||
| self.with_env('KAFKA_OFFSETS_TOPIC_NUM_PARTITIONS', '1') | ||
| self.with_env('KAFKA_LOG_FLUSH_INTERVAL_MESSAGES', '10000000') | ||
| self.with_env('KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS', '0') | ||
|
|
||
| def get_bootstrap_server(self): | ||
| host = self.get_container_host_ip() | ||
| port = self.get_exposed_port(self.port_to_expose) | ||
| return '{}:{}'.format(host, port) | ||
|
|
||
| @wait_container_is_ready() | ||
| def _connect(self): | ||
| bootstrap_server = self.get_bootstrap_server() | ||
| consumer = KafkaConsumer(group_id='test', bootstrap_servers=[bootstrap_server]) | ||
| if not consumer.topics(): | ||
| raise KafkaError("Unable to connect with kafka container!") | ||
|
|
||
| def tc_start(self): | ||
| port = self.get_exposed_port(self.port_to_expose) | ||
| listeners = 'PLAINTEXT://localhost:{},BROKER://$(hostname -i):9092'.format(port) | ||
| data = ( | ||
| dedent( | ||
| """ | ||
| #!/bin/bash | ||
| echo 'clientPort=2181' > zookeeper.properties | ||
| echo 'dataDir=/var/lib/zookeeper/data' >> zookeeper.properties | ||
| echo 'dataLogDir=/var/lib/zookeeper/log' >> zookeeper.properties | ||
| zookeeper-server-start zookeeper.properties & | ||
| export KAFKA_ZOOKEEPER_CONNECT='localhost:2181' | ||
| export KAFKA_ADVERTISED_LISTENERS={} | ||
| . /etc/confluent/docker/bash-config | ||
| /etc/confluent/docker/configure | ||
| /etc/confluent/docker/launch | ||
| """.format(listeners) | ||
| ) | ||
| .strip() | ||
| .encode('utf-8') | ||
| ) | ||
| self.create_file(data, KafkaContainer.TC_START_SCRIPT) | ||
|
|
||
| def start(self): | ||
| script = KafkaContainer.TC_START_SCRIPT | ||
| command = 'sh -c "while [ ! -f {} ]; do sleep 0.1; done; sh {}"'.format(script, script) | ||
| self.with_command(command) | ||
| super().start() | ||
| self.tc_start() | ||
| self._connect() | ||
| return self | ||
|
|
||
| def create_file(self, content: bytes, path: str): | ||
| with BytesIO() as archive, tarfile.TarFile(fileobj=archive, mode="w") as tar: | ||
| tarinfo = tarfile.TarInfo(name=path) | ||
| tarinfo.size = len(content) | ||
| tarinfo.mtime = time.time() | ||
| tar.addfile(tarinfo, BytesIO(content)) | ||
| archive.seek(0) | ||
| self.get_wrapped_container().put_archive("/", archive) |
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,31 @@ | ||
| from kafka import KafkaConsumer, KafkaProducer, TopicPartition | ||
|
|
||
| from testcontainers.kafka import KafkaContainer | ||
|
|
||
|
|
||
| def test_kafka_producer_consumer(): | ||
| with KafkaContainer() as container: | ||
| produce_and_consume_kafka_message(container) | ||
|
|
||
|
|
||
| def test_kafka_producer_consumer_custom_port(): | ||
| with KafkaContainer(port_to_expose=9888) as container: | ||
| assert container.port_to_expose == 9888 | ||
| produce_and_consume_kafka_message(container) | ||
|
|
||
|
|
||
| def produce_and_consume_kafka_message(container): | ||
| topic = 'test-topic' | ||
| bootstrap_server = container.get_bootstrap_server() | ||
|
|
||
| producer = KafkaProducer(bootstrap_servers=[bootstrap_server]) | ||
| producer.send(topic, b"verification message") | ||
| producer.flush() | ||
| producer.close() | ||
|
|
||
| consumer = KafkaConsumer(bootstrap_servers=[bootstrap_server]) | ||
| tp = TopicPartition(topic, 0) | ||
| consumer.assign([tp]) | ||
| consumer.seek_to_beginning() | ||
| assert consumer.end_offsets([tp])[tp] == 1, \ | ||
| "Expected exactly one test message to be present on test topic !" | ||
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.