diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/fixtures.py | 18 | ||||
-rw-r--r-- | test/test_client.py | 9 | ||||
-rw-r--r-- | test/test_client_async.py | 14 | ||||
-rw-r--r-- | test/test_conn_legacy.py | 16 |
4 files changed, 38 insertions, 19 deletions
diff --git a/test/fixtures.py b/test/fixtures.py index 826d037..654e636 100644 --- a/test/fixtures.py +++ b/test/fixtures.py @@ -9,7 +9,7 @@ import time import uuid from six.moves import urllib -from six.moves.urllib.parse import urlparse # pylint: disable=E0611,F0401 +from six.moves.urllib.parse import urlparse # pylint: disable=E0611,F0401 from test.service import ExternalService, SpawnedService from test.testutil import get_open_port @@ -193,7 +193,21 @@ class KafkaFixture(Fixture): else: if port is None: port = get_open_port() - host = "127.0.0.1" + # force IPv6 here because of a confusing point: + # + # - if the string "localhost" is passed, Kafka will *only* bind to the IPv4 address of localhost + # (127.0.0.1); however, kafka-python will attempt to connect on ::1 and fail + # + # - if the address literal 127.0.0.1 is passed, the metadata request during bootstrap will return + # the name "localhost" and we'll go back to the first case. This is odd! + # + # Ideally, Kafka would bind to all loopback addresses when we tell it to listen on "localhost" the + # way it makes an IPv6 socket bound to both 0.0.0.0/0 and ::/0 when we tell it to bind to "" (that is + # to say, when we make a listener of PLAINTEXT://:port. + # + # Note that even though we specify the bind host in bracket notation, Kafka responds to the bootstrap + # metadata request without square brackets later. + host = "[::1]" fixture = KafkaFixture(host, port, broker_id, zk_host, zk_port, zk_chroot, transport=transport, diff --git a/test/test_client.py b/test/test_client.py index 38235fd..4b5a3a8 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -38,7 +38,8 @@ class TestSimpleClient(unittest.TestCase): client = SimpleClient(hosts=['kafka01:9092', 'kafka02:9092', 'kafka03:9092']) self.assertEqual( - sorted([('kafka01', 9092, socket.AF_INET), ('kafka02', 9092, socket.AF_INET), ('kafka03', 9092, socket.AF_INET)]), + sorted([('kafka01', 9092, socket.AF_UNSPEC), ('kafka02', 9092, socket.AF_UNSPEC), + ('kafka03', 9092, socket.AF_UNSPEC)]), sorted(client.hosts)) def test_init_with_csv(self): @@ -46,7 +47,8 @@ class TestSimpleClient(unittest.TestCase): client = SimpleClient(hosts='kafka01:9092,kafka02:9092,kafka03:9092') self.assertEqual( - sorted([('kafka01', 9092, socket.AF_INET), ('kafka02', 9092, socket.AF_INET), ('kafka03', 9092, socket.AF_INET)]), + sorted([('kafka01', 9092, socket.AF_UNSPEC), ('kafka02', 9092, socket.AF_UNSPEC), + ('kafka03', 9092, socket.AF_UNSPEC)]), sorted(client.hosts)) def test_init_with_unicode_csv(self): @@ -54,7 +56,8 @@ class TestSimpleClient(unittest.TestCase): client = SimpleClient(hosts=u'kafka01:9092,kafka02:9092,kafka03:9092') self.assertEqual( - sorted([('kafka01', 9092, socket.AF_INET), ('kafka02', 9092, socket.AF_INET), ('kafka03', 9092, socket.AF_INET)]), + sorted([('kafka01', 9092, socket.AF_UNSPEC), ('kafka02', 9092, socket.AF_UNSPEC), + ('kafka03', 9092, socket.AF_UNSPEC)]), sorted(client.hosts)) @patch.object(SimpleClient, '_get_conn') diff --git a/test/test_client_async.py b/test/test_client_async.py index 922e43c..605ef1a 100644 --- a/test/test_client_async.py +++ b/test/test_client_async.py @@ -20,11 +20,11 @@ from kafka.structs import BrokerMetadata @pytest.mark.parametrize("bootstrap,expected_hosts", [ - (None, [('localhost', 9092, socket.AF_INET)]), - ('foobar:1234', [('foobar', 1234, socket.AF_INET)]), - ('fizzbuzz', [('fizzbuzz', 9092, socket.AF_INET)]), - ('foo:12,bar:34', [('foo', 12, socket.AF_INET), ('bar', 34, socket.AF_INET)]), - (['fizz:56', 'buzz'], [('fizz', 56, socket.AF_INET), ('buzz', 9092, socket.AF_INET)]), + (None, [('localhost', 9092, socket.AF_UNSPEC)]), + ('foobar:1234', [('foobar', 1234, socket.AF_UNSPEC)]), + ('fizzbuzz', [('fizzbuzz', 9092, socket.AF_UNSPEC)]), + ('foo:12,bar:34', [('foo', 12, socket.AF_UNSPEC), ('bar', 34, socket.AF_UNSPEC)]), + (['fizz:56', 'buzz'], [('fizz', 56, socket.AF_UNSPEC), ('buzz', 9092, socket.AF_UNSPEC)]), ]) def test_bootstrap_servers(mocker, bootstrap, expected_hosts): mocker.patch.object(KafkaClient, '_bootstrap') @@ -42,7 +42,7 @@ def test_bootstrap_success(conn): conn.state = ConnectionStates.CONNECTED cli = KafkaClient() args, kwargs = conn.call_args - assert args == ('localhost', 9092, socket.AF_INET) + assert args == ('localhost', 9092, socket.AF_UNSPEC) kwargs.pop('state_change_callback') assert kwargs == cli.config conn.connect.assert_called_with() @@ -55,7 +55,7 @@ def test_bootstrap_failure(conn): conn.state = ConnectionStates.DISCONNECTED cli = KafkaClient() args, kwargs = conn.call_args - assert args == ('localhost', 9092, socket.AF_INET) + assert args == ('localhost', 9092, socket.AF_UNSPEC) kwargs.pop('state_change_callback') assert kwargs == cli.config conn.connect.assert_called_with() diff --git a/test/test_conn_legacy.py b/test/test_conn_legacy.py index 347588e..820c4e7 100644 --- a/test/test_conn_legacy.py +++ b/test/test_conn_legacy.py @@ -48,12 +48,12 @@ class ConnTest(unittest.TestCase): self.MockCreateConn.reset_mock() def test_collect_hosts__happy_path(self): - hosts = "localhost:1234,localhost" + hosts = "127.0.0.1:1234,127.0.0.1" results = collect_hosts(hosts) self.assertEqual(set(results), set([ - ('localhost', 1234, socket.AF_INET), - ('localhost', 9092, socket.AF_INET), + ('127.0.0.1', 1234, socket.AF_INET), + ('127.0.0.1', 9092, socket.AF_INET), ])) def test_collect_hosts__ipv6(self): @@ -72,16 +72,18 @@ class ConnTest(unittest.TestCase): 'localhost', '[localhost]', '2001::1', + '[2001::1]', '[2001::1]:1234', ] results = collect_hosts(hosts) self.assertEqual(set(results), set([ - ('localhost', 1234, socket.AF_INET), - ('localhost', 9092, socket.AF_INET), + ('localhost', 1234, socket.AF_UNSPEC), + ('localhost', 9092, socket.AF_UNSPEC), ('localhost', 9092, socket.AF_INET6), ('2001::1', 9092, socket.AF_INET6), + ('2001::1', 9092, socket.AF_INET6), ('2001::1', 1234, socket.AF_INET6), ])) @@ -90,8 +92,8 @@ class ConnTest(unittest.TestCase): results = collect_hosts(hosts) self.assertEqual(set(results), set([ - ('localhost', 1234, socket.AF_INET), - ('localhost', 9092, socket.AF_INET), + ('localhost', 1234, socket.AF_UNSPEC), + ('localhost', 9092, socket.AF_UNSPEC), ])) |