aboutsummaryrefslogtreecommitdiff
path: root/tools/testing/selftests/net/lib/py/ksft.py
blob: c7210525981c6c6233fdb5b789a54b8c20604124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# SPDX-License-Identifier: GPL-2.0

import builtins
from .consts import KSFT_MAIN_NAME

KSFT_RESULT = None


class KsftSkipEx(Exception):
    pass


class KsftXfailEx(Exception):
    pass


def ksft_pr(*objs, **kwargs):
    print("#", *objs, **kwargs)


def ksft_eq(a, b, comment=""):
    global KSFT_RESULT
    if a != b:
        KSFT_RESULT = False
        ksft_pr("Check failed", a, "!=", b, comment)


def ksft_true(a, comment=""):
    global KSFT_RESULT
    if not a:
        KSFT_RESULT = False
        ksft_pr("Check failed", a, "does not eval to True", comment)


def ksft_in(a, b, comment=""):
    global KSFT_RESULT
    if a not in b:
        KSFT_RESULT = False
        ksft_pr("Check failed", a, "not in", b, comment)


def ksft_ge(a, b, comment=""):
    global KSFT_RESULT
    if a < b:
        KSFT_RESULT = False
        ksft_pr("Check failed", a, "<", b, comment)


def ktap_result(ok, cnt=1, case="", comment=""):
    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:
            for line in str(e).split('\n'):
                ksft_pr("Exception|", line)
            ktap_result(False, cnt, case)
            totals['fail'] += 1
            continue

        ktap_result(KSFT_RESULT, cnt, case)
        totals['pass'] += 1

    print(
        f"# Totals: pass:{totals['pass']} fail:{totals['fail']} xfail:{totals['xfail']} xpass:0 skip:{totals['skip']} error:0"
    )