| 1 |
#! /usr/bin/env python3 |
| 2 |
# SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 3 |
# SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 4 |
# |
| 5 |
# Wrapper around CTest for DUNE |
| 6 |
# |
| 7 |
# CTest returns with an error status not only when tests failed, but also |
| 8 |
# when tests were only skipped. This wrapper checks the log and returns |
| 9 |
# successfully if no tests failed; skipped tests do not result in an error. |
| 10 |
# This behaviour is needed in a continuous integration environment, when |
| 11 |
# building binary packages or in other cases where the testsuite should be |
| 12 |
# run automatically. |
| 13 |
# |
| 14 |
# Moreover, this script also converts the XML test report generated by CTest |
| 15 |
# into a JUnit report file that can be consumed by a lot of reporting |
| 16 |
# software. |
| 17 |
# |
| 18 |
# Author: Ansgar Burchardt <[email protected]> |
| 19 |
# Author: Steffen Müthing <[email protected]> (for the JUnit part) |
| 20 |
|
| 21 |
import errno |
| 22 |
import glob |
| 23 |
import os.path |
| 24 |
import shutil |
| 25 |
import subprocess |
| 26 |
import sys |
| 27 |
import xml.etree.ElementTree as et |
| 28 |
from pathlib import Path |
| 29 |
import os |
| 30 |
import re |
| 31 |
|
| 32 |
|
| 33 |
class CTestParser: |
| 34 |
|
| 35 |
def findCTestOutput(self): |
| 36 |
files = glob.glob("Testing/*/Test.xml") |
| 37 |
if len(files) != 1: |
| 38 |
fn = files.join(", ") |
| 39 |
raise Exception("Found multiple CTest output files: {}".format(files.join(", "))) |
| 40 |
return files[0] |
| 41 |
|
| 42 |
def printTest(self,test,output=None): |
| 43 |
status = test.get("Status") |
| 44 |
name = test.find("Name").text |
| 45 |
fullName = test.find("FullName").text |
| 46 |
if output is not None: |
| 47 |
output = test.find("Results").find("Measurement").find("Value").text |
| 48 |
|
| 49 |
print("======================================================================") |
| 50 |
print("Name: {}".format(name)) |
| 51 |
print("FullName: {}".format(fullName)) |
| 52 |
print("Status: {}".format(status.upper())) |
| 53 |
if output: |
| 54 |
print("Output:") |
| 55 |
for line in output.splitlines(): |
| 56 |
print(" ", line) |
| 57 |
print() |
| 58 |
|
| 59 |
def __init__(self,junitpath=None): |
| 60 |
self.inputpath = self.findCTestOutput() |
| 61 |
if junitpath is None: |
| 62 |
if "CI_PROJECT_DIR" in os.environ: |
| 63 |
buildroot = Path(os.environ["CI_PROJECT_DIR"]) |
| 64 |
# create a slug from the project name |
| 65 |
name = os.environ["CI_PROJECT_NAME"].lower() |
| 66 |
name = re.sub(r"[^-a-z0-9]","-",name); |
| 67 |
junitbasename = "{}-".format(name) |
| 68 |
else: |
| 69 |
buildroot = Path.cwd() |
| 70 |
junitbasename = "" |
| 71 |
junitdir = buildroot / "junit" |
| 72 |
junitdir.mkdir(parents=True,exist_ok=True) |
| 73 |
self.junitpath = junitdir / "{}cmake.xml".format(junitbasename) |
| 74 |
else: |
| 75 |
self.junitpath = Path(junitpath) |
| 76 |
junitdir = junitpath.resolve().parent |
| 77 |
junitdir.mkdir(parents=True,exist_ok=True) |
| 78 |
self.tests = 0 |
| 79 |
self.passed = 0 |
| 80 |
self.failures = 0 |
| 81 |
self.skipped = 0 |
| 82 |
self.errors = 0 |
| 83 |
self.skipped = 0 |
| 84 |
self.time = 0.0 |
| 85 |
|
| 86 |
def createJUnitSkeleton(self): |
| 87 |
self.testsuites = et.Element("testsuites") |
| 88 |
self.testsuite = et.SubElement(self.testsuites,"testsuite") |
| 89 |
self.properties = et.SubElement(self.testsuite,"properties") |
| 90 |
|
| 91 |
def fillJUnitStatistics(self): |
| 92 |
self.testsuite.set("name","cmake") |
| 93 |
self.testsuite.set("tests",str(self.tests)) |
| 94 |
self.testsuite.set("disabled","0") |
| 95 |
self.testsuite.set("errors",str(self.errors)) |
| 96 |
self.testsuite.set("failures",str(self.failures)) |
| 97 |
self.testsuite.set("skipped",str(self.skipped)) |
| 98 |
self.testsuite.set("time",str(self.time)) |
| 99 |
|
| 100 |
def processTest(self,test): |
| 101 |
testcase = et.SubElement(self.testsuite,"testcase") |
| 102 |
testcase.set("name",test.find("Name").text) |
| 103 |
testcase.set("assertions","1") |
| 104 |
testcase.set("classname","cmake") |
| 105 |
time = test.find("./Results/NamedMeasurement[@name='Execution Time']/Value") |
| 106 |
if time is not None: |
| 107 |
self.time += float(time.text) |
| 108 |
testcase.set("time",time.text) |
| 109 |
self.tests += 1 |
| 110 |
outcome = test.get("Status") |
| 111 |
if outcome == "passed": |
| 112 |
testcase.set("status","passed") |
| 113 |
self.passed += 1 |
| 114 |
elif outcome == "failed": |
| 115 |
self.failures += 1 |
| 116 |
testcase.set("status","failure") |
| 117 |
failure = et.SubElement(testcase,"failure") |
| 118 |
failure.set("message","program execution failed") |
| 119 |
failure.text = test.find("./Results/Measurement/Value").text |
| 120 |
self.printTest(test) |
| 121 |
elif outcome == "notrun": |
| 122 |
# This does not exit on older CMake versions, so work around that |
| 123 |
try: |
| 124 |
status = test.find("./Results/NamedMeasurement[@name='Completion Status']/Value").text |
| 125 |
if status == "SKIP_RETURN_CODE=77": |
| 126 |
self.skipped += 1 |
| 127 |
et.SubElement(testcase,"skipped") |
| 128 |
elif status == "Required Files Missing": |
| 129 |
self.errors += 1 |
| 130 |
error = et.SubElement(testcase,"error") |
| 131 |
error.set("message","compilation failed") |
| 132 |
error.set("type","compilation error") |
| 133 |
self.printTest(test,output="Compilation error") |
| 134 |
else: |
| 135 |
error = et.SubElement(testcase,"error") |
| 136 |
error.set("message","unknown error during test execution") |
| 137 |
error.set("type","unknown") |
| 138 |
error.text = test.find("./Results/Measurement/Value").text |
| 139 |
self.errors += 1 |
| 140 |
self.printTest(test) |
| 141 |
except AttributeError: |
| 142 |
output_tag = test.find("./Results/Measurement/Value") |
| 143 |
if output_tag is not None: |
| 144 |
msg = output_tag.text |
| 145 |
if "skipped" in msg: |
| 146 |
self.skipped += 1 |
| 147 |
et.SubElement(testcase,"skipped") |
| 148 |
elif "Unable to find required file" in msg: |
| 149 |
self.errors += 1 |
| 150 |
error = et.SubElement(testcase,"error") |
| 151 |
error.set("message","compilation failed") |
| 152 |
error.set("type","compilation error") |
| 153 |
self.printTest(test,output="Compilation error") |
| 154 |
else: |
| 155 |
error = et.SubElement(testcase,"error") |
| 156 |
error.set("message","unknown error during test execution") |
| 157 |
error.set("type","unknown") |
| 158 |
error.text = msg |
| 159 |
self.errors += 1 |
| 160 |
self.printTest(test) |
| 161 |
else: |
| 162 |
error = et.SubElement(testcase,"error") |
| 163 |
error.set("message","unknown error during test execution") |
| 164 |
error.set("type","unknown") |
| 165 |
error.text = "no message" |
| 166 |
self.errors += 1 |
| 167 |
self.printTest(test) |
| 168 |
|
| 169 |
output_tag = test.find("./Results/Measurement/Value") |
| 170 |
if output_tag is not None: |
| 171 |
out = et.SubElement(testcase,"system-out") |
| 172 |
out.text = output_tag.text |
| 173 |
|
| 174 |
def process(self): |
| 175 |
|
| 176 |
with open(self.inputpath, "r", encoding="utf-8") as fh: |
| 177 |
tree = et.parse(fh) |
| 178 |
|
| 179 |
root = tree.getroot() |
| 180 |
|
| 181 |
self.createJUnitSkeleton() |
| 182 |
|
| 183 |
for test in root.findall(".//Testing/Test"): |
| 184 |
self.processTest(test) |
| 185 |
|
| 186 |
self.fillJUnitStatistics() |
| 187 |
|
| 188 |
with self.junitpath.open("wb") as fh: |
| 189 |
fh.write(et.tostring(self.testsuites,encoding="utf-8")) |
| 190 |
print("JUnit report for CTest results written to {}".format(self.junitpath)) |
| 191 |
|
| 192 |
return self.errors + self.failures |
| 193 |
|
| 194 |
|
| 195 |
def runCTest(argv=[]): |
| 196 |
cmd = ["ctest", |
| 197 |
"--output-on-failure", |
| 198 |
"--dashboard", "ExperimentalTest", |
| 199 |
"--no-compress-output", |
| 200 |
] |
| 201 |
cmd.extend(argv) |
| 202 |
subprocess.call(cmd) |
| 203 |
|
| 204 |
def checkDirectory(): |
| 205 |
if not os.path.exists("CMakeCache.txt"): |
| 206 |
raise Exception("ERROR: dune-ctest must be run in a cmake build directory") |
| 207 |
|
| 208 |
def removeCTestOutput(): |
| 209 |
try: |
| 210 |
shutil.rmtree("Testing") |
| 211 |
except OSError as e: |
| 212 |
if e.errno != errno.ENOENT: |
| 213 |
raise |
| 214 |
|
| 215 |
def main(): |
| 216 |
try: |
| 217 |
checkDirectory() |
| 218 |
removeCTestOutput() |
| 219 |
runCTest(argv=sys.argv[1:]) |
| 220 |
parser = CTestParser() |
| 221 |
errors = parser.process() |
| 222 |
status = 0 if errors == 0 else 1 |
| 223 |
sys.exit(status) |
| 224 |
except Exception as e: |
| 225 |
print("Internal error: {}".format(e)) |
| 226 |
sys.exit(127) |
| 227 |
|
| 228 |
if __name__ == "__main__": |
| 229 |
main() |