blob: 496d9e0c9031a6fb5ff58de78a3a80541a2e0e11 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: MIT
5#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006
Andrew Geissler8f840682023-07-21 09:09:43 -05007import enum
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008import os
9import re
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010
11# A parser that can be used to identify weather a line is a test result or a section statement.
Andrew Geissler99467da2019-02-25 18:54:23 -060012class PtestParser(object):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013 def __init__(self):
Andrew Geissler99467da2019-02-25 18:54:23 -060014 self.results = {}
15 self.sections = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016
Andrew Geissler99467da2019-02-25 18:54:23 -060017 def parse(self, logfile):
18 test_regex = {}
19 test_regex['PASSED'] = re.compile(r"^PASS:(.+)")
Brad Bishopf3fd2882019-06-21 08:06:37 -040020 test_regex['FAILED'] = re.compile(r"^FAIL:([^(]+)")
Andrew Geissler99467da2019-02-25 18:54:23 -060021 test_regex['SKIPPED'] = re.compile(r"^SKIP:(.+)")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050022
Andrew Geissler99467da2019-02-25 18:54:23 -060023 section_regex = {}
24 section_regex['begin'] = re.compile(r"^BEGIN: .*/(.+)/ptest")
25 section_regex['end'] = re.compile(r"^END: .*/(.+)/ptest")
26 section_regex['duration'] = re.compile(r"^DURATION: (.+)")
27 section_regex['exitcode'] = re.compile(r"^ERROR: Exit status is (.+)")
28 section_regex['timeout'] = re.compile(r"^TIMEOUT: .*/(.+)/ptest")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050029
Andrew Geissler82c905d2020-04-13 13:39:40 -050030 # Cache markers so we don't take the re.search() hit all the time.
31 markers = ("PASS:", "FAIL:", "SKIP:", "BEGIN:", "END:", "DURATION:", "ERROR: Exit", "TIMEOUT:")
32
Andrew Geissler99467da2019-02-25 18:54:23 -060033 def newsection():
Andrew Geissler82c905d2020-04-13 13:39:40 -050034 return { 'name': "No-section", 'log': [] }
Andrew Geissler99467da2019-02-25 18:54:23 -060035
36 current_section = newsection()
37
38 with open(logfile, errors='replace') as f:
39 for line in f:
Andrew Geissler82c905d2020-04-13 13:39:40 -050040 if not line.startswith(markers):
41 current_section['log'].append(line)
42 continue
43
Andrew Geissler99467da2019-02-25 18:54:23 -060044 result = section_regex['begin'].search(line)
45 if result:
46 current_section['name'] = result.group(1)
Andrew Geissler028142b2023-05-05 11:29:21 -050047 if current_section['name'] not in self.results:
48 self.results[current_section['name']] = {}
Andrew Geissler99467da2019-02-25 18:54:23 -060049 continue
50
51 result = section_regex['end'].search(line)
52 if result:
53 if current_section['name'] != result.group(1):
54 bb.warn("Ptest END log section mismatch %s vs. %s" % (current_section['name'], result.group(1)))
55 if current_section['name'] in self.sections:
56 bb.warn("Ptest duplicate section for %s" % (current_section['name']))
57 self.sections[current_section['name']] = current_section
58 del self.sections[current_section['name']]['name']
59 current_section = newsection()
60 continue
61
62 result = section_regex['timeout'].search(line)
63 if result:
64 if current_section['name'] != result.group(1):
65 bb.warn("Ptest TIMEOUT log section mismatch %s vs. %s" % (current_section['name'], result.group(1)))
66 current_section['timeout'] = True
67 continue
68
69 for t in ['duration', 'exitcode']:
70 result = section_regex[t].search(line)
71 if result:
72 current_section[t] = result.group(1)
73 continue
74
Andrew Geissler82c905d2020-04-13 13:39:40 -050075 current_section['log'].append(line)
Andrew Geissler99467da2019-02-25 18:54:23 -060076
77 for t in test_regex:
78 result = test_regex[t].search(line)
79 if result:
Patrick Williams520786c2023-06-25 16:20:36 -050080 try:
81 self.results[current_section['name']][result.group(1).strip()] = t
82 except KeyError:
83 bb.warn("Result with no section: %s - %s" % (t, result.group(1).strip()))
Andrew Geissler99467da2019-02-25 18:54:23 -060084
Andrew Geissler82c905d2020-04-13 13:39:40 -050085 # Python performance for repeatedly joining long strings is poor, do it all at once at the end.
86 # For 2.1 million lines in a log this reduces 18 hours to 12s.
87 for section in self.sections:
88 self.sections[section]['log'] = "".join(self.sections[section]['log'])
89
Andrew Geissler99467da2019-02-25 18:54:23 -060090 return self.results, self.sections
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091
92 # Log the results as files. The file name is the section name and the contents are the tests in that section.
Andrew Geissler99467da2019-02-25 18:54:23 -060093 def results_as_files(self, target_dir):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 if not os.path.exists(target_dir):
95 raise Exception("Target directory does not exist: %s" % target_dir)
96
Andrew Geissler99467da2019-02-25 18:54:23 -060097 for section in self.results:
98 prefix = 'No-section'
Brad Bishopd7bf8c12018-02-25 22:55:05 -050099 if section:
Andrew Geissler99467da2019-02-25 18:54:23 -0600100 prefix = section
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 section_file = os.path.join(target_dir, prefix)
102 # purge the file contents if it exists
Andrew Geissler99467da2019-02-25 18:54:23 -0600103 with open(section_file, 'w') as f:
104 for test_name in sorted(self.results[section]):
105 status = self.results[section][test_name]
106 f.write(status + ": " + test_name + "\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107
Brad Bishopc342db32019-05-15 21:57:59 -0400108
Andrew Geissler8f840682023-07-21 09:09:43 -0500109class LtpParser:
110 """
111 Parse the machine-readable LTP log output into a ptest-friendly data structure.
112 """
Brad Bishopc342db32019-05-15 21:57:59 -0400113 def parse(self, logfile):
Andrew Geissler8f840682023-07-21 09:09:43 -0500114 results = {}
115 # Aaccumulate the duration here but as the log rounds quick tests down
116 # to 0 seconds this is very much a lower bound. The caller can replace
117 # the value.
118 section = {"duration": 0, "log": ""}
Brad Bishopc342db32019-05-15 21:57:59 -0400119
Andrew Geissler8f840682023-07-21 09:09:43 -0500120 class LtpExitCode(enum.IntEnum):
121 # Exit codes as defined in ltp/include/tst_res_flags.h
122 TPASS = 0 # Test passed flag
123 TFAIL = 1 # Test failed flag
124 TBROK = 2 # Test broken flag
125 TWARN = 4 # Test warning flag
126 TINFO = 16 # Test information flag
127 TCONF = 32 # Test not appropriate for configuration flag
128
129 with open(logfile, errors="replace") as f:
130 # Lines look like this:
131 # tag=cfs_bandwidth01 stime=1689762564 dur=0 exit=exited stat=32 core=no cu=0 cs=0
Brad Bishopc342db32019-05-15 21:57:59 -0400132 for line in f:
Andrew Geissler8f840682023-07-21 09:09:43 -0500133 if not line.startswith("tag="):
134 continue
Brad Bishopc342db32019-05-15 21:57:59 -0400135
Andrew Geissler8f840682023-07-21 09:09:43 -0500136 values = dict(s.split("=") for s in line.strip().split())
Brad Bishopc342db32019-05-15 21:57:59 -0400137
Andrew Geissler8f840682023-07-21 09:09:43 -0500138 section["duration"] += int(values["dur"])
139 exitcode = int(values["stat"])
140 if values["exit"] == "exited" and exitcode == LtpExitCode.TCONF:
141 # Exited normally with the "invalid configuration" code
142 results[values["tag"]] = "SKIPPED"
143 elif exitcode == LtpExitCode.TPASS:
144 # Successful exit
145 results[values["tag"]] = "PASSED"
146 else:
147 # Other exit
148 results[values["tag"]] = "FAILED"
149
150 return results, section
Brad Bishopc342db32019-05-15 21:57:59 -0400151
152
153# ltp Compliance log parsing
154class LtpComplianceParser(object):
155 def __init__(self):
156 self.results = {}
157 self.section = {'duration': "", 'log': ""}
158
159 def parse(self, logfile):
160 test_regex = {}
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000161 test_regex['FAILED'] = re.compile(r"FAIL")
Brad Bishopc342db32019-05-15 21:57:59 -0400162
163 section_regex = {}
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000164 section_regex['test'] = re.compile(r"^Executing")
Brad Bishopc342db32019-05-15 21:57:59 -0400165
166 with open(logfile, errors='replace') as f:
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000167 name = logfile
168 result = "PASSED"
Brad Bishopc342db32019-05-15 21:57:59 -0400169 for line in f:
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000170 regex_result = section_regex['test'].search(line)
171 if regex_result:
172 name = line.split()[1].strip()
Brad Bishopc342db32019-05-15 21:57:59 -0400173
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000174 regex_result = test_regex['FAILED'].search(line)
175 if regex_result:
176 result = "FAILED"
177 self.results[name] = result
Brad Bishopc342db32019-05-15 21:57:59 -0400178
179 for test in self.results:
180 result = self.results[test]
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000181 print (self.results)
Brad Bishopc342db32019-05-15 21:57:59 -0400182 self.section['log'] = self.section['log'] + ("%s: %s\n" % (result.strip()[:-2], test.strip()))
183
184 return self.results, self.section