Skip to content

Commit 29604bc

Browse files
committed
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. Reviewed-by: Petr Machata <petrm@nvidia.com> Link: https://patch.msgid.link/20250207184140.1730466-1-kuba@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent a980da5 commit 29604bc

File tree

1 file changed

+35
-28
lines changed
  • tools/testing/selftests/drivers/net/lib/py

1 file changed

+35
-28
lines changed

tools/testing/selftests/drivers/net/lib/py/env.py

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,46 @@
1010
from .remote import Remote
1111

1212

13-
def _load_env_file(src_path):
14-
env = os.environ.copy()
15-
16-
src_dir = Path(src_path).parent.resolve()
17-
if not (src_dir / "net.config").exists():
13+
class NetDrvEnvBase:
14+
"""
15+
Base class for a NIC / host envirnoments
16+
"""
17+
def __init__(self, src_path):
18+
self.src_path = src_path
19+
self.env = self._load_env_file()
20+
21+
def _load_env_file(self):
22+
env = os.environ.copy()
23+
24+
src_dir = Path(self.src_path).parent.resolve()
25+
if not (src_dir / "net.config").exists():
26+
return ksft_setup(env)
27+
28+
with open((src_dir / "net.config").as_posix(), 'r') as fp:
29+
for line in fp.readlines():
30+
full_file = line
31+
# Strip comments
32+
pos = line.find("#")
33+
if pos >= 0:
34+
line = line[:pos]
35+
line = line.strip()
36+
if not line:
37+
continue
38+
pair = line.split('=', maxsplit=1)
39+
if len(pair) != 2:
40+
raise Exception("Can't parse configuration line:", full_file)
41+
env[pair[0]] = pair[1]
1842
return ksft_setup(env)
1943

20-
with open((src_dir / "net.config").as_posix(), 'r') as fp:
21-
for line in fp.readlines():
22-
full_file = line
23-
# Strip comments
24-
pos = line.find("#")
25-
if pos >= 0:
26-
line = line[:pos]
27-
line = line.strip()
28-
if not line:
29-
continue
30-
pair = line.split('=', maxsplit=1)
31-
if len(pair) != 2:
32-
raise Exception("Can't parse configuration line:", full_file)
33-
env[pair[0]] = pair[1]
34-
return ksft_setup(env)
35-
36-
37-
class NetDrvEnv:
44+
45+
class NetDrvEnv(NetDrvEnvBase):
3846
"""
3947
Class for a single NIC / host env, with no remote end
4048
"""
4149
def __init__(self, src_path, **kwargs):
42-
self._ns = None
50+
super().__init__(src_path)
4351

44-
self.env = _load_env_file(src_path)
52+
self._ns = None
4553

4654
if 'NETIF' in self.env:
4755
self.dev = ip("link show dev " + self.env['NETIF'], json=True)[0]
@@ -68,7 +76,7 @@ def __del__(self):
6876
self._ns = None
6977

7078

71-
class NetDrvEpEnv:
79+
class NetDrvEpEnv(NetDrvEnvBase):
7280
"""
7381
Class for an environment with a local device and "remote endpoint"
7482
which can be used to send traffic in.
@@ -82,8 +90,7 @@ class NetDrvEpEnv:
8290
nsim_v6_pfx = "2001:db8::"
8391

8492
def __init__(self, src_path, nsim_test=None):
85-
86-
self.env = _load_env_file(src_path)
93+
super().__init__(src_path)
8794

8895
self._stats_settle_time = None
8996

0 commit comments

Comments
 (0)