import binascii
import json
+from pprint import pformat
+
from robot.api import logger
from resources.libraries.python.Constants import Constants
code.
"""
- def __init__(self, papi_reply=None, stdout="", stderr="", ret_code=None,
- requests=None):
+ def __init__(self, papi_reply=None, stdout="", stderr="", requests=None):
"""Construct the Papi response by setting the values needed.
TODO:
:param papi_reply: API reply from last executed PAPI command(s).
:param stdout: stdout from last executed PAPI command(s).
:param stderr: stderr from last executed PAPI command(s).
- :param ret_code: ret_code from last executed PAPI command(s).
:param requests: List of used PAPI requests. It is used while verifying
replies. If None, expected replies must be provided for verify_reply
and verify_replies methods.
:type papi_reply: list or None
:type stdout: str
:type stderr: str
- :type ret_code: int
:type requests: list
"""
# stderr from last executed PAPI command(s).
self.stderr = stderr
- # return code from last executed PAPI command(s).
- self.ret_code = ret_code
-
# List of used PAPI requests.
self.requests = requests
:returns: Readable description.
:rtype: str
"""
- return ("papi_reply={papi_reply},"
- "stdout={stdout},"
- "stderr={stderr},"
- "ret_code={ret_code},"
- "requests={requests}".
- format(papi_reply=self.reply,
- stdout=self.stdout,
- stderr=self.stderr,
- ret_code=self.ret_code,
- requests=self.requests))
+ return (
+ "papi_reply={papi_reply},stdout={stdout},stderr={stderr},"
+ "requests={requests}").format(
+ papi_reply=self.reply, stdout=self.stdout, stderr=self.stderr,
+ requests=self.requests)
def __repr__(self):
"""Return string executable as Python constructor call.
paths = [cmd['api_args']['path'] for cmd in self._api_command_list]
self._api_command_list = list()
- ret_code, stdout, _ = self._execute_papi(paths,
- method='stats',
- err_msg=err_msg,
- timeout=timeout)
+ stdout, _ = self._execute_papi(
+ paths, method='stats', err_msg=err_msg, timeout=timeout)
return json.loads(stdout)
return code.
:rtype: PapiResponse
"""
- response = self._execute(method='request',
- process_reply=process_reply,
- ignore_errors=ignore_errors,
- err_msg=err_msg,
- timeout=timeout)
- return response
+ return self._execute(
+ method='request', process_reply=process_reply,
+ ignore_errors=ignore_errors, err_msg=err_msg, timeout=timeout)
def get_dump(self, err_msg="Failed to get dump.",
process_reply=True, ignore_errors=False, timeout=120):
return code.
:rtype: PapiResponse
"""
+ return self._execute(
+ method='dump', process_reply=process_reply,
+ ignore_errors=ignore_errors, err_msg=err_msg, timeout=timeout)
+
+ @staticmethod
+ def dump_and_log(node, cmds):
+ """Dump and log requested information.
+
+ :param node: DUT node.
+ :param cmds: Dump commands to be executed.
+ :type node: dict
+ :type cmds: list
+ """
+ with PapiExecutor(node) as papi_exec:
+ for cmd in cmds:
+ dump = papi_exec.add(cmd).get_dump()
+ logger.debug("{cmd}:\n{data}".format(
+ cmd=cmd, data=pformat(dump.reply[0]["api_reply"])))
+
+ @staticmethod
+ def run_cli_cmd(node, cmd, log=True):
+ """Run a CLI command.
+
+ :param node: Node to run command on.
+ :param cmd: The CLI command to be run on the node.
+ :param log: If True, the response is logged.
+ :type node: dict
+ :type cmd: str
+ :type log: bool
+ :returns: Verified data from PAPI response.
+ :rtype: dict
+ """
+
+ cli = 'cli_inband'
+ args = dict(cmd=cmd)
+ err_msg = "Failed to run 'cli_inband {cmd}' PAPI command on host " \
+ "{host}".format(host=node['host'], cmd=cmd)
+
+ with PapiExecutor(node) as papi_exec:
+ data = papi_exec.add(cli, **args).get_replies(err_msg). \
+ verify_reply(err_msg=err_msg)
- response = self._execute(method='dump',
- process_reply=process_reply,
- ignore_errors=ignore_errors,
- err_msg=err_msg,
- timeout=timeout)
- return response
+ if log:
+ logger.info("{cmd}:\n{data}".format(cmd=cmd, data=data["reply"]))
+
+ return data
def execute_should_pass(self, err_msg="Failed to execute PAPI command.",
process_reply=True, ignore_errors=False,
:rtype: PapiResponse
:raises AssertionError: If PAPI command(s) execution failed.
"""
-
- response = self.get_replies(process_reply=process_reply,
- ignore_errors=ignore_errors,
- err_msg=err_msg,
- timeout=timeout)
- return response
+ # TODO: Migrate callers to get_replies and delete this method.
+ return self.get_replies(
+ process_reply=process_reply, ignore_errors=ignore_errors,
+ err_msg=err_msg, timeout=timeout)
@staticmethod
def _process_api_data(api_d):
:rtype: list
"""
+ def process_value(val):
+ """Process value.
+
+ :param val: Value to be processed.
+ :type val: object
+ :returns: Processed value.
+ :rtype: dict or str or int
+ """
+ if isinstance(val, dict):
+ val_dict = dict()
+ for val_k, val_v in val.iteritems():
+ val_dict[str(val_k)] = process_value(val_v)
+ return val_dict
+ else:
+ return binascii.hexlify(val) if isinstance(val, str) else val
+
api_data_processed = list()
for api in api_d:
api_args_processed = dict()
for a_k, a_v in api["api_args"].iteritems():
- value = binascii.hexlify(a_v) if isinstance(a_v, str) else a_v
- api_args_processed[str(a_k)] = value
+ api_args_processed[str(a_k)] = process_value(a_v)
api_data_processed.append(dict(api_name=api["api_name"],
api_args=api_args_processed))
return api_data_processed
:returns: Processed API reply / a part of API reply.
:rtype: dict
"""
-
reply_dict = dict()
reply_value = dict()
for reply_key, reply_v in api_r.iteritems():
:returns: Processed API reply.
:rtype: list or dict
"""
-
if isinstance(api_reply, list):
reverted_reply = [self._revert_api_reply(a_r) for a_r in api_reply]
else:
:type method: str
:type err_msg: str
:type timeout: int
+ :returns: Stdout and stderr.
+ :rtype: 2-tuple of str
:raises SSHTimeout: If PAPI command(s) execution has timed out.
:raises RuntimeError: If PAPI executor failed due to another reason.
:raises AssertionError: If PAPI command(s) execution has failed.
json=json_data)
try:
ret_code, stdout, stderr = self._ssh.exec_command_sudo(
- cmd=cmd, timeout=timeout)
+ cmd=cmd, timeout=timeout, log_stdout_err=False)
except SSHTimeout:
logger.error("PAPI command(s) execution timeout on host {host}:"
"\n{apis}".format(host=self._node["host"],
if ret_code != 0:
raise AssertionError(err_msg)
- return ret_code, stdout, stderr
+ return stdout, stderr
def _execute(self, method='request', process_reply=True,
ignore_errors=False, err_msg="", timeout=120):
# Clear first as execution may fail.
self._api_command_list = list()
- ret_code, stdout, stderr = self._execute_papi(local_list,
- method=method,
- err_msg=err_msg,
- timeout=timeout)
+ stdout, stderr = self._execute_papi(
+ local_list, method=method, err_msg=err_msg, timeout=timeout)
papi_reply = list()
if process_reply:
try:
# Log processed papi reply to be able to check API replies changes
logger.debug("Processed PAPI reply: {reply}".format(reply=papi_reply))
- return PapiResponse(papi_reply=papi_reply,
- stdout=stdout,
- stderr=stderr,
- ret_code=ret_code,
- requests=[rqst["api_name"] for rqst in local_list])
+ return PapiResponse(
+ papi_reply=papi_reply, stdout=stdout, stderr=stderr,
+ requests=[rqst["api_name"] for rqst in local_list])