Make sure tsc.py raises meaningfull reason of failure
when command returns rc=1

Risk: Medium
Visibility: High. Impacts "reason" field displayed in TKO

Signed-off-by: Vladimir Samarskiy <vsamarsk@google.com>



git-svn-id: http://test.kernel.org/svn/autotest/trunk@2076 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/client/tests/tsc/tsc.py b/client/tests/tsc/tsc.py
index 5807787..f280204 100755
--- a/client/tests/tsc/tsc.py
+++ b/client/tests/tsc/tsc.py
@@ -1,9 +1,10 @@
 import os
 from autotest_lib.client.bin import test, autotest_utils
-from autotest_lib.client.common_lib import utils
+from autotest_lib.client.common_lib import utils, error
 
 class tsc(test.test):
     version = 2
+
     preserve_srcdir = True
 
     def setup(self):
@@ -16,4 +17,44 @@
 
 
     def run_once(self, args = '-t 650'):
-        utils.system(self.srcdir + '/checktsc ' + args)
+        result = utils.run(self.srcdir + '/checktsc ' + args,
+                           ignore_status=True)
+        print 'result.exit_status', result.exit_status
+        if result.exit_status != 0:
+            default_reason = ("UNKNOWN FAILURE: rc=%d from %s" %
+                              (result.exit_status, result.command))
+            ## Analyze result.stdout to see if it is possible to form qualified
+            ## reason of failure and to raise an appropriate exception.
+            ## For this test we qualify the reason of failure if the 
+            ## following conditions are met:
+            ## (i) result.exit_status = 1
+            ## (ii) result.stdout ends with 'FAIL'
+            ## (iii) "FAIL" is preceeded by one or more
+            ##       lines in the following format:
+            ##       CPU x - CPU y = <delta>
+            ## Set as a reason the line that contains max abs(delta)
+            if result.exit_status == 1:
+                if result.stdout.strip('\n').endswith('FAIL'):
+                    ## find all lines
+                    ## CPU x - CPU y = <delta>
+                    ## and parse out delta of max abs value
+                    max_delta = 0
+                    reason = ''
+                    threshold = int(args.split()[1])
+                    latencies = re.findall("CPU \d+ - CPU \d+ =\s+-*\d+",
+                                           result.stdout)
+                    for ln in latencies:
+                        cur_delta = int(ln.split('=', 2)[1])
+                        if abs(cur_delta) > max_delta:
+                            max_delta = abs(cur_delta)
+                            reason = ln
+                    if max_delta > threshold:
+                        reason = "Latency %s exceeds threshold %d" % (reason,
+                                                                      threshold)
+                        raise error.TestFail(reason)
+
+            ## If we are here, we failed to qualify the reason of test failre
+            ## Consider it as a test error
+            raise error.TestError(default_reason)
+
+