Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion rpms.in.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ packages:
- python3-flufl-lock
- python3-gobject-base
- python3-jsonschema
- python3-junit_xml
- python3-junitparser
- python3-koji
- python3-koji-cli-plugins
- python3-libvirt
Expand Down
2 changes: 1 addition & 1 deletion src/deps.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ python3-resultsdb_conventions-fedora
python3-fedfind

# For creating JUnit test results for Jenkins
python3-junit_xml
python3-junitparser

# For debugging running processes in the pipelines
strace
Expand Down
39 changes: 27 additions & 12 deletions src/kola-junit
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import os
import re
import sys

from junit_xml import TestSuite, TestCase
from junitparser import TestSuite, TestCase, JUnitXml, Failure, Skipped


parser = argparse.ArgumentParser()
Expand All @@ -31,6 +31,12 @@ def strip_ansi(text):
return ansi_escape.sub('', text)


def strip_control_chars(text):
if text is None:
return text
return re.sub(r'[\x00-\x08\x0b\x0c\x0e-\x1f]', '', text)


# In a {kola_outputdir}/{testname}/ dir, there can be multiple directories; one
# per host that was brought up. e.g. the Tang test brings up a Tang server in a
# separate host. We can only report one host, so we choose the one whose dir
Expand Down Expand Up @@ -89,11 +95,12 @@ for test in report['tests']:
# for external tests; we append the kola-runext.service output
if ext_test_out is not None:
test['output'] += f"\n---\n{ext_test_out}"
test['output'] = strip_ansi(test['output'])
test['output'] = strip_control_chars(strip_ansi(test['output']))

if test['result'] == 'PASS':
tc = TestCase(test['name'], args.classname + ".tests",
test['duration'] / 10**9, test["output"])
test['duration'] / 10**9)
tc.system_out = test["output"]
else:
# we only add console/journal for failing tests
console_txt = None
Expand All @@ -102,25 +109,33 @@ for test in report['tests']:
console_fn = os.path.join(host_dir, "console.txt")
if os.path.isfile(console_fn):
with open(console_fn, encoding='utf-8') as f:
console_txt = strip_ansi(f.read())
console_txt = strip_control_chars(strip_ansi(f.read()))
journal_fn = os.path.join(host_dir, "journal.txt")
if os.path.isfile(journal_fn):
with open(journal_fn, encoding='utf-8') as f:
journal_txt = f.read()
journal_txt = strip_control_chars(f.read())

tc = TestCase(test['name'], args.classname + ".tests",
test['duration'] / 10**9, console_txt, journal_txt)
test['duration'] / 10**9)
tc.system_out = console_txt
tc.system_err = journal_txt

if test["result"] == 'FAIL':
tc.add_failure_info(message="Test failed", output=test["output"])
fail = Failure("Test failed")
fail.text = test["output"]
tc.result = [fail]
elif test["result"] == 'SKIP':
tc.add_skipped_info(message="Test skipped", output=test["output"])
skip = Skipped("Test skipped")
skip.text = test["output"]
tc.result = [skip]
Comment thread
c4rt0 marked this conversation as resolved.

test_cases.append(tc)

ts = TestSuite(args.classname, test_cases)
ts = TestSuite(args.classname)
ts.add_testcases(test_cases)
xml = JUnitXml()
xml.add_testsuite(ts)
if args.output == '-':
TestSuite.to_file(sys.stdout, [ts])
xml.write('/dev/stdout')
else:
with open(args.outputxml, "w", encoding='utf-8') as f:
TestSuite.to_file(f, [ts])
xml.write(args.output)
Comment on lines 138 to +141

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Writing to '/dev/stdout' via xml.write() can be problematic in certain restricted environments (such as containerized builds or chroots) where /dev/stdout might not be available or writable. A more robust and portable approach is to write the serialized XML bytes directly to sys.stdout.buffer using xml.tostring().

Suggested change
if args.output == '-':
TestSuite.to_file(sys.stdout, [ts])
xml.write('/dev/stdout')
else:
with open(args.outputxml, "w", encoding='utf-8') as f:
TestSuite.to_file(f, [ts])
xml.write(args.output)
if args.output == '-':
sys.stdout.buffer.write(xml.tostring())
else:
xml.write(args.output)