Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 1 | # test result tool - report text based test results |
| 2 | # |
| 3 | # Copyright (c) 2019, Intel Corporation. |
| 4 | # Copyright (c) 2019, Linux Foundation |
| 5 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 6 | # SPDX-License-Identifier: GPL-2.0-only |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 7 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 8 | |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 9 | import os |
| 10 | import glob |
| 11 | import json |
| 12 | import resulttool.resultutils as resultutils |
| 13 | from oeqa.utils.git import GitRepo |
| 14 | import oeqa.utils.gitarchive as gitarchive |
| 15 | |
| 16 | |
| 17 | class ResultsTextReport(object): |
| 18 | def __init__(self): |
| 19 | self.ptests = {} |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 20 | self.ltptests = {} |
| 21 | self.ltpposixtests = {} |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 22 | self.result_types = {'passed': ['PASSED', 'passed', 'PASS', 'XFAIL'], |
| 23 | 'failed': ['FAILED', 'failed', 'FAIL', 'ERROR', 'error', 'UNKNOWN', 'XPASS'], |
| 24 | 'skipped': ['SKIPPED', 'skipped', 'UNSUPPORTED', 'UNTESTED', 'UNRESOLVED']} |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 25 | |
| 26 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 27 | def handle_ptest_result(self, k, status, result, machine): |
| 28 | if machine not in self.ptests: |
| 29 | self.ptests[machine] = {} |
| 30 | |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 31 | if k == 'ptestresult.sections': |
| 32 | # Ensure tests without any test results still show up on the report |
| 33 | for suite in result['ptestresult.sections']: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 34 | if suite not in self.ptests[machine]: |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame^] | 35 | self.ptests[machine][suite] = { |
| 36 | 'passed': 0, 'failed': 0, 'skipped': 0, 'duration' : '-', |
| 37 | 'failed_testcases': [], "testcases": set(), |
| 38 | } |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 39 | if 'duration' in result['ptestresult.sections'][suite]: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 40 | self.ptests[machine][suite]['duration'] = result['ptestresult.sections'][suite]['duration'] |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 41 | if 'timeout' in result['ptestresult.sections'][suite]: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 42 | self.ptests[machine][suite]['duration'] += " T" |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame^] | 43 | return True |
| 44 | |
| 45 | # process test result |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 46 | try: |
| 47 | _, suite, test = k.split(".", 2) |
| 48 | except ValueError: |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame^] | 49 | return True |
| 50 | |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 51 | # Handle 'glib-2.0' |
| 52 | if 'ptestresult.sections' in result and suite not in result['ptestresult.sections']: |
| 53 | try: |
| 54 | _, suite, suite1, test = k.split(".", 3) |
| 55 | if suite + "." + suite1 in result['ptestresult.sections']: |
| 56 | suite = suite + "." + suite1 |
| 57 | except ValueError: |
| 58 | pass |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame^] | 59 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 60 | if suite not in self.ptests[machine]: |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame^] | 61 | self.ptests[machine][suite] = { |
| 62 | 'passed': 0, 'failed': 0, 'skipped': 0, 'duration' : '-', |
| 63 | 'failed_testcases': [], "testcases": set(), |
| 64 | } |
| 65 | |
| 66 | # do not process duplicate results |
| 67 | if test in self.ptests[machine][suite]["testcases"]: |
| 68 | print("Warning duplicate ptest result '{}.{}' for {}".format(suite, test, machine)) |
| 69 | return False |
| 70 | |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 71 | for tk in self.result_types: |
| 72 | if status in self.result_types[tk]: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 73 | self.ptests[machine][suite][tk] += 1 |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame^] | 74 | self.ptests[machine][suite]["testcases"].add(test) |
| 75 | return True |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 76 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 77 | def handle_ltptest_result(self, k, status, result, machine): |
| 78 | if machine not in self.ltptests: |
| 79 | self.ltptests[machine] = {} |
| 80 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 81 | if k == 'ltpresult.sections': |
| 82 | # Ensure tests without any test results still show up on the report |
| 83 | for suite in result['ltpresult.sections']: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 84 | if suite not in self.ltptests[machine]: |
| 85 | self.ltptests[machine][suite] = {'passed': 0, 'failed': 0, 'skipped': 0, 'duration' : '-', 'failed_testcases': []} |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 86 | if 'duration' in result['ltpresult.sections'][suite]: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 87 | self.ltptests[machine][suite]['duration'] = result['ltpresult.sections'][suite]['duration'] |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 88 | if 'timeout' in result['ltpresult.sections'][suite]: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 89 | self.ltptests[machine][suite]['duration'] += " T" |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 90 | return |
| 91 | try: |
| 92 | _, suite, test = k.split(".", 2) |
| 93 | except ValueError: |
| 94 | return |
| 95 | # Handle 'glib-2.0' |
| 96 | if 'ltpresult.sections' in result and suite not in result['ltpresult.sections']: |
| 97 | try: |
| 98 | _, suite, suite1, test = k.split(".", 3) |
| 99 | print("split2: %s %s %s" % (suite, suite1, test)) |
| 100 | if suite + "." + suite1 in result['ltpresult.sections']: |
| 101 | suite = suite + "." + suite1 |
| 102 | except ValueError: |
| 103 | pass |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 104 | if suite not in self.ltptests[machine]: |
| 105 | self.ltptests[machine][suite] = {'passed': 0, 'failed': 0, 'skipped': 0, 'duration' : '-', 'failed_testcases': []} |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 106 | for tk in self.result_types: |
| 107 | if status in self.result_types[tk]: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 108 | self.ltptests[machine][suite][tk] += 1 |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 109 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 110 | def handle_ltpposixtest_result(self, k, status, result, machine): |
| 111 | if machine not in self.ltpposixtests: |
| 112 | self.ltpposixtests[machine] = {} |
| 113 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 114 | if k == 'ltpposixresult.sections': |
| 115 | # Ensure tests without any test results still show up on the report |
| 116 | for suite in result['ltpposixresult.sections']: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 117 | if suite not in self.ltpposixtests[machine]: |
| 118 | self.ltpposixtests[machine][suite] = {'passed': 0, 'failed': 0, 'skipped': 0, 'duration' : '-', 'failed_testcases': []} |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 119 | if 'duration' in result['ltpposixresult.sections'][suite]: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 120 | self.ltpposixtests[machine][suite]['duration'] = result['ltpposixresult.sections'][suite]['duration'] |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 121 | return |
| 122 | try: |
| 123 | _, suite, test = k.split(".", 2) |
| 124 | except ValueError: |
| 125 | return |
| 126 | # Handle 'glib-2.0' |
| 127 | if 'ltpposixresult.sections' in result and suite not in result['ltpposixresult.sections']: |
| 128 | try: |
| 129 | _, suite, suite1, test = k.split(".", 3) |
| 130 | if suite + "." + suite1 in result['ltpposixresult.sections']: |
| 131 | suite = suite + "." + suite1 |
| 132 | except ValueError: |
| 133 | pass |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 134 | if suite not in self.ltpposixtests[machine]: |
| 135 | self.ltpposixtests[machine][suite] = {'passed': 0, 'failed': 0, 'skipped': 0, 'duration' : '-', 'failed_testcases': []} |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 136 | for tk in self.result_types: |
| 137 | if status in self.result_types[tk]: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 138 | self.ltpposixtests[machine][suite][tk] += 1 |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 139 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 140 | def get_aggregated_test_result(self, logger, testresult, machine): |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 141 | test_count_report = {'passed': 0, 'failed': 0, 'skipped': 0, 'failed_testcases': []} |
| 142 | result = testresult.get('result', []) |
| 143 | for k in result: |
| 144 | test_status = result[k].get('status', []) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame^] | 145 | if k.startswith("ptestresult."): |
| 146 | if not self.handle_ptest_result(k, test_status, result, machine): |
| 147 | continue |
| 148 | elif k.startswith("ltpresult."): |
| 149 | self.handle_ltptest_result(k, test_status, result, machine) |
| 150 | elif k.startswith("ltpposixresult."): |
| 151 | self.handle_ltpposixtest_result(k, test_status, result, machine) |
| 152 | |
| 153 | # process result if it was not skipped by a handler |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 154 | for tk in self.result_types: |
| 155 | if test_status in self.result_types[tk]: |
| 156 | test_count_report[tk] += 1 |
| 157 | if test_status in self.result_types['failed']: |
| 158 | test_count_report['failed_testcases'].append(k) |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 159 | return test_count_report |
| 160 | |
| 161 | def print_test_report(self, template_file_name, test_count_reports): |
| 162 | from jinja2 import Environment, FileSystemLoader |
| 163 | script_path = os.path.dirname(os.path.realpath(__file__)) |
| 164 | file_loader = FileSystemLoader(script_path + '/template') |
| 165 | env = Environment(loader=file_loader, trim_blocks=True) |
| 166 | template = env.get_template(template_file_name) |
| 167 | havefailed = False |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 168 | reportvalues = [] |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 169 | machines = [] |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 170 | cols = ['passed', 'failed', 'skipped'] |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 171 | maxlen = {'passed' : 0, 'failed' : 0, 'skipped' : 0, 'result_id': 0, 'testseries' : 0, 'ptest' : 0 ,'ltptest': 0, 'ltpposixtest': 0} |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 172 | for line in test_count_reports: |
| 173 | total_tested = line['passed'] + line['failed'] + line['skipped'] |
| 174 | vals = {} |
| 175 | vals['result_id'] = line['result_id'] |
| 176 | vals['testseries'] = line['testseries'] |
| 177 | vals['sort'] = line['testseries'] + "_" + line['result_id'] |
| 178 | vals['failed_testcases'] = line['failed_testcases'] |
| 179 | for k in cols: |
| 180 | vals[k] = "%d (%s%%)" % (line[k], format(line[k] / total_tested * 100, '.0f')) |
| 181 | for k in maxlen: |
| 182 | if k in vals and len(vals[k]) > maxlen[k]: |
| 183 | maxlen[k] = len(vals[k]) |
| 184 | reportvalues.append(vals) |
| 185 | if line['failed_testcases']: |
| 186 | havefailed = True |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 187 | if line['machine'] not in machines: |
| 188 | machines.append(line['machine']) |
| 189 | for (machine, report) in self.ptests.items(): |
| 190 | for ptest in self.ptests[machine]: |
| 191 | if len(ptest) > maxlen['ptest']: |
| 192 | maxlen['ptest'] = len(ptest) |
| 193 | for (machine, report) in self.ltptests.items(): |
| 194 | for ltptest in self.ltptests[machine]: |
| 195 | if len(ltptest) > maxlen['ltptest']: |
| 196 | maxlen['ltptest'] = len(ltptest) |
| 197 | for (machine, report) in self.ltpposixtests.items(): |
| 198 | for ltpposixtest in self.ltpposixtests[machine]: |
| 199 | if len(ltpposixtest) > maxlen['ltpposixtest']: |
| 200 | maxlen['ltpposixtest'] = len(ltpposixtest) |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 201 | output = template.render(reportvalues=reportvalues, |
| 202 | havefailed=havefailed, |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 203 | machines=machines, |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 204 | ptests=self.ptests, |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 205 | ltptests=self.ltptests, |
| 206 | ltpposixtests=self.ltpposixtests, |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 207 | maxlen=maxlen) |
| 208 | print(output) |
| 209 | |
| 210 | def view_test_report(self, logger, source_dir, branch, commit, tag): |
| 211 | test_count_reports = [] |
| 212 | if commit: |
| 213 | if tag: |
| 214 | logger.warning("Ignoring --tag as --commit was specified") |
| 215 | tag_name = "{branch}/{commit_number}-g{commit}/{tag_number}" |
| 216 | repo = GitRepo(source_dir) |
| 217 | revs = gitarchive.get_test_revs(logger, repo, tag_name, branch=branch) |
| 218 | rev_index = gitarchive.rev_find(revs, 'commit', commit) |
| 219 | testresults = resultutils.git_get_result(repo, revs[rev_index][2]) |
| 220 | elif tag: |
| 221 | repo = GitRepo(source_dir) |
| 222 | testresults = resultutils.git_get_result(repo, [tag]) |
| 223 | else: |
| 224 | testresults = resultutils.load_resultsdata(source_dir) |
| 225 | for testsuite in testresults: |
| 226 | for resultid in testresults[testsuite]: |
Brad Bishop | c68388fc | 2019-08-26 01:33:31 -0400 | [diff] [blame] | 227 | skip = False |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 228 | result = testresults[testsuite][resultid] |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 229 | machine = result['configuration']['MACHINE'] |
Brad Bishop | c68388fc | 2019-08-26 01:33:31 -0400 | [diff] [blame] | 230 | |
| 231 | # Check to see if there is already results for these kinds of tests for the machine |
| 232 | for key in result['result'].keys(): |
| 233 | testtype = str(key).split('.')[0] |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame^] | 234 | if ((machine in self.ltptests and testtype == "ltpiresult" and self.ltptests[machine]) or |
Brad Bishop | c68388fc | 2019-08-26 01:33:31 -0400 | [diff] [blame] | 235 | (machine in self.ltpposixtests and testtype == "ltpposixresult" and self.ltpposixtests[machine])): |
| 236 | print("Already have test results for %s on %s, skipping %s" %(str(key).split('.')[0], machine, resultid)) |
| 237 | skip = True |
| 238 | break |
| 239 | if skip: |
| 240 | break |
| 241 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 242 | test_count_report = self.get_aggregated_test_result(logger, result, machine) |
| 243 | test_count_report['machine'] = machine |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 244 | test_count_report['testseries'] = result['configuration']['TESTSERIES'] |
| 245 | test_count_report['result_id'] = resultid |
| 246 | test_count_reports.append(test_count_report) |
| 247 | self.print_test_report('test_report_full_text.txt', test_count_reports) |
| 248 | |
| 249 | def report(args, logger): |
| 250 | report = ResultsTextReport() |
| 251 | report.view_test_report(logger, args.source_dir, args.branch, args.commit, args.tag) |
| 252 | return 0 |
| 253 | |
| 254 | def register_commands(subparsers): |
| 255 | """Register subcommands from this plugin""" |
| 256 | parser_build = subparsers.add_parser('report', help='summarise test results', |
| 257 | description='print a text-based summary of the test results', |
| 258 | group='analysis') |
| 259 | parser_build.set_defaults(func=report) |
| 260 | parser_build.add_argument('source_dir', |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 261 | help='source file/directory/URL that contain the test result files to summarise') |
Brad Bishop | 40320b1 | 2019-03-26 16:08:25 -0400 | [diff] [blame] | 262 | parser_build.add_argument('--branch', '-B', default='master', help="Branch to find commit in") |
| 263 | parser_build.add_argument('--commit', help="Revision to report") |
| 264 | parser_build.add_argument('-t', '--tag', default='', |
| 265 | help='source_dir is a git repository, report on the tag specified from that repository') |