Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: set DucktapeJSONEncoder as default encoder in reports. #251

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions ducktape/tests/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def format_time(t):
return r


def json_dumps(obj, **kwargs):
"""
Dump object to json string with Ducktape json encoder.
"""
assert 'cls' not in kwargs, "Passing custom json encoder is forbidden."

return json.dumps(obj, cls=DucktapeJSONEncoder, **kwargs)


class SingleResultReporter(object):
"""Helper class for creating a view of results from a single test."""

Expand All @@ -64,7 +73,7 @@ def result_string(self):
result_lines.append(" " + self.result.summary)

if self.result.data is not None:
result_lines.append(json.dumps(self.result.data))
result_lines.append(json_dumps(self.result.data))

return "\n".join(result_lines)

Expand All @@ -85,7 +94,7 @@ def report(self):
if self.result.data is not None:
data_file = os.path.join(self.result.results_dir, "data.json")
with open(data_file, "w") as fp:
fp.write(json.dumps(self.result.data))
fp.write(json_dumps(self.result.data))


class SummaryReporter(object):
Expand Down Expand Up @@ -146,7 +155,7 @@ def __init__(self, results):
def report(self):
report_file = os.path.abspath(os.path.join(self.results.session_context.results_dir, "report.json"))
with open(report_file, "w") as f:
f.write(json.dumps(self.results, cls=DucktapeJSONEncoder, sort_keys=True, indent=2, separators=(',', ': ')))
f.write(json_dumps(self.results, sort_keys=True, indent=2, separators=(',', ': ')))


class HTMLSummaryReporter(SummaryReporter):
Expand All @@ -159,7 +168,7 @@ def format_test_name(self, result):
if result.injected_args is not None:
lines.append("Arguments:")
lines.append(
json.dumps(result.injected_args, sort_keys=True, indent=2, separators=(',', ': ')))
json_dumps(result.injected_args, sort_keys=True, indent=2, separators=(',', ': ')))

return "\n".join(lines)

Expand All @@ -171,7 +180,7 @@ def format_result(self, result):
"test_result": test_result,
"description": result.description,
"run_time": format_time(result.run_time_seconds),
"data": "" if result.data is None else json.dumps(result.data, sort_keys=True,
"data": "" if result.data is None else json_dumps(result.data, sort_keys=True,
indent=2, separators=(',', ': ')),
"test_log": self.test_results_dir(result)
}
Expand Down Expand Up @@ -199,7 +208,7 @@ def format_report(self):
ignored_result_string = ""

for result in self.results:
json_string = json.dumps(self.format_result(result))
json_string = json_dumps(self.format_result(result))
if result.test_status == PASS:
num_passes += 1
passed_result_string += json_string
Expand Down