Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | |
| 7 | import sys |
| 8 | import os |
| 9 | import re |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | |
| 11 | # 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] | 12 | class PtestParser(object): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | def __init__(self): |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 14 | self.results = {} |
| 15 | self.sections = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 17 | def parse(self, logfile): |
| 18 | test_regex = {} |
| 19 | test_regex['PASSED'] = re.compile(r"^PASS:(.+)") |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 20 | test_regex['FAILED'] = re.compile(r"^FAIL:([^(]+)") |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 21 | test_regex['SKIPPED'] = re.compile(r"^SKIP:(.+)") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 23 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 30 | # 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 Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 33 | def newsection(): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 34 | return { 'name': "No-section", 'log': [] } |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 35 | |
| 36 | current_section = newsection() |
| 37 | |
| 38 | with open(logfile, errors='replace') as f: |
| 39 | for line in f: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 40 | if not line.startswith(markers): |
| 41 | current_section['log'].append(line) |
| 42 | continue |
| 43 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 44 | result = section_regex['begin'].search(line) |
| 45 | if result: |
| 46 | current_section['name'] = result.group(1) |
Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 47 | if current_section['name'] not in self.results: |
| 48 | self.results[current_section['name']] = {} |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 49 | 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 75 | current_section['log'].append(line) |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 76 | |
| 77 | for t in test_regex: |
| 78 | result = test_regex[t].search(line) |
| 79 | if result: |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 80 | 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 Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 84 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 85 | # 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 Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 90 | return self.results, self.sections |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 91 | |
| 92 | # 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] | 93 | def results_as_files(self, target_dir): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | if not os.path.exists(target_dir): |
| 95 | raise Exception("Target directory does not exist: %s" % target_dir) |
| 96 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 97 | for section in self.results: |
| 98 | prefix = 'No-section' |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 99 | if section: |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 100 | prefix = section |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | section_file = os.path.join(target_dir, prefix) |
| 102 | # purge the file contents if it exists |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 103 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 107 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 108 | |
| 109 | # ltp log parsing |
| 110 | class LtpParser(object): |
| 111 | def __init__(self): |
| 112 | self.results = {} |
| 113 | self.section = {'duration': "", 'log': ""} |
| 114 | |
| 115 | def parse(self, logfile): |
| 116 | test_regex = {} |
| 117 | test_regex['PASSED'] = re.compile(r"PASS") |
| 118 | test_regex['FAILED'] = re.compile(r"FAIL") |
| 119 | test_regex['SKIPPED'] = re.compile(r"SKIP") |
| 120 | |
| 121 | with open(logfile, errors='replace') as f: |
| 122 | for line in f: |
| 123 | for t in test_regex: |
| 124 | result = test_regex[t].search(line) |
| 125 | if result: |
| 126 | self.results[line.split()[0].strip()] = t |
| 127 | |
| 128 | for test in self.results: |
| 129 | result = self.results[test] |
| 130 | self.section['log'] = self.section['log'] + ("%s: %s\n" % (result.strip()[:-2], test.strip())) |
| 131 | |
| 132 | return self.results, self.section |
| 133 | |
| 134 | |
| 135 | # ltp Compliance log parsing |
| 136 | class LtpComplianceParser(object): |
| 137 | def __init__(self): |
| 138 | self.results = {} |
| 139 | self.section = {'duration': "", 'log': ""} |
| 140 | |
| 141 | def parse(self, logfile): |
| 142 | test_regex = {} |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 143 | test_regex['FAILED'] = re.compile(r"FAIL") |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 144 | |
| 145 | section_regex = {} |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 146 | section_regex['test'] = re.compile(r"^Executing") |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 147 | |
| 148 | with open(logfile, errors='replace') as f: |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 149 | name = logfile |
| 150 | result = "PASSED" |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 151 | for line in f: |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 152 | regex_result = section_regex['test'].search(line) |
| 153 | if regex_result: |
| 154 | name = line.split()[1].strip() |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 155 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 156 | regex_result = test_regex['FAILED'].search(line) |
| 157 | if regex_result: |
| 158 | result = "FAILED" |
| 159 | self.results[name] = result |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 160 | |
| 161 | for test in self.results: |
| 162 | result = self.results[test] |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 163 | print (self.results) |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 164 | self.section['log'] = self.section['log'] + ("%s: %s\n" % (result.strip()[:-2], test.strip())) |
| 165 | |
| 166 | return self.results, self.section |