blob: 651566188f907fb9419c73fb0baaf0b323c9ea37 [file] [log] [blame]
Nicolas Noblef3585732015-03-15 20:05:24 -07001#!/usr/bin/env python
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
Craig Tillerfe406ec2015-02-24 13:55:12 -080039import re
Nicolas Nobleddef2462015-01-06 18:08:25 -080040import sys
ctiller3040cb72015-01-07 12:13:17 -080041import time
Craig Tiller5058c692015-04-08 09:42:04 -070042import platform
Craig Tiller234b6e72015-05-23 10:12:40 -070043import subprocess
Nicolas Nobleddef2462015-01-06 18:08:25 -080044
45import jobset
ctiller3040cb72015-01-07 12:13:17 -080046import watch_dirs
Nicolas Nobleddef2462015-01-06 18:08:25 -080047
Craig Tillerb50d1662015-01-15 17:28:21 -080048
Craig Tiller2cc2b842015-02-27 11:38:31 -080049ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
50os.chdir(ROOT)
51
52
Craig Tiller738c3342015-01-12 14:28:33 -080053# SimpleConfig: just compile with CONFIG=config, and run the binary to test
54class SimpleConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080055
murgatroid99132ce6a2015-03-04 17:29:14 -080056 def __init__(self, config, environ=None):
57 if environ is None:
58 environ = {}
Craig Tiller738c3342015-01-12 14:28:33 -080059 self.build_config = config
Craig Tillerc7449162015-01-16 14:42:10 -080060 self.allow_hashing = (config != 'gcov')
Craig Tiller547db2b2015-01-30 14:08:39 -080061 self.environ = environ
murgatroid99132ce6a2015-03-04 17:29:14 -080062 self.environ['CONFIG'] = config
Craig Tiller738c3342015-01-12 14:28:33 -080063
Craig Tiller4fc90032015-05-21 10:39:52 -070064 def job_spec(self, cmdline, hash_targets, shortname=None, environ={}):
Craig Tiller49f61322015-03-03 13:02:11 -080065 """Construct a jobset.JobSpec for a test under this config
66
67 Args:
68 cmdline: a list of strings specifying the command line the test
69 would like to run
70 hash_targets: either None (don't do caching of test results), or
71 a list of strings specifying files to include in a
72 binary hash to check if a test has changed
73 -- if used, all artifacts needed to run the test must
74 be listed
75 """
Craig Tiller4fc90032015-05-21 10:39:52 -070076 actual_environ = self.environ.copy()
77 for k, v in environ.iteritems():
78 actual_environ[k] = v
Craig Tiller49f61322015-03-03 13:02:11 -080079 return jobset.JobSpec(cmdline=cmdline,
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -070080 shortname=shortname,
Craig Tiller4fc90032015-05-21 10:39:52 -070081 environ=actual_environ,
Craig Tiller547db2b2015-01-30 14:08:39 -080082 hash_targets=hash_targets
83 if self.allow_hashing else None)
Craig Tiller738c3342015-01-12 14:28:33 -080084
85
86# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
87class ValgrindConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080088
murgatroid99132ce6a2015-03-04 17:29:14 -080089 def __init__(self, config, tool, args=None):
90 if args is None:
91 args = []
Craig Tiller738c3342015-01-12 14:28:33 -080092 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080093 self.tool = tool
Craig Tiller1a305b12015-02-18 13:37:06 -080094 self.args = args
Craig Tillerc7449162015-01-16 14:42:10 -080095 self.allow_hashing = False
Craig Tiller738c3342015-01-12 14:28:33 -080096
Craig Tiller49f61322015-03-03 13:02:11 -080097 def job_spec(self, cmdline, hash_targets):
Craig Tiller1a305b12015-02-18 13:37:06 -080098 return jobset.JobSpec(cmdline=['valgrind', '--tool=%s' % self.tool] +
Craig Tiller49f61322015-03-03 13:02:11 -080099 self.args + cmdline,
Craig Tiller1a305b12015-02-18 13:37:06 -0800100 shortname='valgrind %s' % binary,
101 hash_targets=None)
Craig Tiller738c3342015-01-12 14:28:33 -0800102
103
Craig Tillerc7449162015-01-16 14:42:10 -0800104class CLanguage(object):
105
Craig Tillere9c959d2015-01-18 10:23:26 -0800106 def __init__(self, make_target, test_lang):
Craig Tillerc7449162015-01-16 14:42:10 -0800107 self.make_target = make_target
Craig Tillerd625d812015-04-08 15:52:35 -0700108 if platform.system() == 'Windows':
109 plat = 'windows'
110 else:
111 plat = 'posix'
Nicolas Noblee1445362015-05-11 17:40:26 -0700112 self.platform = plat
Craig Tillere9c959d2015-01-18 10:23:26 -0800113 with open('tools/run_tests/tests.json') as f:
Craig Tiller06b4ff22015-01-18 11:01:25 -0800114 js = json.load(f)
Craig Tillerd625d812015-04-08 15:52:35 -0700115 self.binaries = [tgt
116 for tgt in js
117 if tgt['language'] == test_lang and
118 plat in tgt['platforms']]
Craig Tillerc7449162015-01-16 14:42:10 -0800119
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100120 def test_specs(self, config, travis):
Craig Tiller547db2b2015-01-30 14:08:39 -0800121 out = []
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100122 for target in self.binaries:
123 if travis and target['flaky']:
124 continue
Nicolas Noblee1445362015-05-11 17:40:26 -0700125 if self.platform == 'windows':
126 binary = 'vsprojects\\test_bin\\%s.exe' % (target['name'])
127 else:
128 binary = 'bins/%s/%s' % (config.build_config, target['name'])
Craig Tiller49f61322015-03-03 13:02:11 -0800129 out.append(config.job_spec([binary], [binary]))
Nicolas Noblee1445362015-05-11 17:40:26 -0700130 return sorted(out)
Craig Tillerc7449162015-01-16 14:42:10 -0800131
132 def make_targets(self):
133 return ['buildtests_%s' % self.make_target]
134
135 def build_steps(self):
136 return []
137
murgatroid99132ce6a2015-03-04 17:29:14 -0800138 def supports_multi_config(self):
139 return True
140
141 def __str__(self):
142 return self.make_target
143
Craig Tiller99775822015-01-30 13:07:16 -0800144
murgatroid992c8d5162015-01-26 10:41:21 -0800145class NodeLanguage(object):
146
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100147 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700148 return [config.job_spec(['tools/run_tests/run_node.sh'], None,
149 environ={'GRPC_TRACE': 'surface,batch'})]
murgatroid992c8d5162015-01-26 10:41:21 -0800150
151 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700152 return ['static_c', 'shared_c']
murgatroid992c8d5162015-01-26 10:41:21 -0800153
154 def build_steps(self):
155 return [['tools/run_tests/build_node.sh']]
Craig Tillerc7449162015-01-16 14:42:10 -0800156
murgatroid99132ce6a2015-03-04 17:29:14 -0800157 def supports_multi_config(self):
158 return False
159
160 def __str__(self):
161 return 'node'
162
Craig Tiller99775822015-01-30 13:07:16 -0800163
Craig Tillerc7449162015-01-16 14:42:10 -0800164class PhpLanguage(object):
165
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100166 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700167 return [config.job_spec(['src/php/bin/run_tests.sh'], None,
168 environ={'GRPC_TRACE': 'surface,batch'})]
Craig Tillerc7449162015-01-16 14:42:10 -0800169
170 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700171 return ['static_c', 'shared_c']
Craig Tillerc7449162015-01-16 14:42:10 -0800172
173 def build_steps(self):
174 return [['tools/run_tests/build_php.sh']]
175
murgatroid99132ce6a2015-03-04 17:29:14 -0800176 def supports_multi_config(self):
177 return False
178
179 def __str__(self):
180 return 'php'
181
Craig Tillerc7449162015-01-16 14:42:10 -0800182
Nathaniel Manista840615e2015-01-22 20:31:47 +0000183class PythonLanguage(object):
184
Craig Tiller49f61322015-03-03 13:02:11 -0800185 def __init__(self):
186 with open('tools/run_tests/python_tests.json') as f:
187 self._tests = json.load(f)
188
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100189 def test_specs(self, config, travis):
Masood Malekghassemib2d4a8d2015-03-05 17:04:18 -0800190 modules = [config.job_spec(['tools/run_tests/run_python.sh', '-m',
Craig Tiller4fc90032015-05-21 10:39:52 -0700191 test['module']],
192 None,
193 environ={'GRPC_TRACE': 'surface,batch'},
Craig Tiller83020252015-05-13 14:46:45 -0700194 shortname=test['module'])
Masood Malekghassemib2d4a8d2015-03-05 17:04:18 -0800195 for test in self._tests if 'module' in test]
196 files = [config.job_spec(['tools/run_tests/run_python.sh',
Craig Tiller4fc90032015-05-21 10:39:52 -0700197 test['file']],
198 None,
199 environ={'GRPC_TRACE': 'surface,batch'},
Craig Tiller83020252015-05-13 14:46:45 -0700200 shortname=test['file'])
Craig Tiller4fc90032015-05-21 10:39:52 -0700201 for test in self._tests if 'file' in test]
Masood Malekghassemib2d4a8d2015-03-05 17:04:18 -0800202 return files + modules
Nathaniel Manista840615e2015-01-22 20:31:47 +0000203
204 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700205 return ['static_c', 'grpc_python_plugin', 'shared_c']
Nathaniel Manista840615e2015-01-22 20:31:47 +0000206
207 def build_steps(self):
208 return [['tools/run_tests/build_python.sh']]
209
murgatroid99132ce6a2015-03-04 17:29:14 -0800210 def supports_multi_config(self):
211 return False
212
213 def __str__(self):
214 return 'python'
215
Craig Tillerd625d812015-04-08 15:52:35 -0700216
murgatroid996a4c4fa2015-02-27 12:08:57 -0800217class RubyLanguage(object):
218
219 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700220 return [config.job_spec(['tools/run_tests/run_ruby.sh'], None,
221 environ={'GRPC_TRACE': 'surface,batch'})]
murgatroid996a4c4fa2015-02-27 12:08:57 -0800222
223 def make_targets(self):
Nicolas "Pixel" Noblecbd9c8b2015-05-14 06:22:26 +0200224 return ['run_dep_checks']
murgatroid996a4c4fa2015-02-27 12:08:57 -0800225
226 def build_steps(self):
227 return [['tools/run_tests/build_ruby.sh']]
228
murgatroid99132ce6a2015-03-04 17:29:14 -0800229 def supports_multi_config(self):
230 return False
231
232 def __str__(self):
233 return 'ruby'
234
Craig Tillerd625d812015-04-08 15:52:35 -0700235
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800236class CSharpLanguage(object):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700237 def __init__(self):
238 if platform.system() == 'Windows':
239 plat = 'windows'
240 else:
241 plat = 'posix'
242 self.platform = plat
243
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800244 def test_specs(self, config, travis):
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700245 assemblies = ['Grpc.Core.Tests',
246 'Grpc.Examples.Tests',
247 'Grpc.IntegrationTesting']
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700248 if self.platform == 'windows':
249 cmd = 'tools\\run_tests\\run_csharp.bat'
250 else:
251 cmd = 'tools/run_tests/run_csharp.sh'
252 return [config.job_spec([cmd, assembly],
Craig Tiller4fc90032015-05-21 10:39:52 -0700253 None, shortname=assembly,
254 environ={'GRPC_TRACE': 'surface,batch'})
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700255 for assembly in assemblies ]
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800256
257 def make_targets(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700258 # For Windows, this target doesn't really build anything,
259 # everything is build by buildall script later.
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800260 return ['grpc_csharp_ext']
261
262 def build_steps(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700263 if self.platform == 'windows':
264 return [['src\\csharp\\buildall.bat']]
265 else:
266 return [['tools/run_tests/build_csharp.sh']]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000267
murgatroid99132ce6a2015-03-04 17:29:14 -0800268 def supports_multi_config(self):
269 return False
270
271 def __str__(self):
272 return 'csharp'
273
Craig Tillerd625d812015-04-08 15:52:35 -0700274
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100275class Sanity(object):
276
277 def test_specs(self, config, travis):
278 return [config.job_spec('tools/run_tests/run_sanity.sh', None)]
279
280 def make_targets(self):
281 return ['run_dep_checks']
282
283 def build_steps(self):
284 return []
285
286 def supports_multi_config(self):
287 return False
288
289 def __str__(self):
290 return 'sanity'
291
Nicolas "Pixel" Noblee55cd7f2015-04-14 17:59:13 +0200292
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100293class Build(object):
294
295 def test_specs(self, config, travis):
296 return []
297
298 def make_targets(self):
Nicolas "Pixel" Noblec23827b2015-04-23 06:17:55 +0200299 return ['static']
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100300
301 def build_steps(self):
302 return []
303
304 def supports_multi_config(self):
305 return True
306
307 def __str__(self):
308 return self.make_target
309
310
Craig Tiller738c3342015-01-12 14:28:33 -0800311# different configurations we can run under
312_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -0800313 'dbg': SimpleConfig('dbg'),
314 'opt': SimpleConfig('opt'),
David Klempner1d0302d2015-02-04 16:08:01 -0800315 'tsan': SimpleConfig('tsan', environ={
316 'TSAN_OPTIONS': 'suppressions=tools/tsan_suppressions.txt'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800317 'msan': SimpleConfig('msan'),
Craig Tiller96bd5f62015-02-13 09:04:13 -0800318 'ubsan': SimpleConfig('ubsan'),
Craig Tiller547db2b2015-01-30 14:08:39 -0800319 'asan': SimpleConfig('asan', environ={
Craig Tiller6fbdd492015-05-29 09:10:10 -0700320 'ASAN_OPTIONS': 'detect_leaks=1:color=always:suppressions=tools/tsan_suppressions.txt',
321 'LSAN_OPTIONS': 'report_objects=1'}),
Craig Tiller6efa6eb2015-05-12 09:44:41 -0700322 'asan-noleaks': SimpleConfig('asan', environ={
323 'ASAN_OPTIONS': 'detect_leaks=0:color=always:suppressions=tools/tsan_suppressions.txt'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800324 'gcov': SimpleConfig('gcov'),
Craig Tiller1a305b12015-02-18 13:37:06 -0800325 'memcheck': ValgrindConfig('valgrind', 'memcheck', ['--leak-check=full']),
Craig Tillerb50d1662015-01-15 17:28:21 -0800326 'helgrind': ValgrindConfig('dbg', 'helgrind')
327 }
Craig Tiller738c3342015-01-12 14:28:33 -0800328
329
Nicolas "Pixel" Noble1fb5e822015-03-16 06:20:37 +0100330_DEFAULT = ['opt']
Craig Tillerc7449162015-01-16 14:42:10 -0800331_LANGUAGES = {
Craig Tillere9c959d2015-01-18 10:23:26 -0800332 'c++': CLanguage('cxx', 'c++'),
333 'c': CLanguage('c', 'c'),
murgatroid992c8d5162015-01-26 10:41:21 -0800334 'node': NodeLanguage(),
Nathaniel Manista840615e2015-01-22 20:31:47 +0000335 'php': PhpLanguage(),
336 'python': PythonLanguage(),
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800337 'ruby': RubyLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100338 'csharp': CSharpLanguage(),
339 'sanity': Sanity(),
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100340 'build': Build(),
Craig Tillereb272bc2015-01-30 13:13:14 -0800341 }
Nicolas Nobleddef2462015-01-06 18:08:25 -0800342
343# parse command line
344argp = argparse.ArgumentParser(description='Run grpc tests.')
345argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -0800346 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -0800347 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -0800348 default=_DEFAULT)
David Garcia Quintase90cd372015-05-31 18:15:26 -0700349
350def runs_per_test_type(arg_str):
351 """Auxilary function to parse the "runs_per_test" flag.
352
353 Returns:
354 A positive integer or 0, the latter indicating an infinite number of
355 runs.
356
357 Raises:
358 argparse.ArgumentTypeError: Upon invalid input.
359 """
360 if arg_str == 'inf':
361 return 0
362 try:
363 n = int(arg_str)
364 if n <= 0: raise ValueError
365 except:
366 msg = "'{}' isn't a positive integer or 'inf'".format(arg_str)
367 raise argparse.ArgumentTypeError(msg)
368argp.add_argument('-n', '--runs_per_test', default=1, type=runs_per_test_type,
369 help='A positive integer or "inf". If "inf", all tests will run in an '
370 'infinite loop. Especially useful in combination with "-f"')
Craig Tillerfe406ec2015-02-24 13:55:12 -0800371argp.add_argument('-r', '--regex', default='.*', type=str)
Craig Tiller83762ac2015-05-22 14:04:06 -0700372argp.add_argument('-j', '--jobs', default=2 * multiprocessing.cpu_count(), type=int)
Craig Tiller8451e872015-02-27 09:25:51 -0800373argp.add_argument('-s', '--slowdown', default=1.0, type=float)
ctiller3040cb72015-01-07 12:13:17 -0800374argp.add_argument('-f', '--forever',
375 default=False,
376 action='store_const',
377 const=True)
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100378argp.add_argument('-t', '--travis',
379 default=False,
380 action='store_const',
381 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800382argp.add_argument('--newline_on_success',
383 default=False,
384 action='store_const',
385 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -0800386argp.add_argument('-l', '--language',
Craig Tillerc7449162015-01-16 14:42:10 -0800387 choices=sorted(_LANGUAGES.keys()),
Craig Tiller686fb262015-01-15 07:39:09 -0800388 nargs='+',
Craig Tillerc7449162015-01-16 14:42:10 -0800389 default=sorted(_LANGUAGES.keys()))
Craig Tiller533b1a22015-05-29 08:41:29 -0700390argp.add_argument('-S', '--stop_on_failure',
391 default=False,
392 action='store_const',
393 const=True)
Craig Tiller234b6e72015-05-23 10:12:40 -0700394argp.add_argument('-a', '--antagonists', default=0, type=int)
Nicolas Nobleddef2462015-01-06 18:08:25 -0800395args = argp.parse_args()
396
397# grab config
Craig Tiller738c3342015-01-12 14:28:33 -0800398run_configs = set(_CONFIGS[cfg]
399 for cfg in itertools.chain.from_iterable(
400 _CONFIGS.iterkeys() if x == 'all' else [x]
401 for x in args.config))
402build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tillerf1973b02015-01-16 12:32:13 -0800403
Craig Tillerc7449162015-01-16 14:42:10 -0800404make_targets = []
405languages = set(_LANGUAGES[l] for l in args.language)
murgatroid99132ce6a2015-03-04 17:29:14 -0800406
407if len(build_configs) > 1:
408 for language in languages:
409 if not language.supports_multi_config():
410 print language, 'does not support multiple build configurations'
411 sys.exit(1)
412
Craig Tiller5058c692015-04-08 09:42:04 -0700413if platform.system() == 'Windows':
414 def make_jobspec(cfg, targets):
Jan Tattermusche8243592015-04-17 14:14:01 -0700415 return jobset.JobSpec(['make.bat', 'CONFIG=%s' % cfg] + targets,
416 cwd='vsprojects', shell=True)
Craig Tiller5058c692015-04-08 09:42:04 -0700417else:
418 def make_jobspec(cfg, targets):
419 return jobset.JobSpec(['make',
420 '-j', '%d' % (multiprocessing.cpu_count() + 1),
Craig Tiller533b1a22015-05-29 08:41:29 -0700421 'EXTRA_DEFINES=GRPC_TEST_SLOWDOWN_MACHINE_FACTOR=%f' %
Craig Tiller5058c692015-04-08 09:42:04 -0700422 args.slowdown,
423 'CONFIG=%s' % cfg] + targets)
424
Craig Tiller533b1a22015-05-29 08:41:29 -0700425build_steps = [make_jobspec(cfg,
Craig Tiller5058c692015-04-08 09:42:04 -0700426 list(set(itertools.chain.from_iterable(
427 l.make_targets() for l in languages))))
428 for cfg in build_configs]
429build_steps.extend(set(
murgatroid99132ce6a2015-03-04 17:29:14 -0800430 jobset.JobSpec(cmdline, environ={'CONFIG': cfg})
431 for cfg in build_configs
Craig Tiller547db2b2015-01-30 14:08:39 -0800432 for l in languages
Craig Tiller533b1a22015-05-29 08:41:29 -0700433 for cmdline in l.build_steps()))
Craig Tiller547db2b2015-01-30 14:08:39 -0800434one_run = set(
435 spec
436 for config in run_configs
437 for language in args.language
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100438 for spec in _LANGUAGES[language].test_specs(config, args.travis)
Craig Tillerfe406ec2015-02-24 13:55:12 -0800439 if re.search(args.regex, spec.shortname))
Craig Tillerf1973b02015-01-16 12:32:13 -0800440
Nicolas Nobleddef2462015-01-06 18:08:25 -0800441runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -0800442forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -0800443
Nicolas Nobleddef2462015-01-06 18:08:25 -0800444
Craig Tiller71735182015-01-15 17:07:13 -0800445class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800446 """Cache for running tests."""
447
David Klempner25739582015-02-11 15:57:32 -0800448 def __init__(self, use_cache_results):
Craig Tiller71735182015-01-15 17:07:13 -0800449 self._last_successful_run = {}
David Klempner25739582015-02-11 15:57:32 -0800450 self._use_cache_results = use_cache_results
Craig Tiller71735182015-01-15 17:07:13 -0800451
452 def should_run(self, cmdline, bin_hash):
Craig Tiller71735182015-01-15 17:07:13 -0800453 if cmdline not in self._last_successful_run:
454 return True
455 if self._last_successful_run[cmdline] != bin_hash:
456 return True
David Klempner25739582015-02-11 15:57:32 -0800457 if not self._use_cache_results:
458 return True
Craig Tiller71735182015-01-15 17:07:13 -0800459 return False
460
461 def finished(self, cmdline, bin_hash):
Craig Tiller547db2b2015-01-30 14:08:39 -0800462 self._last_successful_run[cmdline] = bin_hash
Craig Tillerc1f11622015-02-25 09:09:59 -0800463 self.save()
Craig Tiller71735182015-01-15 17:07:13 -0800464
465 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800466 return [{'cmdline': k, 'hash': v}
467 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800468
469 def parse(self, exdump):
470 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
471
472 def save(self):
473 with open('.run_tests_cache', 'w') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800474 f.write(json.dumps(self.dump()))
Craig Tiller71735182015-01-15 17:07:13 -0800475
Craig Tiller1cc11db2015-01-15 22:50:50 -0800476 def maybe_load(self):
477 if os.path.exists('.run_tests_cache'):
478 with open('.run_tests_cache') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800479 self.parse(json.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800480
481
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100482def _build_and_run(check_cancelled, newline_on_success, travis, cache):
ctiller3040cb72015-01-07 12:13:17 -0800483 """Do one pass of building & running tests."""
murgatroid99666450e2015-01-26 13:03:31 -0800484 # build latest sequentially
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100485 if not jobset.run(build_steps, maxjobs=1,
486 newline_on_success=newline_on_success, travis=travis):
Craig Tillerd86a3942015-01-14 12:48:54 -0800487 return 1
ctiller3040cb72015-01-07 12:13:17 -0800488
Craig Tiller234b6e72015-05-23 10:12:40 -0700489 # start antagonists
490 antagonists = [subprocess.Popen(['tools/run_tests/antagonist.py'])
491 for _ in range(0, args.antagonists)]
492 try:
David Garcia Quintase90cd372015-05-31 18:15:26 -0700493 infinite_runs = runs_per_test == 0
Craig Tiller234b6e72015-05-23 10:12:40 -0700494 # run all the tests
David Garcia Quintase90cd372015-05-31 18:15:26 -0700495 runs_sequence = (itertools.repeat(one_run) if infinite_runs
496 else itertools.repeat(one_run, runs_per_test))
497 all_runs = itertools.chain.from_iterable(runs_sequence)
Craig Tiller234b6e72015-05-23 10:12:40 -0700498 if not jobset.run(all_runs, check_cancelled,
499 newline_on_success=newline_on_success, travis=travis,
David Garcia Quintase90cd372015-05-31 18:15:26 -0700500 infinite_runs=infinite_runs,
Craig Tillerda2220a2015-05-27 07:50:53 -0700501 maxjobs=args.jobs,
Craig Tiller533b1a22015-05-29 08:41:29 -0700502 stop_on_failure=args.stop_on_failure,
Craig Tiller234b6e72015-05-23 10:12:40 -0700503 cache=cache):
504 return 2
505 finally:
506 for antagonist in antagonists:
507 antagonist.kill()
Craig Tillerd86a3942015-01-14 12:48:54 -0800508
509 return 0
ctiller3040cb72015-01-07 12:13:17 -0800510
511
David Klempner25739582015-02-11 15:57:32 -0800512test_cache = TestCache(runs_per_test == 1)
Craig Tiller547db2b2015-01-30 14:08:39 -0800513test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800514
ctiller3040cb72015-01-07 12:13:17 -0800515if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800516 success = True
ctiller3040cb72015-01-07 12:13:17 -0800517 while True:
Craig Tiller42bc87c2015-02-23 08:50:19 -0800518 dw = watch_dirs.DirWatcher(['src', 'include', 'test', 'examples'])
ctiller3040cb72015-01-07 12:13:17 -0800519 initial_time = dw.most_recent_change()
520 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800521 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800522 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800523 newline_on_success=False,
Craig Tiller9a5a9402015-04-16 10:39:50 -0700524 travis=args.travis,
Craig Tiller71735182015-01-15 17:07:13 -0800525 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800526 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800527 jobset.message('SUCCESS',
528 'All tests are now passing properly',
529 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800530 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800531 while not have_files_changed():
532 time.sleep(1)
533else:
Craig Tiller71735182015-01-15 17:07:13 -0800534 result = _build_and_run(check_cancelled=lambda: False,
535 newline_on_success=args.newline_on_success,
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100536 travis=args.travis,
Craig Tiller71735182015-01-15 17:07:13 -0800537 cache=test_cache)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800538 if result == 0:
539 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
540 else:
541 jobset.message('FAILED', 'Some tests failed', do_newline=True)
542 sys.exit(result)