]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Move shell and perl util functions to isctest.run
authorNicki Křížek <nicki@isc.org>
Thu, 23 Jan 2025 14:04:30 +0000 (15:04 +0100)
committerMichal Nowak <mnowak@isc.org>
Tue, 4 Feb 2025 12:35:38 +0000 (12:35 +0000)
Previously, these functions have been provided as fixtures. This was
limiting re-use, because it wasn't possible to call these outside of
tests / other fixtures without passing these utility functions around.
Move them into isctest.run package instead.

bin/tests/system/conftest.py
bin/tests/system/isctest/run.py

index 1bb40cf7c992cc83d20cc9d680bd2da882d634a2..8412611426cc2e6b3a055214f4c80272c1434072 100644 (file)
@@ -9,7 +9,6 @@
 # See the COPYRIGHT file distributed with this work for additional
 # information regarding copyright ownership.
 
-from functools import partial
 import filecmp
 import os
 from pathlib import Path
@@ -18,7 +17,7 @@ import shutil
 import subprocess
 import tempfile
 import time
-from typing import Any, List, Optional
+from typing import Any
 
 import pytest
 
@@ -483,46 +482,6 @@ def templates(system_test_dir: Path):
     return isctest.template.TemplateEngine(system_test_dir)
 
 
-def _run_script(
-    system_test_dir: Path,
-    interpreter: str,
-    script: str,
-    args: Optional[List[str]] = None,
-):
-    """Helper function for the shell / perl script invocations (through fixtures below)."""
-    if args is None:
-        args = []
-    path = Path(script)
-    if not path.is_absolute():
-        # make sure relative paths are always relative to system_dir
-        path = system_test_dir.parent / path
-    script = str(path)
-    cwd = os.getcwd()
-    if not path.exists():
-        raise FileNotFoundError(f"script {script} not found in {cwd}")
-    isctest.log.debug("running script: %s %s %s", interpreter, script, " ".join(args))
-    isctest.log.debug("  workdir: %s", cwd)
-    returncode = 1
-
-    cmd = [interpreter, script] + args
-    with subprocess.Popen(
-        cmd,
-        stdout=subprocess.PIPE,
-        stderr=subprocess.STDOUT,
-        bufsize=1,
-        universal_newlines=True,
-        errors="backslashreplace",
-    ) as proc:
-        if proc.stdout:
-            for line in proc.stdout:
-                isctest.log.info("    %s", line.rstrip("\n"))
-        proc.communicate()
-        returncode = proc.returncode
-        if returncode:
-            raise subprocess.CalledProcessError(returncode, cmd)
-        isctest.log.debug("  exited with %d", returncode)
-
-
 def _get_node_path(node) -> Path:
     if isinstance(node.parent, pytest.Session):
         if _pytest_major_ver >= 8:
@@ -533,23 +492,11 @@ def _get_node_path(node) -> Path:
 
 
 @pytest.fixture(scope="module")
-def shell(system_test_dir):
-    """Function to call a shell script with arguments."""
-    return partial(_run_script, system_test_dir, os.environ["SHELL"])
-
-
-@pytest.fixture(scope="module")
-def perl(system_test_dir):
-    """Function to call a perl script with arguments."""
-    return partial(_run_script, system_test_dir, os.environ["PERL"])
-
-
-@pytest.fixture(scope="module")
-def run_tests_sh(system_test_dir, shell):
+def run_tests_sh(system_test_dir):
     """Utility function to execute tests.sh as a python test."""
 
     def run_tests():
-        shell(f"{system_test_dir}/tests.sh")
+        isctest.run.shell(f"{system_test_dir}/tests.sh")
 
     return run_tests
 
@@ -559,8 +506,6 @@ def system_test(
     request,
     system_test_dir,
     templates,
-    shell,
-    perl,
 ):
     """
     Driver of the test setup/teardown process. Used automatically for every test module.
@@ -586,14 +531,16 @@ def system_test(
 
     def check_net_interfaces():
         try:
-            perl("testsock.pl", ["-p", os.environ["PORT"]])
+            isctest.run.perl(
+                f"{os.environ['srcdir']}/testsock.pl", ["-p", os.environ["PORT"]]
+            )
         except subprocess.CalledProcessError as exc:
             isctest.log.error("testsock.pl: exited with code %d", exc.returncode)
             pytest.skip("Network interface aliases not set up.")
 
     def check_prerequisites():
         try:
-            shell(f"{system_test_dir}/prereq.sh")
+            isctest.run.shell(f"{system_test_dir}/prereq.sh")
         except FileNotFoundError:
             pass  # prereq.sh is optional
         except subprocess.CalledProcessError:
@@ -602,7 +549,7 @@ def system_test(
     def setup_test():
         templates.render_auto()
         try:
-            shell(f"{system_test_dir}/setup.sh")
+            isctest.run.shell(f"{system_test_dir}/setup.sh")
         except FileNotFoundError:
             pass  # setup.sh is optional
         except subprocess.CalledProcessError as exc:
@@ -611,14 +558,17 @@ def system_test(
 
     def start_servers():
         try:
-            perl("start.pl", ["--port", os.environ["PORT"], system_test_dir.name])
+            isctest.run.perl(
+                f"{os.environ['srcdir']}/start.pl",
+                ["--port", os.environ["PORT"], system_test_dir.name],
+            )
         except subprocess.CalledProcessError as exc:
             isctest.log.error("Failed to start servers")
             pytest.fail(f"start.pl exited with {exc.returncode}")
 
     def stop_servers():
         try:
-            perl("stop.pl", [system_test_dir.name])
+            isctest.run.perl(f"{os.environ['srcdir']}/stop.pl", [system_test_dir.name])
         except subprocess.CalledProcessError as exc:
             isctest.log.error("Failed to stop servers")
             get_core_dumps()
@@ -626,7 +576,9 @@ def system_test(
 
     def get_core_dumps():
         try:
-            shell("get_core_dumps.sh", [system_test_dir.name])
+            isctest.run.shell(
+                f"{os.environ['srcdir']}/get_core_dumps.sh", [system_test_dir.name]
+            )
         except subprocess.CalledProcessError as exc:
             isctest.log.error("Found core dumps or sanitizer reports")
             pytest.fail(f"get_core_dumps.sh exited with {exc.returncode}")
index dd9ac089063b272cb3a226efaec4afff9a2a11eb..d714f88a97214fcce07ed9c6eecec11e448d7340 100644 (file)
 # information regarding copyright ownership.
 
 import os
+from pathlib import Path
 import subprocess
 import time
-from typing import Optional
+from typing import List, Optional
 
 import isctest.log
 from isctest.compat import dns_rcode
@@ -65,6 +66,51 @@ def cmd(
         return exc
 
 
+def _run_script(
+    interpreter: str,
+    script: str,
+    args: Optional[List[str]] = None,
+):
+    if args is None:
+        args = []
+    path = Path(script)
+    script = str(path)
+    cwd = os.getcwd()
+    if not path.exists():
+        raise FileNotFoundError(f"script {script} not found in {cwd}")
+    isctest.log.debug("running script: %s %s %s", interpreter, script, " ".join(args))
+    isctest.log.debug("  workdir: %s", cwd)
+    returncode = 1
+
+    command = [interpreter, script] + args
+    with subprocess.Popen(
+        command,
+        stdout=subprocess.PIPE,
+        stderr=subprocess.STDOUT,
+        bufsize=1,
+        universal_newlines=True,
+        errors="backslashreplace",
+    ) as proc:
+        if proc.stdout:
+            for line in proc.stdout:
+                isctest.log.info("    %s", line.rstrip("\n"))
+        proc.communicate()
+        returncode = proc.returncode
+        if returncode:
+            raise subprocess.CalledProcessError(returncode, command)
+        isctest.log.debug("  exited with %d", returncode)
+
+
+def shell(script: str, args: Optional[List[str]] = None) -> None:
+    """Run a given script with system's shell interpreter."""
+    _run_script(os.environ["SHELL"], script, args)
+
+
+def perl(script: str, args: Optional[List[str]] = None) -> None:
+    """Run a given script with system's perl interpreter."""
+    _run_script(os.environ["PERL"], script, args)
+
+
 def retry_with_timeout(func, timeout, delay=1, msg=None):
     start_time = time.time()
     while time.time() < start_time + timeout: