blob: aa3245d8303954b68a798f660657b104c2f1bad2 [file] [log] [blame]
Nicolas Nobleddef2462015-01-06 18:08:25 -08001#!/usr/bin/python
2"""Run tests in parallel."""
3
4import argparse
5import glob
6import itertools
7import multiprocessing
8import sys
ctiller3040cb72015-01-07 12:13:17 -08009import time
Nicolas Nobleddef2462015-01-06 18:08:25 -080010
11import jobset
ctiller3040cb72015-01-07 12:13:17 -080012import watch_dirs
Nicolas Nobleddef2462015-01-06 18:08:25 -080013
Craig Tiller738c3342015-01-12 14:28:33 -080014# SimpleConfig: just compile with CONFIG=config, and run the binary to test
15class SimpleConfig(object):
16 def __init__(self, config):
17 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080018 self.maxjobs = 32 * multiprocessing.cpu_count()
Craig Tiller738c3342015-01-12 14:28:33 -080019
20 def run_command(self, binary):
21 return [binary]
22
23
24# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
25class ValgrindConfig(object):
Craig Tiller2aa4d642015-01-14 15:59:44 -080026 def __init__(self, config, tool):
Craig Tiller738c3342015-01-12 14:28:33 -080027 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080028 self.tool = tool
29 self.maxjobs = 4 * multiprocessing.cpu_count()
Craig Tiller738c3342015-01-12 14:28:33 -080030
31 def run_command(self, binary):
Craig Tiller2aa4d642015-01-14 15:59:44 -080032 return ['valgrind', binary, '--tool=%s' % self.tool]
Craig Tiller738c3342015-01-12 14:28:33 -080033
34
35# different configurations we can run under
36_CONFIGS = {
37 'dbg': SimpleConfig('dbg'),
38 'opt': SimpleConfig('opt'),
39 'tsan': SimpleConfig('tsan'),
40 'msan': SimpleConfig('msan'),
41 'asan': SimpleConfig('asan'),
Craig Tiller934baa32015-01-12 18:19:45 -080042 'gcov': SimpleConfig('gcov'),
Craig Tillerec0b8f32015-01-15 07:30:00 -080043 'memcheck': ValgrindConfig('valgrind', 'memcheck'),
Craig Tiller2aa4d642015-01-14 15:59:44 -080044 'helgrind': ValgrindConfig('dbg', 'helgrind')
Craig Tiller738c3342015-01-12 14:28:33 -080045 }
46
47
Craig Tillerb29797b2015-01-12 13:51:54 -080048_DEFAULT = ['dbg', 'opt']
Craig Tiller686fb262015-01-15 07:39:09 -080049_LANGUAGE_TEST_TARGETS = {
50 'c++': 'buildtests_cxx',
51 'c': 'buildtests_c',
52}
Nicolas Nobleddef2462015-01-06 18:08:25 -080053
54# parse command line
55argp = argparse.ArgumentParser(description='Run grpc tests.')
56argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -080057 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -080058 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -080059 default=_DEFAULT)
Nicolas Nobleddef2462015-01-06 18:08:25 -080060argp.add_argument('-t', '--test-filter', nargs='*', default=['*'])
61argp.add_argument('-n', '--runs_per_test', default=1, type=int)
ctiller3040cb72015-01-07 12:13:17 -080062argp.add_argument('-f', '--forever',
63 default=False,
64 action='store_const',
65 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -080066argp.add_argument('--newline_on_success',
67 default=False,
68 action='store_const',
69 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -080070argp.add_argument('-l', '--language',
71 choices=sorted(_LANGUAGE_TEST_TARGETS.keys()),
72 nargs='+',
73 default=sorted(_LANGUAGE_TEST_TARGETS.keys()))
Nicolas Nobleddef2462015-01-06 18:08:25 -080074args = argp.parse_args()
75
76# grab config
Craig Tiller738c3342015-01-12 14:28:33 -080077run_configs = set(_CONFIGS[cfg]
78 for cfg in itertools.chain.from_iterable(
79 _CONFIGS.iterkeys() if x == 'all' else [x]
80 for x in args.config))
81build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tiller686fb262015-01-15 07:39:09 -080082make_targets = set(_LANGUAGE_TEST_TARGETS[x] for x in args.language)
Nicolas Nobleddef2462015-01-06 18:08:25 -080083filters = args.test_filter
84runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -080085forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -080086
Nicolas Nobleddef2462015-01-06 18:08:25 -080087
Nicolas Noble044db742015-01-14 16:57:24 -080088def _build_and_run(check_cancelled, newline_on_success, forever=False):
ctiller3040cb72015-01-07 12:13:17 -080089 """Do one pass of building & running tests."""
90 # build latest, sharing cpu between the various makes
91 if not jobset.run(
92 (['make',
Craig Tiller738c3342015-01-12 14:28:33 -080093 '-j', '%d' % (multiprocessing.cpu_count() + 1),
Craig Tiller686fb262015-01-15 07:39:09 -080094 'CONFIG=%s' % cfg] + list(make_targets)
Craig Tillerbe56a962015-01-14 11:33:03 -080095 for cfg in build_configs),
Craig Tiller7c1d7f72015-01-12 17:08:33 -080096 check_cancelled, maxjobs=1):
Craig Tillerd86a3942015-01-14 12:48:54 -080097 return 1
ctiller3040cb72015-01-07 12:13:17 -080098
99 # run all the tests
Craig Tiller2aa4d642015-01-14 15:59:44 -0800100 if not jobset.run(
101 itertools.ifilter(
102 lambda x: x is not None, (
103 config.run_command(x)
104 for config in run_configs
105 for filt in filters
106 for x in itertools.chain.from_iterable(itertools.repeat(
107 glob.glob('bins/%s/%s_test' % (
108 config.build_config, filt)),
109 runs_per_test)))),
110 check_cancelled,
Nicolas Noble594ef6c2015-01-14 18:02:04 -0800111 newline_on_success=newline_on_success,
Craig Tiller2aa4d642015-01-14 15:59:44 -0800112 maxjobs=min(c.maxjobs for c in run_configs)):
Craig Tillerd86a3942015-01-14 12:48:54 -0800113 return 2
114
115 return 0
ctiller3040cb72015-01-07 12:13:17 -0800116
117
118if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800119 success = True
ctiller3040cb72015-01-07 12:13:17 -0800120 while True:
121 dw = watch_dirs.DirWatcher(['src', 'include', 'test'])
122 initial_time = dw.most_recent_change()
123 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800124 previous_success = success
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800125 success = _build_and_run(have_files_changed,
126 newline_on_success=False,
127 forever=True) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800128 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800129 jobset.message('SUCCESS',
130 'All tests are now passing properly',
131 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800132 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800133 while not have_files_changed():
134 time.sleep(1)
135else:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800136 result = _build_and_run(lambda: False,
137 newline_on_success=args.newline_on_success)
138 if result == 0:
139 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
140 else:
141 jobset.message('FAILED', 'Some tests failed', do_newline=True)
142 sys.exit(result)