rameshiyer27 | f2609a3 | 2023-12-14 14:17:35 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # ============LICENSE_START==================================================== |
rameshiyer27 | f2e4da7 | 2024-01-13 21:26:09 +0000 | [diff] [blame] | 4 | # Copyright (C) 2023-2024 Nordix Foundation. |
rameshiyer27 | f2609a3 | 2023-12-14 14:17:35 +0000 | [diff] [blame] | 5 | # ============================================================================= |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | # SPDX-License-Identifier: Apache-2.0 |
| 19 | # ============LICENSE_END====================================================== |
| 20 | |
| 21 | # Python utility to fetch kafka topic and look for required messages. |
| 22 | # Accepts the arguments {topic_name} and {list of expected values} and {timeout} to verify the kafka topic. |
| 23 | |
| 24 | |
| 25 | from confluent_kafka import Consumer, KafkaException |
| 26 | import sys |
| 27 | import time |
| 28 | |
rameshiyer27 | f2e4da7 | 2024-01-13 21:26:09 +0000 | [diff] [blame] | 29 | |
rameshiyer27 | 31c61d4 | 2024-01-21 14:24:03 +0000 | [diff] [blame] | 30 | def consume_kafka_topic(topic, expected_values, timeout, bootstrap_server): |
rameshiyer27 | f2609a3 | 2023-12-14 14:17:35 +0000 | [diff] [blame] | 31 | config = { |
rameshiyer27 | 31c61d4 | 2024-01-21 14:24:03 +0000 | [diff] [blame] | 32 | 'bootstrap.servers': bootstrap_server, |
rameshiyer27 | f2609a3 | 2023-12-14 14:17:35 +0000 | [diff] [blame] | 33 | 'group.id': 'testgrp', |
| 34 | 'auto.offset.reset': 'earliest' |
| 35 | } |
| 36 | consumer = Consumer(config) |
| 37 | consumer.subscribe([topic]) |
| 38 | try: |
| 39 | start_time = time.time() |
| 40 | while time.time() - start_time < timeout: |
| 41 | msg = consumer.poll(1.0) |
| 42 | if msg is None: |
| 43 | continue |
| 44 | if msg.error(): |
| 45 | if msg.error().code() == KafkaException._PARTITION_EOF: |
| 46 | sys.stderr.write(f"Reached end of topic {msg.topic()} / partition {msg.partition()}\n") |
| 47 | print('ERROR') |
| 48 | sys.exit(404) |
| 49 | else: |
| 50 | # Error |
| 51 | raise KafkaException(msg.error()) |
| 52 | else: |
| 53 | # Message received |
| 54 | message = msg.value().decode('utf-8') |
rameshiyer27 | f2e4da7 | 2024-01-13 21:26:09 +0000 | [diff] [blame] | 55 | if expected_values in message: |
rameshiyer27 | f2609a3 | 2023-12-14 14:17:35 +0000 | [diff] [blame] | 56 | print(message) |
| 57 | sys.exit(200) |
| 58 | finally: |
| 59 | consumer.close() |
| 60 | |
rameshiyer27 | f2609a3 | 2023-12-14 14:17:35 +0000 | [diff] [blame] | 61 | |
| 62 | if __name__ == '__main__': |
| 63 | topic_name = sys.argv[1] |
rameshiyer27 | f2e4da7 | 2024-01-13 21:26:09 +0000 | [diff] [blame] | 64 | timeout = int(sys.argv[2]) # timeout in seconds for verifying the kafka topic |
| 65 | expected_values = sys.argv[3] |
rameshiyer27 | 31c61d4 | 2024-01-21 14:24:03 +0000 | [diff] [blame] | 66 | bootstrap_server = sys.argv[4] |
| 67 | consume_kafka_topic(topic_name, expected_values, timeout, bootstrap_server) |