Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+22 -9
+22 -9
tools/testing/selftests/net/lib/py/ksft.py
··· 135 135 time.sleep(sleep) 136 136 137 137 138 - def ktap_result(ok, cnt=1, case="", comment=""): 138 + def ktap_result(ok, cnt=1, case_name="", comment=""): 139 139 global KSFT_RESULT_ALL 140 140 KSFT_RESULT_ALL = KSFT_RESULT_ALL and ok 141 141 ··· 145 145 res += "ok " 146 146 res += str(cnt) + " " 147 147 res += KSFT_MAIN_NAME 148 - if case: 149 - res += "." + str(case.__name__) 148 + if case_name: 149 + res += "." + case_name 150 150 if comment: 151 151 res += " # " + comment 152 152 print(res, flush=True) ··· 219 219 ksft_pr(f"Ignoring SIGTERM (cnt: {term_cnt}), already exiting...") 220 220 221 221 222 - def ksft_run(cases=None, globs=None, case_pfx=None, args=()): 223 - cases = cases or [] 222 + def _ksft_generate_test_cases(cases, globs, case_pfx, args): 223 + """Generate a flat list of (func, args, name) tuples""" 224 224 225 + cases = cases or [] 226 + test_cases = [] 227 + 228 + # If using the globs method find all relevant functions 225 229 if globs and case_pfx: 226 230 for key, value in globs.items(): 227 231 if not callable(value): ··· 235 231 cases.append(value) 236 232 break 237 233 234 + for func in cases: 235 + test_cases.append((func, args, func.__name__)) 236 + 237 + return test_cases 238 + 239 + 240 + def ksft_run(cases=None, globs=None, case_pfx=None, args=()): 241 + test_cases = _ksft_generate_test_cases(cases, globs, case_pfx, args) 242 + 238 243 global term_cnt 239 244 term_cnt = 0 240 245 prev_sigterm = signal.signal(signal.SIGTERM, _ksft_intr) ··· 251 238 totals = {"pass": 0, "fail": 0, "skip": 0, "xfail": 0} 252 239 253 240 print("TAP version 13", flush=True) 254 - print("1.." + str(len(cases)), flush=True) 241 + print("1.." + str(len(test_cases)), flush=True) 255 242 256 243 global KSFT_RESULT 257 244 cnt = 0 258 245 stop = False 259 - for case in cases: 246 + for func, args, name in test_cases: 260 247 KSFT_RESULT = True 261 248 cnt += 1 262 249 comment = "" 263 250 cnt_key = "" 264 251 265 252 try: 266 - case(*args) 253 + func(*args) 267 254 except KsftSkipEx as e: 268 255 comment = "SKIP " + str(e) 269 256 cnt_key = 'skip' ··· 285 272 if not cnt_key: 286 273 cnt_key = 'pass' if KSFT_RESULT else 'fail' 287 274 288 - ktap_result(KSFT_RESULT, cnt, case, comment=comment) 275 + ktap_result(KSFT_RESULT, cnt, name, comment=comment) 289 276 totals[cnt_key] += 1 290 277 291 278 if stop: