selftests: drv-net-hw: support using Python from net hw tests
authorJakub Kicinski <kuba@kernel.org>
Mon, 29 Apr 2024 14:44:22 +0000 (07:44 -0700)
committerJakub Kicinski <kuba@kernel.org>
Tue, 30 Apr 2024 15:15:31 +0000 (08:15 -0700)
We created a separate directory for HW-only tests, recently.
Glue in the Python test library there, Python is a bit annoying
when it comes to using library code located "lower"
in the directory structure.

Reuse the Env class, but let tests require non-nsim setup.

Reviewed-by: Willem de Bruijn <willemb@google.com>
Link: https://lore.kernel.org/r/20240429144426.743476-3-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
tools/testing/selftests/Makefile
tools/testing/selftests/drivers/net/hw/Makefile
tools/testing/selftests/drivers/net/hw/lib/py/__init__.py [new file with mode: 0644]
tools/testing/selftests/drivers/net/lib/py/env.py

index 2c940e9c4cedfe218bd616102d452cf50b5282a8..9039f3709affb91e91af16741c309e8c5c5de5ef 100644 (file)
@@ -119,7 +119,7 @@ TARGETS_HOTPLUG = cpu-hotplug
 TARGETS_HOTPLUG += memory-hotplug
 
 # Networking tests want the net/lib target, include it automatically
-ifneq ($(filter net drivers/net,$(TARGETS)),)
+ifneq ($(filter net drivers/net drivers/net/hw,$(TARGETS)),)
 ifeq ($(filter net/lib,$(TARGETS)),)
        INSTALL_DEP_TARGETS := net/lib
 endif
index 2259a39a70edf5de4dec81f94856650178bb7d3b..95f32158b0950a7d8a47e2b9fa245a4b84e97a0a 100644 (file)
@@ -16,6 +16,7 @@ TEST_FILES := \
        #
 
 TEST_INCLUDES := \
+       $(wildcard lib/py/*.py ../lib/py/*.py) \
        ../../../net/lib.sh \
        ../../../net/forwarding/lib.sh \
        ../../../net/forwarding/ipip_lib.sh \
diff --git a/tools/testing/selftests/drivers/net/hw/lib/py/__init__.py b/tools/testing/selftests/drivers/net/hw/lib/py/__init__.py
new file mode 100644 (file)
index 0000000..b582885
--- /dev/null
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: GPL-2.0
+
+import sys
+from pathlib import Path
+
+KSFT_DIR = (Path(__file__).parent / "../../../../..").resolve()
+
+try:
+    sys.path.append(KSFT_DIR.as_posix())
+    from net.lib.py import *
+    from drivers.net.lib.py import *
+except ModuleNotFoundError as e:
+    ksft_pr("Failed importing `net` library from kernel sources")
+    ksft_pr(str(e))
+    ktap_result(True, comment="SKIP")
+    sys.exit(4)
index e2ab637e56dcdc690781f51a5cec63493bbd7c71..5c8f695b253665447e42fd812bc41845e01015ed 100644 (file)
@@ -2,7 +2,7 @@
 
 import os
 from pathlib import Path
-from lib.py import KsftSkipEx
+from lib.py import KsftSkipEx, KsftXfailEx
 from lib.py import cmd, ip
 from lib.py import NetNS, NetdevSimDev
 from .remote import Remote
@@ -76,7 +76,7 @@ class NetDrvEpEnv:
     nsim_v4_pfx = "192.0.2."
     nsim_v6_pfx = "2001:db8::"
 
-    def __init__(self, src_path):
+    def __init__(self, src_path, nsim_test=None):
 
         self.env = _load_env_file(src_path)
 
@@ -88,7 +88,10 @@ class NetDrvEpEnv:
         self._ns_peer = None
 
         if "NETIF" in self.env:
+            if nsim_test is True:
+                raise KsftXfailEx("Test only works on netdevsim")
             self._check_env()
+
             self.dev = ip("link show dev " + self.env['NETIF'], json=True)[0]
 
             self.v4 = self.env.get("LOCAL_V4")
@@ -98,6 +101,9 @@ class NetDrvEpEnv:
             kind = self.env["REMOTE_TYPE"]
             args = self.env["REMOTE_ARGS"]
         else:
+            if nsim_test is False:
+                raise KsftXfailEx("Test does not work on netdevsim")
+
             self.create_local()
 
             self.dev = self._ns.nsims[0].dev