1 # SPDX-License-Identifier: GPL-2.0
3 # Parses KTAP test results from a kernel dmesg log and incrementally prints
4 # results with reader-friendly format. Stores and returns test results in a
7 # Copyright (C) 2019, Google LLC.
8 # Author: Felix Guo <felixguoxiuping@gmail.com>
9 # Author: Brendan Higgins <brendanhiggins@google.com>
10 # Author: Rae Moar <rmoar@google.com>
12 from __future__ import annotations
16 from enum import Enum, auto
17 from typing import Iterable, Iterator, List, Optional, Tuple
19 from kunit_printer import stdout
23 A class to represent a test parsed from KTAP results. All KTAP
24 results within a test log are stored in a main Test object as
28 status : TestStatus - status of the test
29 name : str - name of the test
30 expected_count : int - expected number of subtests (0 if single
31 test case and None if unknown expected number of subtests)
32 subtests : List[Test] - list of subtests
33 log : List[str] - log of KTAP lines that correspond to the test
34 counts : TestCounts - counts of the test statuses and errors of
35 subtests or of the test itself if the test is a single
38 def __init__(self) -> None:
39 """Creates Test object with default attributes."""
40 self.status = TestStatus.TEST_CRASHED
42 self.expected_count = 0 # type: Optional[int]
43 self.subtests = [] # type: List[Test]
44 self.log = [] # type: List[str]
45 self.counts = TestCounts()
47 def __str__(self) -> str:
48 """Returns string representation of a Test class object."""
49 return (f'Test({self.status}, {self.name}, {self.expected_count}, '
50 f'{self.subtests}, {self.log}, {self.counts})')
52 def __repr__(self) -> str:
53 """Returns string representation of a Test class object."""
56 def add_error(self, error_message: str) -> None:
57 """Records an error that occurred while parsing this test."""
58 self.counts.errors += 1
59 stdout.print_with_timestamp(stdout.red('[ERROR]') + f' Test: {self.name}: {error_message}')
61 class TestStatus(Enum):
62 """An enumeration class to represent the status of a test."""
68 FAILURE_TO_PARSE_TESTS = auto()
72 Tracks the counts of statuses of all test cases and any errors within
76 passed : int - the number of tests that have passed
77 failed : int - the number of tests that have failed
78 crashed : int - the number of tests that have crashed
79 skipped : int - the number of tests that have skipped
80 errors : int - the number of errors in the test and subtests
83 """Creates TestCounts object with counts of all test
84 statuses and test errors set to 0.
92 def __str__(self) -> str:
93 """Returns the string representation of a TestCounts object."""
94 statuses = [('passed', self.passed), ('failed', self.failed),
95 ('crashed', self.crashed), ('skipped', self.skipped),
96 ('errors', self.errors)]
97 return f'Ran {self.total()} tests: ' + \
98 ', '.join(f'{s}: {n}' for s, n in statuses if n > 0)
100 def total(self) -> int:
101 """Returns the total number of test cases within a test
102 object, where a test case is a test with no subtests.
104 return (self.passed + self.failed + self.crashed +
107 def add_subtest_counts(self, counts: TestCounts) -> None:
109 Adds the counts of another TestCounts object to the current
110 TestCounts object. Used to add the counts of a subtest to the
114 counts - a different TestCounts object whose counts
115 will be added to the counts of the TestCounts object
117 self.passed += counts.passed
118 self.failed += counts.failed
119 self.crashed += counts.crashed
120 self.skipped += counts.skipped
121 self.errors += counts.errors
123 def get_status(self) -> TestStatus:
124 """Returns the aggregated status of a Test using test
127 if self.total() == 0:
128 return TestStatus.NO_TESTS
130 # Crashes should take priority.
131 return TestStatus.TEST_CRASHED
133 return TestStatus.FAILURE
135 # No failures or crashes, looks good!
136 return TestStatus.SUCCESS
137 # We have only skipped tests.
138 return TestStatus.SKIPPED
140 def add_status(self, status: TestStatus) -> None:
141 """Increments the count for `status`."""
142 if status == TestStatus.SUCCESS:
144 elif status == TestStatus.FAILURE:
146 elif status == TestStatus.SKIPPED:
148 elif status != TestStatus.NO_TESTS:
153 A class to represent the lines of kernel output.
154 Provides a lazy peek()/pop() interface over an iterator of
157 _lines: Iterator[Tuple[int, str]]
158 _next: Tuple[int, str]
162 def __init__(self, lines: Iterator[Tuple[int, str]]):
163 """Creates a new LineStream that wraps the given iterator."""
166 self._need_next = True
169 def _get_next(self) -> None:
170 """Advances the LineSteam to the next line, if necessary."""
171 if not self._need_next:
174 self._next = next(self._lines)
175 except StopIteration:
178 self._need_next = False
180 def peek(self) -> str:
181 """Returns the current line, without advancing the LineStream.
186 def pop(self) -> str:
187 """Returns the current line and advances the LineStream to
192 raise ValueError(f'LineStream: going past EOF, last line was {s}')
193 self._need_next = True
196 def __bool__(self) -> bool:
197 """Returns True if stream has more lines."""
199 return not self._done
201 # Only used by kunit_tool_test.py.
202 def __iter__(self) -> Iterator[str]:
203 """Empties all lines stored in LineStream object into
204 Iterator object and returns the Iterator object.
209 def line_number(self) -> int:
210 """Returns the line number of the current line."""
214 # Parsing helper methods:
216 KTAP_START = re.compile(r'KTAP version ([0-9]+)$')
217 TAP_START = re.compile(r'TAP version ([0-9]+)$')
218 KTAP_END = re.compile('(List of all partitions:|'
219 'Kernel panic - not syncing: VFS:|reboot: System halted)')
221 def extract_tap_lines(kernel_output: Iterable[str]) -> LineStream:
222 """Extracts KTAP lines from the kernel output."""
223 def isolate_ktap_output(kernel_output: Iterable[str]) \
224 -> Iterator[Tuple[int, str]]:
227 for line in kernel_output:
229 line = line.rstrip() # remove trailing \n
230 if not started and KTAP_START.search(line):
231 # start extracting KTAP lines and set prefix
232 # to number of characters before version line
234 line.split('KTAP version')[0])
236 yield line_num, line[prefix_len:]
237 elif not started and TAP_START.search(line):
238 # start extracting KTAP lines and set prefix
239 # to number of characters before version line
240 prefix_len = len(line.split('TAP version')[0])
242 yield line_num, line[prefix_len:]
243 elif started and KTAP_END.search(line):
244 # stop extracting KTAP lines
247 # remove prefix and any indention and yield
248 # line with line number
249 line = line[prefix_len:].lstrip()
251 return LineStream(lines=isolate_ktap_output(kernel_output))
254 TAP_VERSIONS = [13, 14]
256 def check_version(version_num: int, accepted_versions: List[int],
257 version_type: str, test: Test) -> None:
259 Adds error to test object if version number is too high or too
263 version_num - The inputted version number from the parsed KTAP or TAP
265 accepted_version - List of accepted KTAP or TAP versions
266 version_type - 'KTAP' or 'TAP' depending on the type of
268 test - Test object for current test being parsed
270 if version_num < min(accepted_versions):
271 test.add_error(f'{version_type} version lower than expected!')
272 elif version_num > max(accepted_versions):
273 test.add_error(f'{version_type} version higer than expected!')
275 def parse_ktap_header(lines: LineStream, test: Test) -> bool:
277 Parses KTAP/TAP header line and checks version number.
278 Returns False if fails to parse KTAP/TAP header line.
281 - 'KTAP version [version number]'
282 - 'TAP version [version number]'
285 lines - LineStream of KTAP output to parse
286 test - Test object for current test being parsed
289 True if successfully parsed KTAP/TAP header line
291 ktap_match = KTAP_START.match(lines.peek())
292 tap_match = TAP_START.match(lines.peek())
294 version_num = int(ktap_match.group(1))
295 check_version(version_num, KTAP_VERSIONS, 'KTAP', test)
297 version_num = int(tap_match.group(1))
298 check_version(version_num, TAP_VERSIONS, 'TAP', test)
301 test.log.append(lines.pop())
304 TEST_HEADER = re.compile(r'^# Subtest: (.*)$')
306 def parse_test_header(lines: LineStream, test: Test) -> bool:
308 Parses test header and stores test name in test object.
309 Returns False if fails to parse test header line.
312 - '# Subtest: [test name]'
315 lines - LineStream of KTAP output to parse
316 test - Test object for current test being parsed
319 True if successfully parsed test header line
321 match = TEST_HEADER.match(lines.peek())
324 test.log.append(lines.pop())
325 test.name = match.group(1)
328 TEST_PLAN = re.compile(r'1\.\.([0-9]+)')
330 def parse_test_plan(lines: LineStream, test: Test) -> bool:
332 Parses test plan line and stores the expected number of subtests in
333 test object. Reports an error if expected count is 0.
334 Returns False and sets expected_count to None if there is no valid test
338 - '1..[number of subtests]'
341 lines - LineStream of KTAP output to parse
342 test - Test object for current test being parsed
345 True if successfully parsed test plan line
347 match = TEST_PLAN.match(lines.peek())
349 test.expected_count = None
351 test.log.append(lines.pop())
352 expected_count = int(match.group(1))
353 test.expected_count = expected_count
356 TEST_RESULT = re.compile(r'^(ok|not ok) ([0-9]+) (- )?([^#]*)( # .*)?$')
358 TEST_RESULT_SKIP = re.compile(r'^(ok|not ok) ([0-9]+) (- )?(.*) # SKIP(.*)$')
360 def peek_test_name_match(lines: LineStream, test: Test) -> bool:
362 Matches current line with the format of a test result line and checks
363 if the name matches the name of the current test.
364 Returns False if fails to match format or name.
367 - '[ok|not ok] [test number] [-] [test name] [optional skip
371 lines - LineStream of KTAP output to parse
372 test - Test object for current test being parsed
375 True if matched a test result line and the name matching the
379 match = TEST_RESULT.match(line)
382 name = match.group(4)
383 return name == test.name
385 def parse_test_result(lines: LineStream, test: Test,
386 expected_num: int) -> bool:
388 Parses test result line and stores the status and name in the test
389 object. Reports an error if the test number does not match expected
391 Returns False if fails to parse test result line.
393 Note that the SKIP directive is the only direction that causes a
397 - '[ok|not ok] [test number] [-] [test name] [optional skip
401 lines - LineStream of KTAP output to parse
402 test - Test object for current test being parsed
403 expected_num - expected test number for current test
406 True if successfully parsed a test result line.
409 match = TEST_RESULT.match(line)
410 skip_match = TEST_RESULT_SKIP.match(line)
412 # Check if line matches test result line format
415 test.log.append(lines.pop())
417 # Set name of test object
419 test.name = skip_match.group(4)
421 test.name = match.group(4)
424 num = int(match.group(2))
425 if num != expected_num:
426 test.add_error(f'Expected test number {expected_num} but found {num}')
428 # Set status of test object
429 status = match.group(1)
431 test.status = TestStatus.SKIPPED
433 test.status = TestStatus.SUCCESS
435 test.status = TestStatus.FAILURE
438 def parse_diagnostic(lines: LineStream) -> List[str]:
440 Parse lines that do not match the format of a test result line or
441 test header line and returns them in list.
443 Line formats that are not parsed:
444 - '# Subtest: [test name]'
445 - '[ok|not ok] [test number] [-] [test name] [optional skip
449 lines - LineStream of KTAP output to parse
452 Log of diagnostic lines
454 log = [] # type: List[str]
455 while lines and not TEST_RESULT.match(lines.peek()) and not \
456 TEST_HEADER.match(lines.peek()):
457 log.append(lines.pop())
461 # Printing helper methods:
465 def format_test_divider(message: str, len_message: int) -> str:
467 Returns string with message centered in fixed width divider.
470 '===================== message example ====================='
473 message - message to be centered in divider line
474 len_message - length of the message to be printed such that
475 any characters of the color codes are not counted
478 String containing message centered in fixed width divider
480 default_count = 3 # default number of dashes
481 len_1 = default_count
482 len_2 = default_count
483 difference = len(DIVIDER) - len_message - 2 # 2 spaces added
485 # calculate number of dashes for each side of the divider
486 len_1 = int(difference / 2)
487 len_2 = difference - len_1
488 return ('=' * len_1) + f' {message} ' + ('=' * len_2)
490 def print_test_header(test: Test) -> None:
492 Prints test header with test name and optionally the expected number
496 '=================== example (2 subtests) ==================='
499 test - Test object representing current test being printed
502 if test.expected_count:
503 if test.expected_count == 1:
504 message += ' (1 subtest)'
506 message += f' ({test.expected_count} subtests)'
507 stdout.print_with_timestamp(format_test_divider(message, len(message)))
509 def print_log(log: Iterable[str]) -> None:
510 """Prints all strings in saved log for test in yellow."""
512 stdout.print_with_timestamp(stdout.yellow(m))
514 def format_test_result(test: Test) -> str:
516 Returns string with formatted test result with colored status and test
523 test - Test object representing current test being printed
526 String containing formatted test result
528 if test.status == TestStatus.SUCCESS:
529 return stdout.green('[PASSED] ') + test.name
530 if test.status == TestStatus.SKIPPED:
531 return stdout.yellow('[SKIPPED] ') + test.name
532 if test.status == TestStatus.NO_TESTS:
533 return stdout.yellow('[NO TESTS RUN] ') + test.name
534 if test.status == TestStatus.TEST_CRASHED:
536 return stdout.red('[CRASHED] ') + test.name
538 return stdout.red('[FAILED] ') + test.name
540 def print_test_result(test: Test) -> None:
542 Prints result line with status of test.
548 test - Test object representing current test being printed
550 stdout.print_with_timestamp(format_test_result(test))
552 def print_test_footer(test: Test) -> None:
554 Prints test footer with status of test.
557 '===================== [PASSED] example ====================='
560 test - Test object representing current test being printed
562 message = format_test_result(test)
563 stdout.print_with_timestamp(format_test_divider(message,
564 len(message) - stdout.color_len()))
566 def print_summary_line(test: Test) -> None:
568 Prints summary line of test object. Color of line is dependent on
569 status of test. Color is green if test passes, yellow if test is
570 skipped, and red if the test fails or crashes. Summary line contains
571 counts of the statuses of the tests subtests or the test itself if it
575 "Testing complete. Passed: 2, Failed: 0, Crashed: 0, Skipped: 0,
578 test - Test object representing current test being printed
580 if test.status == TestStatus.SUCCESS:
582 elif test.status in (TestStatus.SKIPPED, TestStatus.NO_TESTS):
583 color = stdout.yellow
586 stdout.print_with_timestamp(color(f'Testing complete. {test.counts}'))
590 def bubble_up_test_results(test: Test) -> None:
592 If the test has subtests, add the test counts of the subtests to the
593 test and check if any of the tests crashed and if so set the test
594 status to crashed. Otherwise if the test has no subtests add the
595 status of the test to the test counts.
598 test - Test object for current test being parsed
600 subtests = test.subtests
604 counts.add_subtest_counts(t.counts)
605 if counts.total() == 0:
606 counts.add_status(status)
607 elif test.counts.get_status() == TestStatus.TEST_CRASHED:
608 test.status = TestStatus.TEST_CRASHED
610 def parse_test(lines: LineStream, expected_num: int, log: List[str]) -> Test:
612 Finds next test to parse in LineStream, creates new Test object,
613 parses any subtests of the test, populates Test object with all
614 information (status, name) about the test and the Test objects for
615 any subtests, and then returns the Test object. The method accepts
616 three formats of tests:
618 Accepted test formats:
620 - Main KTAP/TAP header
628 - Subtest header line
644 lines - LineStream of KTAP output to parse
645 expected_num - expected test number for test to be parsed
646 log - list of strings containing any preceding diagnostic lines
647 corresponding to the current test
650 Test object populated with characteristics and any subtests
655 main = parse_ktap_header(lines, test)
657 # If KTAP/TAP header is found, attempt to parse
660 parse_test_plan(lines, test)
663 # If KTAP/TAP header is not found, test must be subtest
664 # header or test result line so parse attempt to parser
666 parent_test = parse_test_header(lines, test)
668 # If subtest header is found, attempt to parse
669 # test plan and print header
670 parse_test_plan(lines, test)
671 print_test_header(test)
672 expected_count = test.expected_count
675 while parent_test and (expected_count is None or test_num <= expected_count):
676 # Loop to parse any subtests.
677 # Break after parsing expected number of tests or
678 # if expected number of tests is unknown break when test
679 # result line with matching name to subtest header is found
680 # or no more lines in stream.
681 sub_log = parse_diagnostic(lines)
683 if not lines or (peek_test_name_match(lines, test) and
685 if expected_count and test_num <= expected_count:
686 # If parser reaches end of test before
687 # parsing expected number of subtests, print
688 # crashed subtest and record error
689 test.add_error('missing expected subtest!')
690 sub_test.log.extend(sub_log)
691 test.counts.add_status(
692 TestStatus.TEST_CRASHED)
693 print_test_result(sub_test)
695 test.log.extend(sub_log)
698 sub_test = parse_test(lines, test_num, sub_log)
699 subtests.append(sub_test)
701 test.subtests = subtests
703 # If not main test, look for test result line
704 test.log.extend(parse_diagnostic(lines))
705 if (parent_test and peek_test_name_match(lines, test)) or \
707 parse_test_result(lines, test, expected_num)
709 test.add_error('missing subtest result line!')
711 # Check for there being no tests
712 if parent_test and len(subtests) == 0:
713 # Don't override a bad status if this test had one reported.
714 # Assumption: no subtests means CRASHED is from Test.__init__()
715 if test.status in (TestStatus.TEST_CRASHED, TestStatus.SUCCESS):
716 test.status = TestStatus.NO_TESTS
717 test.add_error('0 tests run!')
719 # Add statuses to TestCounts attribute in Test object
720 bubble_up_test_results(test)
721 if parent_test and not main:
722 # If test has subtests and is not the main test object, print
724 print_test_footer(test)
726 print_test_result(test)
729 def parse_run_tests(kernel_output: Iterable[str]) -> Test:
731 Using kernel output, extract KTAP lines, parse the lines for test
732 results and print condensed test results and summary line.
735 kernel_output - Iterable object contains lines of kernel output
738 Test - the main test object with all subtests.
740 stdout.print_with_timestamp(DIVIDER)
741 lines = extract_tap_lines(kernel_output)
744 test.name = '<missing>'
745 test.add_error('could not find any KTAP output!')
746 test.status = TestStatus.FAILURE_TO_PARSE_TESTS
748 test = parse_test(lines, 0, [])
749 if test.status != TestStatus.NO_TESTS:
750 test.status = test.counts.get_status()
751 stdout.print_with_timestamp(DIVIDER)
752 print_summary_line(test)