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 | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 28 | 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 Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 71 | self.results[current_section['name']][result.group(1).strip()] = t |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 72 | |
| 73 | return self.results, self.sections |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | |
| 75 | # 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] | 76 | def results_as_files(self, target_dir): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | if not os.path.exists(target_dir): |
| 78 | raise Exception("Target directory does not exist: %s" % target_dir) |
| 79 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 80 | for section in self.results: |
| 81 | prefix = 'No-section' |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 82 | if section: |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 83 | prefix = section |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 84 | section_file = os.path.join(target_dir, prefix) |
| 85 | # purge the file contents if it exists |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 86 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 90 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 91 | |
| 92 | # ltp log parsing |
| 93 | class 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 |
| 119 | class 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 |