-__all__ = ["PapiExecutor", "PapiResponse"]
-
-
-class PapiResponse(object):
- """Class for metadata specifying the Papi reply, stdout, stderr and return
- code.
- """
-
- def __init__(self, papi_reply=None, stdout="", stderr="", requests=None):
- """Construct the Papi response by setting the values needed.
-
- TODO:
- Implement 'dump' analogue of verify_replies that would concatenate
- the values, so that call sites do not have to do that themselves.
-
- :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 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 requests: list
- """
-
- # API reply from last executed PAPI command(s).
- self.reply = papi_reply
-
- # stdout from last executed PAPI command(s).
- self.stdout = stdout
-
- # stderr from last executed PAPI command(s).
- self.stderr = stderr
-
- # List of used PAPI requests.
- self.requests = requests
-
- # List of expected PAPI replies. It is used while verifying replies.
- if self.requests:
- self.expected_replies = \
- ["{rqst}_reply".format(rqst=rqst) for rqst in self.requests]
-
- def __str__(self):
- """Return string with human readable description of the PapiResponse.
-
- :returns: Readable description.
- :rtype: str
- """
- 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.
-
- :returns: Executable constructor call.
- :rtype: str
- """
- return "PapiResponse({str})".format(str=str(self))
-
- def verify_reply(self, cmd_reply=None, idx=0,
- err_msg="Failed to verify PAPI reply."):
- """Verify and return data from the PAPI response.
-
- Note: Use only with a simple request / reply command. In this case the
- PAPI reply includes 'retval' which is checked in this method.
-
- Do not use with 'dump' and 'vpp-stats' methods.
-
- Use if PAPI response includes only one command reply.
-
- Use it this way (preferred):
-
- with PapiExecutor(node) as papi_exec:
- data = papi_exec.add('show_version').get_replies().verify_reply()
-
- or if you must provide the expected reply (not recommended):
-
- with PapiExecutor(node) as papi_exec:
- data = papi_exec.add('show_version').get_replies().\
- verify_reply('show_version_reply')
-
- :param cmd_reply: PAPI reply. If None, list of 'requests' should have
- been provided to the __init__ method as pre-generated list of
- replies is used in this method in this case.
- The PapiExecutor._execute() method provides the requests
- automatically.
- :param idx: Index to PapiResponse.reply list.
- :param err_msg: The message used if the verification fails.
- :type cmd_reply: str
- :type idx: int
- :type err_msg: str or None
- :returns: Verified data from PAPI response.
- :rtype: dict
- :raises AssertionError: If the PAPI return value is not 0, so the reply
- is not valid.
- :raises KeyError, IndexError: If the reply does not have expected
- structure.
- """
- cmd_rpl = self.expected_replies[idx] if cmd_reply is None else cmd_reply
-
- data = self.reply[idx]['api_reply'][cmd_rpl]
- if data['retval'] != 0:
- raise AssertionError("{msg}\nidx={idx}, cmd_reply={reply}".
- format(msg=err_msg, idx=idx, reply=cmd_rpl))
-
- return data
-
- def verify_replies(self, cmd_replies=None,
- err_msg="Failed to verify PAPI reply."):
- """Verify and return data from the PAPI response.
-
- Note: Use only with request / reply commands. In this case each
- PAPI reply includes 'retval' which is checked.
-
- Do not use with 'dump' and 'vpp-stats' methods.
-
- Use if PAPI response includes more than one command reply.
-
- Use it this way:
-
- with PapiExecutor(node) as papi_exec:
- papi_exec.add(cmd1, **args1).add(cmd2, **args2).add(cmd2, **args3).\
- get_replies(err_msg).verify_replies()
-
- or if you need the data from the PAPI response:
-
- with PapiExecutor(node) as papi_exec:
- data = papi_exec.add(cmd1, **args1).add(cmd2, **args2).\
- add(cmd2, **args3).get_replies(err_msg).verify_replies()
-
- or if you must provide the list of expected replies (not recommended):
-
- with PapiExecutor(node) as papi_exec:
- data = papi_exec.add(cmd1, **args1).add(cmd2, **args2).\
- add(cmd2, **args3).get_replies(err_msg).\
- verify_replies(cmd_replies=cmd_replies)
-
- :param cmd_replies: List of PAPI command replies. If None, list of
- 'requests' should have been provided to the __init__ method as
- pre-generated list of replies is used in this method in this case.
- The PapiExecutor._execute() method provides the requests
- automatically.
- :param err_msg: The message used if the verification fails.
- :type cmd_replies: list of str or None
- :type err_msg: str
- :returns: List of verified data from PAPI response.
- :rtype list
- :raises AssertionError: If the PAPI response does not include at least
- one of specified command replies.
- """
- data = list()
-
- cmd_rpls = self.expected_replies if cmd_replies is None else cmd_replies
-
- if len(self.reply) != len(cmd_rpls):
- raise AssertionError(err_msg)
- for idx, cmd_reply in enumerate(cmd_rpls):
- data.append(self.verify_reply(cmd_reply, idx, err_msg))
-
- return data