Skip to content

Commit 2c36d69

Browse files
feat(testing): add more logging during test suite
Activate `pytest`'s `logcli` setting to better read the client lifecycle during testing. Add `log()` function to KazooTestHarness class so that it is possible to log like crazy when something is not working Display the ZK client port in logs when starting a ZK server (useful for test "debug") Be able to get more than the last 100 lines of ZK logs (can be useful, believe me)
1 parent d218dc9 commit 2c36d69

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

kazoo/testing/common.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,9 @@ def run(self):
221221
)
222222
self.process = subprocess.Popen(args=args)
223223
log.info(
224-
"Started zookeeper process %s using args %s",
224+
"Started zookeeper process %s on port %s using args %s",
225225
self.process.pid,
226+
self.server_info.client_port,
226227
args,
227228
)
228229
self._running = True
@@ -304,12 +305,12 @@ def destroy(self):
304305

305306
shutil.rmtree(self.working_path, True)
306307

307-
def get_logs(self):
308+
def get_logs(self, num_lines=100):
308309
log_path = pathlib.Path(self.working_path, "zookeeper.log")
309310
if log_path.exists():
310311
log_file = log_path.open("r")
311312
lines = log_file.readlines()
312-
return lines[-100:]
313+
return lines[-num_lines:]
313314
return []
314315

315316

kazoo/testing/harness.py

+3
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ def __init__(self, *args, **kw):
167167
def cluster(self):
168168
return get_global_cluster()
169169

170+
def log(self, level, msg, *args, **kwargs):
171+
log.log(level, msg, *args, **kwargs)
172+
170173
@property
171174
def servers(self):
172175
return ",".join([s.address for s in self.cluster])

kazoo/tests/conftest.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55

66
def pytest_exception_interact(node, call, report):
7-
if hasattr(node._testcase, "cluster"):
7+
try:
88
cluster = node._testcase.cluster
99
log.error("Zookeeper cluster logs:")
1010
for logs in cluster.get_logs():
1111
log.error(logs)
12+
except Exception:
13+
log.exception("Cannot get ZK logs:")

pyproject.toml

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ extend-exclude = '''
2121

2222
[tool.pytest.ini_options]
2323
addopts = "-ra -v"
24+
log_cli = true
25+
log_cli_date_format = "%Y-%m-%d %H:%M:%S"
26+
log_cli_format = "%(asctime)s %(levelname)s %(message)s"
27+
log_cli_level = "INFO"
2428

2529
[tool.mypy]
2630

0 commit comments

Comments
 (0)