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 B014641DC6; Fri, 3 Mar 2023 11:25:27 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 45A6240687; Fri, 3 Mar 2023 11:25:16 +0100 (CET) Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) by mails.dpdk.org (Postfix) with ESMTP id 9B18E400D6 for ; Fri, 3 Mar 2023 11:25:13 +0100 (CET) Received: by mail-ed1-f51.google.com with SMTP id i34so8286225eda.7 for ; Fri, 03 Mar 2023 02:25:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pantheon-tech.20210112.gappssmtp.com; s=20210112; t=1677839113; 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=jo50OZNIc9zbtDnSq17fZGa3hHN1p1QrO9doJGqXJVzrEEyosuYmAFacYWnyW+hyaB LHqUL/tBZz3ALwlxkofMjuV8+O6urrpXmQ309M1jf7JCTIjgMtWjtAq4XdOWSY9czs8P AJa/98I+e1UNOyTAfu7tkIro2Hx3ZtCfzsAxYWHI4EzNNxgsbBwGEezx2coeuRKGWojB 18HNOZY8ZFeMZc6az4iEmcZmR50hw85TR7gTRjGjkXjJCpbEhutn66dt4YyHpmBo0T9Z n03JCCkflQldW/w6iomi8cUAeQPNe8QUZvEhSHhvnZkWASwmvGPHHFFg7uHj4OW36RVj aWZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677839113; 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=BJmB1XDFIWSvk5QarVm4fkpAQhx8Ia+s3U6yr1MKPTFFOHgZogpSsXUuzwqIR1R5sD E3H24lQEr1u1iIOqlEvH/wRIRCEquy39BC4K9GqRZzzT4FnhIkJjnEov+1h9+NDVA6su 2633G7A9O2UOCLX8XQHII1RDXMDKd+H8TBXceDIr/v54rQEPvyZWk8OlGYVWpZ0qaEct riAM2cyZ6z3mcWXm9Z6F4TP9bUwXW5c0BJy1lLOI4KvwPEPOfkCJnz+SbnSsjxgT4vmz VrwFJNJv9CjwksGxTCUO/qFxYBAaQ4VvGAYPXW4CVjOD/vNUJJlcC1RgEmC1vMrCJRu+ bJVw== X-Gm-Message-State: AO0yUKWhRViwd0lTonjcmcRPNrOe6+dahHEP4lTPDT5DP+3INuOhYYbN hsvcbSuk0Tpcps0mJSvNO8D/cg== X-Google-Smtp-Source: AK7set+lbSms69x9/fw3+m2INXxqtIuDd3cuhclYwarjscDsMoNMYg8lG60I8vik7VUvJZbzYTcR1A== X-Received: by 2002:aa7:cf04:0:b0:4ac:bb85:c895 with SMTP id a4-20020aa7cf04000000b004acbb85c895mr1276315edy.1.1677839113269; Fri, 03 Mar 2023 02:25:13 -0800 (PST) Received: from localhost.localdomain (ip-46.34.234.35.o2inet.sk. [46.34.234.35]) by smtp.gmail.com with ESMTPSA id j19-20020a508a93000000b004c3e3a6136dsm984028edj.21.2023.03.03.02.25.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 02:25:13 -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 v6 02/10] dts: add ssh command verification Date: Fri, 3 Mar 2023 11:24:59 +0100 Message-Id: <20230303102507.527790-3-juraj.linkes@pantheon.tech> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230303102507.527790-1-juraj.linkes@pantheon.tech> References: <20230223152840.634183-1-juraj.linkes@pantheon.tech> <20230303102507.527790-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