From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id ECE0C41D52; Thu, 23 Feb 2023 16:29:04 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8FE3942D55; Thu, 23 Feb 2023 16:28:51 +0100 (CET) Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) by mails.dpdk.org (Postfix) with ESMTP id 4177342B7E for ; Thu, 23 Feb 2023 16:28:47 +0100 (CET) Received: by mail-ed1-f53.google.com with SMTP id cq23so43170966edb.1 for ; Thu, 23 Feb 2023 07:28:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pantheon-tech.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cITyQzQ1yMMw0k6eIsv77ruoFg0csPndwFcLwVrtIFk=; b=7iJ0pj0bK2zhE6KMpZn5cjySu0gffGX+R+XQyFpj8HVctYYaikmh45rt9FQGkfUBAT 97LzF1oPv/D42xqgEcZkdv3yrpJ3AnbY8Ti8GPP0aJacMj6D6nbtdCSVwwn+oesATXkd 8A8yd4ZPDwiPke+uOIPyB6YcmBRS25urqCNyc92RLUIxhO3NHe++KxhariWJIYg0Zk/j HaSPxCm8CqX4Gju/WO8F0eeJ5tDvWihbg8Bbf8n/qSfXT1NLrT+q2WWnnTfn0YHgc4Uk NhwUUIjBa7Gq3fI6QjRCYDA+aYRTs1+qD58GAx/MOr1SPKmoN+O7bG9u1vvtBnKnyYYT yu9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cITyQzQ1yMMw0k6eIsv77ruoFg0csPndwFcLwVrtIFk=; b=Cyfv5ISSt5v3P6y9o9Fh+BYUPNYR3bZ9HkaAQ5Y3GfLeyWcYH5O8n94m362DZlOG1V W1CcSHjMmDJmpPuqoQZwNQMFnmdpshdJME8U+M5pS1cFVuVieX9LRlo5QB3FQX4aVS7i +YSHr8eV9JTlgroH+axAqRLtwbGiunWvza+GkAoeGdBU3j1HiO7qykkJdBBzczhIoQsG wSgnvfnm0TQYYPXl3GwqVVF/9e0BgP24RubpWCvnm73d2e+Zb+oudJ5rGMm0/X+j2F+d CVUX3JMUvm5usM4OoNj4V1DT9NUARc2rHAiryP+VEi6YgyILSqp2G1zyKBk+gRt6V5pp Svgg== X-Gm-Message-State: AO0yUKXjFuQBJeLQiPjzbfzCWjd7kVlkMoYBK6v5ohrrfFg/wl3L3wlE TPggzbwV4Klol87FuUpNxvHLkA== X-Google-Smtp-Source: AK7set+MXbSkMZHmJuj9VTWl8dDc09s93KExOWxh36e3bglTyUZQd6XqgPl4Q2sjfBs2ZjjAtDAyJg== X-Received: by 2002:a50:fc05:0:b0:4af:59c0:5a30 with SMTP id i5-20020a50fc05000000b004af59c05a30mr8059674edr.38.1677166127004; Thu, 23 Feb 2023 07:28:47 -0800 (PST) Received: from localhost.localdomain ([84.245.121.112]) by smtp.gmail.com with ESMTPSA id r6-20020a50c006000000b004af6a8617ffsm1158892edb.46.2023.02.23.07.28.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 07:28:46 -0800 (PST) From: =?UTF-8?q?Juraj=20Linke=C5=A1?= To: thomas@monjalon.net, Honnappa.Nagarahalli@arm.com, lijuan.tu@intel.com, bruce.richardson@intel.com, probb@iol.unh.edu Cc: dev@dpdk.org, =?UTF-8?q?Juraj=20Linke=C5=A1?= Subject: [PATCH v5 02/10] dts: add ssh command verification Date: Thu, 23 Feb 2023 16:28:32 +0100 Message-Id: <20230223152840.634183-3-juraj.linkes@pantheon.tech> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230223152840.634183-1-juraj.linkes@pantheon.tech> References: <20230213152846.284191-1-juraj.linkes@pantheon.tech> <20230223152840.634183-1-juraj.linkes@pantheon.tech> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This is a basic capability needed to check whether the command execution was successful or not. If not, raise a RemoteCommandExecutionError. When a failure is expected, the caller is supposed to catch the exception. Signed-off-by: Juraj Linkeš --- dts/framework/exception.py | 23 +++++++- .../remote_session/remote/remote_session.py | 55 +++++++++++++------ .../remote_session/remote/ssh_session.py | 12 +++- 3 files changed, 69 insertions(+), 21 deletions(-) diff --git a/dts/framework/exception.py b/dts/framework/exception.py index 121a0f7296..e776b42bd9 100644 --- a/dts/framework/exception.py +++ b/dts/framework/exception.py @@ -21,7 +21,8 @@ class ErrorSeverity(IntEnum): NO_ERR = 0 GENERIC_ERR = 1 CONFIG_ERR = 2 - SSH_ERR = 3 + REMOTE_CMD_EXEC_ERR = 3 + SSH_ERR = 4 class DTSError(Exception): @@ -90,3 +91,23 @@ class ConfigurationError(DTSError): """ severity: ClassVar[ErrorSeverity] = ErrorSeverity.CONFIG_ERR + + +class RemoteCommandExecutionError(DTSError): + """ + Raised when a command executed on a Node returns a non-zero exit status. + """ + + command: str + command_return_code: int + severity: ClassVar[ErrorSeverity] = ErrorSeverity.REMOTE_CMD_EXEC_ERR + + def __init__(self, command: str, command_return_code: int): + self.command = command + self.command_return_code = command_return_code + + def __str__(self) -> str: + return ( + f"Command {self.command} returned a non-zero exit code: " + f"{self.command_return_code}" + ) diff --git a/dts/framework/remote_session/remote/remote_session.py b/dts/framework/remote_session/remote/remote_session.py index 7c7b30225f..5ac395ec79 100644 --- a/dts/framework/remote_session/remote/remote_session.py +++ b/dts/framework/remote_session/remote/remote_session.py @@ -7,15 +7,29 @@ from abc import ABC, abstractmethod from framework.config import NodeConfiguration +from framework.exception import RemoteCommandExecutionError from framework.logger import DTSLOG from framework.settings import SETTINGS @dataclasses.dataclass(slots=True, frozen=True) -class HistoryRecord: +class CommandResult: + """ + The result of remote execution of a command. + """ + name: str command: str - output: str | int + stdout: str + stderr: str + return_code: int + + def __str__(self) -> str: + return ( + f"stdout: '{self.stdout}'\n" + f"stderr: '{self.stderr}'\n" + f"return_code: '{self.return_code}'" + ) class RemoteSession(ABC): @@ -34,7 +48,7 @@ class RemoteSession(ABC): port: int | None username: str password: str - history: list[HistoryRecord] + history: list[CommandResult] _logger: DTSLOG _node_config: NodeConfiguration @@ -68,28 +82,33 @@ def _connect(self) -> None: Create connection to assigned node. """ - def send_command(self, command: str, timeout: float = SETTINGS.timeout) -> str: + def send_command( + self, command: str, timeout: float = SETTINGS.timeout, verify: bool = False + ) -> CommandResult: """ - Send a command and return the output. + Send a command to the connected node and return CommandResult. + If verify is True, check the return code of the executed command + and raise a RemoteCommandExecutionError if the command failed. """ - self._logger.info(f"Sending: {command}") - out = self._send_command(command, timeout) - self._logger.debug(f"Received from {command}: {out}") - self._history_add(command=command, output=out) - return out + self._logger.info(f"Sending: '{command}'") + result = self._send_command(command, timeout) + if verify and result.return_code: + self._logger.debug( + f"Command '{command}' failed with return code '{result.return_code}'" + ) + self._logger.debug(f"stdout: '{result.stdout}'") + self._logger.debug(f"stderr: '{result.stderr}'") + raise RemoteCommandExecutionError(command, result.return_code) + self._logger.debug(f"Received from '{command}':\n{result}") + self.history.append(result) + return result @abstractmethod - def _send_command(self, command: str, timeout: float) -> str: + def _send_command(self, command: str, timeout: float) -> CommandResult: """ - Use the underlying protocol to execute the command and return the output - of the command. + Use the underlying protocol to execute the command and return CommandResult. """ - def _history_add(self, command: str, output: str) -> None: - self.history.append( - HistoryRecord(name=self.name, command=command, output=output) - ) - def close(self, force: bool = False) -> None: """ Close the remote session and free all used resources. diff --git a/dts/framework/remote_session/remote/ssh_session.py b/dts/framework/remote_session/remote/ssh_session.py index 96175f5284..c2362e2fdf 100644 --- a/dts/framework/remote_session/remote/ssh_session.py +++ b/dts/framework/remote_session/remote/ssh_session.py @@ -12,7 +12,7 @@ from framework.logger import DTSLOG from framework.utils import GREEN, RED -from .remote_session import RemoteSession +from .remote_session import CommandResult, RemoteSession class SSHSession(RemoteSession): @@ -66,6 +66,7 @@ def _connect(self) -> None: self.send_expect("stty -echo", "#") self.send_expect("stty columns 1000", "#") + self.send_expect("bind 'set enable-bracketed-paste off'", "#") except Exception as e: self._logger.error(RED(str(e))) if getattr(self, "port", None): @@ -163,7 +164,14 @@ def _flush(self) -> None: def is_alive(self) -> bool: return self.session.isalive() - def _send_command(self, command: str, timeout: float) -> str: + def _send_command(self, command: str, timeout: float) -> CommandResult: + output = self._send_command_get_output(command, timeout) + return_code = int(self._send_command_get_output("echo $?", timeout)) + + # we're capturing only stdout + return CommandResult(self.name, command, output, "", return_code) + + def _send_command_get_output(self, command: str, timeout: float) -> str: try: self._clean_session() self._send_line(command) -- 2.30.2