blob: 60e16d500eddc24ffede693e72b4c554a5ac1be8 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: MIT
3#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05004
5import sys
6import os
7import re
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008
9# 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 -060010class PtestParser(object):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011 def __init__(self):
Andrew Geissler99467da2019-02-25 18:54:23 -060012 self.results = {}
13 self.sections = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014
Andrew Geissler99467da2019-02-25 18:54:23 -060015 def parse(self, logfile):
16 test_regex = {}
17 test_regex['PASSED'] = re.compile(r"^PASS:(.+)")
Brad Bishopf3fd2882019-06-21 08:06:37 -040018 test_regex['FAILED'] = re.compile(r"^FAIL:([^(]+)")
Andrew Geissler99467da2019-02-25 18:54:23 -060019 test_regex['SKIPPED'] = re.compile(r"^SKIP:(.+)")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050020
Andrew Geissler99467da2019-02-25 18:54:23 -060021 section_regex = {}
22 section_regex['begin'] = re.compile(r"^BEGIN: .*/(.+)/ptest")
23 section_regex['end'] = re.compile(r"^END: .*/(.+)/ptest")
24 section_regex['duration'] = re.compile(r"^DURATION: (.+)")
25 section_regex['exitcode'] = re.compile(r"^ERROR: Exit status is (.+)")
26 section_regex['timeout'] = re.compile(r"^TIMEOUT: .*/(.+)/ptest")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027
Andrew Geissler82c905d2020-04-13 13:39:40 -050028 # Cache markers so we don't take the re.search() hit all the time.
29 markers = ("PASS:", "FAIL:", "SKIP:", "BEGIN:", "END:", "DURATION:", "ERROR: Exit", "TIMEOUT:")
30
Andrew Geissler99467da2019-02-25 18:54:23 -060031 def newsection():
Andrew Geissler82c905d2020-04-13 13:39:40 -050032 return { 'name': "No-section", 'log': [] }
Andrew Geissler99467da2019-02-25 18:54:23 -060033
34 current_section = newsection()
35
36 with open(logfile, errors='replace') as f:
37 for line in f:
Andrew Geissler82c905d2020-04-13 13:39:40 -050038 if not line.startswith(markers):
39 current_section['log'].append(line)
40 continue
41
Andrew Geissler99467da2019-02-25 18:54:23 -060042 result = section_regex['begin'].search(line)
43 if result:
44 current_section['name'] = result.group(1)
45 continue
46
47 result = section_regex['end'].search(line)
48 if result:
49 if current_section['name'] != result.group(1):
50 bb.warn("Ptest END log section mismatch %s vs. %s" % (current_section['name'], result.group(1)))
51 if current_section['name'] in self.sections:
52 bb.warn("Ptest duplicate section for %s" % (current_section['name']))
53 self.sections[current_section['name']] = current_section
54 del self.sections[current_section['name']]['name']
55 current_section = newsection()
56 continue
57
58 result = section_regex['timeout'].search(line)
59 if result:
60 if current_section['name'] != result.group(1):
61 bb.warn("Ptest TIMEOUT log section mismatch %s vs. %s" % (current_section['name'], result.group(1)))
62 current_section['timeout'] = True
63 continue
64
65 for t in ['duration', 'exitcode']:
66 result = section_regex[t].search(line)
67 if result:
68 current_section[t] = result.group(1)
69 continue
70
Andrew Geissler82c905d2020-04-13 13:39:40 -050071 current_section['log'].append(line)
Andrew Geissler99467da2019-02-25 18:54:23 -060072
73 for t in test_regex:
74 result = test_regex[t].search(line)
75 if result:
76 if current_section['name'] not in self.results:
77 self.results[current_section['name']] = {}
Brad Bishopf3fd2882019-06-21 08:06:37 -040078 self.results[current_section['name']][result.group(1).strip()] = t
Andrew Geissler99467da2019-02-25 18:54:23 -060079
Andrew Geissler82c905d2020-04-13 13:39:40 -050080 # Python performance for repeatedly joining long strings is poor, do it all at once at the end.
81 # For 2.1 million lines in a log this reduces 18 hours to 12s.
82 for section in self.sections:
83 self.sections[section]['log'] = "".join(self.sections[section]['log'])
84
Andrew Geissler99467da2019-02-25 18:54:23 -060085 return self.results, self.sections
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086
87 # 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 -060088 def results_as_files(self, target_dir):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089 if not os.path.exists(target_dir):
90 raise Exception("Target directory does not exist: %s" % target_dir)
91
Andrew Geissler99467da2019-02-25 18:54:23 -060092 for section in self.results:
93 prefix = 'No-section'
Brad Bishopd7bf8c12018-02-25 22:55:05 -050094 if section:
Andrew Geissler99467da2019-02-25 18:54:23 -060095 prefix = section
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096 section_file = os.path.join(target_dir, prefix)
97 # purge the file contents if it exists
Andrew Geissler99467da2019-02-25 18:54:23 -060098 with open(section_file, 'w') as f:
99 for test_name in sorted(self.results[section]):
100 status = self.results[section][test_name]
101 f.write(status + ": " + test_name + "\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102
Brad Bishopc342db32019-05-15 21:57:59 -0400103
104# ltp log parsing
105class LtpParser(object):
106 def __init__(self):
107 self.results = {}
108 self.section = {'duration': "", 'log': ""}
109
110 def parse(self, logfile):
111 test_regex = {}
112 test_regex['PASSED'] = re.compile(r"PASS")
113 test_regex['FAILED'] = re.compile(r"FAIL")
114 test_regex['SKIPPED'] = re.compile(r"SKIP")
115
116 with open(logfile, errors='replace') as f:
117 for line in f:
118 for t in test_regex:
119 result = test_regex[t].search(line)
120 if result:
121 self.results[line.split()[0].strip()] = t
122
123 for test in self.results:
124 result = self.results[test]
125 self.section['log'] = self.section['log'] + ("%s: %s\n" % (result.strip()[:-2], test.strip()))
126
127 return self.results, self.section
128
129
130# ltp Compliance log parsing
131class LtpComplianceParser(object):
132 def __init__(self):
133 self.results = {}
134 self.section = {'duration': "", 'log': ""}
135
136 def parse(self, logfile):
137 test_regex = {}
138 test_regex['PASSED'] = re.compile(r"^PASS")
139 test_regex['FAILED'] = re.compile(r"^FAIL")
140 test_regex['SKIPPED'] = re.compile(r"(?:UNTESTED)|(?:UNSUPPORTED)")
141
142 section_regex = {}
143 section_regex['test'] = re.compile(r"^Testing")
144
145 with open(logfile, errors='replace') as f:
146 for line in f:
147 result = section_regex['test'].search(line)
148 if result:
149 self.name = ""
150 self.name = line.split()[1].strip()
151 self.results[self.name] = "PASSED"
152 failed = 0
153
154 failed_result = test_regex['FAILED'].search(line)
155 if failed_result:
156 failed = line.split()[1].strip()
157 if int(failed) > 0:
158 self.results[self.name] = "FAILED"
159
160 for test in self.results:
161 result = self.results[test]
162 self.section['log'] = self.section['log'] + ("%s: %s\n" % (result.strip()[:-2], test.strip()))
163
164 return self.results, self.section