blob: 7313df8ec35b04863f073a4d5ddc15f91f721a74 [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 Geissler99467da2019-02-25 18:54:23 -060028 def newsection():
29 return { 'name': "No-section", 'log': "" }
30
31 current_section = newsection()
32
33 with open(logfile, errors='replace') as f:
34 for line in f:
35 result = section_regex['begin'].search(line)
36 if result:
37 current_section['name'] = result.group(1)
38 continue
39
40 result = section_regex['end'].search(line)
41 if result:
42 if current_section['name'] != result.group(1):
43 bb.warn("Ptest END log section mismatch %s vs. %s" % (current_section['name'], result.group(1)))
44 if current_section['name'] in self.sections:
45 bb.warn("Ptest duplicate section for %s" % (current_section['name']))
46 self.sections[current_section['name']] = current_section
47 del self.sections[current_section['name']]['name']
48 current_section = newsection()
49 continue
50
51 result = section_regex['timeout'].search(line)
52 if result:
53 if current_section['name'] != result.group(1):
54 bb.warn("Ptest TIMEOUT log section mismatch %s vs. %s" % (current_section['name'], result.group(1)))
55 current_section['timeout'] = True
56 continue
57
58 for t in ['duration', 'exitcode']:
59 result = section_regex[t].search(line)
60 if result:
61 current_section[t] = result.group(1)
62 continue
63
64 current_section['log'] = current_section['log'] + line
65
66 for t in test_regex:
67 result = test_regex[t].search(line)
68 if result:
69 if current_section['name'] not in self.results:
70 self.results[current_section['name']] = {}
Brad Bishopf3fd2882019-06-21 08:06:37 -040071 self.results[current_section['name']][result.group(1).strip()] = t
Andrew Geissler99467da2019-02-25 18:54:23 -060072
73 return self.results, self.sections
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074
75 # 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 -060076 def results_as_files(self, target_dir):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 if not os.path.exists(target_dir):
78 raise Exception("Target directory does not exist: %s" % target_dir)
79
Andrew Geissler99467da2019-02-25 18:54:23 -060080 for section in self.results:
81 prefix = 'No-section'
Brad Bishopd7bf8c12018-02-25 22:55:05 -050082 if section:
Andrew Geissler99467da2019-02-25 18:54:23 -060083 prefix = section
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084 section_file = os.path.join(target_dir, prefix)
85 # purge the file contents if it exists
Andrew Geissler99467da2019-02-25 18:54:23 -060086 with open(section_file, 'w') as f:
87 for test_name in sorted(self.results[section]):
88 status = self.results[section][test_name]
89 f.write(status + ": " + test_name + "\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050090
Brad Bishopc342db32019-05-15 21:57:59 -040091
92# ltp log parsing
93class LtpParser(object):
94 def __init__(self):
95 self.results = {}
96 self.section = {'duration': "", 'log': ""}
97
98 def parse(self, logfile):
99 test_regex = {}
100 test_regex['PASSED'] = re.compile(r"PASS")
101 test_regex['FAILED'] = re.compile(r"FAIL")
102 test_regex['SKIPPED'] = re.compile(r"SKIP")
103
104 with open(logfile, errors='replace') as f:
105 for line in f:
106 for t in test_regex:
107 result = test_regex[t].search(line)
108 if result:
109 self.results[line.split()[0].strip()] = t
110
111 for test in self.results:
112 result = self.results[test]
113 self.section['log'] = self.section['log'] + ("%s: %s\n" % (result.strip()[:-2], test.strip()))
114
115 return self.results, self.section
116
117
118# ltp Compliance log parsing
119class LtpComplianceParser(object):
120 def __init__(self):
121 self.results = {}
122 self.section = {'duration': "", 'log': ""}
123
124 def parse(self, logfile):
125 test_regex = {}
126 test_regex['PASSED'] = re.compile(r"^PASS")
127 test_regex['FAILED'] = re.compile(r"^FAIL")
128 test_regex['SKIPPED'] = re.compile(r"(?:UNTESTED)|(?:UNSUPPORTED)")
129
130 section_regex = {}
131 section_regex['test'] = re.compile(r"^Testing")
132
133 with open(logfile, errors='replace') as f:
134 for line in f:
135 result = section_regex['test'].search(line)
136 if result:
137 self.name = ""
138 self.name = line.split()[1].strip()
139 self.results[self.name] = "PASSED"
140 failed = 0
141
142 failed_result = test_regex['FAILED'].search(line)
143 if failed_result:
144 failed = line.split()[1].strip()
145 if int(failed) > 0:
146 self.results[self.name] = "FAILED"
147
148 for test in self.results:
149 result = self.results[test]
150 self.section['log'] = self.section['log'] + ("%s: %s\n" % (result.strip()[:-2], test.strip()))
151
152 return self.results, self.section