selftests: drv-net: factor out a DrvEnv base class
Subsystems (3)
Jakub Kicinski
maintainerPATCH 1/2 selftests: drv-net: factor out a DrvEnv base class
We have separate Env classes for local tests and tests with a remote endpoint. Make it easier to share the code by creating a base class. Make env loading a method of this class.
--- .../selftests/drivers/net/lib/py/env.py | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-)
tools/testing/selftests/drivers/net/lib/py/env.py +33 -26
@@ -10,38 +10,46 @@ from lib.py import NetNS, NetdevSimDev from .remote import Remote -def _load_env_file(src_path): - env = os.environ.copy() +class NetDrvEnvBase: + """ + Base class for a NIC / host envirnoments + """ + def __init__(self, src_path): + self.src_path = src_path + self.env = self._load_env_file() - src_dir = Path(src_path).parent.resolve() - if not (src_dir / "net.config").exists(): + def _load_env_file(self): + env = os.environ.copy() + + src_dir = Path(self.src_path).parent.resolve() + if not (src_dir / "net.config").exists(): + return ksft_setup(env) + + with open((src_dir / "net.config").as_posix(), 'r') as fp: + for line in fp.readlines(): + full_file = line + # Strip comments + pos = line.find("#") + if pos >= 0: + line = line[:pos] + line = line.strip() + if not line: + continue + pair = line.split('=', maxsplit=1) + if len(pair) != 2: + raise Exception("Can't parse configuration line:", full_file) + env[pair[0]] = pair[1] return ksft_setup(env) - with open((src_dir / "net.config").as_posix(), 'r') as fp: - for line in fp.readlines(): - full_file = line - # Strip comments - pos = line.find("#") - if pos >= 0: - line = line[:pos] - line = line.strip() - if not line: - continue - pair = line.split('=', maxsplit=1) - if len(pair) != 2: - raise Exception("Can't parse configuration line:", full_file) - env[pair[0]] = pair[1] - return ksft_setup(env) - -class NetDrvEnv: +class NetDrvEnv(NetDrvEnvBase): """ Class for a single NIC / host env, with no remote end """ def __init__(self, src_path, **kwargs): - self._ns = None + super().__init__(src_path) - self.env = _load_env_file(src_path) + self._ns = None if 'NETIF' in self.env: self.dev = ip("link show dev " + self.env['NETIF'], json=True)[0]
@@ -68,7 +76,7 @@ from .remote import Remote self._ns = None -class NetDrvEpEnv: +class NetDrvEpEnv(NetDrvEnvBase): """ Class for an environment with a local device and "remote endpoint" which can be used to send traffic in.
@@ -82,8 +90,7 @@ from .remote import Remote nsim_v6_pfx = "2001:db8::" def __init__(self, src_path, nsim_test=None): - - self.env = _load_env_file(src_path) + super().__init__(src_path) self._stats_settle_time = None
Signature · 1 line
2.48.1
Jakub Kicinski
maintainerPATCH 2/2 selftests: drv-net: add helper for path resolution
Refering to C binaries from Python code is going to be a common need. Add a helper to convert from path in relation to the test. Meaning, if the test is in the same directory as the binary, the call would be simply: cfg.rpath("binary").
The helper name "rpath" is not great. I can't think of a better name that would be accurate yet concise.
--- tools/testing/selftests/drivers/net/hw/csum.py | 2 +- tools/testing/selftests/drivers/net/lib/py/env.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-)
tools/testing/selftests/drivers/net/hw/csum.py +1 -1
@@ -100,7 +100,7 @@ from lib.py import bkg, cmd, wait_port_listen with NetDrvEpEnv(__file__, nsim_test=False) as cfg: check_nic_features(cfg) - cfg.bin_local = path.abspath(path.dirname(__file__) + "/../../../net/lib/csum") + cfg.bin_local = cfg.rpath("../../../net/lib/csum") cfg.bin_remote = cfg.remote.deploy(cfg.bin_local) cases = []
tools/testing/selftests/drivers/net/lib/py/env.py +12 -0
@@ -18,6 +18,18 @@ from .remote import Remote self.src_path = src_path self.env = self._load_env_file() + def rpath(self, path): + """ + Get an absolute path to a file based on a path relative to the directory + containing the test which constructed env. + + For example, if the test.py is in the same directory as + a binary (built from helper.c), the test can use env.rpath("helper") + to get the absolute path to the binary + """ + src_dir = Path(self.src_path).parent.resolve() + return (src_dir / path).as_posix() + def _load_env_file(self): env = os.environ.copy()
Signature · 1 line
2.48.1
Quoted 5 lines · Jakub Kicinski
We have separate Env classes for local tests and tests with a remote endpoint. Make it easier to share the code by creating a base class. Make env loading a method of this class.
Signed-off-by Jakub Kicinski <[email protected]>
Re: [PATCH net-next 2/2] selftests: drv-net: add helper for path resolution
In reply to Jakub KicinskiQuoted 9 lines · Jakub Kicinski
Refering to C binaries from Python code is going to be a common need. Add a helper to convert from path in relation to the test. Meaning, if the test is in the same directory as the binary, the call would be simply:
cfg.rpath("binary").The helper name "rpath" is not great. I can't think of a better name that would be accurate yet concise.
Signed-off-by Jakub Kicinski <[email protected]>
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Fri, 7 Feb 2025 10:41:39 -0800 you wrote:
We have separate Env classes for local tests and tests with a remote endpoint. Make it easier to share the code by creating a base class. Make env loading a method of this class.
Signed-off-by Jakub Kicinski <[email protected]>--- .../selftests/drivers/net/lib/py/env.py | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-)
Here is the summary with links:
- [net-next,1/2] selftests: drv-net: factor out a DrvEnv base class
https://git.kernel.org/netdev/net-next/c/29604bc2aaed - [net-next,2/2] selftests: drv-net: add helper for path resolution https://git.kernel.org/netdev/net-next/c/3337064f4204
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html