xref: /linux/tools/testing/selftests/net/lib/py/ynl.py (revision 4f9786035f9e519db41375818e1d0b5f20da2f10)
1# SPDX-License-Identifier: GPL-2.0
2
3import sys
4from pathlib import Path
5from .consts import KSRC, KSFT_DIR
6from .ksft import ksft_pr, ktap_result
7
8# Resolve paths
9try:
10    if (KSFT_DIR / "kselftest-list.txt").exists():
11        # Running in "installed" selftests
12        tools_full_path = KSFT_DIR
13        SPEC_PATH = KSFT_DIR / "net/lib/specs"
14
15        sys.path.append(tools_full_path.as_posix())
16        from net.lib.ynl.pyynl.lib import YnlFamily, NlError
17    else:
18        # Running in tree
19        tools_full_path = KSRC / "tools"
20        SPEC_PATH = KSRC / "Documentation/netlink/specs"
21
22        sys.path.append(tools_full_path.as_posix())
23        from net.ynl.pyynl.lib import YnlFamily, NlError
24except ModuleNotFoundError as e:
25    ksft_pr("Failed importing `ynl` library from kernel sources")
26    ksft_pr(str(e))
27    ktap_result(True, comment="SKIP")
28    sys.exit(4)
29
30#
31# Wrapper classes, loading the right specs
32# Set schema='' to avoid jsonschema validation, it's slow
33#
34class EthtoolFamily(YnlFamily):
35    def __init__(self, recv_size=0):
36        super().__init__((SPEC_PATH / Path('ethtool.yaml')).as_posix(),
37                         schema='', recv_size=recv_size)
38
39
40class RtnlFamily(YnlFamily):
41    def __init__(self, recv_size=0):
42        super().__init__((SPEC_PATH / Path('rt_link.yaml')).as_posix(),
43                         schema='', recv_size=recv_size)
44
45class RtnlAddrFamily(YnlFamily):
46    def __init__(self, recv_size=0):
47        super().__init__((SPEC_PATH / Path('rt_addr.yaml')).as_posix(),
48                         schema='', recv_size=recv_size)
49
50class NetdevFamily(YnlFamily):
51    def __init__(self, recv_size=0):
52        super().__init__((SPEC_PATH / Path('netdev.yaml')).as_posix(),
53                         schema='', recv_size=recv_size)
54
55class NetshaperFamily(YnlFamily):
56    def __init__(self, recv_size=0):
57        super().__init__((SPEC_PATH / Path('net_shaper.yaml')).as_posix(),
58                         schema='', recv_size=recv_size)
59