From e4a66f830731d21e7f6abb19f2a0f08c65531858 Mon Sep 17 00:00:00 2001 From: narayana006 <69020110+narayana006@users.noreply.github.com> Date: Sat, 21 Nov 2026 09:36:12 -0500 Subject: [PATCH] Verify ZooKeeper fixture identity before reporting readiness --- servers/resources/0.0/zookeeper.properties | 1 + servers/resources/1.0/zookeeper.properties | 1 + test/integration/fixtures.py | 41 +++++++- test/test_zookeeper_fixture.py | 112 +++++++++++++++++++++ 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 test/test_zookeeper_fixture.py diff --git a/servers/resources/0.0/zookeeper.properties b/servers/resources/0.0/zookeeper.properties index b146fac9e..52c95450e 100644 --- a/servers/resources/0.0/zookeeper.properties +++ b/servers/resources/0.0/zookeeper.properties @@ -20,3 +20,4 @@ clientPortAddress={host} # disable the per-ip limit on the number of connections since this is a non-production config maxClientCnxns=0 admin.enableServer=false +4lw.commands.whitelist=conf diff --git a/servers/resources/1.0/zookeeper.properties b/servers/resources/1.0/zookeeper.properties index b146fac9e..52c95450e 100644 --- a/servers/resources/1.0/zookeeper.properties +++ b/servers/resources/1.0/zookeeper.properties @@ -20,3 +20,4 @@ clientPortAddress={host} # disable the per-ip limit on the number of connections since this is a non-production config maxClientCnxns=0 admin.enableServer=false +4lw.commands.whitelist=conf diff --git a/test/integration/fixtures.py b/test/integration/fixtures.py index c6f018de5..912479850 100644 --- a/test/integration/fixtures.py +++ b/test/integration/fixtures.py @@ -319,7 +319,7 @@ def open(self): self.child = SpawnedService(args, env) self.child.start() timeout = min(timeout, max(end_at - time.time(), 0)) - if self.child.wait_for(r"binding to port", timeout=timeout): + if self._wait_for_ready(timeout=timeout): break self.child.dump_logs() self.child.stop() @@ -332,6 +332,45 @@ def open(self): self.out("Done!") atexit.register(self.close) + def _wait_for_ready(self, timeout): + # The bind log precedes the actual bind. A response from an unrelated + # listener is also insufficient: verify this fixture's unique data dir. + deadline = time.monotonic() + timeout + expected_dir = os.path.realpath(self.tmp_dir.join('version-2').strpath) + while self.child.is_alive(): + remaining = deadline - time.monotonic() + if remaining <= 0: + return False + try: + with socket.create_connection((self.host, self.port), + timeout=min(remaining, 0.5)) as connection: + connection.sendall(b'conf') + response = bytearray() + while True: + remaining = deadline - time.monotonic() + if remaining <= 0: + return False + connection.settimeout(min(remaining, 0.5)) + chunk = connection.recv(4096) + if not chunk: + break + response.extend(chunk) + if len(response) > 16384: + return False + configuration = dict(line.split('=', 1) for line in + response.decode('utf-8', errors='replace').splitlines() + if '=' in line) + if 'dataDir' in configuration: + return (configuration.get('clientPort') == str(self.port) + and os.path.realpath(configuration['dataDir']) == expected_dir + and self.child.is_alive()) + except OSError: + pass + remaining = deadline - time.monotonic() + if remaining > 0: + time.sleep(min(remaining, 0.1)) + return False + def close(self): if self.child is None: return diff --git a/test/test_zookeeper_fixture.py b/test/test_zookeeper_fixture.py new file mode 100644 index 000000000..7815da648 --- /dev/null +++ b/test/test_zookeeper_fixture.py @@ -0,0 +1,112 @@ +from unittest.mock import MagicMock +from types import SimpleNamespace + +import pytest + +from test.integration.fixtures import Fixture, ZookeeperFixture + + +@pytest.fixture +def zookeeper(tmpdir, monkeypatch): + monkeypatch.setattr(Fixture, 'kafka_root', str(tmpdir)) + fixture = ZookeeperFixture('127.0.0.1', 2181, tmp_dir=tmpdir) + fixture.child = MagicMock() + fixture.child.is_alive.return_value = True + yield fixture + fixture.child = None + + +def test_startup_rejects_a_pre_bind_log(zookeeper, monkeypatch): + service = MagicMock() + service.wait_for.return_value = True + monkeypatch.setattr('test.integration.fixtures.SpawnedService', lambda *args: service) + monkeypatch.setattr(zookeeper, 'render_template', lambda *args: None) + ready = MagicMock(return_value=False) + monkeypatch.setattr(zookeeper, '_wait_for_ready', ready, raising=False) + times = iter([0, 0, 0, 121]) + monkeypatch.setattr('test.integration.fixtures.time', SimpleNamespace( + time=lambda: next(times), sleep=lambda delay: None)) + + with pytest.raises(RuntimeError, match='Failed to start Zookeeper'): + zookeeper.open() + + ready.assert_called_once_with(timeout=5) + service.stop.assert_called_once_with() + + +@pytest.mark.parametrize('identity', ['ours', 'another-directory', 'another-port']) +def test_readiness_checks_the_server_identity(zookeeper, monkeypatch, identity): + directory = zookeeper.tmp_dir.join('version-2').strpath + port = zookeeper.port + if identity == 'another-directory': + directory += '-unrelated' + if identity == 'another-port': + port += 1 + response = ('clientPort=%d\ndataDir=%s\n' % (port, directory)).encode() + connection = MagicMock() + connection.__enter__.return_value = connection + connection.recv.side_effect = [response[:9], response[9:], b''] + monkeypatch.setattr('test.integration.fixtures.socket.create_connection', + lambda *args, **kwargs: connection) + + assert zookeeper._wait_for_ready(timeout=1) is (identity == 'ours') + + connection.sendall.assert_called_once_with(b'conf') + + +def test_readiness_retries_a_refused_connection(zookeeper, monkeypatch): + response = ('clientPort=%d\ndataDir=%s\n' % ( + zookeeper.port, zookeeper.tmp_dir.join('version-2').strpath)).encode() + connection = MagicMock() + connection.__enter__.return_value = connection + connection.recv.side_effect = [response, b''] + connect = MagicMock(side_effect=[ConnectionRefusedError(), connection]) + monkeypatch.setattr('test.integration.fixtures.socket.create_connection', connect) + monkeypatch.setattr('test.integration.fixtures.time.sleep', lambda delay: None) + + assert zookeeper._wait_for_ready(timeout=1) + + assert connect.call_count == 2 + + +def test_readiness_stops_when_the_child_exits(zookeeper, monkeypatch): + zookeeper.child.is_alive.return_value = False + connect = MagicMock() + monkeypatch.setattr('test.integration.fixtures.socket.create_connection', connect) + + assert not zookeeper._wait_for_ready(timeout=1) + + connect.assert_not_called() + + +def test_auto_port_retries_after_a_failed_identity_check(zookeeper, monkeypatch): + zookeeper.port = None + services = [MagicMock(), MagicMock()] + spawn = MagicMock(side_effect=services) + monkeypatch.setattr('test.integration.fixtures.SpawnedService', spawn) + monkeypatch.setattr(zookeeper, 'render_template', lambda *args: None) + monkeypatch.setattr('test.integration.fixtures.get_open_port', + MagicMock(side_effect=[2181, 2182])) + ready = MagicMock(side_effect=[False, True]) + monkeypatch.setattr(zookeeper, '_wait_for_ready', ready) + monkeypatch.setattr('test.integration.fixtures.time', SimpleNamespace( + time=lambda: 0, sleep=lambda delay: None)) + + zookeeper.open() + + assert zookeeper.port == 2182 + assert ready.call_count == 2 + services[0].stop.assert_called_once_with() + services[1].stop.assert_not_called() + + +def test_readiness_bounds_the_configuration_response(zookeeper, monkeypatch): + connection = MagicMock() + connection.__enter__.return_value = connection + connection.recv.side_effect = [b'x' * 4096] * 4 + [b'x'] + monkeypatch.setattr('test.integration.fixtures.socket.create_connection', + lambda *args, **kwargs: connection) + + assert not zookeeper._wait_for_ready(timeout=1) + + assert connection.recv.call_count == 5