1+ import pytest
12from unittest .mock import patch
23
34from confluent_kafka .admin import BrokerMetadata , ClusterMetadata , TopicMetadata
45
56from saluki .sniff import sniff
67
8+ @pytest .fixture ()
9+ def fake_cluster_md ():
10+ """
11+ Returns a fake cluster metadata object with two topics;
12+ one with 1 partition and the other with 2.
13+ """
14+ fake_cluster_md = ClusterMetadata ()
15+ broker1 = BrokerMetadata ()
16+ broker1 .id = "id1" # type: ignore
17+ broker1 .host = "mybroker" # type: ignore
18+ broker1 .port = 9093
19+ fake_cluster_md .brokers = {0 : broker1 }
720
8- def test_sniff_with_two_partitions_in_a_topic ():
21+ topic1 = TopicMetadata ()
22+ topic1 .partitions = {0 : {}}
23+
24+ topic2 = TopicMetadata ()
25+ topic2 .partitions = {0 : {}, 1 : {}}
26+
27+ fake_cluster_md .topics = {"topic1" : topic1 , "topic2" : topic2 }
28+ return fake_cluster_md
29+
30+ def test_sniff_with_two_partitions_in_a_topic (fake_cluster_md ):
931 with (
1032 patch ("saluki.sniff.AdminClient" ) as a ,
1133 patch ("saluki.sniff.Consumer" ) as c ,
1234 patch ("saluki.sniff.logger" ) as logger ,
1335 ):
14- fake_cluster_md = ClusterMetadata ()
15- broker1 = BrokerMetadata ()
16- broker1 .id = "id1" # type: ignore
17- broker1 .host = "mybroker" # type: ignore
18- broker1 .port = 9093
19- fake_cluster_md .brokers = {0 : broker1 }
20-
21- topic1 = TopicMetadata ()
22- topic1 .partitions = {0 : {}}
23- topic2 = TopicMetadata ()
24- topic2 .partitions = {0 : {}, 1 : {}}
25-
26- fake_cluster_md .topics = {"topic1" : topic1 , "topic2" : topic2 }
2736 a ().list_topics .return_value = fake_cluster_md
2837 c ().get_watermark_offsets .return_value = 1 , 2
2938 sniff ("whatever" )
@@ -40,3 +49,29 @@ def test_sniff_with_two_partitions_in_a_topic():
4049
4150 topic2_call2 = logger .info .call_args_list [8 ]
4251 assert "1 - low:1, high:2, num_messages:1" in topic2_call2 .args [0 ]
52+
53+ def test_sniff_with_single_topic (fake_cluster_md ):
54+ with (
55+ patch ("saluki.sniff.AdminClient" ) as a ,
56+ patch ("saluki.sniff.Consumer" ) as c ,
57+ patch ("saluki.sniff.logger" ) as logger ,
58+ ):
59+
60+ a ().list_topics .return_value = fake_cluster_md
61+ c ().get_watermark_offsets .return_value = 1 , 2
62+ sniff ("mybroker:9093" , "topic1" )
63+
64+ assert "\t topic1" in logger .info .call_args_list [0 ].args [0 ]
65+ assert "\t \t 0 - low:1, high:2, num_messages:1" in logger .info .call_args_list [1 ].args [0 ]
66+
67+
68+ def test_sniff_with_single_nonexistent_topic ():
69+ with (
70+ patch ("saluki.sniff.AdminClient" ) as a ,
71+ patch ("saluki.sniff.Consumer" ),
72+ patch ("saluki.sniff.logger" ) as logger ,
73+ ):
74+ # Deliberately blank cluster metadata ie. no topics
75+ a ().list_topics .return_value = ClusterMetadata ()
76+ sniff ("somebroker:9092" , "sometopic" )
77+ logger .warning .assert_called_with ("Topic sometopic not found on broker somebroker:9092" )
0 commit comments