X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FPapiExecutor.py;h=5e35422a1bfe9ff22eb28cceaf7f79690fbbb772;hp=e296e23c1f88a399e120c5cac555fde91acd71db;hb=3c863def2096b573832499985e3a12bbccf82ea8;hpb=72ae46cda69c2f12efdbe731e37dce200a48f424 diff --git a/resources/libraries/python/PapiExecutor.py b/resources/libraries/python/PapiExecutor.py index e296e23c1f..5e35422a1b 100644 --- a/resources/libraries/python/PapiExecutor.py +++ b/resources/libraries/python/PapiExecutor.py @@ -17,6 +17,8 @@ import binascii import json +from pprint import pformat + from robot.api import logger from resources.libraries.python.Constants import Constants @@ -325,6 +327,25 @@ class PapiExecutor(object): return json.loads(stdout) + def get_stats_reply(self, err_msg="Failed to get statistics.", timeout=120): + """Get VPP Stats reply from VPP Python API. + + :param err_msg: The message used if the PAPI command(s) execution fails. + :param timeout: Timeout in seconds. + :type err_msg: str + :type timeout: int + :returns: Requested VPP statistics. + :rtype: list + """ + + args = self._api_command_list[0]['api_args'] + self._api_command_list = list() + + stdout, _ = self._execute_papi( + args, method='stats_request', err_msg=err_msg, timeout=timeout) + + return json.loads(stdout) + def get_replies(self, err_msg="Failed to get replies.", process_reply=True, ignore_errors=False, timeout=120): """Get reply/replies from VPP Python API. @@ -365,6 +386,49 @@ class PapiExecutor(object): 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) + + 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, timeout=120): @@ -491,7 +555,8 @@ class PapiExecutor(object): if not api_data: RuntimeError("No API data provided.") - json_data = json.dumps(api_data) if method == "stats" \ + json_data = json.dumps(api_data) \ + if method in ("stats", "stats_request") \ else json.dumps(self._process_api_data(api_data)) cmd = "{fw_dir}/{papi_provider} --method {method} --data '{json}'".\