blob: da849f04cb0f5c0dfaaa81c58ad975bfed3ff1fb [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
Craig Tiller261dd982015-01-16 16:41:45 -08007import json
Nicolas Nobleddef2462015-01-06 18:08:25 -08008import multiprocessing
Craig Tiller1cc11db2015-01-15 22:50:50 -08009import os
Nicolas Nobleddef2462015-01-06 18:08:25 -080010import sys
ctiller3040cb72015-01-07 12:13:17 -080011import time
Nicolas Nobleddef2462015-01-06 18:08:25 -080012
13import jobset
ctiller3040cb72015-01-07 12:13:17 -080014import watch_dirs
Nicolas Nobleddef2462015-01-06 18:08:25 -080015
Craig Tillerb50d1662015-01-15 17:28:21 -080016
Craig Tiller738c3342015-01-12 14:28:33 -080017# SimpleConfig: just compile with CONFIG=config, and run the binary to test
18class SimpleConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080019
Craig Tiller738c3342015-01-12 14:28:33 -080020 def __init__(self, config):
21 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080022 self.maxjobs = 32 * multiprocessing.cpu_count()
Craig Tillerc7449162015-01-16 14:42:10 -080023 self.allow_hashing = (config != 'gcov')
Craig Tiller738c3342015-01-12 14:28:33 -080024
25 def run_command(self, binary):
26 return [binary]
27
28
29# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
30class ValgrindConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080031
Craig Tiller2aa4d642015-01-14 15:59:44 -080032 def __init__(self, config, tool):
Craig Tiller738c3342015-01-12 14:28:33 -080033 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080034 self.tool = tool
35 self.maxjobs = 4 * multiprocessing.cpu_count()
Craig Tillerc7449162015-01-16 14:42:10 -080036 self.allow_hashing = False
Craig Tiller738c3342015-01-12 14:28:33 -080037
38 def run_command(self, binary):
Craig Tiller2aa4d642015-01-14 15:59:44 -080039 return ['valgrind', binary, '--tool=%s' % self.tool]
Craig Tiller738c3342015-01-12 14:28:33 -080040
41
Craig Tillerc7449162015-01-16 14:42:10 -080042class CLanguage(object):
43
Craig Tillere9c959d2015-01-18 10:23:26 -080044 def __init__(self, make_target, test_lang):
Craig Tillerc7449162015-01-16 14:42:10 -080045 self.allow_hashing = True
46 self.make_target = make_target
Craig Tillere9c959d2015-01-18 10:23:26 -080047 with open('tools/run_tests/tests.json') as f:
Craig Tiller06b4ff22015-01-18 11:01:25 -080048 js = json.load(f)
Craig Tillere9c959d2015-01-18 10:23:26 -080049 self.binaries = [tgt['name']
50 for tgt in js
51 if tgt['language'] == test_lang]
Craig Tillerc7449162015-01-16 14:42:10 -080052
53 def test_binaries(self, config):
Craig Tillere9c959d2015-01-18 10:23:26 -080054 return ['bins/%s/%s' % (config, binary) for binary in self.binaries]
Craig Tillerc7449162015-01-16 14:42:10 -080055
56 def make_targets(self):
57 return ['buildtests_%s' % self.make_target]
58
59 def build_steps(self):
60 return []
61
62
63class PhpLanguage(object):
64
65 def __init__(self):
66 self.allow_hashing = False
67
68 def test_binaries(self, config):
69 return ['src/php/bin/run_tests.sh']
70
71 def make_targets(self):
72 return []
73
74 def build_steps(self):
75 return [['tools/run_tests/build_php.sh']]
76
77
Nathaniel Manista840615e2015-01-22 20:31:47 +000078class PythonLanguage(object):
79
80 def __init__(self):
81 self.allow_hashing = False
82
83 def test_binaries(self, config):
84 return ['tools/run_tests/run_python.sh']
85
86 def make_targets(self):
87 return[]
88
89 def build_steps(self):
90 return [['tools/run_tests/build_python.sh']]
91
92
Craig Tiller738c3342015-01-12 14:28:33 -080093# different configurations we can run under
94_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -080095 'dbg': SimpleConfig('dbg'),
96 'opt': SimpleConfig('opt'),
97 'tsan': SimpleConfig('tsan'),
98 'msan': SimpleConfig('msan'),
99 'asan': SimpleConfig('asan'),
100 'gcov': SimpleConfig('gcov'),
101 'memcheck': ValgrindConfig('valgrind', 'memcheck'),
102 'helgrind': ValgrindConfig('dbg', 'helgrind')
103 }
Craig Tiller738c3342015-01-12 14:28:33 -0800104
105
Craig Tillerb29797b2015-01-12 13:51:54 -0800106_DEFAULT = ['dbg', 'opt']
Craig Tillerc7449162015-01-16 14:42:10 -0800107_LANGUAGES = {
Craig Tillere9c959d2015-01-18 10:23:26 -0800108 'c++': CLanguage('cxx', 'c++'),
109 'c': CLanguage('c', 'c'),
Nathaniel Manista840615e2015-01-22 20:31:47 +0000110 'php': PhpLanguage(),
111 'python': PythonLanguage(),
Craig Tiller686fb262015-01-15 07:39:09 -0800112}
Nicolas Nobleddef2462015-01-06 18:08:25 -0800113
114# parse command line
115argp = argparse.ArgumentParser(description='Run grpc tests.')
116argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -0800117 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -0800118 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -0800119 default=_DEFAULT)
Nicolas Nobleddef2462015-01-06 18:08:25 -0800120argp.add_argument('-n', '--runs_per_test', default=1, type=int)
ctiller3040cb72015-01-07 12:13:17 -0800121argp.add_argument('-f', '--forever',
122 default=False,
123 action='store_const',
124 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800125argp.add_argument('--newline_on_success',
126 default=False,
127 action='store_const',
128 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -0800129argp.add_argument('-l', '--language',
Craig Tillerc7449162015-01-16 14:42:10 -0800130 choices=sorted(_LANGUAGES.keys()),
Craig Tiller686fb262015-01-15 07:39:09 -0800131 nargs='+',
Craig Tillerc7449162015-01-16 14:42:10 -0800132 default=sorted(_LANGUAGES.keys()))
Nicolas Nobleddef2462015-01-06 18:08:25 -0800133args = argp.parse_args()
134
135# grab config
Craig Tiller738c3342015-01-12 14:28:33 -0800136run_configs = set(_CONFIGS[cfg]
137 for cfg in itertools.chain.from_iterable(
138 _CONFIGS.iterkeys() if x == 'all' else [x]
139 for x in args.config))
140build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tillerf1973b02015-01-16 12:32:13 -0800141
Craig Tillerc7449162015-01-16 14:42:10 -0800142make_targets = []
143languages = set(_LANGUAGES[l] for l in args.language)
144build_steps = [['make',
145 '-j', '%d' % (multiprocessing.cpu_count() + 1),
146 'CONFIG=%s' % cfg] + list(set(
147 itertools.chain.from_iterable(l.make_targets()
148 for l in languages)))
149 for cfg in build_configs] + list(
150 itertools.chain.from_iterable(l.build_steps()
151 for l in languages))
Craig Tillerf1973b02015-01-16 12:32:13 -0800152
Nicolas Nobleddef2462015-01-06 18:08:25 -0800153runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -0800154forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -0800155
Nicolas Nobleddef2462015-01-06 18:08:25 -0800156
Craig Tiller71735182015-01-15 17:07:13 -0800157class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800158 """Cache for running tests."""
159
Craig Tiller71735182015-01-15 17:07:13 -0800160 def __init__(self):
161 self._last_successful_run = {}
162
163 def should_run(self, cmdline, bin_hash):
164 cmdline = ' '.join(cmdline)
165 if cmdline not in self._last_successful_run:
166 return True
167 if self._last_successful_run[cmdline] != bin_hash:
168 return True
169 return False
170
171 def finished(self, cmdline, bin_hash):
172 self._last_successful_run[' '.join(cmdline)] = bin_hash
173
174 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800175 return [{'cmdline': k, 'hash': v}
176 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800177
178 def parse(self, exdump):
179 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
180
181 def save(self):
182 with open('.run_tests_cache', 'w') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800183 f.write(json.dumps(self.dump()))
Craig Tiller71735182015-01-15 17:07:13 -0800184
Craig Tiller1cc11db2015-01-15 22:50:50 -0800185 def maybe_load(self):
186 if os.path.exists('.run_tests_cache'):
187 with open('.run_tests_cache') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800188 self.parse(json.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800189
190
191def _build_and_run(check_cancelled, newline_on_success, cache):
ctiller3040cb72015-01-07 12:13:17 -0800192 """Do one pass of building & running tests."""
193 # build latest, sharing cpu between the various makes
Craig Tillerf1973b02015-01-16 12:32:13 -0800194 if not jobset.run(build_steps):
Craig Tillerd86a3942015-01-14 12:48:54 -0800195 return 1
ctiller3040cb72015-01-07 12:13:17 -0800196
197 # run all the tests
Craig Tillerc7449162015-01-16 14:42:10 -0800198 one_run = dict(
199 (' '.join(config.run_command(x)), config.run_command(x))
200 for config in run_configs
201 for language in args.language
202 for x in _LANGUAGES[language].test_binaries(config.build_config)
203 ).values()
204 all_runs = itertools.chain.from_iterable(
205 itertools.repeat(one_run, runs_per_test))
206 if not jobset.run(all_runs, check_cancelled,
207 newline_on_success=newline_on_success,
208 maxjobs=min(c.maxjobs for c in run_configs),
209 cache=cache):
Craig Tillerd86a3942015-01-14 12:48:54 -0800210 return 2
211
212 return 0
ctiller3040cb72015-01-07 12:13:17 -0800213
214
Craig Tillerc7449162015-01-16 14:42:10 -0800215test_cache = (None
216 if not all(x.allow_hashing
217 for x in itertools.chain(languages, run_configs))
Craig Tillerb50d1662015-01-15 17:28:21 -0800218 else TestCache())
Craig Tiller71735182015-01-15 17:07:13 -0800219if test_cache:
Craig Tiller1cc11db2015-01-15 22:50:50 -0800220 test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800221
ctiller3040cb72015-01-07 12:13:17 -0800222if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800223 success = True
ctiller3040cb72015-01-07 12:13:17 -0800224 while True:
225 dw = watch_dirs.DirWatcher(['src', 'include', 'test'])
226 initial_time = dw.most_recent_change()
227 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800228 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800229 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800230 newline_on_success=False,
Craig Tiller71735182015-01-15 17:07:13 -0800231 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800232 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800233 jobset.message('SUCCESS',
234 'All tests are now passing properly',
235 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800236 jobset.message('IDLE', 'No change detected')
Craig Tillerc7449162015-01-16 14:42:10 -0800237 if test_cache: test_cache.save()
ctiller3040cb72015-01-07 12:13:17 -0800238 while not have_files_changed():
239 time.sleep(1)
240else:
Craig Tiller71735182015-01-15 17:07:13 -0800241 result = _build_and_run(check_cancelled=lambda: False,
242 newline_on_success=args.newline_on_success,
243 cache=test_cache)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800244 if result == 0:
245 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
246 else:
247 jobset.message('FAILED', 'Some tests failed', do_newline=True)
Craig Tillerc7449162015-01-16 14:42:10 -0800248 if test_cache: test_cache.save()
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800249 sys.exit(result)