[v2,7/8] dts: add dts workflow module

Message ID 20220711145126.295427-8-juraj.linkes@pantheon.tech (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series ssh connection to a node |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Juraj Linkeš July 11, 2022, 2:51 p.m. UTC
  The module implements methods needed to run DTS. It handles the creation
of objects and eventually the whole DTS workflow, such as running node
setups, test gathering, setup and execution and various cleanups.

Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
---
 dts/framework/dts.py   | 76 ++++++++++++++++++++++++++++++++++++++++++
 dts/framework/utils.py | 35 +++++++++++++++++--
 2 files changed, 109 insertions(+), 2 deletions(-)
 create mode 100644 dts/framework/dts.py
  

Patch

diff --git a/dts/framework/dts.py b/dts/framework/dts.py
new file mode 100644
index 0000000000..52ec0638df
--- /dev/null
+++ b/dts/framework/dts.py
@@ -0,0 +1,76 @@ 
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2010-2019 Intel Corporation
+# Copyright(c) 2022 PANTHEON.tech s.r.o.
+# Copyright(c) 2022 University of New Hampshire
+#
+
+import sys
+from typing import Iterable, Optional
+
+import framework.logger as logger
+
+from .config import Configuration, load_config
+from .logger import getLogger
+from .node import Node
+from .utils import check_dts_python_version, create_parallel_locks
+
+log_handler: Optional[logger.DTSLOG] = None
+
+
+def dts_nodes_exit(nodes: Iterable[Node]) -> None:
+    """
+    Call SUT and TG exit function after execution finished
+    """
+    for node in nodes:
+        node.node_exit()
+
+
+def run_all(
+    config_file,
+    verbose,
+) -> None:
+    """
+    Main process of DTS, it will run all test suites in the config file.
+    """
+
+    global log_handler
+
+    # check the python version of the server that run dts
+    check_dts_python_version()
+
+    # init log_handler handler
+    if verbose is True:
+        logger.set_verbose()
+
+    log_handler = getLogger("dts")
+
+    # parse input config file
+    config: Configuration = load_config(config_file)
+
+    # init global lock
+    create_parallel_locks(len(config.nodes))
+
+    nodes = []
+    try:
+        nodes = [
+            Node(node_config, sut_id=i)
+            for i, node_config in enumerate(config.nodes)
+        ]
+        dts_nodes_exit(nodes)
+    finally:
+        quit_execution(nodes)
+
+
+def quit_execution(nodes: Iterable[Node]) -> None:
+    """
+    Close session to SUT and TG before quit.
+    Return exit status when failure occurred.
+    """
+    for node in nodes:
+        # close all session
+        node.node_exit()
+
+    if log_handler is not None:
+        log_handler.info("DTS ended")
+
+    sys.exit(0)
diff --git a/dts/framework/utils.py b/dts/framework/utils.py
index a637c4641e..1f7f28d0c5 100644
--- a/dts/framework/utils.py
+++ b/dts/framework/utils.py
@@ -4,15 +4,29 @@ 
 # Copyright(c) 2022 University of New Hampshire
 #
 
+import sys
 import threading
 from functools import wraps
 from typing import Any, Callable, TypeVar
 
 locks_info: list[dict[str, Any]] = list()
 
-T = TypeVar("T")
+
+def create_parallel_locks(num_suts: int) -> None:
+    """
+    Create thread lock dictionary based on SUTs number
+    """
+    global locks_info
+
+    locks_info = list()
+    for _ in range(num_suts):
+        lock_info = dict()
+        lock_info["update_lock"] = threading.RLock()
+        locks_info.append(lock_info)
 
 
+T = TypeVar("T")
+
 def parallel_lock(num: int = 1) -> Callable[[Callable[..., T]], Callable[..., T]]:
     """
     Wrapper function for protect parallel threads, allow multiple threads
@@ -21,7 +35,6 @@  def parallel_lock(num: int = 1) -> Callable[[Callable[..., T]], Callable[..., T]
     Parameter:
         num: Number of parallel threads for the lock
     """
-    global locks_info
 
     def decorate(func: Callable[..., T]) -> Callable[..., T]:
         # mypy does not know how to handle the types of this function, so Any is required
@@ -97,3 +110,21 @@  def RED(text: str) -> str:
 
 def GREEN(text: str) -> str:
     return f"\u001B[32;1m{str(text)}\u001B[0m"
+
+
+def check_dts_python_version() -> None:
+    if (
+        sys.version_info.major < 3
+        or (sys.version_info.major == 3 and sys.version_info.minor < 10)
+    ):
+        print(
+            RED(
+                (
+                    "WARNING: Dts running node python version is lower than python 3.10, "
+                    "it is deprecated for use in DTS, "
+                    "and will not work in future releases."
+                )
+            ),
+            file=sys.stderr,
+        )
+        print(RED("Please use Python >= 3.10 instead"), file=sys.stderr)
\ No newline at end of file