Skip to contentSkip to conversation

kunit: tool: add ability to parse test metadata

PATCH New Rae Moar 1 participant 1 message Nov 20, 2024

Subsystems (2)
lore
Share & tools
lore

Rae Moar

author
#0 lore raw

PATCH 1/1 kunit: tool: add ability to parse test metadata

Add ability for kunit parser to parse test metadata introduced in KTAPv2.

Example of test metadata:

KTAP version 2
#:ktap_test: test_name_example
#:ktap_speed: slow
#:ktap_test_file: /sys/kernel/...
1..1
ok 1 test

Also add tests for this feature.

Note this patch would no longer allow the case where the main test is missing a test plan and the subtest does not use a KTAP version line in the header. However, this is also not an accepted KTAP format. Example:

KTAP version 2
// missing test plan
// missing KTAP version line
# Subtest: test_suite
1..1
ok 1 test
ok 1 test_suite

---
 lib/kunit/attributes.c                        | 23 ++++--
 lib/kunit/debugfs.c                           |  2 +-
 lib/kunit/test.c                              |  9 +--
 tools/testing/kunit/kunit_parser.py           | 79 +++++++++++++------
 tools/testing/kunit/kunit_tool_test.py        | 12 ++-
 .../test_is_test_passed-missing_plan.log      |  2 +
 .../kunit/test_data/test_parse_attributes.log |  6 +-
 .../kunit/test_data/test_parse_metadata.log   | 11 +++
 8 files changed, 106 insertions(+), 38 deletions(-)
 create mode 100644 tools/testing/kunit/test_data/test_parse_metadata.log
lib/kunit/attributes.c +18 -5
index 2cf04cc09372..85db4555d332 100644
@@ -286,11 +286,17 @@ void kunit_print_attr(void *test_or_suite, bool is_test, unsigned int test_level
 {
 	int i;
 	bool to_free = false;
+	bool printed = false;
 	void *attr;
 	const char *attr_name, *attr_str;
 	struct kunit_suite *suite = is_test ? NULL : test_or_suite;
 	struct kunit_case *test = is_test ? test_or_suite : NULL;
 
+	if (suite) {
+		kunit_log(KERN_INFO, suite, "%*s#:ktap_test: %s",
+					KUNIT_INDENT_LEN * test_level, "", suite->name);
+	}
+
 	for (i = 0; i < ARRAY_SIZE(kunit_attr_list); i++) {
 		if (kunit_attr_list[i].print == PRINT_NEVER ||
 				(test && kunit_attr_list[i].print == PRINT_SUITE))
@@ -300,12 +306,19 @@ void kunit_print_attr(void *test_or_suite, bool is_test, unsigned int test_level
 			attr_name = kunit_attr_list[i].name;
 			attr_str = kunit_attr_list[i].to_string(attr, &to_free);
 			if (test) {
-				kunit_log(KERN_INFO, test, "%*s# %s.%s: %s",
-					KUNIT_INDENT_LEN * test_level, "", test->name,
-					attr_name, attr_str);
+				if (!printed) {
+					kunit_log(KERN_INFO, test, "%*s#:ktap_test: %s",
+							KUNIT_INDENT_LEN * test_level, "",
+							test->name);
+					printed = true;
+				}
+				kunit_log(KERN_INFO, test, "%*s#:ktap_%s: %s",
+						KUNIT_INDENT_LEN * test_level, "",
+						attr_name, attr_str);
 			} else {
-				kunit_log(KERN_INFO, suite, "%*s# %s: %s",
-					KUNIT_INDENT_LEN * test_level, "", attr_name, attr_str);
+				kunit_log(KERN_INFO, suite, "%*s#:ktap_%s: %s",
+						KUNIT_INDENT_LEN * test_level, "",
+						attr_name, attr_str);
 			}
 
 			/* Free to_string of attribute if needed */
lib/kunit/debugfs.c +1 -1
index af71911f4a07..035cdae9d8b8 100644
@@ -78,7 +78,7 @@ static int debugfs_print_results(struct seq_file *seq, void *v)
 
 	/* Print suite header because it is not stored in the test logs. */
 	seq_puts(seq, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
-	seq_printf(seq, KUNIT_SUBTEST_INDENT "# Subtest: %s\n", suite->name);
+	seq_printf(seq, KUNIT_SUBTEST_INDENT "#:ktap_test: %s\n", suite->name);
 	seq_printf(seq, KUNIT_SUBTEST_INDENT "1..%zd\n", kunit_suite_num_test_cases(suite));
 
 	kunit_suite_for_each_test_case(suite, test_case)
lib/kunit/test.c +4 -5
index 089c832e3cdb..4fcc39e87983 100644
@@ -158,8 +158,6 @@ static void kunit_print_suite_start(struct kunit_suite *suite)
 	 * representation.
 	 */
 	pr_info(KUNIT_SUBTEST_INDENT "KTAP version 1\n");
-	pr_info(KUNIT_SUBTEST_INDENT "# Subtest: %s\n",
-		  suite->name);
 	kunit_print_attr((void *)suite, false, KUNIT_LEVEL_CASE);
 	pr_info(KUNIT_SUBTEST_INDENT "1..%zd\n",
 		  kunit_suite_num_test_cases(suite));
@@ -627,9 +625,11 @@ int kunit_run_tests(struct kunit_suite *suite)
 		if (test_case->status == KUNIT_SKIPPED) {
 			/* Test marked as skip */
 			test.status = KUNIT_SKIPPED;
+			kunit_print_attr((void *)test_case, true, KUNIT_LEVEL_CASE);
 			kunit_update_stats(&param_stats, test.status);
 		} else if (!test_case->generate_params) {
 			/* Non-parameterised test. */
+			kunit_print_attr((void *)test_case, true, KUNIT_LEVEL_CASE);
 			test_case->status = KUNIT_SKIPPED;
 			kunit_run_case_catch_errors(suite, test_case, &test);
 			kunit_update_stats(&param_stats, test.status);
@@ -641,7 +641,8 @@ int kunit_run_tests(struct kunit_suite *suite)
 			kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
 				  "KTAP version 1\n");
 			kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
-				  "# Subtest: %s", test_case->name);
+				  "#:ktap_test: %s", test_case->name);
+			kunit_print_attr((void *)test_case, true, KUNIT_LEVEL_CASE);
 
 			while (test.param_value) {
 				kunit_run_case_catch_errors(suite, test_case, &test);
@@ -669,8 +670,6 @@ int kunit_run_tests(struct kunit_suite *suite)
 			}
 		}
 
-		kunit_print_attr((void *)test_case, true, KUNIT_LEVEL_CASE);
-
 		kunit_print_test_stats(&test, param_stats);
 
 		kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE, test_case->status,
tools/testing/kunit/kunit_parser.py +57 -22

138 lines not on this page · raw message

tools/testing/kunit/kunit_tool_test.py +10 -2
index 0bcb0cc002f8..6ff422399130 100755
@@ -345,8 +345,8 @@ class KUnitParserTest(unittest.TestCase):
 		self.print_mock.assert_any_call(StrContains('suite (1 subtest)'))
 
 		# Ensure attributes in correct test log
-		self.assertContains('# module: example', result.subtests[0].log)
-		self.assertContains('# test.speed: slow', result.subtests[0].subtests[0].log)
+		self.assertContains('#:ktap_module: example', result.subtests[0].log)
+		self.assertContains('#:ktap_speed: slow', result.subtests[0].subtests[0].log)
 
 	def test_show_test_output_on_failure(self):
 		output = """
@@ -363,6 +363,14 @@ class KUnitParserTest(unittest.TestCase):
 		self.print_mock.assert_any_call(StrContains('  Indented more.'))
 		self.noPrintCallContains('not ok 1 test1')
 
+	def test_metadata(self):
+		name_log = test_data_path('test_parse_metadata.log')
+		with open(name_log) as file:
+			result = kunit_parser.parse_run_tests(file.readlines(), stdout)
+		self.assertEqual(kunit_parser.TestStatus.SUCCESS, result.status)
+		self.assertEqual("main_test", result.name)
+		self.assertContains("#:ktap_speed: slow", result.subtests[0].log)
+
 def line_stream_from_strs(strs: Iterable[str]) -> kunit_parser.LineStream:
 	return kunit_parser.LineStream(enumerate(strs, start=1))
 
tools/testing/kunit/test_data/test_is_test_passed-missing_plan.log +2 -0
index 5cd17b7f818a..1e952b0430e1 100644
@@ -1,4 +1,5 @@
 KTAP version 1
+	KTAP version 1
 	# Subtest: sysctl_test
 	# sysctl_test_dointvec_null_tbl_data: sysctl_test_dointvec_null_tbl_data passed
 	ok 1 - sysctl_test_dointvec_null_tbl_data
@@ -18,6 +19,7 @@ KTAP version 1
 	ok 8 - sysctl_test_dointvec_single_greater_int_max
 kunit sysctl_test: all tests passed
 ok 1 - sysctl_test
+	KTAP version 1
 	# Subtest: example
 	1..2
 init_suite
tools/testing/kunit/test_data/test_parse_attributes.log +3 -3
index 1a13c371fe9d..d7961422c66f 100644
@@ -1,9 +1,9 @@
 KTAP version 1
 1..1
   KTAP version 1
-  # Subtest: suite
-  # module: example
+  #:ktap_test: suite
+  #:ktap_module: example
   1..1
-  # test.speed: slow
+  #:ktap_speed: slow
   ok 1 test
 ok 1 suite
\ No newline at end of file
tools/testing/kunit/test_data/test_parse_metadata.log new file +11 -0
new file mode 100644
index 000000000000..2094867110e5
@@ -0,0 +1,11 @@
+KTAP version 2
+#:ktap_test: main_test
+#:ktap_module: example
+1..2
+  KTAP version 2
+  #:ktap_test: test_1
+  #:ktap_speed: slow
+  1..1
+  ok 1 subtest_1
+ok 1 test_1
+ok 2 test_2
\ No newline at end of file

base-commit: 62adcae479fe5bc04fa3b6c3f93bd340441f8b25

Signature · 1 line
2.47.0.338.g60cca15819-goog