[v2,6/8] dts: add Node base class

Message ID 20220711145126.295427-7-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 base class implements basic node management methods - connect and
execute commands.

Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
---
 dts/framework/node.py     | 108 ++++++++++++++++++++++++++++++++++++++
 dts/framework/settings.py |   3 ++
 2 files changed, 111 insertions(+)
 create mode 100644 dts/framework/node.py
  

Patch

diff --git a/dts/framework/node.py b/dts/framework/node.py
new file mode 100644
index 0000000000..09f567f32f
--- /dev/null
+++ b/dts/framework/node.py
@@ -0,0 +1,108 @@ 
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2010-2014 Intel Corporation
+# Copyright(c) 2022 PANTHEON.tech s.r.o.
+# Copyright(c) 2022 University of New Hampshire
+#
+
+from typing import Optional
+
+from .config import NodeConfiguration
+from .logger import DTSLOG, getLogger
+from .settings import TIMEOUT
+from .ssh_connection import SSHConnection
+
+"""
+A node is a generic host that DTS connects to and manages.
+"""
+
+
+class Node(object):
+    """
+    Basic module for node management. This module implements methods that
+    manage a node, such as information gathering (of CPU/PCI/NIC) and
+    environment setup.
+    """
+
+    _config: NodeConfiguration
+    name: str
+    sut_id: int
+    logger: DTSLOG
+    session: SSHConnection
+
+    def __init__(self, node_config: NodeConfiguration, sut_id: int = 0):
+        self._config = node_config
+        self.name = node_config.name
+        self.sut_id = sut_id
+
+        self.logger = getLogger(self.name)
+        self.logger.info(f"Created node: {self.name}")
+        self.session = SSHConnection(
+            self.get_ip_address(),
+            self.name,
+            self.logger,
+            self.get_username(),
+            self.get_password(),
+            self.sut_id,
+        )
+
+    def get_ip_address(self) -> str:
+        """
+        Get SUT's ip address.
+        """
+        return self._config.hostname
+
+    def get_password(self) -> Optional[str]:
+        """
+        Get SUT's login password.
+        """
+        return self._config.password
+
+    def get_username(self) -> str:
+        """
+        Get SUT's login username.
+        """
+        return self._config.user
+
+    def send_expect(
+        self,
+        command: str,
+        expected: str,
+        timeout: int = TIMEOUT,
+        verify: bool = False,
+        trim_whitespace: bool = True,
+    ) -> str | int:
+        """
+        Send commands to node and return string before expected string. If
+        there's no expected string found before timeout, TimeoutException will
+        be raised.
+
+        By default, it will trim the whitespace from the expected string. This
+        behavior can be turned off via the trim_whitespace argument.
+        """
+
+        if trim_whitespace:
+            expected = expected.strip()
+
+        return self.session.send_expect(command, expected, timeout, verify)
+
+    def send_command(self, cmds: str, timeout: float = TIMEOUT) -> str:
+        """
+        Send commands to node and return string before timeout.
+        """
+
+        return self.session.send_command(cmds, timeout)
+
+    def close(self) -> None:
+        """
+        Close ssh session of SUT.
+        """
+        if self.session:
+            self.session.close()
+            self.session = None
+
+    def node_exit(self) -> None:
+        """
+        Recover all resource before node exit
+        """
+        self.close()
+        self.logger.logger_exit()
diff --git a/dts/framework/settings.py b/dts/framework/settings.py
index ca3cbd71b1..f5028e88c9 100644
--- a/dts/framework/settings.py
+++ b/dts/framework/settings.py
@@ -7,6 +7,9 @@ 
 import os
 import re
 
+# Default session timeout.
+TIMEOUT: int = 15
+
 DEFAULT_CONFIG_FILE_PATH: str = "./conf.yaml"
 
 # DTS global environment variables