Files
linux/tools/testing/selftests/net/lib/py/ksft.py
Jakub Kicinski 4fa6bd4b33 selftests: net: set the exit code correctly in Python tests
Test cases need to exit with non-zero status if they failed,
we currently don't do that:

  # KTAP version 1
  # 1..3
  # # At /root/ksft-net-drv/drivers/net/./ping.py line 18:
  # # Check failed 1 != 2
  # not ok 1 ping.test_v4
  # ok 2 ping.test_v6
  # ok 3 ping.test_tcp
  # # Totals: pass:2 fail:1 xfail:0 xpass:0 skip:0 error:0
  ok 1 selftests: drivers/net: ping.py
  ^^^^

It's a bit tempting to make the exit part of ksft_run(),
but that only works well for very trivial setups. We can
revisit this later, if people forget to call ksft_exit().

Link: https://lore.kernel.org/r/20240417231146.2435572-3-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2024-04-18 16:15:31 -07:00

127 lines
2.8 KiB
Python

# SPDX-License-Identifier: GPL-2.0
import builtins
import inspect
import sys
import time
import traceback
from .consts import KSFT_MAIN_NAME
KSFT_RESULT = None
KSFT_RESULT_ALL = True
class KsftSkipEx(Exception):
pass
class KsftXfailEx(Exception):
pass
def ksft_pr(*objs, **kwargs):
print("#", *objs, **kwargs)
def _fail(*args):
global KSFT_RESULT
KSFT_RESULT = False
frame = inspect.stack()[2]
ksft_pr("At " + frame.filename + " line " + str(frame.lineno) + ":")
ksft_pr(*args)
def ksft_eq(a, b, comment=""):
global KSFT_RESULT
if a != b:
_fail("Check failed", a, "!=", b, comment)
def ksft_true(a, comment=""):
if not a:
_fail("Check failed", a, "does not eval to True", comment)
def ksft_in(a, b, comment=""):
if a not in b:
_fail("Check failed", a, "not in", b, comment)
def ksft_ge(a, b, comment=""):
if a < b:
_fail("Check failed", a, "<", b, comment)
def ksft_busy_wait(cond, sleep=0.005, deadline=1, comment=""):
end = time.monotonic() + deadline
while True:
if cond():
return
if time.monotonic() > end:
_fail("Waiting for condition timed out", comment)
return
time.sleep(sleep)
def ktap_result(ok, cnt=1, case="", comment=""):
global KSFT_RESULT_ALL
KSFT_RESULT_ALL = KSFT_RESULT_ALL and ok
res = ""
if not ok:
res += "not "
res += "ok "
res += str(cnt) + " "
res += KSFT_MAIN_NAME
if case:
res += "." + str(case.__name__)
if comment:
res += " # " + comment
print(res)
def ksft_run(cases, args=()):
totals = {"pass": 0, "fail": 0, "skip": 0, "xfail": 0}
print("KTAP version 1")
print("1.." + str(len(cases)))
global KSFT_RESULT
cnt = 0
for case in cases:
KSFT_RESULT = True
cnt += 1
try:
case(*args)
except KsftSkipEx as e:
ktap_result(True, cnt, case, comment="SKIP " + str(e))
totals['skip'] += 1
continue
except KsftXfailEx as e:
ktap_result(True, cnt, case, comment="XFAIL " + str(e))
totals['xfail'] += 1
continue
except Exception as e:
tb = traceback.format_exc()
for line in tb.strip().split('\n'):
ksft_pr("Exception|", line)
ktap_result(False, cnt, case)
totals['fail'] += 1
continue
ktap_result(KSFT_RESULT, cnt, case)
if KSFT_RESULT:
totals['pass'] += 1
else:
totals['fail'] += 1
print(
f"# Totals: pass:{totals['pass']} fail:{totals['fail']} xfail:{totals['xfail']} xpass:0 skip:{totals['skip']} error:0"
)
def ksft_exit():
global KSFT_RESULT_ALL
sys.exit(0 if KSFT_RESULT_ALL else 1)