PAPI: Reduce the amount of logged information
[csit.git] / resources / libraries / python / PapiExecutor.py
index f6df200..e296e23 100644 (file)
@@ -32,8 +32,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 +42,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 +60,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 +74,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 +320,8 @@ 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)
 
@@ -354,12 +341,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 +361,9 @@ class PapiExecutor(object):
             return code.
         :rtype: PapiResponse
         """
-
-        response = self._execute(method='dump',
-                                 process_reply=process_reply,
-                                 ignore_errors=ignore_errors,
-                                 err_msg=err_msg,
-                                 timeout=timeout)
-        return response
+        return self._execute(
+            method='dump', process_reply=process_reply,
+            ignore_errors=ignore_errors, err_msg=err_msg, timeout=timeout)
 
     def execute_should_pass(self, err_msg="Failed to execute PAPI command.",
                             process_reply=True, ignore_errors=False,
@@ -410,12 +390,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 +407,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,12 +445,12 @@ 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():
             for a_k, a_v in reply_v.iteritems():
-                reply_value[a_k] = a_v
+                reply_value[a_k] = binascii.unhexlify(a_v) \
+                    if isinstance(a_v, unicode) else a_v
             reply_dict[reply_key] = reply_value
         return reply_dict
 
@@ -469,7 +462,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:
@@ -489,6 +481,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.
@@ -507,7 +501,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"],
@@ -520,7 +514,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):
@@ -557,10 +551,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:
@@ -581,8 +573,9 @@ class PapiExecutor(object):
                         raise
                 papi_reply.append(api_reply_processed)
 
-        return PapiResponse(papi_reply=papi_reply,
-                            stdout=stdout,
-                            stderr=stderr,
-                            ret_code=ret_code,
-                            requests=[rqst["api_name"] for rqst in local_list])
+        # 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,
+            requests=[rqst["api_name"] for rqst in local_list])