Skip to content

Commit

Permalink
feat(testing): add more logging during test suite
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
StephenSorriaux committed Apr 24, 2023
1 parent d218dc9 commit 2c36d69
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
7 changes: 4 additions & 3 deletions kazoo/testing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,9 @@ def run(self):
)
self.process = subprocess.Popen(args=args)
log.info(
"Started zookeeper process %s using args %s",
"Started zookeeper process %s on port %s using args %s",
self.process.pid,
self.server_info.client_port,
args,
)
self._running = True
Expand Down Expand Up @@ -304,12 +305,12 @@ def destroy(self):

shutil.rmtree(self.working_path, True)

def get_logs(self):
def get_logs(self, num_lines=100):
log_path = pathlib.Path(self.working_path, "zookeeper.log")
if log_path.exists():
log_file = log_path.open("r")
lines = log_file.readlines()
return lines[-100:]
return lines[-num_lines:]
return []


Expand Down
3 changes: 3 additions & 0 deletions kazoo/testing/harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ def __init__(self, *args, **kw):
def cluster(self):
return get_global_cluster()

def log(self, level, msg, *args, **kwargs):
log.log(level, msg, *args, **kwargs)

@property
def servers(self):
return ",".join([s.address for s in self.cluster])
Expand Down
4 changes: 3 additions & 1 deletion kazoo/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@


def pytest_exception_interact(node, call, report):
if hasattr(node._testcase, "cluster"):
try:
cluster = node._testcase.cluster
log.error("Zookeeper cluster logs:")
for logs in cluster.get_logs():
log.error(logs)
except Exception:
log.exception("Cannot get ZK logs:")
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ extend-exclude = '''

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

[tool.mypy]

Expand Down

0 comments on commit 2c36d69

Please sign in to comment.