Skip to contentSkip to conversation

selftests: net: py: add test case filtering and listing

PATCH Superseded Jakub Kicinski 4 participants 6 messages Apr 7 – Apr 9

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

PATCH 1/1 selftests: net: py: add test case filtering and listing

When developing new test cases and reproducing failures in existing ones we currently have to run the entire test which can take minutes to finish.

Add command line options for test selection, modeled after kselftest_harness.h:

  -l       list tests (all or filtered)
  -t name  include test
  -T name  exclude test

Since we don't have as clean separation into fixture / variant / test as kselftest_harness this is not really a 1 to 1 match. We have to lean on glob patterns instead.

Like in kselftest_harness filters are evaluated in order, first match wins. If only exclusions are specified everything else is included and vice versa.

Glob patterns (*, ?, [) are supported in addition to exact matching.

---
Building on the support for colored output.. :)

---
 tools/testing/selftests/net/lib/py/ksft.py | 65 +++++++++++++++++++++-
 1 file changed, 62 insertions(+), 3 deletions(-)
tools/testing/selftests/net/lib/py/ksft.py +62 -3
index 7b8af463e35d..abde32874c17 100644
@@ -1,6 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0
 
+import fnmatch
 import functools
+import getopt
 import inspect
 import os
 import signal
@@ -32,6 +34,34 @@ KSFT_DISRUPTIVE = True
     pass
 
 
+class _KsftArgs:
+    def __init__(self):
+        self.list_tests = False
+        self.filters = []
+
+        try:
+            opts, _ = getopt.getopt(sys.argv[1:], 'hlt:T:')
+        except getopt.GetoptError as e:
+            print(e, file=sys.stderr)
+            sys.exit(1)
+
+        for opt, val in opts:
+            if opt == '-h':
+                print(f"Usage: {sys.argv[0]} [-h|-l] [-t|-T name]\n"
+                      f"\t-h       print help\n"
+                      f"\t-l       list all tests\n"
+                      f"\t-t name  include test\n"
+                      f"\t-T name  exclude test",
+                      file=sys.stderr)
+                sys.exit(0)
+            elif opt == '-l':
+                self.list_tests = True
+            elif opt == '-t':
+                self.filters.append((True, val))
+            elif opt == '-T':
+                self.filters.append((False, val))
+
+
 @functools.lru_cache()
 def _ksft_supports_color():
     if os.environ.get("NO_COLOR") is not None:
@@ -298,8 +328,26 @@ KsftCaseFunction = namedtuple("KsftCaseFunction",
         ksft_pr(f"Ignoring SIGTERM (cnt: {term_cnt}), already exiting...")
 
 
-def _ksft_generate_test_cases(cases, globs, case_pfx, args):
-    """Generate a flat list of (func, args, name) tuples"""
+def _ksft_name_matches(name, pattern):
+    if '*' in pattern or '?' in pattern or '[' in pattern:
+        return fnmatch.fnmatchcase(name, pattern)
+    return name == pattern
+
+
+def _ksft_test_enabled(name, filters):
+    has_positive = False
+    for include, pattern in filters:
+        has_positive |= include
+        if _ksft_name_matches(name, pattern):
+            return include
+    return not has_positive
+
+
+def _ksft_generate_test_cases(cases, globs, case_pfx, args, cli_args):
+    """Generate a filtered list of (func, args, name) tuples.
+
+    If -l is given, prints matching test names and exits.
+    """
 
     cases = cases or []
     test_cases = []
@@ -329,11 +377,22 @@ KsftCaseFunction = namedtuple("KsftCaseFunction",
         else:
             test_cases.append((func, args, func.__name__))
 
+    if cli_args.filters:
+        test_cases = [tc for tc in test_cases
+                      if _ksft_test_enabled(tc[2], cli_args.filters)]
+
+    if cli_args.list_tests:
+        for _, _, name in test_cases:
+            print(name)
+        sys.exit(0)
+
     return test_cases
 
 
 def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
-    test_cases = _ksft_generate_test_cases(cases, globs, case_pfx, args)
+    cli_args = _KsftArgs()
+    test_cases = _ksft_generate_test_cases(cases, globs, case_pfx, args,
+                                           cli_args)
 
     global term_cnt
     term_cnt = 0
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Jakub Kicinski

When developing new test cases and reproducing failures in existing ones we currently have to run the entire test which can take minutes to finish.

Add command line options for test selection, modeled after kselftest_harness.h:

  -l       list tests (all or filtered)
  -t name  include test
  -T name  exclude test

Since we don't have as clean separation into fixture / variant / test as kselftest_harness this is not really a 1 to 1 match. We have to lean on glob patterns instead.

Quoted 8 lines

Like in kselftest_harness filters are evaluated in order, first match wins. If only exclusions are specified everything else is included and vice versa.

Glob patterns (*, ?, [) are supported in addition to exact matching.

Tested-by on v1 1/1
#2 lore raw
In reply to Jakub Kicinski
Quoted 15 lines · Jakub Kicinski

When developing new test cases and reproducing failures in existing ones we currently have to run the entire test which can take minutes to finish.

Add command line options for test selection, modeled after kselftest_harness.h:

  -l       list tests (all or filtered)
  -t name  include test
  -T name  exclude test

Since we don't have as clean separation into fixture / variant / test as kselftest_harness this is not really a 1 to 1 match. We have to lean on glob patterns instead.

Quoted 8 lines

Like in kselftest_harness filters are evaluated in order, first match wins. If only exclusions are specified everything else is included and vice versa.

Glob patterns (*, ?, [) are supported in addition to exact matching.

This is very useful!

Reviewed-by on v1 1/1
#3 lore raw
In reply to Jakub Kicinski
Quoted 8 lines · Jakub Kicinski

When developing new test cases and reproducing failures in existing ones we currently have to run the entire test which can take minutes to finish.

Add command line options for test selection, modeled after kselftest_harness.h:

-l list tests (all or filtered)

<snip>

tools/testing/selftests/net/lib/py/ksft.py KSFT_DISRUPTIVE = True · line 52

Quoted 3 lines
tools/testing/selftests/net/lib/py/ksft.py +3 -0
+                print(f"Usage: {sys.argv[0]} [-h|-l] [-t|-T name]\n"
+                      f"\t-h       print help\n"
+                      f"\t-l       list all tests\n"

I initially expected the help text to mention "(all or filtered)" based on the commit message, but since this option lists all tests unconditionally, the current wording is correct.

author maintainer
#4 lore raw
Quoted 7 lines · On Thu, 9 Apr 2026 05:41:59 -0700 Breno Leitao wrote:

tools/testing/selftests/net/lib/py/ksft.py KSFT_DISRUPTIVE = True · line 52

Quoted 3 lines
tools/testing/selftests/net/lib/py/ksft.py +3 -0
+                print(f"Usage: {sys.argv[0]} [-h|-l] [-t|-T name]\n"
+                      f"\t-h       print help\n"
+                      f"\t-l       list all tests\n"  

I initially expected the help text to mention "(all or filtered)" based on the commit message, but since this option lists all tests unconditionally, the current wording is correct.

Ugh, good catch. Not sure how I lost this. It does display a filtered list. I wanted the filtering to take effect so that one can see what tests would have been executed with the filters without running them. Sort of like a --dry-run.

LMK if you have any thoughts on this, otherwise I'll rephrase as:

f"\t-l list tests (filtered, if filters were specified)\n"

Signature · 1 line
pw-bot: cr
#5 lore raw
Quoted 9 lines · Jakub Kicinski
Quoted 7 lines · On Thu, 9 Apr 2026 05:41:59 -0700 Breno Leitao wrote:

tools/testing/selftests/net/lib/py/ksft.py KSFT_DISRUPTIVE = True · line 52

Quoted 3 lines
tools/testing/selftests/net/lib/py/ksft.py +3 -0
+                print(f"Usage: {sys.argv[0]} [-h|-l] [-t|-T name]\n"
+                      f"\t-h       print help\n"
+                      f"\t-l       list all tests\n"  

I initially expected the help text to mention "(all or filtered)" based on the commit message, but since this option lists all tests unconditionally, the current wording is correct.

Quoted 8 lines

Ugh, good catch. Not sure how I lost this. It does display a filtered list. I wanted the filtering to take effect so that one can see what tests would have been executed with the filters without running them. Sort of like a --dry-run.

LMK if you have any thoughts on this, otherwise I'll rephrase as:

f"\t-l list tests (filtered, if filters were specified)\n"

Ack, the above is fine.