mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-09-10 08:41:03 -04:00
selftests: net: py: extract the case generation logic
In preparation for adding test variants move the test case collection logic to a dedicated function. New helper returns (function, args, name, ) tuples. The main test loop can simply run them, not much logic or discernment needed. Reviewed-by: Petr Machata <petrm@nvidia.com> Reviewed-by: Willem de Bruijn <willemb@google.com> Link: https://patch.msgid.link/20251120021024.2944527-3-kuba@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
@@ -135,7 +135,7 @@ def ksft_busy_wait(cond, sleep=0.005, deadline=1, comment=""):
|
|||||||
time.sleep(sleep)
|
time.sleep(sleep)
|
||||||
|
|
||||||
|
|
||||||
def ktap_result(ok, cnt=1, case="", comment=""):
|
def ktap_result(ok, cnt=1, case_name="", comment=""):
|
||||||
global KSFT_RESULT_ALL
|
global KSFT_RESULT_ALL
|
||||||
KSFT_RESULT_ALL = KSFT_RESULT_ALL and ok
|
KSFT_RESULT_ALL = KSFT_RESULT_ALL and ok
|
||||||
|
|
||||||
@@ -145,8 +145,8 @@ def ktap_result(ok, cnt=1, case="", comment=""):
|
|||||||
res += "ok "
|
res += "ok "
|
||||||
res += str(cnt) + " "
|
res += str(cnt) + " "
|
||||||
res += KSFT_MAIN_NAME
|
res += KSFT_MAIN_NAME
|
||||||
if case:
|
if case_name:
|
||||||
res += "." + str(case.__name__)
|
res += "." + case_name
|
||||||
if comment:
|
if comment:
|
||||||
res += " # " + comment
|
res += " # " + comment
|
||||||
print(res, flush=True)
|
print(res, flush=True)
|
||||||
@@ -219,9 +219,13 @@ def _ksft_intr(signum, frame):
|
|||||||
ksft_pr(f"Ignoring SIGTERM (cnt: {term_cnt}), already exiting...")
|
ksft_pr(f"Ignoring SIGTERM (cnt: {term_cnt}), already exiting...")
|
||||||
|
|
||||||
|
|
||||||
def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
|
def _ksft_generate_test_cases(cases, globs, case_pfx, args):
|
||||||
cases = cases or []
|
"""Generate a flat list of (func, args, name) tuples"""
|
||||||
|
|
||||||
|
cases = cases or []
|
||||||
|
test_cases = []
|
||||||
|
|
||||||
|
# If using the globs method find all relevant functions
|
||||||
if globs and case_pfx:
|
if globs and case_pfx:
|
||||||
for key, value in globs.items():
|
for key, value in globs.items():
|
||||||
if not callable(value):
|
if not callable(value):
|
||||||
@@ -231,6 +235,15 @@ def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
|
|||||||
cases.append(value)
|
cases.append(value)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
for func in cases:
|
||||||
|
test_cases.append((func, args, func.__name__))
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
global term_cnt
|
global term_cnt
|
||||||
term_cnt = 0
|
term_cnt = 0
|
||||||
prev_sigterm = signal.signal(signal.SIGTERM, _ksft_intr)
|
prev_sigterm = signal.signal(signal.SIGTERM, _ksft_intr)
|
||||||
@@ -238,19 +251,19 @@ def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
|
|||||||
totals = {"pass": 0, "fail": 0, "skip": 0, "xfail": 0}
|
totals = {"pass": 0, "fail": 0, "skip": 0, "xfail": 0}
|
||||||
|
|
||||||
print("TAP version 13", flush=True)
|
print("TAP version 13", flush=True)
|
||||||
print("1.." + str(len(cases)), flush=True)
|
print("1.." + str(len(test_cases)), flush=True)
|
||||||
|
|
||||||
global KSFT_RESULT
|
global KSFT_RESULT
|
||||||
cnt = 0
|
cnt = 0
|
||||||
stop = False
|
stop = False
|
||||||
for case in cases:
|
for func, args, name in test_cases:
|
||||||
KSFT_RESULT = True
|
KSFT_RESULT = True
|
||||||
cnt += 1
|
cnt += 1
|
||||||
comment = ""
|
comment = ""
|
||||||
cnt_key = ""
|
cnt_key = ""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
case(*args)
|
func(*args)
|
||||||
except KsftSkipEx as e:
|
except KsftSkipEx as e:
|
||||||
comment = "SKIP " + str(e)
|
comment = "SKIP " + str(e)
|
||||||
cnt_key = 'skip'
|
cnt_key = 'skip'
|
||||||
@@ -272,7 +285,7 @@ def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
|
|||||||
if not cnt_key:
|
if not cnt_key:
|
||||||
cnt_key = 'pass' if KSFT_RESULT else 'fail'
|
cnt_key = 'pass' if KSFT_RESULT else 'fail'
|
||||||
|
|
||||||
ktap_result(KSFT_RESULT, cnt, case, comment=comment)
|
ktap_result(KSFT_RESULT, cnt, name, comment=comment)
|
||||||
totals[cnt_key] += 1
|
totals[cnt_key] += 1
|
||||||
|
|
||||||
if stop:
|
if stop:
|
||||||
|
|||||||
Reference in New Issue
Block a user