blob: df201c409e3e221883069abb8886f2dfcbe9ce39 [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
David Garcia Quintas79e389f2015-06-02 17:49:42 -070039import platform
40import random
Craig Tillerfe406ec2015-02-24 13:55:12 -080041import re
David Garcia Quintas79e389f2015-06-02 17:49:42 -070042import subprocess
Nicolas Nobleddef2462015-01-06 18:08:25 -080043import sys
ctiller3040cb72015-01-07 12:13:17 -080044import time
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +020045import xml.etree.cElementTree as ET
Nicolas Nobleddef2462015-01-06 18:08:25 -080046
47import jobset
ctiller3040cb72015-01-07 12:13:17 -080048import watch_dirs
Nicolas Nobleddef2462015-01-06 18:08:25 -080049
Craig Tiller2cc2b842015-02-27 11:38:31 -080050ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
51os.chdir(ROOT)
52
53
Craig Tiller06805272015-06-11 14:46:47 -070054_FORCE_ENVIRON_FOR_WRAPPERS = {}
55
56
Craig Tillerd50993d2015-08-05 08:04:36 -070057def platform_string():
58 if platform.system() == 'Windows':
59 return 'windows'
60 elif platform.system() == 'Darwin':
61 return 'mac'
62 elif platform.system() == 'Linux':
63 return 'linux'
64 else:
65 return 'posix'
66
67
Craig Tiller738c3342015-01-12 14:28:33 -080068# SimpleConfig: just compile with CONFIG=config, and run the binary to test
69class SimpleConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080070
murgatroid99132ce6a2015-03-04 17:29:14 -080071 def __init__(self, config, environ=None):
72 if environ is None:
73 environ = {}
Craig Tiller738c3342015-01-12 14:28:33 -080074 self.build_config = config
Craig Tillerc7449162015-01-16 14:42:10 -080075 self.allow_hashing = (config != 'gcov')
Craig Tiller547db2b2015-01-30 14:08:39 -080076 self.environ = environ
murgatroid99132ce6a2015-03-04 17:29:14 -080077 self.environ['CONFIG'] = config
Craig Tiller738c3342015-01-12 14:28:33 -080078
Craig Tiller4fc90032015-05-21 10:39:52 -070079 def job_spec(self, cmdline, hash_targets, shortname=None, environ={}):
Craig Tiller49f61322015-03-03 13:02:11 -080080 """Construct a jobset.JobSpec for a test under this config
81
82 Args:
83 cmdline: a list of strings specifying the command line the test
84 would like to run
85 hash_targets: either None (don't do caching of test results), or
86 a list of strings specifying files to include in a
87 binary hash to check if a test has changed
88 -- if used, all artifacts needed to run the test must
89 be listed
90 """
Craig Tiller4fc90032015-05-21 10:39:52 -070091 actual_environ = self.environ.copy()
92 for k, v in environ.iteritems():
93 actual_environ[k] = v
Craig Tiller49f61322015-03-03 13:02:11 -080094 return jobset.JobSpec(cmdline=cmdline,
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -070095 shortname=shortname,
Craig Tiller4fc90032015-05-21 10:39:52 -070096 environ=actual_environ,
Craig Tiller547db2b2015-01-30 14:08:39 -080097 hash_targets=hash_targets
98 if self.allow_hashing else None)
Craig Tiller738c3342015-01-12 14:28:33 -080099
100
101# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
102class ValgrindConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800103
murgatroid99132ce6a2015-03-04 17:29:14 -0800104 def __init__(self, config, tool, args=None):
105 if args is None:
106 args = []
Craig Tiller738c3342015-01-12 14:28:33 -0800107 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -0800108 self.tool = tool
Craig Tiller1a305b12015-02-18 13:37:06 -0800109 self.args = args
Craig Tillerc7449162015-01-16 14:42:10 -0800110 self.allow_hashing = False
Craig Tiller738c3342015-01-12 14:28:33 -0800111
Craig Tiller49f61322015-03-03 13:02:11 -0800112 def job_spec(self, cmdline, hash_targets):
Craig Tiller1a305b12015-02-18 13:37:06 -0800113 return jobset.JobSpec(cmdline=['valgrind', '--tool=%s' % self.tool] +
Craig Tiller49f61322015-03-03 13:02:11 -0800114 self.args + cmdline,
Craig Tiller71ec6cb2015-06-03 00:51:11 -0700115 shortname='valgrind %s' % cmdline[0],
Craig Tiller1a305b12015-02-18 13:37:06 -0800116 hash_targets=None)
Craig Tiller738c3342015-01-12 14:28:33 -0800117
118
Craig Tillerc7449162015-01-16 14:42:10 -0800119class CLanguage(object):
120
Craig Tillere9c959d2015-01-18 10:23:26 -0800121 def __init__(self, make_target, test_lang):
Craig Tillerc7449162015-01-16 14:42:10 -0800122 self.make_target = make_target
Craig Tillerd50993d2015-08-05 08:04:36 -0700123 self.platform = platform_string()
Craig Tillere9c959d2015-01-18 10:23:26 -0800124 with open('tools/run_tests/tests.json') as f:
Craig Tiller06b4ff22015-01-18 11:01:25 -0800125 js = json.load(f)
Craig Tillerd625d812015-04-08 15:52:35 -0700126 self.binaries = [tgt
127 for tgt in js
128 if tgt['language'] == test_lang and
129 plat in tgt['platforms']]
Craig Tillerc7449162015-01-16 14:42:10 -0800130
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100131 def test_specs(self, config, travis):
Craig Tiller547db2b2015-01-30 14:08:39 -0800132 out = []
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100133 for target in self.binaries:
134 if travis and target['flaky']:
135 continue
Nicolas Noblee1445362015-05-11 17:40:26 -0700136 if self.platform == 'windows':
Jan Tattermusch12e8a042015-06-15 17:07:14 -0700137 binary = 'vsprojects/test_bin/%s.exe' % (target['name'])
Nicolas Noblee1445362015-05-11 17:40:26 -0700138 else:
139 binary = 'bins/%s/%s' % (config.build_config, target['name'])
Craig Tiller49f61322015-03-03 13:02:11 -0800140 out.append(config.job_spec([binary], [binary]))
Nicolas Noblee1445362015-05-11 17:40:26 -0700141 return sorted(out)
Craig Tillerc7449162015-01-16 14:42:10 -0800142
143 def make_targets(self):
Craig Tiller7552f0f2015-06-19 17:46:20 -0700144 return ['buildtests_%s' % self.make_target, 'tools_%s' % self.make_target]
Craig Tillerc7449162015-01-16 14:42:10 -0800145
146 def build_steps(self):
147 return []
148
murgatroid99132ce6a2015-03-04 17:29:14 -0800149 def supports_multi_config(self):
150 return True
151
152 def __str__(self):
153 return self.make_target
154
Craig Tiller99775822015-01-30 13:07:16 -0800155
murgatroid992c8d5162015-01-26 10:41:21 -0800156class NodeLanguage(object):
157
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100158 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700159 return [config.job_spec(['tools/run_tests/run_node.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700160 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid992c8d5162015-01-26 10:41:21 -0800161
162 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700163 return ['static_c', 'shared_c']
murgatroid992c8d5162015-01-26 10:41:21 -0800164
165 def build_steps(self):
166 return [['tools/run_tests/build_node.sh']]
Craig Tillerc7449162015-01-16 14:42:10 -0800167
murgatroid99132ce6a2015-03-04 17:29:14 -0800168 def supports_multi_config(self):
169 return False
170
171 def __str__(self):
172 return 'node'
173
Craig Tiller99775822015-01-30 13:07:16 -0800174
Craig Tillerc7449162015-01-16 14:42:10 -0800175class PhpLanguage(object):
176
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100177 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700178 return [config.job_spec(['src/php/bin/run_tests.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700179 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
Craig Tillerc7449162015-01-16 14:42:10 -0800180
181 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700182 return ['static_c', 'shared_c']
Craig Tillerc7449162015-01-16 14:42:10 -0800183
184 def build_steps(self):
185 return [['tools/run_tests/build_php.sh']]
186
murgatroid99132ce6a2015-03-04 17:29:14 -0800187 def supports_multi_config(self):
188 return False
189
190 def __str__(self):
191 return 'php'
192
Craig Tillerc7449162015-01-16 14:42:10 -0800193
Nathaniel Manista840615e2015-01-22 20:31:47 +0000194class PythonLanguage(object):
195
Craig Tiller49f61322015-03-03 13:02:11 -0800196 def __init__(self):
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700197 self._build_python_versions = ['2.7']
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700198 self._has_python_versions = []
Craig Tiller49f61322015-03-03 13:02:11 -0800199
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100200 def test_specs(self, config, travis):
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700201 environment = dict(_FORCE_ENVIRON_FOR_WRAPPERS)
202 environment['PYVER'] = '2.7'
203 return [config.job_spec(
204 ['tools/run_tests/run_python.sh'],
205 None,
206 environ=environment,
207 shortname='py.test',
208 )]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000209
210 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700211 return ['static_c', 'grpc_python_plugin', 'shared_c']
Nathaniel Manista840615e2015-01-22 20:31:47 +0000212
213 def build_steps(self):
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700214 commands = []
215 for python_version in self._build_python_versions:
216 try:
217 with open(os.devnull, 'w') as output:
218 subprocess.check_call(['which', 'python' + python_version],
219 stdout=output, stderr=output)
220 commands.append(['tools/run_tests/build_python.sh', python_version])
221 self._has_python_versions.append(python_version)
222 except:
223 jobset.message('WARNING', 'Missing Python ' + python_version,
224 do_newline=True)
225 return commands
Nathaniel Manista840615e2015-01-22 20:31:47 +0000226
murgatroid99132ce6a2015-03-04 17:29:14 -0800227 def supports_multi_config(self):
228 return False
229
230 def __str__(self):
231 return 'python'
232
Craig Tillerd625d812015-04-08 15:52:35 -0700233
murgatroid996a4c4fa2015-02-27 12:08:57 -0800234class RubyLanguage(object):
235
236 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700237 return [config.job_spec(['tools/run_tests/run_ruby.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700238 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid996a4c4fa2015-02-27 12:08:57 -0800239
240 def make_targets(self):
murgatroid99a43c14f2015-07-30 13:31:23 -0700241 return ['static_c']
murgatroid996a4c4fa2015-02-27 12:08:57 -0800242
243 def build_steps(self):
244 return [['tools/run_tests/build_ruby.sh']]
245
murgatroid99132ce6a2015-03-04 17:29:14 -0800246 def supports_multi_config(self):
247 return False
248
249 def __str__(self):
250 return 'ruby'
251
Craig Tillerd625d812015-04-08 15:52:35 -0700252
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800253class CSharpLanguage(object):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700254 def __init__(self):
Craig Tillerd50993d2015-08-05 08:04:36 -0700255 self.platform = platform_string()
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700256
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800257 def test_specs(self, config, travis):
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700258 assemblies = ['Grpc.Core.Tests',
259 'Grpc.Examples.Tests',
260 'Grpc.IntegrationTesting']
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700261 if self.platform == 'windows':
262 cmd = 'tools\\run_tests\\run_csharp.bat'
263 else:
264 cmd = 'tools/run_tests/run_csharp.sh'
265 return [config.job_spec([cmd, assembly],
Craig Tiller4fc90032015-05-21 10:39:52 -0700266 None, shortname=assembly,
Craig Tiller06805272015-06-11 14:46:47 -0700267 environ=_FORCE_ENVIRON_FOR_WRAPPERS)
Craig Tillerd50993d2015-08-05 08:04:36 -0700268 for assembly in assemblies]
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800269
270 def make_targets(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700271 # For Windows, this target doesn't really build anything,
272 # everything is build by buildall script later.
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800273 return ['grpc_csharp_ext']
274
275 def build_steps(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700276 if self.platform == 'windows':
277 return [['src\\csharp\\buildall.bat']]
278 else:
279 return [['tools/run_tests/build_csharp.sh']]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000280
murgatroid99132ce6a2015-03-04 17:29:14 -0800281 def supports_multi_config(self):
282 return False
283
284 def __str__(self):
285 return 'csharp'
286
Craig Tillerd625d812015-04-08 15:52:35 -0700287
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700288class ObjCLanguage(object):
289
290 def test_specs(self, config, travis):
291 return [config.job_spec(['src/objective-c/tests/run_tests.sh'], None,
292 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
293
294 def make_targets(self):
Jorge Canizalesd0b32e92015-07-30 23:08:43 -0700295 return ['grpc_objective_c_plugin', 'interop_server']
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700296
297 def build_steps(self):
Jorge Canizalesd0b32e92015-07-30 23:08:43 -0700298 return [['src/objective-c/tests/build_tests.sh']]
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700299
300 def supports_multi_config(self):
301 return False
302
303 def __str__(self):
304 return 'objc'
305
306
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100307class Sanity(object):
308
309 def test_specs(self, config, travis):
Craig Tillerf75fc122015-06-25 06:58:00 -0700310 return [config.job_spec('tools/run_tests/run_sanity.sh', None),
311 config.job_spec('tools/run_tests/check_sources_and_headers.py', None)]
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100312
313 def make_targets(self):
314 return ['run_dep_checks']
315
316 def build_steps(self):
317 return []
318
319 def supports_multi_config(self):
320 return False
321
322 def __str__(self):
323 return 'sanity'
324
Nicolas "Pixel" Noblee55cd7f2015-04-14 17:59:13 +0200325
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100326class Build(object):
327
328 def test_specs(self, config, travis):
329 return []
330
331 def make_targets(self):
Nicolas "Pixel" Noblec23827b2015-04-23 06:17:55 +0200332 return ['static']
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100333
334 def build_steps(self):
335 return []
336
337 def supports_multi_config(self):
338 return True
339
340 def __str__(self):
341 return self.make_target
342
343
Craig Tiller738c3342015-01-12 14:28:33 -0800344# different configurations we can run under
345_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -0800346 'dbg': SimpleConfig('dbg'),
347 'opt': SimpleConfig('opt'),
David Klempner1d0302d2015-02-04 16:08:01 -0800348 'tsan': SimpleConfig('tsan', environ={
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700349 'TSAN_OPTIONS': 'suppressions=tools/tsan_suppressions.txt:halt_on_error=1:second_deadlock_stack=1'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800350 'msan': SimpleConfig('msan'),
Craig Tiller96bd5f62015-02-13 09:04:13 -0800351 'ubsan': SimpleConfig('ubsan'),
Craig Tiller547db2b2015-01-30 14:08:39 -0800352 'asan': SimpleConfig('asan', environ={
Craig Tillerd4b13622015-05-29 09:10:10 -0700353 'ASAN_OPTIONS': 'detect_leaks=1:color=always:suppressions=tools/tsan_suppressions.txt',
354 'LSAN_OPTIONS': 'report_objects=1'}),
Craig Tiller810725c2015-05-12 09:44:41 -0700355 'asan-noleaks': SimpleConfig('asan', environ={
356 'ASAN_OPTIONS': 'detect_leaks=0:color=always:suppressions=tools/tsan_suppressions.txt'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800357 'gcov': SimpleConfig('gcov'),
Craig Tiller1a305b12015-02-18 13:37:06 -0800358 'memcheck': ValgrindConfig('valgrind', 'memcheck', ['--leak-check=full']),
Craig Tillerb50d1662015-01-15 17:28:21 -0800359 'helgrind': ValgrindConfig('dbg', 'helgrind')
360 }
Craig Tiller738c3342015-01-12 14:28:33 -0800361
362
Nicolas "Pixel" Noble1fb5e822015-03-16 06:20:37 +0100363_DEFAULT = ['opt']
Craig Tillerc7449162015-01-16 14:42:10 -0800364_LANGUAGES = {
Craig Tillere9c959d2015-01-18 10:23:26 -0800365 'c++': CLanguage('cxx', 'c++'),
366 'c': CLanguage('c', 'c'),
murgatroid992c8d5162015-01-26 10:41:21 -0800367 'node': NodeLanguage(),
Nathaniel Manista840615e2015-01-22 20:31:47 +0000368 'php': PhpLanguage(),
369 'python': PythonLanguage(),
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800370 'ruby': RubyLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100371 'csharp': CSharpLanguage(),
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700372 'objc' : ObjCLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100373 'sanity': Sanity(),
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100374 'build': Build(),
Craig Tillereb272bc2015-01-30 13:13:14 -0800375 }
Nicolas Nobleddef2462015-01-06 18:08:25 -0800376
377# parse command line
378argp = argparse.ArgumentParser(description='Run grpc tests.')
379argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -0800380 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -0800381 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -0800382 default=_DEFAULT)
David Garcia Quintase90cd372015-05-31 18:15:26 -0700383
384def runs_per_test_type(arg_str):
385 """Auxilary function to parse the "runs_per_test" flag.
386
387 Returns:
388 A positive integer or 0, the latter indicating an infinite number of
389 runs.
390
391 Raises:
392 argparse.ArgumentTypeError: Upon invalid input.
393 """
394 if arg_str == 'inf':
395 return 0
396 try:
397 n = int(arg_str)
398 if n <= 0: raise ValueError
Craig Tiller50e53e22015-06-01 20:18:21 -0700399 return n
David Garcia Quintase90cd372015-05-31 18:15:26 -0700400 except:
401 msg = "'{}' isn't a positive integer or 'inf'".format(arg_str)
402 raise argparse.ArgumentTypeError(msg)
403argp.add_argument('-n', '--runs_per_test', default=1, type=runs_per_test_type,
404 help='A positive integer or "inf". If "inf", all tests will run in an '
405 'infinite loop. Especially useful in combination with "-f"')
Craig Tillerfe406ec2015-02-24 13:55:12 -0800406argp.add_argument('-r', '--regex', default='.*', type=str)
Craig Tiller83762ac2015-05-22 14:04:06 -0700407argp.add_argument('-j', '--jobs', default=2 * multiprocessing.cpu_count(), type=int)
Craig Tiller8451e872015-02-27 09:25:51 -0800408argp.add_argument('-s', '--slowdown', default=1.0, type=float)
ctiller3040cb72015-01-07 12:13:17 -0800409argp.add_argument('-f', '--forever',
410 default=False,
411 action='store_const',
412 const=True)
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100413argp.add_argument('-t', '--travis',
414 default=False,
415 action='store_const',
416 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800417argp.add_argument('--newline_on_success',
418 default=False,
419 action='store_const',
420 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -0800421argp.add_argument('-l', '--language',
Craig Tiller60f15e62015-05-13 09:05:17 -0700422 choices=['all'] + sorted(_LANGUAGES.keys()),
Craig Tiller686fb262015-01-15 07:39:09 -0800423 nargs='+',
Craig Tiller60f15e62015-05-13 09:05:17 -0700424 default=['all'])
Craig Tillercd43da82015-05-29 08:41:29 -0700425argp.add_argument('-S', '--stop_on_failure',
426 default=False,
427 action='store_const',
428 const=True)
Craig Tiller234b6e72015-05-23 10:12:40 -0700429argp.add_argument('-a', '--antagonists', default=0, type=int)
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200430argp.add_argument('-x', '--xml_report', default=None, type=str,
431 help='Generates a JUnit-compatible XML report')
Nicolas Nobleddef2462015-01-06 18:08:25 -0800432args = argp.parse_args()
433
434# grab config
Craig Tiller738c3342015-01-12 14:28:33 -0800435run_configs = set(_CONFIGS[cfg]
436 for cfg in itertools.chain.from_iterable(
437 _CONFIGS.iterkeys() if x == 'all' else [x]
438 for x in args.config))
439build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tillerf1973b02015-01-16 12:32:13 -0800440
Craig Tiller06805272015-06-11 14:46:47 -0700441if args.travis:
442 _FORCE_ENVIRON_FOR_WRAPPERS = {'GRPC_TRACE': 'surface,batch'}
443
Craig Tillerc7449162015-01-16 14:42:10 -0800444make_targets = []
Craig Tiller60f15e62015-05-13 09:05:17 -0700445languages = set(_LANGUAGES[l]
446 for l in itertools.chain.from_iterable(
447 _LANGUAGES.iterkeys() if x == 'all' else [x]
448 for x in args.language))
murgatroid99132ce6a2015-03-04 17:29:14 -0800449
450if len(build_configs) > 1:
451 for language in languages:
452 if not language.supports_multi_config():
453 print language, 'does not support multiple build configurations'
454 sys.exit(1)
455
Craig Tiller5058c692015-04-08 09:42:04 -0700456if platform.system() == 'Windows':
457 def make_jobspec(cfg, targets):
Jan Tattermusche8243592015-04-17 14:14:01 -0700458 return jobset.JobSpec(['make.bat', 'CONFIG=%s' % cfg] + targets,
459 cwd='vsprojects', shell=True)
Craig Tiller5058c692015-04-08 09:42:04 -0700460else:
461 def make_jobspec(cfg, targets):
Nicolas "Pixel" Noble4243ca82015-07-23 23:47:56 +0200462 return jobset.JobSpec([os.getenv('MAKE', 'make'),
Craig Tiller5058c692015-04-08 09:42:04 -0700463 '-j', '%d' % (multiprocessing.cpu_count() + 1),
Craig Tiller533b1a22015-05-29 08:41:29 -0700464 'EXTRA_DEFINES=GRPC_TEST_SLOWDOWN_MACHINE_FACTOR=%f' %
Craig Tiller5058c692015-04-08 09:42:04 -0700465 args.slowdown,
466 'CONFIG=%s' % cfg] + targets)
467
Craig Tiller533b1a22015-05-29 08:41:29 -0700468build_steps = [make_jobspec(cfg,
Craig Tiller5058c692015-04-08 09:42:04 -0700469 list(set(itertools.chain.from_iterable(
470 l.make_targets() for l in languages))))
471 for cfg in build_configs]
472build_steps.extend(set(
murgatroid99132ce6a2015-03-04 17:29:14 -0800473 jobset.JobSpec(cmdline, environ={'CONFIG': cfg})
474 for cfg in build_configs
Craig Tiller547db2b2015-01-30 14:08:39 -0800475 for l in languages
Craig Tiller533b1a22015-05-29 08:41:29 -0700476 for cmdline in l.build_steps()))
Craig Tiller547db2b2015-01-30 14:08:39 -0800477one_run = set(
478 spec
479 for config in run_configs
Craig Tiller60f15e62015-05-13 09:05:17 -0700480 for language in languages
481 for spec in language.test_specs(config, args.travis)
Craig Tillerfe406ec2015-02-24 13:55:12 -0800482 if re.search(args.regex, spec.shortname))
Craig Tillerf1973b02015-01-16 12:32:13 -0800483
Nicolas Nobleddef2462015-01-06 18:08:25 -0800484runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -0800485forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -0800486
Nicolas Nobleddef2462015-01-06 18:08:25 -0800487
Craig Tiller71735182015-01-15 17:07:13 -0800488class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800489 """Cache for running tests."""
490
David Klempner25739582015-02-11 15:57:32 -0800491 def __init__(self, use_cache_results):
Craig Tiller71735182015-01-15 17:07:13 -0800492 self._last_successful_run = {}
David Klempner25739582015-02-11 15:57:32 -0800493 self._use_cache_results = use_cache_results
Craig Tiller69cd2372015-06-11 09:38:09 -0700494 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800495
496 def should_run(self, cmdline, bin_hash):
Craig Tiller71735182015-01-15 17:07:13 -0800497 if cmdline not in self._last_successful_run:
498 return True
499 if self._last_successful_run[cmdline] != bin_hash:
500 return True
David Klempner25739582015-02-11 15:57:32 -0800501 if not self._use_cache_results:
502 return True
Craig Tiller71735182015-01-15 17:07:13 -0800503 return False
504
505 def finished(self, cmdline, bin_hash):
Craig Tiller547db2b2015-01-30 14:08:39 -0800506 self._last_successful_run[cmdline] = bin_hash
Craig Tiller69cd2372015-06-11 09:38:09 -0700507 if time.time() - self._last_save > 1:
508 self.save()
Craig Tiller71735182015-01-15 17:07:13 -0800509
510 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800511 return [{'cmdline': k, 'hash': v}
512 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800513
514 def parse(self, exdump):
515 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
516
517 def save(self):
518 with open('.run_tests_cache', 'w') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800519 f.write(json.dumps(self.dump()))
Craig Tiller69cd2372015-06-11 09:38:09 -0700520 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800521
Craig Tiller1cc11db2015-01-15 22:50:50 -0800522 def maybe_load(self):
523 if os.path.exists('.run_tests_cache'):
524 with open('.run_tests_cache') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800525 self.parse(json.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800526
527
Craig Tillerbb309712015-06-28 16:04:47 -0700528def _build_and_run(check_cancelled, newline_on_success, travis, cache, xml_report=None):
ctiller3040cb72015-01-07 12:13:17 -0800529 """Do one pass of building & running tests."""
murgatroid99666450e2015-01-26 13:03:31 -0800530 # build latest sequentially
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100531 if not jobset.run(build_steps, maxjobs=1,
532 newline_on_success=newline_on_success, travis=travis):
Craig Tillerd86a3942015-01-14 12:48:54 -0800533 return 1
ctiller3040cb72015-01-07 12:13:17 -0800534
Craig Tiller234b6e72015-05-23 10:12:40 -0700535 # start antagonists
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700536 antagonists = [subprocess.Popen(['tools/run_tests/antagonist.py'])
Craig Tiller234b6e72015-05-23 10:12:40 -0700537 for _ in range(0, args.antagonists)]
538 try:
David Garcia Quintase90cd372015-05-31 18:15:26 -0700539 infinite_runs = runs_per_test == 0
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700540 # When running on travis, we want out test runs to be as similar as possible
541 # for reproducibility purposes.
542 if travis:
543 massaged_one_run = sorted(one_run, key=lambda x: x.shortname)
544 else:
545 # whereas otherwise, we want to shuffle things up to give all tests a
546 # chance to run.
547 massaged_one_run = list(one_run) # random.shuffle needs an indexable seq.
548 random.shuffle(massaged_one_run) # which it modifies in-place.
Craig Tillerf7b7c892015-06-22 14:33:25 -0700549 if infinite_runs:
550 assert len(massaged_one_run) > 0, 'Must have at least one test for a -n inf run'
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700551 runs_sequence = (itertools.repeat(massaged_one_run) if infinite_runs
552 else itertools.repeat(massaged_one_run, runs_per_test))
David Garcia Quintase90cd372015-05-31 18:15:26 -0700553 all_runs = itertools.chain.from_iterable(runs_sequence)
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200554
555 root = ET.Element('testsuites') if xml_report else None
556 testsuite = ET.SubElement(root, 'testsuite', id='1', package='grpc', name='tests') if xml_report else None
557
Craig Tiller234b6e72015-05-23 10:12:40 -0700558 if not jobset.run(all_runs, check_cancelled,
559 newline_on_success=newline_on_success, travis=travis,
David Garcia Quintase90cd372015-05-31 18:15:26 -0700560 infinite_runs=infinite_runs,
Craig Tillerda2220a2015-05-27 07:50:53 -0700561 maxjobs=args.jobs,
Craig Tillercd43da82015-05-29 08:41:29 -0700562 stop_on_failure=args.stop_on_failure,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200563 cache=cache if not xml_report else None,
564 xml_report=testsuite):
Craig Tiller234b6e72015-05-23 10:12:40 -0700565 return 2
566 finally:
567 for antagonist in antagonists:
568 antagonist.kill()
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200569 if xml_report:
570 tree = ET.ElementTree(root)
571 tree.write(xml_report, encoding='UTF-8')
Craig Tillerd86a3942015-01-14 12:48:54 -0800572
Craig Tiller69cd2372015-06-11 09:38:09 -0700573 if cache: cache.save()
574
Craig Tillerd86a3942015-01-14 12:48:54 -0800575 return 0
ctiller3040cb72015-01-07 12:13:17 -0800576
577
David Klempner25739582015-02-11 15:57:32 -0800578test_cache = TestCache(runs_per_test == 1)
Craig Tiller547db2b2015-01-30 14:08:39 -0800579test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800580
ctiller3040cb72015-01-07 12:13:17 -0800581if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800582 success = True
ctiller3040cb72015-01-07 12:13:17 -0800583 while True:
Craig Tiller42bc87c2015-02-23 08:50:19 -0800584 dw = watch_dirs.DirWatcher(['src', 'include', 'test', 'examples'])
ctiller3040cb72015-01-07 12:13:17 -0800585 initial_time = dw.most_recent_change()
586 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800587 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800588 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800589 newline_on_success=False,
Craig Tiller9a5a9402015-04-16 10:39:50 -0700590 travis=args.travis,
Craig Tiller71735182015-01-15 17:07:13 -0800591 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800592 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800593 jobset.message('SUCCESS',
594 'All tests are now passing properly',
595 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800596 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800597 while not have_files_changed():
598 time.sleep(1)
599else:
Craig Tiller71735182015-01-15 17:07:13 -0800600 result = _build_and_run(check_cancelled=lambda: False,
601 newline_on_success=args.newline_on_success,
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100602 travis=args.travis,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200603 cache=test_cache,
604 xml_report=args.xml_report)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800605 if result == 0:
606 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
607 else:
608 jobset.message('FAILED', 'Some tests failed', do_newline=True)
609 sys.exit(result)