[test] Implemented indicators as processors

Added simple system tests for different progress indicators.

Bug: v8:6917
Change-Id: I906ddfd06e82cc19d3b2210e09457456be00309b
Cq-Include-Trybots: luci.v8.try:v8_linux64_fyi_rel_ng
Reviewed-on: https://chromium-review.googlesource.com/852495
Commit-Queue: Michał Majewski <majeski@google.com>
Reviewed-by: Michael Achenbach <machenbach@chromium.org>
Cr-Commit-Position: refs/heads/master@{#50406}
This commit is contained in:
Michal Majewski 2018-01-08 13:38:00 +01:00 committed by Commit Bot
parent cd9e121128
commit 501413b9b9
4 changed files with 254 additions and 2 deletions

View File

@ -177,6 +177,9 @@ class DotsProgressIndicator(SimpleProgressIndicator):
sys.stdout.write('.')
sys.stdout.flush()
def ToProgressIndicatorProc(self):
return progress_proc.DotsProgressIndicator()
class CompactProgressIndicator(ProgressIndicator):
"""Abstract base class for {Color,Monochrome}ProgressIndicator"""
@ -251,6 +254,9 @@ class ColorProgressIndicator(CompactProgressIndicator):
def ClearLine(self, last_line_length):
print "\033[1K\r",
def ToProgressIndicatorProc(self):
return progress_proc.ColorProgressIndicator()
class MonochromeProgressIndicator(CompactProgressIndicator):
@ -266,11 +272,15 @@ class MonochromeProgressIndicator(CompactProgressIndicator):
def ClearLine(self, last_line_length):
print ("\r" + (" " * last_line_length) + "\r"),
def ToProgressIndicatorProc(self):
return progress_proc.MonochromeProgressIndicator()
class JUnitTestProgressIndicator(ProgressIndicator):
def __init__(self, junitout, junittestsuite):
super(JUnitTestProgressIndicator, self).__init__()
self.junitout = junitout
self.juinttestsuite = junittestsuite
self.outputter = junit_output.JUnitTestOutput(junittestsuite)
if junitout:
self.outfile = open(junitout, "w")
@ -291,7 +301,7 @@ class JUnitTestProgressIndicator(ProgressIndicator):
stderr = output.stderr.strip()
if len(stderr):
fail_text += "stderr:\n%s\n" % stderr
fail_text += "Command: %s" % self.test.cmd.to_string()
fail_text += "Command: %s" % test.cmd.to_string()
if output.HasCrashed():
fail_text += "exit code: %d\n--- CRASHED ---" % output.exit_code
if output.HasTimedOut():
@ -302,6 +312,12 @@ class JUnitTestProgressIndicator(ProgressIndicator):
test_duration=output.duration,
test_failure=fail_text)
def ToProgressIndicatorProc(self):
if self.outfile != sys.stdout:
self.outfile.close()
return progress_proc.JUnitTestProgressIndicator(self.junitout,
self.junittestsuite)
class JsonTestProgressIndicator(ProgressIndicator):

View File

@ -18,6 +18,10 @@ class TestProc(object):
def result_for(self, test, result, is_last):
raise NotImplementedError()
def heartbeat(self):
if self._prev_proc:
self._prev_proc.heartbeat()
### Communication
def _send_test(self, test):
return self._next_proc.next_test(test)
@ -36,12 +40,19 @@ class TestProcObserver(TestProc):
self._on_result_for(test, result, is_last)
self._send_result(test, result, is_last)
def heartbeat(self):
self._on_heartbeat()
super(TestProcObserver, self).heartbeat()
def _on_next_test(self, test):
pass
def _on_result_for(self, test, result, is_last):
pass
def _on_heartbeat(self):
pass
class TestProcProducer(TestProc):
def __init__(self, name):

View File

@ -5,8 +5,10 @@
import json
import os
import sys
import time
from . import base
from ..local import junit_output
def print_failure_header(test):
@ -114,6 +116,177 @@ class VerboseProgressIndicator(SimpleProgressIndicator):
print 'Done running %s: %s' % (test, outcome)
sys.stdout.flush()
def _on_heartbeat(self):
print 'Still working...'
sys.stdout.flush()
class DotsProgressIndicator(SimpleProgressIndicator):
def __init__(self):
super(DotsProgressIndicator, self).__init__()
self._count = 0
def _on_result_for(self, test, result, is_last):
self._count += 1
if self._count > 1 and self._count % 50 == 1:
sys.stdout.write('\n')
if result.has_unexpected_output:
if result.output.HasCrashed():
sys.stdout.write('C')
sys.stdout.flush()
elif result.output.HasTimedOut():
sys.stdout.write('T')
sys.stdout.flush()
else:
sys.stdout.write('F')
sys.stdout.flush()
else:
sys.stdout.write('.')
sys.stdout.flush()
class CompactProgressIndicator(ProgressIndicator):
def __init__(self, templates):
super(CompactProgressIndicator, self).__init__()
self._templates = templates
self._last_status_length = 0
self._start_time = time.time()
self._total = 0
self._passed = 0
self._failed = 0
def _on_next_test(self, test):
self._total += 1
def _on_result_for(self, test, result, is_last):
if not is_last:
# Some processor further in the chain created several subtests of one
# test, so lets add them to the total amount.
self._total += 1
if result.has_unexpected_output:
self._failed += 1
else:
self._passed += 1
self._print_progress(str(test))
if result.has_unexpected_output:
output = result.output
stdout = output.stdout.strip()
stderr = output.stderr.strip()
self._clear_line(self._last_status_length)
print_failure_header(test)
if len(stdout):
print self._templates['stdout'] % stdout
if len(stderr):
print self._templates['stderr'] % stderr
print "Command: %s" % test.cmd
if output.HasCrashed():
print "exit code: %d" % output.exit_code
print "--- CRASHED ---"
if output.HasTimedOut():
print "--- TIMEOUT ---"
def finished(self):
self._print_progress('Done')
print
def _print_progress(self, name):
self._clear_line(self._last_status_length)
elapsed = time.time() - self._start_time
if not self._total:
progress = 0
else:
progress = (self._passed + self._failed) * 100 // self._total
status = self._templates['status_line'] % {
'passed': self._passed,
'progress': progress,
'failed': self._failed,
'test': name,
'mins': int(elapsed) / 60,
'secs': int(elapsed) % 60
}
status = self._truncate(status, 78)
self._last_status_length = len(status)
print status,
sys.stdout.flush()
def _truncate(self, string, length):
if length and len(string) > (length - 3):
return string[:(length - 3)] + "..."
else:
return string
def _clear_line(self, last_length):
raise NotImplementedError()
class ColorProgressIndicator(CompactProgressIndicator):
def __init__(self):
templates = {
'status_line': ("[%(mins)02i:%(secs)02i|"
"\033[34m%%%(progress) 4d\033[0m|"
"\033[32m+%(passed) 4d\033[0m|"
"\033[31m-%(failed) 4d\033[0m]: %(test)s"),
'stdout': "\033[1m%s\033[0m",
'stderr': "\033[31m%s\033[0m",
}
super(ColorProgressIndicator, self).__init__(templates)
def _clear_line(self, last_length):
print "\033[1K\r",
class MonochromeProgressIndicator(CompactProgressIndicator):
def __init__(self):
templates = {
'status_line': ("[%(mins)02i:%(secs)02i|%%%(progress) 4d|"
"+%(passed) 4d|-%(failed) 4d]: %(test)s"),
'stdout': '%s',
'stderr': '%s',
}
super(MonochromeProgressIndicator, self).__init__(templates)
def _clear_line(self, last_length):
print ("\r" + (" " * last_length) + "\r"),
class JUnitTestProgressIndicator(ProgressIndicator):
def __init__(self, junitout, junittestsuite):
super(JUnitTestProgressIndicator, self).__init__()
self.outputter = junit_output.JUnitTestOutput(junittestsuite)
if junitout:
self.outfile = open(junitout, "w")
else:
self.outfile = sys.stdout
def _on_result_for(self, test, result, is_last):
fail_text = ""
output = result.output
if result.has_unexpected_output:
stdout = output.stdout.strip()
if len(stdout):
fail_text += "stdout:\n%s\n" % stdout
stderr = output.stderr.strip()
if len(stderr):
fail_text += "stderr:\n%s\n" % stderr
fail_text += "Command: %s" % test.cmd.to_string()
if output.HasCrashed():
fail_text += "exit code: %d\n--- CRASHED ---" % output.exit_code
if output.HasTimedOut():
fail_text += "--- TIMEOUT ---"
self.outputter.HasRunTest(
test_name=str(test),
test_cmd=test.cmd.to_string(relative=True),
test_duration=output.duration,
test_failure=fail_text)
def finished(self):
self.outputter.FinishAndWrite(self.outfile)
if self.outfile != sys.stdout:
self.outfile.close()
class JsonTestProgressIndicator(ProgressIndicator):
def __init__(self, json_test_results, arch, mode, random_seed):

View File

@ -531,5 +531,57 @@ class SystemTest(unittest.TestCase):
self.assertTrue(statusfile.PresubmitCheck(
os.path.join(basedir, 'test', 'sweet', 'sweet.status')))
def testDotsProgressProc(self):
self.testDotsProgress(infra_staging=True)
def testDotsProgress(self, infra_staging=False):
with temp_base() as basedir:
result = run_tests(
basedir,
'--mode=Release',
'--progress=dots',
'sweet/cherries',
'sweet/bananas',
'--no-sorting', '-j1', # make results order deterministic
infra_staging=infra_staging,
)
self.assertIn('Running 2 tests', result.stdout, result)
self.assertIn('F.', result.stdout, result)
self.assertEqual(1, result.returncode, result)
def testMonoProgressProc(self):
self._testCompactProgress('mono', True)
def testMonoProgress(self):
self._testCompactProgress('mono', False)
def testColorProgressProc(self):
self._testCompactProgress('color', True)
def testColorProgress(self):
self._testCompactProgress('color', False)
def _testCompactProgress(self, name, infra_staging):
with temp_base() as basedir:
result = run_tests(
basedir,
'--mode=Release',
'--progress=%s' % name,
'sweet/cherries',
'sweet/bananas',
infra_staging=infra_staging,
)
if name == 'color':
expected = ('\033[34m% 100\033[0m|'
'\033[32m+ 1\033[0m|'
'\033[31m- 1\033[0m]: Done')
else:
expected = '% 100|+ 1|- 1]: Done'
self.assertIn(expected, result.stdout)
self.assertIn('sweet/cherries', result.stdout)
self.assertIn('sweet/bananas', result.stdout)
self.assertEqual(1, result.returncode, result)
if __name__ == '__main__':
unittest.main()