ksft.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # SPDX-License-Identifier: GPL-2.0
  2. import builtins
  3. import functools
  4. import inspect
  5. import sys
  6. import time
  7. import traceback
  8. from .consts import KSFT_MAIN_NAME
  9. from .utils import global_defer_queue
  10. KSFT_RESULT = None
  11. KSFT_RESULT_ALL = True
  12. KSFT_DISRUPTIVE = True
  13. class KsftFailEx(Exception):
  14. pass
  15. class KsftSkipEx(Exception):
  16. pass
  17. class KsftXfailEx(Exception):
  18. pass
  19. def ksft_pr(*objs, **kwargs):
  20. print("#", *objs, **kwargs)
  21. def _fail(*args):
  22. global KSFT_RESULT
  23. KSFT_RESULT = False
  24. stack = inspect.stack()
  25. started = False
  26. for frame in reversed(stack[2:]):
  27. # Start printing from the test case function
  28. if not started:
  29. if frame.function == 'ksft_run':
  30. started = True
  31. continue
  32. ksft_pr("Check| At " + frame.filename + ", line " + str(frame.lineno) +
  33. ", in " + frame.function + ":")
  34. ksft_pr("Check| " + frame.code_context[0].strip())
  35. ksft_pr(*args)
  36. def ksft_eq(a, b, comment=""):
  37. global KSFT_RESULT
  38. if a != b:
  39. _fail("Check failed", a, "!=", b, comment)
  40. def ksft_ne(a, b, comment=""):
  41. global KSFT_RESULT
  42. if a == b:
  43. _fail("Check failed", a, "==", b, comment)
  44. def ksft_true(a, comment=""):
  45. if not a:
  46. _fail("Check failed", a, "does not eval to True", comment)
  47. def ksft_in(a, b, comment=""):
  48. if a not in b:
  49. _fail("Check failed", a, "not in", b, comment)
  50. def ksft_ge(a, b, comment=""):
  51. if a < b:
  52. _fail("Check failed", a, "<", b, comment)
  53. def ksft_lt(a, b, comment=""):
  54. if a >= b:
  55. _fail("Check failed", a, ">=", b, comment)
  56. class ksft_raises:
  57. def __init__(self, expected_type):
  58. self.exception = None
  59. self.expected_type = expected_type
  60. def __enter__(self):
  61. return self
  62. def __exit__(self, exc_type, exc_val, exc_tb):
  63. if exc_type is None:
  64. _fail(f"Expected exception {str(self.expected_type.__name__)}, none raised")
  65. elif self.expected_type != exc_type:
  66. _fail(f"Expected exception {str(self.expected_type.__name__)}, raised {str(exc_type.__name__)}")
  67. self.exception = exc_val
  68. # Suppress the exception if its the expected one
  69. return self.expected_type == exc_type
  70. def ksft_busy_wait(cond, sleep=0.005, deadline=1, comment=""):
  71. end = time.monotonic() + deadline
  72. while True:
  73. if cond():
  74. return
  75. if time.monotonic() > end:
  76. _fail("Waiting for condition timed out", comment)
  77. return
  78. time.sleep(sleep)
  79. def ktap_result(ok, cnt=1, case="", comment=""):
  80. global KSFT_RESULT_ALL
  81. KSFT_RESULT_ALL = KSFT_RESULT_ALL and ok
  82. res = ""
  83. if not ok:
  84. res += "not "
  85. res += "ok "
  86. res += str(cnt) + " "
  87. res += KSFT_MAIN_NAME
  88. if case:
  89. res += "." + str(case.__name__)
  90. if comment:
  91. res += " # " + comment
  92. print(res)
  93. def ksft_flush_defer():
  94. global KSFT_RESULT
  95. i = 0
  96. qlen_start = len(global_defer_queue)
  97. while global_defer_queue:
  98. i += 1
  99. entry = global_defer_queue.pop()
  100. try:
  101. entry.exec_only()
  102. except:
  103. ksft_pr(f"Exception while handling defer / cleanup (callback {i} of {qlen_start})!")
  104. tb = traceback.format_exc()
  105. for line in tb.strip().split('\n'):
  106. ksft_pr("Defer Exception|", line)
  107. KSFT_RESULT = False
  108. def ksft_disruptive(func):
  109. """
  110. Decorator that marks the test as disruptive (e.g. the test
  111. that can down the interface). Disruptive tests can be skipped
  112. by passing DISRUPTIVE=False environment variable.
  113. """
  114. @functools.wraps(func)
  115. def wrapper(*args, **kwargs):
  116. if not KSFT_DISRUPTIVE:
  117. raise KsftSkipEx(f"marked as disruptive")
  118. return func(*args, **kwargs)
  119. return wrapper
  120. def ksft_setup(env):
  121. """
  122. Setup test framework global state from the environment.
  123. """
  124. def get_bool(env, name):
  125. value = env.get(name, "").lower()
  126. if value in ["yes", "true"]:
  127. return True
  128. if value in ["no", "false"]:
  129. return False
  130. try:
  131. return bool(int(value))
  132. except:
  133. raise Exception(f"failed to parse {name}")
  134. if "DISRUPTIVE" in env:
  135. global KSFT_DISRUPTIVE
  136. KSFT_DISRUPTIVE = get_bool(env, "DISRUPTIVE")
  137. return env
  138. def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
  139. cases = cases or []
  140. if globs and case_pfx:
  141. for key, value in globs.items():
  142. if not callable(value):
  143. continue
  144. for prefix in case_pfx:
  145. if key.startswith(prefix):
  146. cases.append(value)
  147. break
  148. totals = {"pass": 0, "fail": 0, "skip": 0, "xfail": 0}
  149. print("KTAP version 1")
  150. print("1.." + str(len(cases)))
  151. global KSFT_RESULT
  152. cnt = 0
  153. stop = False
  154. for case in cases:
  155. KSFT_RESULT = True
  156. cnt += 1
  157. comment = ""
  158. cnt_key = ""
  159. try:
  160. case(*args)
  161. except KsftSkipEx as e:
  162. comment = "SKIP " + str(e)
  163. cnt_key = 'skip'
  164. except KsftXfailEx as e:
  165. comment = "XFAIL " + str(e)
  166. cnt_key = 'xfail'
  167. except BaseException as e:
  168. stop |= isinstance(e, KeyboardInterrupt)
  169. tb = traceback.format_exc()
  170. for line in tb.strip().split('\n'):
  171. ksft_pr("Exception|", line)
  172. if stop:
  173. ksft_pr("Stopping tests due to KeyboardInterrupt.")
  174. KSFT_RESULT = False
  175. cnt_key = 'fail'
  176. ksft_flush_defer()
  177. if not cnt_key:
  178. cnt_key = 'pass' if KSFT_RESULT else 'fail'
  179. ktap_result(KSFT_RESULT, cnt, case, comment=comment)
  180. totals[cnt_key] += 1
  181. if stop:
  182. break
  183. print(
  184. f"# Totals: pass:{totals['pass']} fail:{totals['fail']} xfail:{totals['xfail']} xpass:0 skip:{totals['skip']} error:0"
  185. )
  186. def ksft_exit():
  187. global KSFT_RESULT_ALL
  188. sys.exit(0 if KSFT_RESULT_ALL else 1)