Skip to contentSkip to conversation

oeqa/core/utils/concurrencytest.py: Handle exceptions and details

PATCH New Nathan Rossi 1 participant 2 messages Sep 27, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 oeqa/core/utils/concurrencytest.py: Handle exceptions and details

Handle the streaming of exception content with details data. The testtools package allows both 'err' and 'details' kwargs but can only pass one of them to the parent.

To handle the passing of exception traceback and details data at the same time, encode the traceback into the details object and remove the 'err' arg from the add* result call. This encodes the traceback similar to how 'err' is handled without any details object. Decoding is already done by testtools when the traceback is encoded in the details object.

---
 meta/lib/oeqa/core/utils/concurrencytest.py | 31 +++++++++++++++++------------
 1 file changed, 18 insertions(+), 13 deletions(-)
meta/lib/oeqa/core/utils/concurrencytest.py +18 -13
index 6293cf94ec..0f7b3dcc11 100644
@@ -78,29 +78,29 @@ class ProxyTestResult:
     def __init__(self, target):
         self.result = target
 
-    def _addResult(self, method, test, *args, **kwargs):
+    def _addResult(self, method, test, *args, exception = False, **kwargs):
         return method(test, *args, **kwargs)
 
-    def addError(self, test, *args, **kwargs):
-        self._addResult(self.result.addError, test, *args, **kwargs)
+    def addError(self, test, err = None, **kwargs):
+        self._addResult(self.result.addError, test, err, exception = True, **kwargs)
 
-    def addFailure(self, test, *args, **kwargs):
-        self._addResult(self.result.addFailure, test, *args, **kwargs)
+    def addFailure(self, test, err = None, **kwargs):
+        self._addResult(self.result.addFailure, test, err, exception = True, **kwargs)
 
-    def addSuccess(self, test, *args, **kwargs):
-        self._addResult(self.result.addSuccess, test, *args, **kwargs)
+    def addSuccess(self, test, **kwargs):
+        self._addResult(self.result.addSuccess, test, **kwargs)
 
-    def addExpectedFailure(self, test, *args, **kwargs):
-        self._addResult(self.result.addExpectedFailure, test, *args, **kwargs)
+    def addExpectedFailure(self, test, err = None, **kwargs):
+        self._addResult(self.result.addExpectedFailure, test, err, exception = True, **kwargs)
 
-    def addUnexpectedSuccess(self, test, *args, **kwargs):
-        self._addResult(self.result.addUnexpectedSuccess, test, *args, **kwargs)
+    def addUnexpectedSuccess(self, test, **kwargs):
+        self._addResult(self.result.addUnexpectedSuccess, test, **kwargs)
 
     def __getattr__(self, attr):
         return getattr(self.result, attr)
 
 class ExtraResultsDecoderTestResult(ProxyTestResult):
-    def _addResult(self, method, test, *args, **kwargs):
+    def _addResult(self, method, test, *args, exception = False, **kwargs):
         if "details" in kwargs and "extraresults" in kwargs["details"]:
             if isinstance(kwargs["details"]["extraresults"], Content):
                 kwargs = kwargs.copy()
@@ -114,7 +114,7 @@ class ExtraResultsDecoderTestResult(ProxyTestResult):
         return method(test, *args, **kwargs)
 
 class ExtraResultsEncoderTestResult(ProxyTestResult):
-    def _addResult(self, method, test, *args, **kwargs):
+    def _addResult(self, method, test, *args, exception = False, **kwargs):
         if hasattr(test, "extraresults"):
             extras = lambda : [json.dumps(test.extraresults).encode()]
             kwargs = kwargs.copy()
@@ -123,6 +123,11 @@ class ExtraResultsEncoderTestResult(ProxyTestResult):
             else:
                 kwargs["details"] = kwargs["details"].copy()
             kwargs["details"]["extraresults"] = Content(ContentType("application", "json", {'charset': 'utf8'}), extras)
+        # if using details, need to encode any exceptions into the details obj,
+        # testtools does not handle "err" and "details" together.
+        if "details" in kwargs and exception and (len(args) >= 1 and args[0] is not None):
+            kwargs["details"]["traceback"] = testtools.content.TracebackContent(args[0], test)
+            args = []
         return method(test, *args, **kwargs)
 
 #

---
2.23.0

#1 lore raw

PATCH 2/2 oeqa/core/case.py: Encode binary data of log

Do not decode the log content into a string only to re-encode it as binary data again. Some logs might un-intentionally contain bytes that do not decode as utf-8, as such preserve the log file content as it was on disk.

Handle the decoding on the resulttool side, but also handle the failure to decode the data.

---
 meta/lib/oeqa/core/case.py            | 4 ++--
 scripts/lib/resulttool/resultutils.py | 6 +++++-
 2 files changed, 7 insertions(+), 3 deletions(-)
meta/lib/oeqa/core/case.py +2 -2
index 180635ac6c..aae451fef2 100644
@@ -59,7 +59,7 @@ class OEPTestResultTestCase:
     """
     @staticmethod
     def _compress_log(log):
-        logdata = log.encode("utf-8")
+        logdata = log.encode("utf-8") if isinstance(log, str) else log
         logdata = zlib.compress(logdata)
         logdata = base64.b64encode(logdata).decode("utf-8")
         return {"compressed" : logdata}
@@ -80,7 +80,7 @@ class OEPTestResultTestCase:
         if log is not None:
             sections[section]["log"] = self._compress_log(log)
         elif logfile is not None:
-            with open(logfile, "r") as f:
+            with open(logfile, "rb") as f:
                 sections[section]["log"] = self._compress_log(f.read())
 
         if duration is not None:
scripts/lib/resulttool/resultutils.py +5 -1
index 177fb25f93..7cb85a6aa9 100644
@@ -126,7 +126,11 @@ def decode_log(logdata):
         if "compressed" in logdata:
             data = logdata.get("compressed")
             data = base64.b64decode(data.encode("utf-8"))
-            return zlib.decompress(data).decode("utf-8")
+            data = zlib.decompress(data)
+            try:
+                return data.decode("utf-8")
+            except UnicodeDecodeError:
+                return data
     return None
 
 def ptestresult_get_log(results, section):

---
2.23.0