|  | 
| 1 | 1 | # pylint: skip-file | 
| 2 | 2 | from __future__ import absolute_import | 
| 3 | 3 | 
 | 
| 4 |  | -from kafka.cluster import ClusterMetadata | 
|  | 4 | +import socket | 
|  | 5 | + | 
|  | 6 | +from kafka.cluster import ClusterMetadata, collect_hosts | 
| 5 | 7 | from kafka.protocol.metadata import MetadataResponse | 
| 6 | 8 | 
 | 
| 7 | 9 | 
 | 
| @@ -132,3 +134,60 @@ def test_metadata_v7(): | 
| 132 | 134 |     assert cluster.cluster_id == 'cluster-foo' | 
| 133 | 135 |     assert cluster._partitions['topic-1'][0].offline_replicas == [12] | 
| 134 | 136 |     assert cluster._partitions['topic-1'][0].leader_epoch == 0 | 
|  | 137 | + | 
|  | 138 | + | 
|  | 139 | +def test_collect_hosts__happy_path(): | 
|  | 140 | +    hosts = "127.0.0.1:1234,127.0.0.1" | 
|  | 141 | +    results = collect_hosts(hosts) | 
|  | 142 | +    assert set(results) == set([ | 
|  | 143 | +        ('127.0.0.1', 1234, socket.AF_INET), | 
|  | 144 | +        ('127.0.0.1', 9092, socket.AF_INET), | 
|  | 145 | +    ]) | 
|  | 146 | + | 
|  | 147 | + | 
|  | 148 | +def test_collect_hosts__ipv6(): | 
|  | 149 | +    hosts = "[localhost]:1234,[2001:1000:2000::1],[2001:1000:2000::1]:1234" | 
|  | 150 | +    results = collect_hosts(hosts) | 
|  | 151 | +    assert set(results) == set([ | 
|  | 152 | +        ('localhost', 1234, socket.AF_INET6), | 
|  | 153 | +        ('2001:1000:2000::1', 9092, socket.AF_INET6), | 
|  | 154 | +        ('2001:1000:2000::1', 1234, socket.AF_INET6), | 
|  | 155 | +    ]) | 
|  | 156 | + | 
|  | 157 | + | 
|  | 158 | +def test_collect_hosts__string_list(): | 
|  | 159 | +    hosts = [ | 
|  | 160 | +        'localhost:1234', | 
|  | 161 | +        'localhost', | 
|  | 162 | +        '[localhost]', | 
|  | 163 | +        '2001::1', | 
|  | 164 | +        '[2001::1]', | 
|  | 165 | +        '[2001::1]:1234', | 
|  | 166 | +    ] | 
|  | 167 | +    results = collect_hosts(hosts) | 
|  | 168 | +    assert set(results) == set([ | 
|  | 169 | +        ('localhost', 1234, socket.AF_UNSPEC), | 
|  | 170 | +        ('localhost', 9092, socket.AF_UNSPEC), | 
|  | 171 | +        ('localhost', 9092, socket.AF_INET6), | 
|  | 172 | +        ('2001::1', 9092, socket.AF_INET6), | 
|  | 173 | +        ('2001::1', 9092, socket.AF_INET6), | 
|  | 174 | +        ('2001::1', 1234, socket.AF_INET6), | 
|  | 175 | +    ]) | 
|  | 176 | + | 
|  | 177 | + | 
|  | 178 | +def test_collect_hosts__with_spaces(): | 
|  | 179 | +    hosts = "localhost:1234, localhost" | 
|  | 180 | +    results = collect_hosts(hosts) | 
|  | 181 | +    assert set(results) == set([ | 
|  | 182 | +        ('localhost', 1234, socket.AF_UNSPEC), | 
|  | 183 | +        ('localhost', 9092, socket.AF_UNSPEC), | 
|  | 184 | +    ]) | 
|  | 185 | + | 
|  | 186 | + | 
|  | 187 | +def test_collect_hosts__protocol(): | 
|  | 188 | +    hosts = "SASL_SSL://foo.bar:1234,SASL_SSL://fizz.buzz:5678" | 
|  | 189 | +    results = collect_hosts(hosts) | 
|  | 190 | +    assert set(results) == set([ | 
|  | 191 | +        ('foo.bar', 1234, socket.AF_UNSPEC), | 
|  | 192 | +        ('fizz.buzz', 5678, socket.AF_UNSPEC), | 
|  | 193 | +    ]) | 
0 commit comments