+ def __exit__(self, exc_type, exc_val, exc_tb):
+ """No-op, the client instance remains in cache in connected state."""
+
+ @classmethod
+ def disconnect_by_key(cls, key):
+ """Disconnect a connected client instance, noop it not connected.
+
+ Also remove the local sockets by deleting the temporary directory.
+ Put disconnected client instances to the reuse list.
+ The added attributes are not cleaned up,
+ as their values will get overwritten on next connect.
+
+ This method is useful for disconnect_all type of work.
+
+ :param key: Tuple identifying the node (and socket).
+ :type key: tuple of str
+ """
+ client_instance = cls.conn_cache.get(key, None)
+ if client_instance is None:
+ return
+ logger.debug(f"Disconnecting by key: {key}")
+ client_instance.disconnect()
+ run(
+ [
+ "ssh",
+ "-S",
+ client_instance.csit_control_socket,
+ "-O",
+ "exit",
+ "0.0.0.0",
+ ],
+ check=False,
+ )
+ # Temp dir has autoclean, but deleting explicitly
+ # as an error can happen.
+ try:
+ client_instance.csit_temp_dir.cleanup()
+ except FileNotFoundError:
+ # There is a race condition with ssh removing its ssh.sock file.
+ # Single retry should be enough to ensure the complete removal.
+ shutil.rmtree(client_instance.csit_temp_dir.name)
+ # Finally, put disconnected clients to reuse list.
+ cls.reusable_vpp_client_list.append(client_instance)
+ # Invalidate cache last. Repeated errors are better than silent leaks.
+ del cls.conn_cache[key]
+
+ @classmethod
+ def disconnect_by_node_and_socket(
+ cls, node, remote_socket=Constants.SOCKSVR_PATH
+ ):
+ """Disconnect a connected client instance, noop it not connected.
+
+ Also remove the local sockets by deleting the temporary directory.
+ Put disconnected client instances to the reuse list.
+ The added attributes are not cleaned up,
+ as their values will get overwritten on next connect.
+
+ Call this method just before killing/restarting remote VPP instance.
+ """
+ key = cls.key_for_node_and_socket(node, remote_socket)
+ return cls.disconnect_by_key(key)
+
+ @classmethod
+ def disconnect_all_sockets_by_node(cls, node):
+ """Disconnect all socket connected client instance.
+
+ Noop if not connected.
+
+ Also remove the local sockets by deleting the temporary directory.
+ Put disconnected client instances to the reuse list.
+ The added attributes are not cleaned up,
+ as their values will get overwritten on next connect.
+
+ Call this method just before killing/restarting remote VPP instance.
+ """
+ sockets = Topology.get_node_sockets(node, socket_type=SocketType.PAPI)
+ if sockets:
+ for socket in sockets.values():
+ # TODO: Remove sockets from topology.
+ PapiSocketExecutor.disconnect_by_node_and_socket(node, socket)
+ # Always attempt to disconnect the default socket.
+ return cls.disconnect_by_node_and_socket(node)
+
+ @staticmethod
+ def disconnect_all_papi_connections():
+ """Disconnect all connected client instances, tear down the SSH tunnels.
+
+ Also remove the local sockets by deleting the temporary directory.
+ Put disconnected client instances to the reuse list.
+ The added attributes are not cleaned up,
+ as their values will get overwritten on next connect.
+
+ This should be a class method,
+ but we prefer to call static methods from Robot.
+
+ Call this method just before killing/restarting all VPP instances.
+ """
+ cls = PapiSocketExecutor
+ # Iterate over copy of entries so deletions do not mess with iterator.
+ keys_copy = list(cls.conn_cache.keys())
+ for key in keys_copy:
+ cls.disconnect_by_key(key)
+
+ def add(self, csit_papi_command, history=True, **kwargs):
+ """Add next command to internal command list; return self.
+
+ Unless disabled, new entry to papi history is also added at this point.
+ The kwargs dict is serialized or deep-copied, so it is safe to use
+ the original with partial modifications for subsequent calls.
+
+ Any pending conflicts from .api.json processing are raised.
+ Then the command name is checked for known CRCs.
+ Unsupported commands raise an exception, as CSIT change
+ should not start using messages without making sure which CRCs
+ are supported.
+ Each CRC issue is raised only once, so subsequent tests
+ can raise other issues.
+
+ With async handling mode, this method also serializes and sends
+ the command, skips CRC check to gain speed, and saves memory
+ by putting a sentinel (instead of deepcopy) to api command list.
+
+ For scale tests, the call sites are responsible to set history values
+ in a way that hints what is done without overwhelming the papi history.
+
+ Note to contributors: Do not rename "csit_papi_command"
+ to anything VPP could possibly use as an API field name.
+
+ :param csit_papi_command: VPP API command.
+ :param history: Enable/disable adding command to PAPI command history.
+ :param kwargs: Optional key-value arguments.
+ :type csit_papi_command: str
+ :type history: bool
+ :type kwargs: dict
+ :returns: self, so that method chaining is possible.
+ :rtype: PapiSocketExecutor
+ :raises RuntimeError: If unverified or conflicting CRC is encountered.
+ """
+ self.crc_checker.report_initial_conflicts()
+ if history:
+ # No need for deepcopy yet, serialization isolates from edits.
+ PapiHistory.add_to_papi_history(
+ self._node, csit_papi_command, **kwargs
+ )
+ self.crc_checker.check_api_name(csit_papi_command)
+ if self._is_async:
+ # Save memory but still count the number of expected replies.
+ self._api_command_list.append(0)
+ api_object = self.get_connected_client(check_connected=False).api
+ func = getattr(api_object, csit_papi_command)
+ # No need for deepcopy yet, serialization isolates from edits.
+ func(**kwargs)
+ else:
+ # No serialization, so deepcopy is needed here.
+ self._api_command_list.append(
+ dict(api_name=csit_papi_command, api_args=copy.deepcopy(kwargs))
+ )
+ return self
+
+ def get_replies(self, err_msg="Failed to get replies."):
+ """Get reply for each command from VPP Python API.
+
+ This method expects one reply per command,
+ and gains performance by reading replies only after
+ sending all commands.
+
+ The replies are parsed into dict-like objects,
+ "retval" field (if present) is guaranteed to be zero on success.
+
+ Do not use this for messages with variable number of replies,
+ use get_details instead.
+ Do not use for commands trigering VPP-2033,
+ use series of get_reply instead.
+
+ :param err_msg: The message used if the PAPI command(s) execution fails.
+ :type err_msg: str
+ :returns: Responses, dict objects with fields due to API and "retval".
+ :rtype: list of dict
+ :raises RuntimeError: If retval is nonzero, parsing or ssh error.
+ """
+ if not self._is_async:
+ raise RuntimeError("Sync handling does not suport get_replies.")
+ return self._execute(err_msg=err_msg, do_async=True)
+
+ def get_reply(self, err_msg="Failed to get reply."):
+ """Get reply to single command from VPP Python API.
+
+ This method waits for a single reply (no control ping),
+ thus avoiding bugs like VPP-2033.
+
+ The reply is parsed into a dict-like object,
+ "retval" field (if present) is guaranteed to be zero on success.
+
+ :param err_msg: The message used if the PAPI command(s) execution fails.
+ :type err_msg: str
+ :returns: Response, dict object with fields due to API and "retval".
+ :rtype: dict
+ :raises AssertionError: If retval is nonzero, parsing or ssh error.
+ """
+ if self._is_async:
+ raise RuntimeError("Async handling does not suport get_reply.")
+ replies = self._execute(err_msg=err_msg, do_async=False)
+ if len(replies) != 1:
+ raise RuntimeError(f"Expected single reply, got {replies!r}")
+ return replies[0]
+
+ def get_sw_if_index(self, err_msg="Failed to get reply."):
+ """Get sw_if_index from reply from VPP Python API.
+
+ Frequently, the caller is only interested in sw_if_index field
+ of the reply, this wrapper around get_reply (thus safe against VPP-2033)
+ makes such call sites shorter.
+
+ :param err_msg: The message used if the PAPI command(s) execution fails.
+ :type err_msg: str
+ :returns: Response, sw_if_index value of the reply.
+ :rtype: int
+ :raises AssertionError: If retval is nonzero, parsing or ssh error.