run_test_suite 15.6 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
#!/usr/bin/python

import atexit, errno, imp, os, pprint, random, re, socket, shlex, shutil
import signal, string, subprocess, sys, threading, time, urlparse, xmlrpclib

SVN_UP_REV=re.compile(r'^(?:At|Updated to) revision (\d+).$')
SVN_CHANGED_REV=re.compile(r'^Last Changed Rev.*:\s*(\d+)', re.MULTILINE)

def killallIfParentDies():
  os.setsid()
  atexit.register(lambda: os.kill(0, 9))
  from ctypes import cdll
  libc = cdll.LoadLibrary('libc.so.6')
  def PR_SET_PDEATHSIG(sig):
    if libc.prctl(1, sig):
      raise OSError
  PR_SET_PDEATHSIG(signal.SIGINT)

_format_command_search = re.compile("[[\\s $({?*\\`#~';<>&|]").search
_format_command_escape = lambda s: "'%s'" % r"'\''".join(s.split("'"))
def format_command(*args, **kw):
  cmdline = []
  for k, v in sorted(kw.items()):
    if _format_command_search(v):
      v = _format_command_escape(v)
    cmdline.append('%s=%s' % (k, v))
  for v in args:
    if _format_command_search(v):
      v = _format_command_escape(v)
    cmdline.append(v)
  return ' '.join(cmdline)

33
def subprocess_capture(p, quiet=False):
34 35 36 37 38
  def readerthread(input, output, buffer):
    while True:
      data = input.readline()
      if not data:
        break
39
      output(data)
40 41 42
      buffer.append(data)
  if p.stdout:
    stdout = []
43
    output = quiet and (lambda data: None) or sys.stdout.write
44
    stdout_thread = threading.Thread(target=readerthread,
45
                                     args=(p.stdout, output, stdout))
46 47 48 49 50
    stdout_thread.setDaemon(True)
    stdout_thread.start()
  if p.stderr:
    stderr = []
    stderr_thread = threading.Thread(target=readerthread,
51
                                     args=(p.stderr, sys.stderr.write, stderr))
52 53 54 55 56 57 58 59 60 61 62
    stderr_thread.setDaemon(True)
    stderr_thread.start()
  if p.stdout:
    stdout_thread.join()
  if p.stderr:
    stderr_thread.join()
  p.wait()
  return (p.stdout and ''.join(stdout),
          p.stderr and ''.join(stderr))


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 98
class Persistent(object):
  """Very simple persistent data storage for optimization purpose

  This tool should become a standalone daemon communicating only with an ERP5
  instance. But for the moment, it only execute 1 test suite and exists,
  and test suite classes may want some information from previous runs.
  """

  def __init__(self, filename):
    self._filename = filename

  def __getattr__(self, attr):
    if attr == '_db':
      try:
        db = file(self._filename, 'r+')
      except IOError, e:
        if e.errno != errno.ENOENT:
          raise
        db = file(self._filename, 'w+')
      else:
        try:
          self.__dict__.update(eval(db.read()))
        except StandardError:
          pass
      self._db = db
      return db
    self._db
    return super(Persistent, self).__getattribute__(attr)

  def sync(self):
    self._db.seek(0)
    db = dict(x for x in self.__dict__.iteritems() if x[0][:1] != '_')
    pprint.pprint(db, self._db)
    self._db.truncate()


99 100 101 102 103 104 105 106 107 108 109
class SubprocessError(EnvironmentError):
  def __init__(self, status_dict):
    self.status_dict = status_dict
  def __getattr__(self, name):
    return self.status_dict[name]
  def __str__(self):
    return 'Error %i' % self.status_code


class Updater(object):

110
  _git_cache = {}
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  realtime_output = True
  stdin = file(os.devnull)

  def __init__(self, revision=None):
    self.revision = revision
    self._path_list = []

  def deletePycFiles(self, path):
    """Delete *.pyc files so that deleted/moved files can not be imported"""
    for path, dir_list, file_list in os.walk(path):
      for file in file_list:
        if file[-4:] in ('.pyc', '.pyo'):
          # allow several processes clean the same folder at the same time
          try:
            os.remove(os.path.join(path, file))
          except OSError, e:
            if e.errno != errno.ENOENT:
              raise

  def spawn(self, *args, **kw):
131
    quiet = kw.pop('quiet', False)
132 133 134 135 136 137 138
    env = kw and dict(os.environ, **kw) or None
    command = format_command(*args, **kw)
    print '\n$ ' + command
    sys.stdout.flush()
    p = subprocess.Popen(args, stdin=self.stdin, stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE, env=env)
    if self.realtime_output:
139
      stdout, stderr = subprocess_capture(p, quiet)
140 141
    else:
      stdout, stderr = p.communicate()
142 143
      if not quiet:
        sys.stdout.write(stdout)
144 145 146 147 148 149 150 151 152 153
      sys.stderr.write(stderr)
    result = dict(status_code=p.returncode, command=command,
                  stdout=stdout, stderr=stderr)
    if p.returncode:
      raise SubprocessError(result)
    return result

  def _git(self, *args, **kw):
    return self.spawn('git', *args, **kw)['stdout'].strip()

154 155
  def _git_find_rev(self, ref):
    try:
156
      return self._git_cache[ref]
157
    except KeyError:
158 159 160 161 162 163 164
      if os.path.exists('.git/svn'):
        r = self._git('svn', 'find-rev', ref)
        assert r
        self._git_cache[ref[0] != 'r' and 'r%u' % int(r) or r] = ref
      else:
        r = self._git('rev-list', '--topo-order', '--count', ref), ref
      self._git_cache[ref] = r
165 166
      return r

167 168 169
  def getRevision(self, *path_list):
    if not path_list:
      path_list = self._path_list
170
    if os.path.isdir('.git'):
171
      h = self._git('log', '-1', '--format=%H', '--', *path_list)
172
      return self._git_find_rev(h)
173
    if os.path.isdir('.svn'):
174
      stdout = self.spawn('svn', 'info', *path_list)['stdout']
175 176 177 178
      return str(max(map(int, SVN_CHANGED_REV.findall(stdout))))
    raise NotImplementedError

  def checkout(self, *path_list):
179 180
    if not path_list:
      path_list = '.',
181 182 183 184
    revision = self.revision
    if os.path.isdir('.git'):
      # edit .git/info/sparse-checkout if you want sparse checkout
      if revision:
185 186 187 188
        if type(revision) is str:
          h = self._git_find_rev('r' + revision)
        else:
          h = revision[1]
189 190 191 192 193
        if h != self._git('rev-parse', 'HEAD'):
          self.deletePycFiles('.')
          self._git('reset', '--merge', h)
      else:
        self.deletePycFiles('.')
194 195 196 197 198
        if os.path.exists('.git/svn'):
          self._git('svn', 'rebase')
        else:
          self._git('pull', '--ff-only')
        self.revision = self._git_find_rev(self._git('rev-parse', 'HEAD'))
199 200 201 202 203 204 205
    elif os.path.isdir('.svn'):
      # following code allows sparse checkout
      def svn_mkdirs(path):
        path = os.path.dirname(path)
        if path and not os.path.isdir(path):
          svn_mkdirs(path)
          self.spawn(*(args + ['--depth=empty', path]))
206
      for path in path_list:
207 208 209 210
        args = ['svn', 'up', '--force', '--non-interactive']
        if revision:
          args.append('-r%s' % revision)
        svn_mkdirs(path)
211
        args += '--set-depth=infinity', path
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        self.deletePycFiles(path)
        try:
          status_dict = self.spawn(*args)
        except SubprocessError, e:
          if 'cleanup' not in e.stderr:
            raise
          self.spawn('svn', 'cleanup', path)
          status_dict = self.spawn(*args)
        if not revision:
          self.revision = revision = SVN_UP_REV.findall(
            status_dict['stdout'].splitlines()[-1])[0]
    else:
      raise NotImplementedError
    self._path_list += path_list


class TestSuite(Updater):

  mysql_db_count = 1
  allow_restart = False

  def __init__(self, max_instance_count, **kw):
    self.__dict__.update(kw)
    self._path_list = ['tests']
    pool = threading.Semaphore(max_instance_count)
    self.acquire = pool.acquire
    self.release = pool.release
    self._instance = threading.local()
    self._pool = max_instance_count == 1 and [None] or \
                 range(1, max_instance_count + 1)
    self._ready = set()
    self.running = {}
    if max_instance_count != 1:
      self.realtime_output = False
    elif os.isatty(1):
      self.realtime_output = True
248 249
    self.persistent = Persistent('run_test_suite-%s.tmp'
                                 % self.__class__.__name__)
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

  instance = property(lambda self: self._instance.id)

  def start(self, test, on_stop=None):
    assert test not in self.running
    self.running[test] = instance = self._pool.pop(0)
    def run():
      self._instance.id = instance
      if instance not in self._ready:
        self._ready.add(instance)
        self.setup()
      status_dict = self.run(test)
      if on_stop is not None:
        on_stop(status_dict)
      self._pool.append(self.running.pop(test))
      self.release()
266 267 268
    thread = threading.Thread(target=run)
    thread.setDaemon(True)
    thread.start()
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

  def update(self):
    self.checkout() # by default, update everything

  def setup(self):
    pass

  def run(self, test):
    raise NotImplementedError

  def getTestList(self):
    raise NotImplementedError


class ERP5TypeTestSuite(TestSuite):

  RUN_RE = re.compile(
    r'Ran (?P<all_tests>\d+) tests? in (?P<seconds>\d+\.\d+)s',
    re.DOTALL)

  STATUS_RE = re.compile(r"""
    (OK|FAILED)\s+\(
      (failures=(?P<failures>\d+),?\s*)?
      (errors=(?P<errors>\d+),?\s*)?
      (skipped=(?P<skips>\d+),?\s*)?
      (expected\s+failures=(?P<expected_failures>\d+),?\s*)?
      (unexpected\s+successes=(?P<unexpected_successes>\d+),?\s*)?
    \)
    """, re.DOTALL | re.VERBOSE)

  def setup(self):
    instance_home = self.instance and 'unit_test.%u' % self.instance \
                                   or 'unit_test'
    tests = os.path.join(instance_home, 'tests')
    if os.path.exists(tests):
      shutil.rmtree(instance_home + '.previous', True)
      shutil.move(tests, instance_home + '.previous')

  def run(self, test):
    return self.runUnitTest(test)

  def runUnitTest(self, *args, **kw):
    if self.instance:
      args = ('--instance_home=unit_test.%u' % self.instance,) + args
313
    mysql_db_list = [string.Template(x).substitute(I=self.instance or '1')
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
                      for x in self.mysql_db_list]
    if len(mysql_db_list) > 1:
      kw['extra_sql_connection_string_list'] = ','.join(mysql_db_list[1:])
    try:
      runUnitTest = os.environ.get('RUN_UNIT_TEST',
                                   'Products/ERP5Type/tests/runUnitTest.py')
      args = tuple(shlex.split(runUnitTest)) \
           + ('--verbose', '--erp5_sql_connection_string=' + mysql_db_list[0]) \
           + args
      status_dict = self.spawn(*args, **kw)
    except SubprocessError, e:
      status_dict = e.status_dict
    test_log = status_dict['stderr']
    search = self.RUN_RE.search(test_log)
    if search:
      groupdict = search.groupdict()
      status_dict.update(duration=float(groupdict['seconds']),
                         test_count=int(groupdict['all_tests']))
    search = self.STATUS_RE.search(test_log)
    if search:
      groupdict = search.groupdict()
335 336 337 338 339 340
      status_dict.update(
        error_count=int(groupdict['errors'] or 0),
        failure_count=int(groupdict['failures'] or 0)
                     +int(groupdict['unexpected_successes'] or 0),
        skip_count=int(groupdict['skips'] or 0)
                  +int(groupdict['expected_failures'] or 0))
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    return status_dict


#class LoadSaveExample(ERP5TypeTestSuite):
#  def getTestList(self):
#    return [test_path.split(os.sep)[-1][:-3]
#            for test_path in glob.glob('tests/test*.py')]
#
#  def setup(self):
#    TestSuite.setup(self)
#    return self.runUnitTest(self, '--save', 'testFoo')
#
#  def run(self, test):
#    return self.runUnitTest(self, '--load', test)


sys.modules['test_suite'] = module = imp.new_module('test_suite')
358
for var in SubprocessError, TestSuite, ERP5TypeTestSuite:
359 360 361
  setattr(module, var.__name__, var)


362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
class DummyTaskDistributionTool(object):

  def __init__(self):
    self.lock = threading.Lock()

  def createTestResult(self, name, revision, test_name_list, allow_restart):
    self.test_name_list = list(test_name_list)
    return None, revision

  def startUnitTest(self, test_result_path, exclude_list=()):
    self.lock.acquire()
    try:
      for i, test in enumerate(self.test_name_list):
        if test not in exclude_list:
          del self.test_name_list[i]
          return None, test
    finally:
      self.lock.release()

  def stopUnitTest(self, test_path, status_dict):
    pass


385 386 387 388 389 390 391 392 393 394 395 396 397
def safeRpcCall(function, *args):
  retry = 64
  while True:
    try:
      return function(*args)
    except (socket.error, xmlrpclib.ProtocolError), e:
      print >>sys.stderr, e
      pprint.pprint(args, file(function._Method__name, 'w'))
      time.sleep(retry)
      retry += retry >> 1

def getOptionParser():
  from optparse import OptionParser
398 399
  parser = OptionParser(
    usage="%prog [options] [SUITE_CLASS@]<SUITE_NAME>[=<MAX_INSTANCES>]")
400 401 402 403 404 405 406 407 408 409 410 411 412
  _ = parser.add_option
  _("--master", help="URL of ERP5 instance, used as master node")
  _("--mysql_db_list", help="comma-separated list of connection strings")
  return parser

def main():
  os.environ['LC_ALL'] = 'C'

  parser = getOptionParser()
  options, args = parser.parse_args()
  try:
    name, = args
    if '=' in name:
413
      name, max_instance_count = name.rsplit('=', 1)
414 415 416
      max_instance_count = int(max_instance_count)
    else:
      max_instance_count = 1
417 418 419 420
    if '@' in name:
      suite_class_name, name = name.split('@', 1)
    else:
      suite_class_name = name
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
  except ValueError:
    parser.error("invalid arguments")
  db_list = options.mysql_db_list
  if db_list:
    db_list = db_list.split(',')
    multi = max_instance_count != 1
    try:
      for db in db_list:
        if db == string.Template(db).substitute(I=1) and multi:
          raise KeyError
    except KeyError:
      parser.error("invalid value for --mysql_db_list")
  else:
    db_list = (max_instance_count == 1 and 'test test' or 'test$I test'),

  def makeSuite(revision=None):
    updater = Updater(revision)
438 439
    if portal_url:
      updater.checkout('tests')
440 441 442
    for k in sys.modules.keys():
      if k == 'tests' or k.startswith('tests.'):
        del sys.modules[k]
443
    module_name, class_name = ('tests.' + suite_class_name).rsplit('.', 1)
444
    try:
445 446 447
      suite_class = getattr(__import__(module_name, None, None, [class_name]),
                            class_name)
    except (AttributeError, ImportError):
448 449 450 451 452 453 454
      parser.error("unknown test suite")
    if len(db_list) < suite_class.mysql_db_count:
      parser.error("%r suite needs %u DB (only %u given)" %
        (name, suite_class.mysql_db_count, len(db_list)))
    suite = suite_class(revision=updater.revision,
                        max_instance_count=max_instance_count,
                        mysql_db_list=db_list[:suite_class.mysql_db_count])
455 456
    if portal_url:
      suite.update()
457 458 459
    return suite

  portal_url = options.master
460 461 462 463 464 465 466 467
  if portal_url:
    if portal_url[-1] != '/':
      portal_url += '/'
    portal = xmlrpclib.ServerProxy(portal_url, allow_none=1)
    master = portal.portal_task_distribution
    assert master.getProtocolRevision() == 1
  else:
    master = DummyTaskDistributionTool()
468 469 470 471 472 473 474

  suite = makeSuite()
  revision = suite.getRevision()
  test_result = safeRpcCall(master.createTestResult,
    name, revision, suite.getTestList(), suite.allow_restart)
  if test_result:
    test_result_path, test_revision = test_result
475 476 477 478
    if portal_url: # for buildbot
      url_parts = list(urlparse.urlparse(portal_url + test_result_path))
      url_parts[1] = url_parts[1].split('@')[-1]
      print 'ERP5_TEST_URL %s OK' % urlparse.urlunparse(url_parts)
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
    while suite.acquire():
      test = safeRpcCall(master.startUnitTest, test_result_path,
                         suite.running.keys())
      if test:
        if revision != test_revision:
          suite = makeSuite(test_revision)
          revision = test_revision
          suite.acquire()
        suite.start(test[1], lambda status_dict, __test_path=test[0]:
          safeRpcCall(master.stopUnitTest, __test_path, status_dict))
      elif not suite.running:
        break
      # We are finishing the suite. Let's disable idle nodes.


if __name__ == '__main__':
495
  sys.path[0] = ''
496 497 498
  if not os.isatty(0):
    killallIfParentDies()
  sys.exit(main())