def _fail(*args): global KSFT_RESULT
KSFT_RESULT = False
stack = inspect.stack()
started = False for frame in reversed(stack[2:]): # Start printing from the test case function ifnot started: if frame.function == 'ksft_run':
started = True continue
ksft_pr("Check| At " + frame.filename + ", line " + str(frame.lineno) + ", in " + frame.function + ":")
ksft_pr("Check| " + frame.code_context[0].strip())
ksft_pr(*args)
def ksft_eq(a, b, comment=""): global KSFT_RESULT if a != b:
_fail("Check failed", a, "!=", b, comment)
def ksft_ne(a, b, comment=""): global KSFT_RESULT if a == b:
_fail("Check failed", a, "==", b, comment)
def ksft_true(a, comment=""): ifnot a:
_fail("Check failed", a, "does not eval to True", comment)
def ksft_in(a, b, comment=""): if a notin b:
_fail("Check failed", a, "not in", b, comment)
def ksft_not_in(a, b, comment=""): if a in b:
_fail("Check failed", a, "in", b, comment)
def ksft_is(a, b, comment=""): if a isnot b:
_fail("Check failed", a, "is not", b, comment)
def ksft_ge(a, b, comment=""): if a < b:
_fail("Check failed", a, "<", b, comment)
def ksft_lt(a, b, comment=""): if a >= b:
_fail("Check failed", a, ">=", b, comment)
def __exit__(self, exc_type, exc_val, exc_tb): if exc_type isNone:
_fail(f"Expected exception {str(self.expected_type.__name__)}, none raised") elif self.expected_type != exc_type:
_fail(f"Expected exception {str(self.expected_type.__name__)}, raised {str(exc_type.__name__)}")
self.exception = exc_val # Suppress the exception if its the expected one return self.expected_type == exc_type
def ksft_busy_wait(cond, sleep=0.005, deadline=1, comment=""):
end = time.monotonic() + deadline whileTrue: 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 = "" ifnot ok:
res += "not "
res += "ok "
res += str(cnt) + " "
res += KSFT_MAIN_NAME if case:
res += "." + str(case.__name__) if comment:
res += " # " + comment
print(res, flush=True)
def ksft_flush_defer(): global KSFT_RESULT
i = 0
qlen_start = len(global_defer_queue) while global_defer_queue:
i += 1
entry = global_defer_queue.pop() try:
entry.exec_only() except:
ksft_pr(f"Exception while handling defer / cleanup (callback {i} of {qlen_start})!")
tb = traceback.format_exc() for line in tb.strip().split('\n'):
ksft_pr("Defer Exception|", line)
KSFT_RESULT = False
def ksft_disruptive(func): """
Decorator that marks the test as disruptive (e.g. the test
that can down the interface). Disruptive tests can be skipped
by passing DISRUPTIVE=False environment variable. """
def ksft_setup(env): """
Setup test framework global state from the environment. """
def get_bool(env, name):
value = env.get(name, "").lower() if value in ["yes", "true"]: returnTrue if value in ["no", "false"]: returnFalse try: return bool(int(value)) except: raise Exception(f"failed to parse {name}")
if"DISRUPTIVE"in env: global KSFT_DISRUPTIVE
KSFT_DISRUPTIVE = get_bool(env, "DISRUPTIVE")
return env
def _ksft_intr(signum, frame): # ksft runner.sh sends 2 SIGTERMs in a row on a timeout # if we don't ignore the second one it will stop us from handling cleanup global term_cnt
term_cnt += 1 if term_cnt == 1: raise KsftTerminate() else:
ksft_pr(f"Ignoring SIGTERM (cnt: {term_cnt}), already exiting...")
def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
cases = cases or []
if globs and case_pfx: for key, value in globs.items(): ifnot callable(value): continue for prefix in case_pfx: if key.startswith(prefix):
cases.append(value) break
global term_cnt
term_cnt = 0
prev_sigterm = signal.signal(signal.SIGTERM, _ksft_intr)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.