def generate_value_for_list_topics(binary_messages: List[BinaryMessage], topic_id: str) -> ClusterMetadata: cluster_meta = ClusterMetadata() topic_meta = TopicMetadata() topic_meta.partitions = {msg.partition: None for msg in binary_messages} cluster_meta.topics = {topic_id: topic_meta} return cluster_meta
def list_topics(self, topic: str) -> ClusterMetadata: meta = ClusterMetadata() topic_meta = TopicMetadata() topic_meta.topic = topic topic_meta.partitions = {0: PartitionMetadata()} meta.topics = {topic: topic_meta} return meta
def list_topics(self, timeout=-1): raw_metadata = ClusterMetadata() raw_metadata.topics = {} raw_metadata.cluster_id = "1234" raw_metadata.brokers = { "testBroker0": BrokerMetadata(), "testBroker1": BrokerMetadata() } return raw_metadata
def cluster_meta(topics): brokers = [ broker(100, "broker1"), broker(101, "broker2"), broker(102, "broker3") ] cluster = ClusterMetadata() cluster.brokers = {b.id: b for b in brokers} cluster.topics = {t.topic: t for t in topics} cluster.controller_id = 101 return cluster
def test_passing_simple_test_module(mocker): """ Given: - initialized KafkaCommunicator When: - testing the module without fetch Then: - Assert 'ok' if ClusterMetadata object is returned from Kafka """ mocker.patch.object(KafkaV3, 'KConsumer') mocker.patch.object(KafkaV3, 'KProducer') mocker.patch.object(KConsumer, 'list_topics', return_value=ClusterMetadata()) mocker.patch.object(KProducer, 'list_topics', return_value=ClusterMetadata()) assert command_test_module(KAFKA, {'isFetch': False}) == 'ok'
def create_cluster_metadata(topic_partitions): """Create ClusterMetada out of a dict structure for easier mocking. topic_partitions should be in the format of {'topic1': [partition1, partition2], 'topic2': [partition3]...} """ cluster_metadata = ClusterMetadata() topics_dict = {} for topic in topic_partitions.keys(): topic_metadata = TopicMetadata() partitions = topic_partitions[topic] partitions_dict = {} for partition in partitions: partition_metadata = PartitionMetadata() partition_metadata.id = partition partitions_dict.update({partition: partition_metadata}) topic_metadata.partitions = partitions_dict topic_metadata.topic = topic topics_dict.update({topic: topic_metadata}) cluster_metadata.topics = topics_dict return cluster_metadata
def test_failing_simple_test_module(mocker): """ Given: - initialized KafkaCommunicator When: - testing the module without fetch Then: - Assert relevant error is raised if communication failed. """ mocker.patch.object(KConsumer, '__init__', return_value=None) mocker.patch.object(KProducer, '__init__', return_value=None) def raise_kafka_error(): raise Exception('Some connection error') mocker.patch.object(KConsumer, 'list_topics', return_value=ClusterMetadata(), side_effect=raise_kafka_error) mocker.patch.object(KProducer, 'list_topics', return_value=ClusterMetadata(), side_effect=raise_kafka_error) with pytest.raises(DemistoException) as exception_info: command_test_module(KAFKA, {'isFetch': False}) assert 'Error connecting to kafka' in str(exception_info.value)
def test_print_topics_no_topics(mocker, demisto_args): """ Given: - initialized KafkaCommunicator When: - running kafka-print-topics command without any topics Then: - Assert the 'No topics found.' response and that no errors are raised. """ mocker.patch.object(KProducer, '__init__', return_value=None) mocker.patch.object(KProducer, 'list_topics', return_value=ClusterMetadata()) assert print_topics(KAFKA, demisto_args) == 'No topics found.'
def list_topics(self, timeout=-1): ''' Mock AdminClient returns a description of topic from test_data/test_metadata_one_topic.json ''' test_topic_metadata = TopicMetadata() test_topic_metadata.partitions = { 0: PartitionMetadata(), 1: PartitionMetadata(), 2: PartitionMetadata() } raw_metadata = ClusterMetadata() raw_metadata.topics = { "temperature": test_topic_metadata, "__consumer_offsets": test_topic_metadata } raw_metadata.cluster_id = "1234" raw_metadata.brokers = { "testBroker0": BrokerMetadata(), "testBroker1": BrokerMetadata() } return raw_metadata
def test_fetch_partitions_no_topics(mocker, demisto_args): """ Given: - initialized KafkaCommunicator When: - running fetch-partitions command without topics in kafka Then: - Assert the relevant error was raised. """ mocker.patch.object(KProducer, '__init__', return_value=None) mocker.patch.object(KProducer, 'list_topics', return_value=ClusterMetadata()) with pytest.raises(DemistoException) as exception_info: fetch_partitions(KAFKA, demisto_args) assert f'Topic {demisto_args["topic"]} was not found in Kafka' in str(exception_info.value)