Skip to contentSkip to conversation

kunit: tool: add parsing of all files in directory

PATCH In review Rae Moar 2 participants 3 messages Feb 22, 2024 – Feb 27, 2024

Subsystems (2)
lore
Share & tools
lore

Rae Moar

author
#0 lore raw

PATCH 1/1 kunit: tool: add parsing of all files in directory

Add ability to parse all files within a directory. Additionally add the ability to parse all results in the KUnit debugfs repository.

How to parse all files in directory:

./tools/testing/kunit/kunit.py parse [directory path]

How to parse KUnit debugfs repository:

./tools/testing/kunit/kunit.py parse debugfs

For each file, the parser outputs the file name, results, and test summary. At the end of all parsing, the parser outputs a total summary line.

This feature can be easily tested on the tools/testing/kunit/test_data/ directory.

---
 tools/testing/kunit/kunit.py | 45 ++++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 12 deletions(-)
tools/testing/kunit/kunit.py +33 -12
index bc74088c458a..827e6dac40ae 100755
@@ -511,19 +511,40 @@ def exec_handler(cli_args: argparse.Namespace) -> None:
 
 
 def parse_handler(cli_args: argparse.Namespace) -> None:
-	if cli_args.file is None:
+	parsed_files = []
+	total_test = kunit_parser.Test()
+	total_test.status = kunit_parser.TestStatus.SUCCESS
+	if cli_args.file_path is None:
 		sys.stdin.reconfigure(errors='backslashreplace')  # type: ignore
 		kunit_output = sys.stdin  # type: Iterable[str]
-	else:
-		with open(cli_args.file, 'r', errors='backslashreplace') as f:
+	elif cli_args.file_path == "debugfs":
+		for (root, _, files) in os.walk("/sys/kernel/debug/kunit"):
+			for file in files:
+				if file == "results":
+					parsed_files.append(os.path.join(root, file))
+	elif os.path.isdir(cli_args.file_path):
+		for (root, _, files) in os.walk(cli_args.file_path):
+			for file in files:
+				parsed_files.append(os.path.join(root, file))
+	elif os.path.isfile(cli_args.file_path):
+		parsed_files.append(cli_args.file_path)
+
+	for file in parsed_files:
+		print(file)
+		with open(file, 'r', errors='backslashreplace') as f:
 			kunit_output = f.read().splitlines()
-	# We know nothing about how the result was created!
-	metadata = kunit_json.Metadata()
-	request = KunitParseRequest(raw_output=cli_args.raw_output,
-					json=cli_args.json)
-	result, _ = parse_tests(request, metadata, kunit_output)
-	if result.status != KunitStatus.SUCCESS:
-		sys.exit(1)
+		# We know nothing about how the result was created!
+		metadata = kunit_json.Metadata()
+		request = KunitParseRequest(raw_output=cli_args.raw_output,
+						json=cli_args.json)
+		_, test = parse_tests(request, metadata, kunit_output)
+		total_test.subtests.append(test)
+
+	if len(parsed_files) > 1: # if more than one file was parsed output total summary
+		print('All files parsed.')
+		stdout.print_with_timestamp(kunit_parser.DIVIDER)
+		kunit_parser.bubble_up_test_results(total_test)
+		kunit_parser.print_summary_line(total_test)
 
 
 subcommand_handlers_map = {
@@ -569,8 +590,8 @@ def main(argv: Sequence[str]) -> None:
 					    help='Parses KUnit results from a file, '
 					    'and parses formatted results.')
 	add_parse_opts(parse_parser)
-	parse_parser.add_argument('file',
-				  help='Specifies the file to read results from.',
+	parse_parser.add_argument('file_path',
+				  help='Specifies the file path to read results from.',
 				  type=str, nargs='?', metavar='input_file')
 
 	cli_args = parser.parse_args(massage_argv(argv))

base-commit: 08c454e26daab6f843e5883fb96f680f11784fa6

Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
#1 lore raw
Quoted 3 lines · Rae Moar

Add ability to parse all files within a directory. Additionally add the ability to parse all results in the KUnit debugfs repository.

Nice, I'd been hoping for this.
It's enough to pull me back in for a bit :)

<snip>

tools/testing/kunit/kunit.py def exec_handler(cli_args: argparse.Namespace) -> None: · line 514

Quoted 13 lines
 tools/testing/kunit/kunit.py | 45 ++++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 12 deletions(-)
tools/testing/kunit/kunit.py +1 -1
index bc74088c458a..827e6dac40ae 100755
@@ -511,19 +511,40 @@ def exec_handler(cli_args: argparse.Namespace) -> None:
 
 
 def parse_handler(cli_args: argparse.Namespace) -> None:
-       if cli_args.file is None:
+       parsed_files = []

optional: can we annotate the type?
parsed_files = [] # type: List[str]

tools/testing/kunit/kunit.py def exec_handler(cli_args: argparse.Namespace) -> None: · line 519

Quoted 5 lines
tools/testing/kunit/kunit.py +3 -0
+       total_test = kunit_parser.Test()
+       total_test.status = kunit_parser.TestStatus.SUCCESS
+       if cli_args.file_path is None:
                sys.stdin.reconfigure(errors='backslashreplace')  # type: ignore
                kunit_output = sys.stdin  # type: Iterable[str]

This branch no longer does anything, since we only parse what's in parsed_files

E.g. if you try

$ kunit.py parse $FILENAME
it'll work whereas
$ kunit.py parse < $FILENAME
will do nothing

We'll need to rework the control flow somehow

tools/testing/kunit/kunit.py def exec_handler(cli_args: argparse.Namespace) -> None: · line 528

Quoted 11 lines
tools/testing/kunit/kunit.py +9 -2
-       else:
-               with open(cli_args.file, 'r', errors='backslashreplace') as f:
+       elif cli_args.file_path == "debugfs":
+               for (root, _, files) in os.walk("/sys/kernel/debug/kunit"):
+                       for file in files:
+                               if file == "results":
+                                       parsed_files.append(os.path.join(root, file))
+       elif os.path.isdir(cli_args.file_path):
+               for (root, _, files) in os.walk(cli_args.file_path):
+                       for file in files:
+                               parsed_files.append(os.path.join(root, file))

just a note here, we could make this a bit terser via
parsed_files.extend(os.path.join(root, f) for f in files)

and the debugfs branch could be rendered as
parsed_files.extend(os.path.join(root, f) for f in files if f == "results")

tools/testing/kunit/kunit.py def exec_handler(cli_args: argparse.Namespace) -> None: · line 530

Quoted 2 lines
tools/testing/kunit/kunit.py +2 -0
+       elif os.path.isfile(cli_args.file_path):
+               parsed_files.append(cli_args.file_path)

nit: should there be an else here that prints a warning?

Example that would trigger this case and silently do nothing

$ mkfifo /tmp/example_fifo
$ ./tools/testing/kunit/kunit.py parse /tmp/example_fifo
<no output>

<snip>

tools/testing/kunit/kunit.py def main(argv: Sequence[str]) -> None: · line 594

Quoted 8 lines
tools/testing/kunit/kunit.py +2 -2
@@ -569,8 +590,8 @@ def main(argv: Sequence[str]) -> None:
                                            help='Parses KUnit results from a file, '
                                            'and parses formatted results.')
        add_parse_opts(parse_parser)
-       parse_parser.add_argument('file',
-                                 help='Specifies the file to read results from.',
+       parse_parser.add_argument('file_path',
+                                 help='Specifies the file path to read results from.',

Should this mention that the make debugfs string works?

tools/testing/kunit/kunit.py def main(argv: Sequence[str]) -> None: · line 595

Quoted 1 line
tools/testing/kunit/kunit.py +0 -0
                                  type=str, nargs='?', metavar='input_file')

Tangent: would it be useful to allow the user to pass in multiple files now and set this to nargs='*'?

E.g.

$ kunit.py parse /my/dir/some_prefix*

It would also let people implement their own version of the debugfs logic via

$ find /other/debugfs/dir -name 'results' | xargs kunit.py parse

That could be useful if the user has recursively copied off the debugfs from a test machine and wants to inspect it elsewhere, for example.

Thanks,
Daniel

author
#2 lore raw
Quoted 21 lines · Daniel Latypov
Quoted 3 lines · Rae Moar

Add ability to parse all files within a directory. Additionally add the ability to parse all results in the KUnit debugfs repository.

Nice, I'd been hoping for this.
It's enough to pull me back in for a bit :)

<snip>

Quoted 10 lines
 tools/testing/kunit/kunit.py | 45 ++++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 12 deletions(-)
tools/testing/kunit/kunit.py +0 -0
index bc74088c458a..827e6dac40ae 100755
@@ -511,19 +511,40 @@ def exec_handler(cli_args: argparse.Namespace) -> None:
 
 
Quoted 6 lines

tools/testing/kunit/kunit.py def exec_handler(cli_args: argparse.Namespace) -> None: · line 514

Quoted 3 lines
tools/testing/kunit/kunit.py +1 -1
 def parse_handler(cli_args: argparse.Namespace) -> None:
-       if cli_args.file is None:
+       parsed_files = []

optional: can we annotate the type?
parsed_files = [] # type: List[str]

Hi Daniel!

Yes, happy to make this change for the next version.

Quoted 10 lines

tools/testing/kunit/kunit.py def exec_handler(cli_args: argparse.Namespace) -> None: · line 519

Quoted 6 lines
tools/testing/kunit/kunit.py +3 -0
+       total_test = kunit_parser.Test()
+       total_test.status = kunit_parser.TestStatus.SUCCESS
+       if cli_args.file_path is None:
                sys.stdin.reconfigure(errors='backslashreplace')  # type: ignore
                kunit_output = sys.stdin  # type: Iterable[str]

This branch no longer does anything, since we only parse what's in parsed_files

Quoted 7 lines

E.g. if you try

$ kunit.py parse $FILENAME
it'll work whereas
$ kunit.py parse < $FILENAME
will do nothing

We'll need to rework the control flow somehow

Ahh I see. Thanks for bringing this to my attention! I will change this for the next version.

Quoted 13 lines

tools/testing/kunit/kunit.py def exec_handler(cli_args: argparse.Namespace) -> None: · line 528

Quoted 12 lines
tools/testing/kunit/kunit.py +9 -2
-       else:
-               with open(cli_args.file, 'r', errors='backslashreplace') as f:
+       elif cli_args.file_path == "debugfs":
+               for (root, _, files) in os.walk("/sys/kernel/debug/kunit"):
+                       for file in files:
+                               if file == "results":
+                                       parsed_files.append(os.path.join(root, file))
+       elif os.path.isdir(cli_args.file_path):
+               for (root, _, files) in os.walk(cli_args.file_path):
+                       for file in files:
+                               parsed_files.append(os.path.join(root, file))
Quoted 6 lines

just a note here, we could make this a bit terser via
parsed_files.extend(os.path.join(root, f) for f in files)

and the debugfs branch could be rendered as
parsed_files.extend(os.path.join(root, f) for f in files if f == "results")

Will do.

Quoted 10 lines

tools/testing/kunit/kunit.py def exec_handler(cli_args: argparse.Namespace) -> None: · line 530

Quoted 2 lines
tools/testing/kunit/kunit.py +2 -0
+       elif os.path.isfile(cli_args.file_path):
+               parsed_files.append(cli_args.file_path)

nit: should there be an else here that prints a warning?

Example that would trigger this case and silently do nothing

$ mkfifo /tmp/example_fifo
$ ./tools/testing/kunit/kunit.py parse /tmp/example_fifo
<no output>

Yep you are definitely right I will add one here.

Quoted 20 lines

<snip>

tools/testing/kunit/kunit.py def main(argv: Sequence[str]) -> None: · line 594

Quoted 8 lines
tools/testing/kunit/kunit.py +2 -2
@@ -569,8 +590,8 @@ def main(argv: Sequence[str]) -> None:
                                            help='Parses KUnit results from a file, '
                                            'and parses formatted results.')
        add_parse_opts(parse_parser)
-       parse_parser.add_argument('file',
-                                 help='Specifies the file to read results from.',
+       parse_parser.add_argument('file_path',
+                                 help='Specifies the file path to read results from.',

Should this mention that the make debugfs string works?

tools/testing/kunit/kunit.py def main(argv: Sequence[str]) -> None: · line 595

Quoted 1 line
tools/testing/kunit/kunit.py +0 -0
                                  type=str, nargs='?', metavar='input_file')

Tangent: would it be useful to allow the user to pass in multiple files now and set this to nargs='*'?

E.g.

Quoted 9 lines
$ kunit.py parse /my/dir/some_prefix*

It would also let people implement their own version of the debugfs logic via

$ find /other/debugfs/dir -name 'results' | xargs kunit.py parse

That could be useful if the user has recursively copied off the debugfs from a test machine and wants to inspect it elsewhere, for example.

Oh this is an interesting idea. I will play around with it!

Thanks for looking this patch over!
-Rae

Quoted 2 lines

Thanks,
Daniel