runTestSuite.in 3.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#!{{runTestSuite_py}}
from __future__ import print_function
import argparse, os, re, subprocess, sys
from time import gmtime, strftime, time
from erp5.util import taskdistribution
from erp5.util.testsuite import SubprocessError, TestSuite
from zc.buildout.buildout import Buildout

slapos_buildout = {{repr(slapos_buildout)}}
test_dict = {
  'zc.buildout': slapos_buildout,
  'zc.recipe.egg': os.path.join(slapos_buildout, 'zc.recipe.egg_'),
}

class DummyTestResult:

  class DummyTestResultLine:
    def stop(self, **kw):
      pass

  done = 0

  def __init__(self, test_name_list):
    self.test_name_list = test_name_list

  def start(self):
    test_result_line = self.DummyTestResultLine()
    try:
      test_result_line.name = self.test_name_list[self.done]
    except IndexError:
      return
    self.done += 1
    return test_result_line

class BuildoutTestSuite(TestSuite):

  RUN_RE = re.compile(
    r'Ran (?P<all_tests>\d+) tests with'
    ' (?P<failures>\d+) failures,'
    ' (?P<errors>\d+) errors and'
    ' (?P<skips>\d+) skipped in')

  def run(self, test):
    start = time()
    try:
      status_dict = self.spawn(os.path.join('bin', 'zope-testrunner'),
        '--test-path', os.path.join(test_dict[test], 'src'))
    except SubprocessError, e:
      status_dict = e.status_dict
    end = time()
    status_dict.update(
      date = strftime("%Y/%m/%d %H:%M:%S", gmtime(end)),
      duration = end - start)
    search = self.RUN_RE.search(status_dict['stdout'])
    if search:
      groupdict = search.groupdict()
      status_dict.update(
        test_count = int(groupdict['all_tests']),
        error_count = int(groupdict['errors']),
        failure_count = int(groupdict['failures']),
        skip_count = int(groupdict['skips']))
    return status_dict

def main():
  os.environ['TEMP'] = {{repr(temp_directory)}}
  parser = argparse.ArgumentParser(description='Run a test suite.')
  parser.add_argument('--test_suite', help='The test suite name')
  parser.add_argument('--test_suite_title', help='The test suite title')
  parser.add_argument('--test_node_title', help='The test node title')
  parser.add_argument('--project_title', help='The project title')
  parser.add_argument('--revision', help='The revision to test',
                      default='dummy_revision')
  parser.add_argument('--master_url',
                      help='The Url of Master controling many suites')

  args = parser.parse_args()

  test_title = args.test_suite_title or args.test_suite
  if args.master_url:
    tool = taskdistribution.TaskDistributionTool(args.master_url)
    test_result = tool.createTestResult(args.revision,
                                        list(test_dict),
                                        args.test_node_title,
                                        test_title=test_title,
                                        project_title=args.project_title)
    if test_result is None:
      return
  else:
    test_result = DummyTestResult(list(test_dict))

  fd = os.open('buildout.cfg', os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0666)
  try:
    os.write(fd, """\
[buildout]
extends = %s
develop =%s
parts = test
98
newest = false
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

[versions]
%s

[bootstrap]
recipe = zc.recipe.egg
eggs = zc.buildout

[test]
recipe = zc.recipe.egg
eggs +=
  zope.testrunner
scripts =
  zope-testrunner
""" % (os.path.join(slapos_buildout, 'buildout.cfg'),
       ''.join('\n  ' + x for x in test_dict.itervalues()),
       '\n'.join(x + ' =' for x in test_dict)))
  finally:
    os.close(fd)
  Buildout('buildout.cfg', {}).install(['bootstrap'])
  subprocess.check_call((os.path.join('bin', 'buildout'),))

  test_suite = BuildoutTestSuite(1)
  while 1:
    test_result_line = test_result.start()
    if not test_result_line:
      break
    test_result_line.stop(**test_suite.run(test_result_line.name))

if __name__ == "__main__":
    main()