X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FPapiExecutor.py;h=5e35422a1bfe9ff22eb28cceaf7f79690fbbb772;hp=a0261992e9dc8875466fbf2ccefabcf3b9162e77;hb=3c863def2096b573832499985e3a12bbccf82ea8;hpb=81853d468a1ff40b0e03343b73412aff96a46dd0 diff --git a/resources/libraries/python/PapiExecutor.py b/resources/libraries/python/PapiExecutor.py index a0261992e9..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 @@ -32,8 +34,7 @@ class PapiResponse(object): 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: @@ -43,14 +44,12 @@ class PapiResponse(object): :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 """ @@ -63,9 +62,6 @@ class PapiResponse(object): # 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 @@ -80,16 +76,11 @@ class PapiResponse(object): :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. @@ -331,10 +322,27 @@ class PapiExecutor(object): 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) + + 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) @@ -354,12 +362,9 @@ class PapiExecutor(object): 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): @@ -377,13 +382,52 @@ class PapiExecutor(object): 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 + """ - response = self._execute(method='dump', - process_reply=process_reply, - ignore_errors=ignore_errors, - err_msg=err_msg, - timeout=timeout) - return response + 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, @@ -410,12 +454,10 @@ class PapiExecutor(object): :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): @@ -429,12 +471,27 @@ class PapiExecutor(object): :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 @@ -452,7 +509,6 @@ class PapiExecutor(object): :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(): @@ -470,7 +526,6 @@ class PapiExecutor(object): :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: @@ -490,6 +545,8 @@ class PapiExecutor(object): :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. @@ -498,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}'".\ @@ -508,7 +566,7 @@ class PapiExecutor(object): 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"], @@ -521,7 +579,7 @@ class PapiExecutor(object): 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): @@ -558,10 +616,8 @@ class PapiExecutor(object): # 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: @@ -585,8 +641,6 @@ class PapiExecutor(object): # 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])