Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 4 | |
| 5 | import sys |
| 6 | import os |
| 7 | import re |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | |
| 9 | # A parser that can be used to identify weather a line is a test result or a section statement. |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 10 | class PtestParser(object): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | def __init__(self): |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 12 | self.results = {} |
| 13 | self.sections = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 14 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 15 | def parse(self, logfile): |
| 16 | test_regex = {} |
| 17 | test_regex['PASSED'] = re.compile(r"^PASS:(.+)") |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 18 | test_regex['FAILED'] = re.compile(r"^FAIL:([^(]+)") |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 19 | test_regex['SKIPPED'] = re.compile(r"^SKIP:(.+)") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 21 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 28 | # 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 Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 31 | def newsection(): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 32 | return { 'name': "No-section", 'log': [] } |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 33 | |
| 34 | current_section = newsection() |
| 35 | |
| 36 | with open(logfile, errors='replace') as f: |
| 37 | for line in f: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 38 | if not line.startswith(markers): |
| 39 | current_section['log'].append(line) |
| 40 | continue |
| 41 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 42 | 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 71 | current_section['log'].append(line) |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 72 | |
| 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 Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 78 | self.results[current_section['name']][result.group(1).strip()] = t |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 79 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 80 | # 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 Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 85 | return self.results, self.sections |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 86 | |
| 87 | # Log the results as files. The file name is the section name and the contents are the tests in that section. |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 88 | def results_as_files(self, target_dir): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 89 | if not os.path.exists(target_dir): |
| 90 | raise Exception("Target directory does not exist: %s" % target_dir) |
| 91 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 92 | for section in self.results: |
| 93 | prefix = 'No-section' |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 94 | if section: |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 95 | prefix = section |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 96 | section_file = os.path.join(target_dir, prefix) |
| 97 | # purge the file contents if it exists |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 98 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 102 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 103 | |
| 104 | # ltp log parsing |
| 105 | class 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 |
| 131 | class 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 |