selftests: drivers: add scaffolding for Netlink tests in Python

Add drivers/net as a target for mixed-use tests.
The setup is expected to work similarly to the forwarding tests.
Since we only need one interface (unlike forwarding tests)
read the target device name from NETIF. If not present we'll
try to run the test against netdevsim.

Reviewed-by: Petr Machata <petrm@nvidia.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Jakub Kicinski
2024-04-04 19:45:25 -07:00
committed by David S. Miller
parent f216306bfb
commit b4db9f8402
7 changed files with 224 additions and 1 deletions

View File

@@ -0,0 +1,52 @@
# SPDX-License-Identifier: GPL-2.0
import os
import shlex
from pathlib import Path
from lib.py import ip
from lib.py import NetdevSimDev
class NetDrvEnv:
def __init__(self, src_path):
self._ns = None
self.env = os.environ.copy()
self._load_env_file(src_path)
if 'NETIF' in self.env:
self.dev = ip("link show dev " + self.env['NETIF'], json=True)[0]
else:
self._ns = NetdevSimDev()
self.dev = self._ns.nsims[0].dev
self.ifindex = self.dev['ifindex']
def __enter__(self):
return self
def __exit__(self, ex_type, ex_value, ex_tb):
"""
__exit__ gets called at the end of a "with" block.
"""
self.__del__()
def __del__(self):
if self._ns:
self._ns.remove()
self._ns = None
def _load_env_file(self, src_path):
src_dir = Path(src_path).parent.resolve()
if not (src_dir / "net.config").exists():
return
lexer = shlex.shlex(open((src_dir / "net.config").as_posix(), 'r').read())
k = None
for token in lexer:
if k is None:
k = token
self.env[k] = ""
elif token == "=":
pass
else:
self.env[k] = token
k = None