remote_session - Remote Session ABC
Base remote session.
This module contains the abstract base class for remote sessions and defines the structure of the result of a command execution.
- class CommandResult
Bases:
object
The result of remote execution of a command.
- name
The name of the session that executed the command.
- Type:
str
- command
The executed command.
- Type:
str
- stdout
The standard output the command produced.
- Type:
str
- stderr
The standard error output the command produced.
- Type:
str
- return_code
The return code the command exited with.
- Type:
int
- __init__(name: str, command: str, init_stdout: dataclasses.InitVar[str], init_stderr: dataclasses.InitVar[str], return_code: int) None
- class RemoteSession
Bases:
ABC
Non-interactive remote session.
The abstract methods must be implemented in order to connect to a remote host (node) and maintain a remote session. The subclasses must use (or implement) some underlying transport protocol (e.g. SSH) to implement the methods. On top of that, it provides some basic services common to all subclasses, such as keeping history and logging what’s being executed on the remote node.
- name
The name of the session.
- Type:
str
- hostname
The node’s hostname. Could be an IP (possibly with port, separated by a colon) or a domain name.
- Type:
str
- ip
The IP address of the node or a domain name, whichever was used in hostname.
- Type:
str
- port
The port of the node, if given in hostname.
- Type:
int | None
- username
The username used in the connection.
- Type:
str
- password
The password used in the connection. Most frequently empty, as the use of passwords is discouraged.
- Type:
str
- history
The executed commands during this session.
- __init__(node_config: NodeConfiguration, session_name: str, logger: DTSLogger)
Connect to the node during initialization.
- Parameters:
node_config (NodeConfiguration) – The test run configuration of the node to connect to.
session_name (str) – The name of the session.
logger (DTSLogger) – The logger instance this session will use.
- Raises:
SSHConnectionError – If the connection to the node was not successful.
- send_command(command: str, timeout: float = 15, verify: bool = False, env: dict | None = None) CommandResult
Send command to the connected node.
The
--timeout
command line argument and theDTS_TIMEOUT
environment variable configure the timeout of command execution.- Parameters:
command (str) – The command to execute.
timeout (float) – Wait at most this long in seconds for command execution to complete.
verify (bool) – If
True
, will check the exit code of command.env (dict | None) – A dictionary with environment variables to be used with command execution.
- Raises:
SSHSessionDeadError – If the session isn’t alive when sending command.
SSHTimeoutError – If command execution timed out.
RemoteCommandExecutionError – If verify is
True
and command execution failed.
- Returns:
The output of the command along with the return code.
- Return type:
- abstract is_alive() bool
Check whether the remote session is still responding.
- abstract copy_from(source_file: str | pathlib.PurePath, destination_dir: str | pathlib.Path) None
Copy a file from the remote Node to the local filesystem.
Copy source_file from the remote Node associated with this remote session to destination_dir on the local filesystem.
- Parameters:
source_file (str | pathlib.PurePath) – The file on the remote Node.
destination_dir (str | pathlib.Path) – The directory path on the local filesystem where the source_file will be saved.
- abstract copy_to(source_file: str | pathlib.Path, destination_dir: str | pathlib.PurePath) None
Copy a file from local filesystem to the remote Node.
Copy source_file from local filesystem to destination_dir on the remote Node associated with this remote session.
- Parameters:
source_file (str | pathlib.Path) – The file on the local filesystem.
destination_dir (str | pathlib.PurePath) – The directory path on the remote Node where the source_file will be saved.
- abstract close() None
Close the remote session and free all used resources.