Skip to contentSkip to conversation

kunit: tool: add parsing of test attributes

PATCH New Rae Moar 1 participant 2 messages Sep 14, 2023

Subsystems (2)
lore
Share & tools
lore

Rae Moar

author
#0 lore raw

PATCH 1/2 kunit: tool: add parsing of test attributes

Add parsing of attributes as diagnostic data. Fixes issue with test plan being parsed incorrectly as diagnostic data when located after suite-level attributes.

Note that if there does not exist a test plan line, the diagnostic lines between the suite header and the first result will be saved in the suite log rather than the first test log.

This could be changed to do the opposite if preferred.

---
 tools/testing/kunit/kunit_parser.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/kunit/kunit_parser.py +3 -1
index 79d8832c862a..ce34be15c929 100644
@@ -450,7 +450,7 @@ def parse_diagnostic(lines: LineStream) -> List[str]:
 	Log of diagnostic lines
 	"""
 	log = []  # type: List[str]
-	non_diagnostic_lines = [TEST_RESULT, TEST_HEADER, KTAP_START, TAP_START]
+	non_diagnostic_lines = [TEST_RESULT, TEST_HEADER, KTAP_START, TAP_START, TEST_PLAN]
 	while lines and not any(re.match(lines.peek())
 			for re in non_diagnostic_lines):
 		log.append(lines.pop())
@@ -726,6 +726,7 @@ def parse_test(lines: LineStream, expected_num: int, log: List[str], is_subtest:
 		# test plan
 		test.name = "main"
 		ktap_line = parse_ktap_header(lines, test)
+		test.log.extend(parse_diagnostic(lines))
 		parse_test_plan(lines, test)
 		parent_test = True
 	else:
@@ -737,6 +738,7 @@ def parse_test(lines: LineStream, expected_num: int, log: List[str], is_subtest:
 		if parent_test:
 			# If KTAP version line and/or subtest header is found, attempt
 			# to parse test plan and print test header
+			test.log.extend(parse_diagnostic(lines))
 			parse_test_plan(lines, test)
 			print_test_header(test)
 	expected_count = test.expected_count

base-commit: 9076bc476d7ebf0565903c4b048442131825c1c3

Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
author
#1 lore raw

PATCH 2/2 kunit: tool: add attributes output to kunit.py

Output attributes in kunit.py results.

Example of format:

[21:33:23] =================== example (7 subtests) =================== [21:33:23] module: kunit_example_test
[21:33:23] [PASSED] example_simple_test
...
[21:33:23] [PASSED] example_slow_test
[21:33:23] speed: slow
[21:33:23] =================== [PASSED] example =======================

The attributes are outputted directly below the associated test.

Note that the attributes lines are saved in the log and then the log is parsed for attributes rather than saving attributes in their own field.

This is due to diagnostic lines being saved prior to the creation of the associated test object.

---
 tools/testing/kunit/kunit_parser.py | 31 +++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
tools/testing/kunit/kunit_parser.py +31 -0
index ce34be15c929..ffa9cfdcf5d6 100644
@@ -509,6 +509,34 @@ def print_test_header(test: Test) -> None:
 			message += f'({test.expected_count} subtests)'
 	stdout.print_with_timestamp(format_test_divider(message, len(message)))
 
+TEST_HEADER_ATTR = re.compile(r'^\s*# (.*): (.*)$')
+TEST_ATTR = re.compile(r'^\s*# (.*)\.(.*): (.*)$')
+
+def print_test_attr(test: Test) -> None:
+	"""
+	Attributes, if present, will be printed in a list separated by
+	commas.
+
+	Example:
+	'module: example_test, speed: slow'
+
+	Parameters:
+	test - Test object representing current test being printed
+	"""
+	attr_list = ["module", "speed"]
+	output = []
+
+	for line in test.log:
+		test_match = TEST_ATTR.match(line)
+		header_match = TEST_HEADER_ATTR.match(line)
+		if test_match and (test_match.group(1) == test.name
+					  and (test_match.group(2) in attr_list)):
+			output.append(test_match.group(2) + ": " + test_match.group(3))
+		elif header_match and (header_match.group(1) in attr_list):
+			output.append(header_match.group(1) + ": " + header_match.group(2))
+	if output:
+		stdout.print_with_timestamp(", ".join(output))
+
 def print_log(log: Iterable[str]) -> None:
 	"""Prints all strings in saved log for test in yellow."""
 	formatted = textwrap.dedent('\n'.join(log))
@@ -741,6 +769,7 @@ def parse_test(lines: LineStream, expected_num: int, log: List[str], is_subtest:
 			test.log.extend(parse_diagnostic(lines))
 			parse_test_plan(lines, test)
 			print_test_header(test)
+			print_test_attr(test)
 	expected_count = test.expected_count
 	subtests = []
 	test_num = 1
@@ -763,6 +792,7 @@ def parse_test(lines: LineStream, expected_num: int, log: List[str], is_subtest:
 				test.counts.add_status(
 					TestStatus.TEST_CRASHED)
 				print_test_result(sub_test)
+				print_test_attr(sub_test)
 			else:
 				test.log.extend(sub_log)
 				break
@@ -796,6 +826,7 @@ def parse_test(lines: LineStream, expected_num: int, log: List[str], is_subtest:
 		print_test_footer(test)
 	elif is_subtest:
 		print_test_result(test)
+		print_test_attr(test)
 	return test
 
 def parse_run_tests(kernel_output: Iterable[str]) -> Test:
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog