Skip to content

Commit 4c3f1aa

Browse files
authored
Add step to verify junit report from shared dir (#65371)
Junitparser's verify subcommand fails if there is only one test suite This can be removed when weiwei/junitparser#142 is merged
1 parent a99b800 commit 4c3f1aa

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../OWNERS
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/bin/bash
2+
3+
set -o nounset
4+
set -o errexit
5+
set -o pipefail
6+
set -x
7+
8+
# Fix user IDs in a container
9+
~/fix_uid.sh
10+
11+
pip install --user junitparser
12+
13+
# Need to do this because of a bug in junitparser's verify subcommand which fails if there is only one test suite
14+
# This can be removed when https://github.com/weiwei/junitparser/pull/142 is merged
15+
cat << EOF_SCRIPT > fail_if_any_test_failed.py
16+
import sys
17+
import os
18+
import glob
19+
from junitparser import JUnitXml, TestSuite
20+
21+
def verify_junit_reports(directory):
22+
"""
23+
Verify all JUnit XML reports in the given directory.
24+
Returns 1 if any test failed, 0 if all tests passed or were skipped.
25+
"""
26+
if not os.path.exists(directory):
27+
print(f"Error: Directory {directory} does not exist")
28+
return 1
29+
30+
# Find all XML files in the directory
31+
xml_pattern = os.path.join(directory, "*.xml")
32+
xml_files = glob.glob(xml_pattern)
33+
34+
if not xml_files:
35+
print(f"Warning: No XML files found in {directory}")
36+
return 0
37+
38+
print(f"Found {len(xml_files)} XML file(s) to verify:")
39+
for xml_file in xml_files:
40+
print(f" - {os.path.basename(xml_file)}")
41+
42+
failed_tests = 0
43+
total_tests = 0
44+
45+
for xml_file in xml_files:
46+
try:
47+
print(f"\nProcessing: {os.path.basename(xml_file)}")
48+
xml = JUnitXml.fromfile(xml_file)
49+
50+
# Handle single testsuite case
51+
if isinstance(xml, TestSuite):
52+
xml = [xml]
53+
54+
for suite in xml:
55+
suite_name = suite.name or "Unknown Suite"
56+
suite_tests = 0
57+
suite_failures = 0
58+
59+
for case in suite:
60+
total_tests += 1
61+
suite_tests += 1
62+
63+
if not case.is_passed and not case.is_skipped:
64+
failed_tests += 1
65+
suite_failures += 1
66+
print(f" FAILED: {case.name} in {suite_name}")
67+
68+
if suite_failures == 0:
69+
print(f" ✓ Suite '{suite_name}': {suite_tests} test(s) passed")
70+
else:
71+
print(f" ✗ Suite '{suite_name}': {suite_failures}/{suite_tests} test(s) failed")
72+
73+
except Exception as e:
74+
print(f"Error parsing {xml_file}: {e}")
75+
return 1
76+
77+
print(f"\n=== Summary ===")
78+
print(f"Total tests: {total_tests}")
79+
print(f"Failed tests: {failed_tests}")
80+
81+
if failed_tests > 0:
82+
print(f"❌ {failed_tests} test(s) failed")
83+
return 1
84+
else:
85+
print("✅ All tests passed or were skipped")
86+
return 0
87+
88+
if __name__ == "__main__":
89+
shared_dir = "${SHARED_DIR}"
90+
sys.exit(verify_junit_reports(shared_dir))
91+
EOF_SCRIPT
92+
93+
python3 ./fail_if_any_test_failed.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"path": "telcov10n/verify-junit-reports/telcov10n-verify-junit-reports-ref.yaml",
3+
"owners": {
4+
"approvers": [
5+
"natifridman",
6+
"shaior",
7+
"ccardenosa",
8+
"cplacani",
9+
"mvk",
10+
"eifrach"
11+
]
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ref:
2+
as: telcov10n-verify-junit-reports
3+
from: src
4+
commands: telcov10n-verify-junit-reports-commands.sh
5+
grace_period: 4h
6+
timeout: 4h
7+
resources:
8+
requests:
9+
cpu: 100m
10+
memory: 200Mi

0 commit comments

Comments
 (0)