blob: 7cb79a8402aab4ca028b5e14c1b83f63b84ea34e [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
7import sys
8import 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)
47 continue
48
49 result = section_regex['end'].search(line)
50 if result:
51 if current_section['name'] != result.group(1):
52 bb.warn("Ptest END log section mismatch %s vs. %s" % (current_section['name'], result.group(1)))
53 if current_section['name'] in self.sections:
54 bb.warn("Ptest duplicate section for %s" % (current_section['name']))
55 self.sections[current_section['name']] = current_section
56 del self.sections[current_section['name']]['name']
57 current_section = newsection()
58 continue
59
60 result = section_regex['timeout'].search(line)
61 if result:
62 if current_section['name'] != result.group(1):
63 bb.warn("Ptest TIMEOUT log section mismatch %s vs. %s" % (current_section['name'], result.group(1)))
64 current_section['timeout'] = True
65 continue
66
67 for t in ['duration', 'exitcode']:
68 result = section_regex[t].search(line)
69 if result:
70 current_section[t] = result.group(1)
71 continue
72
Andrew Geissler82c905d2020-04-13 13:39:40 -050073 current_section['log'].append(line)
Andrew Geissler99467da2019-02-25 18:54:23 -060074
75 for t in test_regex:
76 result = test_regex[t].search(line)
77 if result:
78 if current_section['name'] not in self.results:
79 self.results[current_section['name']] = {}
Brad Bishopf3fd2882019-06-21 08:06:37 -040080 self.results[current_section['name']][result.group(1).strip()] = t
Andrew Geissler99467da2019-02-25 18:54:23 -060081
Andrew Geissler82c905d2020-04-13 13:39:40 -050082 # Python performance for repeatedly joining long strings is poor, do it all at once at the end.
83 # For 2.1 million lines in a log this reduces 18 hours to 12s.
84 for section in self.sections:
85 self.sections[section]['log'] = "".join(self.sections[section]['log'])
86
Andrew Geissler99467da2019-02-25 18:54:23 -060087 return self.results, self.sections
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088
89 # 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 -060090 def results_as_files(self, target_dir):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091 if not os.path.exists(target_dir):
92 raise Exception("Target directory does not exist: %s" % target_dir)
93
Andrew Geissler99467da2019-02-25 18:54:23 -060094 for section in self.results:
95 prefix = 'No-section'
Brad Bishopd7bf8c12018-02-25 22:55:05 -050096 if section:
Andrew Geissler99467da2019-02-25 18:54:23 -060097 prefix = section
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 section_file = os.path.join(target_dir, prefix)
99 # purge the file contents if it exists
Andrew Geissler99467da2019-02-25 18:54:23 -0600100 with open(section_file, 'w') as f:
101 for test_name in sorted(self.results[section]):
102 status = self.results[section][test_name]
103 f.write(status + ": " + test_name + "\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104
Brad Bishopc342db32019-05-15 21:57:59 -0400105
106# ltp log parsing
107class LtpParser(object):
108 def __init__(self):
109 self.results = {}
110 self.section = {'duration': "", 'log': ""}
111
112 def parse(self, logfile):
113 test_regex = {}
114 test_regex['PASSED'] = re.compile(r"PASS")
115 test_regex['FAILED'] = re.compile(r"FAIL")
116 test_regex['SKIPPED'] = re.compile(r"SKIP")
117
118 with open(logfile, errors='replace') as f:
119 for line in f:
120 for t in test_regex:
121 result = test_regex[t].search(line)
122 if result:
123 self.results[line.split()[0].strip()] = t
124
125 for test in self.results:
126 result = self.results[test]
127 self.section['log'] = self.section['log'] + ("%s: %s\n" % (result.strip()[:-2], test.strip()))
128
129 return self.results, self.section
130
131
132# ltp Compliance log parsing
133class LtpComplianceParser(object):
134 def __init__(self):
135 self.results = {}
136 self.section = {'duration': "", 'log': ""}
137
138 def parse(self, logfile):
139 test_regex = {}
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000140 test_regex['FAILED'] = re.compile(r"FAIL")
Brad Bishopc342db32019-05-15 21:57:59 -0400141
142 section_regex = {}
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000143 section_regex['test'] = re.compile(r"^Executing")
Brad Bishopc342db32019-05-15 21:57:59 -0400144
145 with open(logfile, errors='replace') as f:
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000146 name = logfile
147 result = "PASSED"
Brad Bishopc342db32019-05-15 21:57:59 -0400148 for line in f:
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000149 regex_result = section_regex['test'].search(line)
150 if regex_result:
151 name = line.split()[1].strip()
Brad Bishopc342db32019-05-15 21:57:59 -0400152
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000153 regex_result = test_regex['FAILED'].search(line)
154 if regex_result:
155 result = "FAILED"
156 self.results[name] = result
Brad Bishopc342db32019-05-15 21:57:59 -0400157
158 for test in self.results:
159 result = self.results[test]
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000160 print (self.results)
Brad Bishopc342db32019-05-15 21:57:59 -0400161 self.section['log'] = self.section['log'] + ("%s: %s\n" % (result.strip()[:-2], test.strip()))
162
163 return self.results, self.section