tests: Add support for getting corefile patterns on FreeBSD
[vpp.git] / test / vpp_iperf.py
index 42b5ea8..b325399 100644 (file)
@@ -36,7 +36,7 @@ class VppIperf:
         self.server_args = ""
         self.logger = logger
         # Set the iperf executable
-        self.iperf = os.path.join(os.getenv("TEST_DATA_DIR") or "/", "usr/bin/iperf")
+        self.iperf = self.get_iperf()
 
     def ensure_init(self):
         if self.server_ns and self.client_ns and self.server_ip:
@@ -46,7 +46,26 @@ class VppIperf:
                 "Error: Cannot Start." "iPerf object has not been initialized"
             )
 
+    def get_iperf(self):
+        """Return the iperf executable for running tests.
+
+        Look for the iperf executable in the following order
+        1. ${TEST_DATA_DIR}/usr/bin/iperf  # running tests inside the VM
+        2. /usr/bin/iperf3                 # running tests on the host
+        """
+        vm_test_dir = os.getenv("TEST_DATA_DIR", "/tmp/vpp-vm-tests")
+        if os.path.isdir(vm_test_dir):
+            iperf = os.path.join(vm_test_dir, "/usr/bin/iperf")
+        else:
+            iperf = "/usr/bin/iperf3"
+        if os.path.exists(iperf):
+            return iperf
+        else:
+            self.logger.error(f"Could not find an iperf executable for running tests")
+            sys.exit(1)
+
     def start_iperf_server(self):
+        """Starts the  iperf server and returns the process cmdline args."""
         args = [
             "ip",
             "netns",
@@ -57,10 +76,11 @@ class VppIperf:
             "-D",
         ]
         args.extend(self.server_args.split())
-        args = " ".join(args)
+        cmd = " ".join(args)
+        self.logger.debug(f"Starting iperf server: {cmd}")
         try:
-            return subprocess.run(
-                args,
+            subprocess.run(
+                cmd,
                 timeout=self.duration + 5,
                 encoding="utf-8",
                 shell=True,
@@ -69,6 +89,7 @@ class VppIperf:
             )
         except subprocess.TimeoutExpired as e:
             raise Exception("Error: Timeout expired for iPerf", e.output)
+        return args[4:]
 
     def start_iperf_client(self):
         args = [
@@ -106,7 +127,7 @@ class VppIperf:
         """
         self.ensure_init()
         if not client_only:
-            self.start_iperf_server()
+            return self.start_iperf_server()
         if not server_only:
             result = self.start_iperf_client()
             self.logger.debug(f"Iperf client args: {result.args}")
@@ -173,17 +194,22 @@ def start_iperf(
     return iperf.start(server_only=server_only, client_only=client_only)
 
 
-def stop_iperf():
-    args = ["pkill", "iperf"]
-    args = " ".join(args)
-    try:
-        return subprocess.run(
-            args,
-            encoding="utf-8",
-            shell=True,
-        )
-    except Exception:
-        pass
+def stop_iperf(iperf_cmd):
+    """Stop the iperf process matching the iperf_cmd string."""
+    args = ["pgrep", "-x", "-f", iperf_cmd]
+    p = subprocess.Popen(
+        args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8"
+    )
+    stdout, _ = p.communicate()
+    for pid in stdout.split():
+        try:
+            subprocess.run(
+                f"kill -9 {pid}",
+                encoding="utf-8",
+                shell=True,
+            )
+        except Exception:
+            pass
 
 
 if __name__ == "__main__":