1 # SPDX-License-Identifier: GPL-2.0
3 # Parses test results from a kernel dmesg log.
5 # Copyright (C) 2019, Google LLC.
6 # Author: Felix Guo <felixguoxiuping@gmail.com>
7 # Author: Brendan Higgins <brendanhiggins@google.com>
11 from collections import namedtuple
12 from datetime import datetime
13 from enum import Enum, auto
14 from functools import reduce
15 from typing import List
17 TestResult = namedtuple('TestResult', ['status','suites','log'])
19 class TestSuite(object):
26 return 'TestSuite(' + self.status + ',' + self.name + ',' + str(self.cases) + ')'
31 class TestCase(object):
38 return 'TestCase(' + self.status + ',' + self.name + ',' + str(self.log) + ')'
43 class TestStatus(Enum):
49 kunit_start_re = re.compile(r'TAP version [0-9]+$')
50 kunit_end_re = re.compile('(List of all partitions:|'
51 'Kernel panic - not syncing: VFS:|reboot: System halted)')
53 def isolate_kunit_output(kernel_output):
55 for line in kernel_output:
56 if kunit_start_re.search(line):
57 prefix_len = len(line.split('TAP version')[0])
59 yield line[prefix_len:] if prefix_len > 0 else line
60 elif kunit_end_re.search(line):
63 yield line[prefix_len:] if prefix_len > 0 else line
65 def raw_output(kernel_output):
66 for line in kernel_output:
75 return '\033[1;31m' + text + RESET
78 return '\033[1;33m' + text + RESET
81 return '\033[1;32m' + text + RESET
83 def print_with_timestamp(message):
84 print('[%s] %s' % (datetime.now().strftime('%H:%M:%S'), message))
86 def format_suite_divider(message):
87 return '======== ' + message + ' ========'
89 def print_suite_divider(message):
90 print_with_timestamp(DIVIDER)
91 print_with_timestamp(format_suite_divider(message))
95 print_with_timestamp(m)
97 TAP_ENTRIES = re.compile(r'^(TAP|[\s]*ok|[\s]*not ok|[\s]*[0-9]+\.\.[0-9]+|[\s]*#).*$')
99 def consume_non_diagnositic(lines: List[str]) -> None:
100 while lines and not TAP_ENTRIES.match(lines[0]):
103 def save_non_diagnositic(lines: List[str], test_case: TestCase) -> None:
104 while lines and not TAP_ENTRIES.match(lines[0]):
105 test_case.log.append(lines[0])
108 OkNotOkResult = namedtuple('OkNotOkResult', ['is_ok','description', 'text'])
110 OK_NOT_OK_SUBTEST = re.compile(r'^[\s]+(ok|not ok) [0-9]+ - (.*)$')
112 OK_NOT_OK_MODULE = re.compile(r'^(ok|not ok) [0-9]+ - (.*)$')
114 def parse_ok_not_ok_test_case(lines: List[str], test_case: TestCase) -> bool:
115 save_non_diagnositic(lines, test_case)
117 test_case.status = TestStatus.TEST_CRASHED
120 match = OK_NOT_OK_SUBTEST.match(line)
121 while not match and lines:
123 match = OK_NOT_OK_SUBTEST.match(line)
125 test_case.log.append(lines.pop(0))
126 test_case.name = match.group(2)
127 if test_case.status == TestStatus.TEST_CRASHED:
129 if match.group(1) == 'ok':
130 test_case.status = TestStatus.SUCCESS
132 test_case.status = TestStatus.FAILURE
137 SUBTEST_DIAGNOSTIC = re.compile(r'^[\s]+# .*?: (.*)$')
138 DIAGNOSTIC_CRASH_MESSAGE = 'kunit test case crashed!'
140 def parse_diagnostic(lines: List[str], test_case: TestCase) -> bool:
141 save_non_diagnositic(lines, test_case)
145 match = SUBTEST_DIAGNOSTIC.match(line)
147 test_case.log.append(lines.pop(0))
148 if match.group(1) == DIAGNOSTIC_CRASH_MESSAGE:
149 test_case.status = TestStatus.TEST_CRASHED
154 def parse_test_case(lines: List[str]) -> TestCase:
155 test_case = TestCase()
156 save_non_diagnositic(lines, test_case)
157 while parse_diagnostic(lines, test_case):
159 if parse_ok_not_ok_test_case(lines, test_case):
164 SUBTEST_HEADER = re.compile(r'^[\s]+# Subtest: (.*)$')
166 def parse_subtest_header(lines: List[str]) -> str:
167 consume_non_diagnositic(lines)
170 match = SUBTEST_HEADER.match(lines[0])
173 return match.group(1)
177 SUBTEST_PLAN = re.compile(r'[\s]+[0-9]+\.\.([0-9]+)')
179 def parse_subtest_plan(lines: List[str]) -> int:
180 consume_non_diagnositic(lines)
181 match = SUBTEST_PLAN.match(lines[0])
184 return int(match.group(1))
188 def max_status(left: TestStatus, right: TestStatus) -> TestStatus:
189 if left == TestStatus.TEST_CRASHED or right == TestStatus.TEST_CRASHED:
190 return TestStatus.TEST_CRASHED
191 elif left == TestStatus.FAILURE or right == TestStatus.FAILURE:
192 return TestStatus.FAILURE
193 elif left != TestStatus.SUCCESS:
195 elif right != TestStatus.SUCCESS:
198 return TestStatus.SUCCESS
200 def parse_ok_not_ok_test_suite(lines: List[str], test_suite: TestSuite) -> bool:
201 consume_non_diagnositic(lines)
203 test_suite.status = TestStatus.TEST_CRASHED
206 match = OK_NOT_OK_MODULE.match(line)
209 if match.group(1) == 'ok':
210 test_suite.status = TestStatus.SUCCESS
212 test_suite.status = TestStatus.FAILURE
217 def bubble_up_errors(to_status, status_container_list) -> TestStatus:
218 status_list = map(to_status, status_container_list)
219 return reduce(max_status, status_list, TestStatus.SUCCESS)
221 def bubble_up_test_case_errors(test_suite: TestSuite) -> TestStatus:
222 max_test_case_status = bubble_up_errors(lambda x: x.status, test_suite.cases)
223 return max_status(max_test_case_status, test_suite.status)
225 def parse_test_suite(lines: List[str]) -> TestSuite:
228 consume_non_diagnositic(lines)
229 test_suite = TestSuite()
230 test_suite.status = TestStatus.SUCCESS
231 name = parse_subtest_header(lines)
234 test_suite.name = name
235 expected_test_case_num = parse_subtest_plan(lines)
236 if not expected_test_case_num:
238 while expected_test_case_num > 0:
239 test_case = parse_test_case(lines)
242 test_suite.cases.append(test_case)
243 expected_test_case_num -= 1
244 if parse_ok_not_ok_test_suite(lines, test_suite):
245 test_suite.status = bubble_up_test_case_errors(test_suite)
248 print_with_timestamp(red('[ERROR] ') + 'ran out of lines before end token')
251 print('failed to parse end of suite' + lines[0])
254 TAP_HEADER = re.compile(r'^TAP version 14$')
256 def parse_tap_header(lines: List[str]) -> bool:
257 consume_non_diagnositic(lines)
258 if TAP_HEADER.match(lines[0]):
264 def bubble_up_suite_errors(test_suite_list: List[TestSuite]) -> TestStatus:
265 return bubble_up_errors(lambda x: x.status, test_suite_list)
267 def parse_test_result(lines: List[str]) -> TestResult:
268 consume_non_diagnositic(lines)
269 if not lines or not parse_tap_header(lines):
270 return TestResult(TestStatus.NO_TESTS, [], lines)
272 test_suite = parse_test_suite(lines)
274 test_suites.append(test_suite)
275 test_suite = parse_test_suite(lines)
276 return TestResult(bubble_up_suite_errors(test_suites), test_suites, lines)
278 def parse_run_tests(kernel_output) -> TestResult:
282 test_result = parse_test_result(list(isolate_kunit_output(kernel_output)))
283 if test_result.status == TestStatus.NO_TESTS:
284 print_with_timestamp(red('[ERROR] ') + 'no kunit output detected')
285 for test_suite in test_result.suites:
286 if test_suite.status == TestStatus.SUCCESS:
287 print_suite_divider(green('[PASSED] ') + test_suite.name)
288 elif test_suite.status == TestStatus.TEST_CRASHED:
289 print_suite_divider(red('[CRASHED] ' + test_suite.name))
291 print_suite_divider(red('[FAILED] ') + test_suite.name)
292 for test_case in test_suite.cases:
294 if test_case.status == TestStatus.SUCCESS:
295 print_with_timestamp(green('[PASSED] ') + test_case.name)
296 elif test_case.status == TestStatus.TEST_CRASHED:
298 print_with_timestamp(red('[CRASHED] ' + test_case.name))
299 print_log(map(yellow, test_case.log))
300 print_with_timestamp('')
303 print_with_timestamp(red('[FAILED] ') + test_case.name)
304 print_log(map(yellow, test_case.log))
305 print_with_timestamp('')
306 print_with_timestamp(DIVIDER)
307 fmt = green if test_result.status == TestStatus.SUCCESS else red
308 print_with_timestamp(
309 fmt('Testing complete. %d tests run. %d failed. %d crashed.' %
310 (total_tests, failed_tests, crashed_tests)))