|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright (C) 2022 Intel Corporation |
| 4 | +# |
| 5 | +# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. |
| 6 | +# See LICENSE.TXT |
| 7 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | + |
| 9 | +from subprocess import Popen, DEVNULL, PIPE |
| 10 | +import argparse |
| 11 | +import os |
| 12 | +import json |
| 13 | + |
| 14 | +TMP_RESULTS_FILE = "tmp-results-file.json" |
| 15 | + |
| 16 | + |
| 17 | +def get_cts_test_suite_names(working_directory): |
| 18 | + process = Popen( |
| 19 | + ["ctest", "--show-only=json-v1"], |
| 20 | + cwd=working_directory, |
| 21 | + stdout=PIPE, |
| 22 | + env=os.environ.copy(), |
| 23 | + ) |
| 24 | + out, _ = process.communicate() |
| 25 | + testsuites = json.loads(out) |
| 26 | + return [test["name"][: test["name"].rfind("-")] for test in testsuites["tests"]] |
| 27 | + |
| 28 | + |
| 29 | +def percent(amount, total): |
| 30 | + return round((amount / (total or 1)) * 100, 2) |
| 31 | + |
| 32 | + |
| 33 | +def summarize_results(results): |
| 34 | + total = results["Total"] |
| 35 | + total_passed = len(results["Passed"]) |
| 36 | + total_skipped = len(results["Skipped"]) |
| 37 | + total_failed = len(results["Failed"]) |
| 38 | + total_crashed = total - (total_passed + total_skipped + total_failed) |
| 39 | + |
| 40 | + pass_rate_incl_skipped = percent(total_passed + total_skipped, total) |
| 41 | + pass_rate_excl_skipped = percent(total_passed, total) |
| 42 | + |
| 43 | + skipped_rate = percent(total_skipped, total) |
| 44 | + failed_rate = percent(total_failed, total) |
| 45 | + crash_rate = percent(total_crashed, total) |
| 46 | + |
| 47 | + ljust_param = len(str(total)) |
| 48 | + |
| 49 | + print( |
| 50 | + f"""[CTest Parser] Results: |
| 51 | + Total [{str(total).ljust(ljust_param)}] |
| 52 | + Passed [{str(total_passed).ljust(ljust_param)}] ({pass_rate_incl_skipped}%) - ({pass_rate_excl_skipped}% with skipped tests excluded) |
| 53 | + Skipped [{str(total_skipped).ljust(ljust_param)}] ({skipped_rate}%) |
| 54 | + Failed [{str(total_failed).ljust(ljust_param)}] ({failed_rate}%) |
| 55 | + Crashed [{str(total_crashed).ljust(ljust_param)}] ({crash_rate}%) |
| 56 | +""" |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def parse_results(results): |
| 61 | + parsed_results = { |
| 62 | + "Passed": {}, |
| 63 | + "Skipped": {}, |
| 64 | + "Failed": {}, |
| 65 | + "Crashed": {}, |
| 66 | + "Total": 0, |
| 67 | + "Success": True, |
| 68 | + } |
| 69 | + for _, result in results.items(): |
| 70 | + if result["actual"] is None: |
| 71 | + parsed_results["Success"] = False |
| 72 | + parsed_results["Total"] += result["expected"]["tests"] |
| 73 | + continue |
| 74 | + |
| 75 | + parsed_results["Total"] += result["actual"]["tests"] |
| 76 | + for testsuite in result["actual"].get("testsuites"): |
| 77 | + for test in testsuite.get("testsuite"): |
| 78 | + test_name = f"{testsuite['name']}.{test['name']}" |
| 79 | + test_time = test["time"] |
| 80 | + if "failures" in test: |
| 81 | + parsed_results["Failed"][test_name] = {"time": test_time} |
| 82 | + elif test["result"] == "SKIPPED": |
| 83 | + parsed_results["Skipped"][test_name] = {"time": test_time} |
| 84 | + else: |
| 85 | + parsed_results["Passed"][test_name] = {"time": test_time} |
| 86 | + return parsed_results |
| 87 | + |
| 88 | + |
| 89 | +def run(args): |
| 90 | + results = {} |
| 91 | + |
| 92 | + tmp_results_file = f"{args.ctest_path}/{TMP_RESULTS_FILE}" |
| 93 | + env = os.environ.copy() |
| 94 | + env["GTEST_OUTPUT"] = f"json:{tmp_results_file}" |
| 95 | + |
| 96 | + test_suite_names = get_cts_test_suite_names(f"{args.ctest_path}/test/conformance/") |
| 97 | + |
| 98 | + ## try and list all the available tests |
| 99 | + for suite in test_suite_names: |
| 100 | + results[suite] = {} |
| 101 | + test_executable = f"{args.ctest_path}/bin/test-{suite}" |
| 102 | + process = Popen( |
| 103 | + [test_executable, "--gtest_list_tests"], |
| 104 | + env=env, |
| 105 | + stdout=DEVNULL if args.quiet else None, |
| 106 | + stderr=DEVNULL if args.quiet else None, |
| 107 | + ) |
| 108 | + process.wait() |
| 109 | + try: |
| 110 | + with open(tmp_results_file, "r") as test_list: |
| 111 | + all_tests = json.load(test_list) |
| 112 | + results[suite]["expected"] = all_tests |
| 113 | + os.remove(tmp_results_file) |
| 114 | + except FileNotFoundError: |
| 115 | + print(f"Could not discover tests for {suite}") |
| 116 | + |
| 117 | + for suite in test_suite_names: |
| 118 | + ctest_path = f"{args.ctest_path}/test/conformance/{suite}" |
| 119 | + process = Popen( |
| 120 | + ["ctest", ctest_path], |
| 121 | + env=env, |
| 122 | + cwd=ctest_path, |
| 123 | + stdout=DEVNULL if args.quiet else None, |
| 124 | + stderr=DEVNULL if args.quiet else None, |
| 125 | + ) |
| 126 | + process.wait() |
| 127 | + |
| 128 | + try: |
| 129 | + with open(tmp_results_file, "r") as results_file: |
| 130 | + json_data = json.load(results_file) |
| 131 | + results[suite]["actual"] = json_data |
| 132 | + os.remove(tmp_results_file) |
| 133 | + except FileNotFoundError: |
| 134 | + results[suite]["actual"] = None |
| 135 | + print( |
| 136 | + "\033[91m" |
| 137 | + + f"Conformance test suite '{suite}' : likely crashed!" |
| 138 | + + "\033[0m" |
| 139 | + ) |
| 140 | + |
| 141 | + return results |
| 142 | + |
| 143 | + |
| 144 | +def dir_path(string): |
| 145 | + if os.path.isdir(string): |
| 146 | + return os.path.abspath(string) |
| 147 | + else: |
| 148 | + raise NotADirectoryError(string) |
| 149 | + |
| 150 | + |
| 151 | +def main(): |
| 152 | + parser = argparse.ArgumentParser() |
| 153 | + parser.add_argument( |
| 154 | + "ctest_path", |
| 155 | + type=dir_path, |
| 156 | + nargs="?", |
| 157 | + default=".", |
| 158 | + help="Optional path to test directory containing " |
| 159 | + "CTestTestfile. Defaults to current directory.", |
| 160 | + ) |
| 161 | + parser.add_argument( |
| 162 | + "-q", "--quiet", action="store_true", help="Output only failed tests." |
| 163 | + ) |
| 164 | + args = parser.parse_args() |
| 165 | + |
| 166 | + raw_results = run(args) |
| 167 | + parsed_results = parse_results(raw_results) |
| 168 | + summarize_results(parsed_results) |
| 169 | + |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + try: |
| 173 | + main() |
| 174 | + except KeyboardInterrupt: |
| 175 | + exit(130) |
0 commit comments