blob: 72a4b0cd122b8ef3387ae116a5710b2bf20e60fc [file] [log] [blame]
David Klempnerf94838b2015-02-02 16:56:46 -08001#!/usr/bin/python2.7
Craig Tillerc2c79212015-02-16 12:00:01 -08002# Copyright 2015, Google Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
Nicolas Nobleddef2462015-01-06 18:08:25 -080031"""Run tests in parallel."""
32
33import argparse
34import glob
35import itertools
Craig Tiller261dd982015-01-16 16:41:45 -080036import json
Nicolas Nobleddef2462015-01-06 18:08:25 -080037import multiprocessing
Craig Tiller1cc11db2015-01-15 22:50:50 -080038import os
Nicolas Nobleddef2462015-01-06 18:08:25 -080039import sys
ctiller3040cb72015-01-07 12:13:17 -080040import time
Nicolas Nobleddef2462015-01-06 18:08:25 -080041
42import jobset
ctiller3040cb72015-01-07 12:13:17 -080043import watch_dirs
Nicolas Nobleddef2462015-01-06 18:08:25 -080044
Craig Tillerb50d1662015-01-15 17:28:21 -080045
Craig Tiller738c3342015-01-12 14:28:33 -080046# SimpleConfig: just compile with CONFIG=config, and run the binary to test
47class SimpleConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080048
Craig Tiller547db2b2015-01-30 14:08:39 -080049 def __init__(self, config, environ={}):
Craig Tiller738c3342015-01-12 14:28:33 -080050 self.build_config = config
Craig Tillere68de0e2015-01-26 08:44:00 -080051 self.maxjobs = 2 * multiprocessing.cpu_count()
Craig Tillerc7449162015-01-16 14:42:10 -080052 self.allow_hashing = (config != 'gcov')
Craig Tiller547db2b2015-01-30 14:08:39 -080053 self.environ = environ
Craig Tiller738c3342015-01-12 14:28:33 -080054
Craig Tiller547db2b2015-01-30 14:08:39 -080055 def job_spec(self, binary, hash_targets):
56 return jobset.JobSpec(cmdline=[binary],
57 environ=self.environ,
58 hash_targets=hash_targets
59 if self.allow_hashing else None)
Craig Tiller738c3342015-01-12 14:28:33 -080060
61
62# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
63class ValgrindConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080064
Craig Tiller2aa4d642015-01-14 15:59:44 -080065 def __init__(self, config, tool):
Craig Tiller738c3342015-01-12 14:28:33 -080066 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080067 self.tool = tool
Craig Tillere68de0e2015-01-26 08:44:00 -080068 self.maxjobs = 2 * multiprocessing.cpu_count()
Craig Tillerc7449162015-01-16 14:42:10 -080069 self.allow_hashing = False
Craig Tiller738c3342015-01-12 14:28:33 -080070
Craig Tiller547db2b2015-01-30 14:08:39 -080071 def job_spec(self, binary, hash_targets):
Nicolas Noble0af000b2015-02-11 14:48:04 -080072 return jobset.JobSpec(cmdline=['valgrind', '--tool=%s' % self.tool, binary],
Craig Tiller547db2b2015-01-30 14:08:39 -080073 hash_targets=None)
Craig Tiller738c3342015-01-12 14:28:33 -080074
75
Craig Tillerc7449162015-01-16 14:42:10 -080076class CLanguage(object):
77
Craig Tillere9c959d2015-01-18 10:23:26 -080078 def __init__(self, make_target, test_lang):
Craig Tillerc7449162015-01-16 14:42:10 -080079 self.make_target = make_target
Craig Tillere9c959d2015-01-18 10:23:26 -080080 with open('tools/run_tests/tests.json') as f:
Craig Tiller06b4ff22015-01-18 11:01:25 -080081 js = json.load(f)
murgatroid992c8d5162015-01-26 10:41:21 -080082 self.binaries = [tgt['name']
83 for tgt in js
Craig Tillere9c959d2015-01-18 10:23:26 -080084 if tgt['language'] == test_lang]
Craig Tillerc7449162015-01-16 14:42:10 -080085
Craig Tiller547db2b2015-01-30 14:08:39 -080086 def test_specs(self, config):
87 out = []
88 for name in self.binaries:
89 binary = 'bins/%s/%s' % (config.build_config, name)
90 out.append(config.job_spec(binary, [binary]))
91 return out
Craig Tillerc7449162015-01-16 14:42:10 -080092
93 def make_targets(self):
94 return ['buildtests_%s' % self.make_target]
95
96 def build_steps(self):
97 return []
98
Craig Tiller99775822015-01-30 13:07:16 -080099
murgatroid992c8d5162015-01-26 10:41:21 -0800100class NodeLanguage(object):
101
Craig Tiller547db2b2015-01-30 14:08:39 -0800102 def test_specs(self, config):
103 return [config.job_spec('tools/run_tests/run_node.sh', None)]
murgatroid992c8d5162015-01-26 10:41:21 -0800104
105 def make_targets(self):
murgatroid99c2791652015-01-26 11:33:39 -0800106 return ['static_c']
murgatroid992c8d5162015-01-26 10:41:21 -0800107
108 def build_steps(self):
109 return [['tools/run_tests/build_node.sh']]
Craig Tillerc7449162015-01-16 14:42:10 -0800110
Craig Tiller99775822015-01-30 13:07:16 -0800111
Craig Tillerc7449162015-01-16 14:42:10 -0800112class PhpLanguage(object):
113
Craig Tiller547db2b2015-01-30 14:08:39 -0800114 def test_specs(self, config):
115 return [config.job_spec('src/php/bin/run_tests.sh', None)]
Craig Tillerc7449162015-01-16 14:42:10 -0800116
117 def make_targets(self):
murgatroid99564b9442015-01-26 11:07:59 -0800118 return ['static_c']
Craig Tillerc7449162015-01-16 14:42:10 -0800119
120 def build_steps(self):
121 return [['tools/run_tests/build_php.sh']]
122
123
Nathaniel Manista840615e2015-01-22 20:31:47 +0000124class PythonLanguage(object):
125
Craig Tiller547db2b2015-01-30 14:08:39 -0800126 def test_specs(self, config):
127 return [config.job_spec('tools/run_tests/run_python.sh', None)]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000128
129 def make_targets(self):
130 return[]
131
132 def build_steps(self):
133 return [['tools/run_tests/build_python.sh']]
134
135
Craig Tiller738c3342015-01-12 14:28:33 -0800136# different configurations we can run under
137_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -0800138 'dbg': SimpleConfig('dbg'),
139 'opt': SimpleConfig('opt'),
David Klempner1d0302d2015-02-04 16:08:01 -0800140 'tsan': SimpleConfig('tsan', environ={
141 'TSAN_OPTIONS': 'suppressions=tools/tsan_suppressions.txt'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800142 'msan': SimpleConfig('msan'),
Craig Tiller96bd5f62015-02-13 09:04:13 -0800143 'ubsan': SimpleConfig('ubsan'),
Craig Tiller547db2b2015-01-30 14:08:39 -0800144 'asan': SimpleConfig('asan', environ={
David Klempner1d0302d2015-02-04 16:08:01 -0800145 'ASAN_OPTIONS': 'detect_leaks=1:color=always:suppressions=tools/tsan_suppressions.txt'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800146 'gcov': SimpleConfig('gcov'),
147 'memcheck': ValgrindConfig('valgrind', 'memcheck'),
148 'helgrind': ValgrindConfig('dbg', 'helgrind')
149 }
Craig Tiller738c3342015-01-12 14:28:33 -0800150
151
Craig Tillerb29797b2015-01-12 13:51:54 -0800152_DEFAULT = ['dbg', 'opt']
Craig Tillerc7449162015-01-16 14:42:10 -0800153_LANGUAGES = {
Craig Tillere9c959d2015-01-18 10:23:26 -0800154 'c++': CLanguage('cxx', 'c++'),
155 'c': CLanguage('c', 'c'),
murgatroid992c8d5162015-01-26 10:41:21 -0800156 'node': NodeLanguage(),
Nathaniel Manista840615e2015-01-22 20:31:47 +0000157 'php': PhpLanguage(),
158 'python': PythonLanguage(),
Craig Tillereb272bc2015-01-30 13:13:14 -0800159 }
Nicolas Nobleddef2462015-01-06 18:08:25 -0800160
161# parse command line
162argp = argparse.ArgumentParser(description='Run grpc tests.')
163argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -0800164 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -0800165 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -0800166 default=_DEFAULT)
Nicolas Nobleddef2462015-01-06 18:08:25 -0800167argp.add_argument('-n', '--runs_per_test', default=1, type=int)
Craig Tillerc2c79212015-02-16 12:00:01 -0800168argp.add_argument('-j', '--jobs', default=1000, type=int)
ctiller3040cb72015-01-07 12:13:17 -0800169argp.add_argument('-f', '--forever',
170 default=False,
171 action='store_const',
172 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800173argp.add_argument('--newline_on_success',
174 default=False,
175 action='store_const',
176 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -0800177argp.add_argument('-l', '--language',
Craig Tillerc7449162015-01-16 14:42:10 -0800178 choices=sorted(_LANGUAGES.keys()),
Craig Tiller686fb262015-01-15 07:39:09 -0800179 nargs='+',
Craig Tillerc7449162015-01-16 14:42:10 -0800180 default=sorted(_LANGUAGES.keys()))
Nicolas Nobleddef2462015-01-06 18:08:25 -0800181args = argp.parse_args()
182
183# grab config
Craig Tiller738c3342015-01-12 14:28:33 -0800184run_configs = set(_CONFIGS[cfg]
185 for cfg in itertools.chain.from_iterable(
186 _CONFIGS.iterkeys() if x == 'all' else [x]
187 for x in args.config))
188build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tillerf1973b02015-01-16 12:32:13 -0800189
Craig Tillerc7449162015-01-16 14:42:10 -0800190make_targets = []
191languages = set(_LANGUAGES[l] for l in args.language)
Craig Tiller547db2b2015-01-30 14:08:39 -0800192build_steps = [jobset.JobSpec(['make',
193 '-j', '%d' % (multiprocessing.cpu_count() + 1),
194 'CONFIG=%s' % cfg] + list(set(
195 itertools.chain.from_iterable(
196 l.make_targets() for l in languages))))
197 for cfg in build_configs] + list(set(
198 jobset.JobSpec(cmdline)
199 for l in languages
200 for cmdline in l.build_steps()))
201one_run = set(
202 spec
203 for config in run_configs
204 for language in args.language
205 for spec in _LANGUAGES[language].test_specs(config))
Craig Tillerf1973b02015-01-16 12:32:13 -0800206
Nicolas Nobleddef2462015-01-06 18:08:25 -0800207runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -0800208forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -0800209
Nicolas Nobleddef2462015-01-06 18:08:25 -0800210
Craig Tiller71735182015-01-15 17:07:13 -0800211class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800212 """Cache for running tests."""
213
David Klempner25739582015-02-11 15:57:32 -0800214 def __init__(self, use_cache_results):
Craig Tiller71735182015-01-15 17:07:13 -0800215 self._last_successful_run = {}
David Klempner25739582015-02-11 15:57:32 -0800216 self._use_cache_results = use_cache_results
Craig Tiller71735182015-01-15 17:07:13 -0800217
218 def should_run(self, cmdline, bin_hash):
Craig Tiller71735182015-01-15 17:07:13 -0800219 if cmdline not in self._last_successful_run:
220 return True
221 if self._last_successful_run[cmdline] != bin_hash:
222 return True
David Klempner25739582015-02-11 15:57:32 -0800223 if not self._use_cache_results:
224 return True
Craig Tiller71735182015-01-15 17:07:13 -0800225 return False
226
227 def finished(self, cmdline, bin_hash):
Craig Tiller547db2b2015-01-30 14:08:39 -0800228 self._last_successful_run[cmdline] = bin_hash
Craig Tiller71735182015-01-15 17:07:13 -0800229
230 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800231 return [{'cmdline': k, 'hash': v}
232 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800233
234 def parse(self, exdump):
235 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
236
237 def save(self):
238 with open('.run_tests_cache', 'w') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800239 f.write(json.dumps(self.dump()))
Craig Tiller71735182015-01-15 17:07:13 -0800240
Craig Tiller1cc11db2015-01-15 22:50:50 -0800241 def maybe_load(self):
242 if os.path.exists('.run_tests_cache'):
243 with open('.run_tests_cache') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800244 self.parse(json.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800245
246
247def _build_and_run(check_cancelled, newline_on_success, cache):
ctiller3040cb72015-01-07 12:13:17 -0800248 """Do one pass of building & running tests."""
murgatroid99666450e2015-01-26 13:03:31 -0800249 # build latest sequentially
murgatroid99c2791652015-01-26 11:33:39 -0800250 if not jobset.run(build_steps, maxjobs=1):
Craig Tillerd86a3942015-01-14 12:48:54 -0800251 return 1
ctiller3040cb72015-01-07 12:13:17 -0800252
253 # run all the tests
Craig Tillerc7449162015-01-16 14:42:10 -0800254 all_runs = itertools.chain.from_iterable(
255 itertools.repeat(one_run, runs_per_test))
256 if not jobset.run(all_runs, check_cancelled,
257 newline_on_success=newline_on_success,
Craig Tillerc2c79212015-02-16 12:00:01 -0800258 maxjobs=min(args.jobs, min(c.maxjobs for c in run_configs)),
Craig Tillerc7449162015-01-16 14:42:10 -0800259 cache=cache):
Craig Tillerd86a3942015-01-14 12:48:54 -0800260 return 2
261
262 return 0
ctiller3040cb72015-01-07 12:13:17 -0800263
264
David Klempner25739582015-02-11 15:57:32 -0800265test_cache = TestCache(runs_per_test == 1)
Craig Tiller547db2b2015-01-30 14:08:39 -0800266test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800267
ctiller3040cb72015-01-07 12:13:17 -0800268if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800269 success = True
ctiller3040cb72015-01-07 12:13:17 -0800270 while True:
271 dw = watch_dirs.DirWatcher(['src', 'include', 'test'])
272 initial_time = dw.most_recent_change()
273 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800274 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800275 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800276 newline_on_success=False,
Craig Tiller71735182015-01-15 17:07:13 -0800277 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800278 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800279 jobset.message('SUCCESS',
280 'All tests are now passing properly',
281 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800282 jobset.message('IDLE', 'No change detected')
Craig Tiller547db2b2015-01-30 14:08:39 -0800283 test_cache.save()
ctiller3040cb72015-01-07 12:13:17 -0800284 while not have_files_changed():
285 time.sleep(1)
286else:
Craig Tiller71735182015-01-15 17:07:13 -0800287 result = _build_and_run(check_cancelled=lambda: False,
288 newline_on_success=args.newline_on_success,
289 cache=test_cache)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800290 if result == 0:
291 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
292 else:
293 jobset.message('FAILED', 'Some tests failed', do_newline=True)
Craig Tiller547db2b2015-01-30 14:08:39 -0800294 test_cache.save()
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800295 sys.exit(result)