blob: 0f02b4738b6a7b676df839e2aaeb72868c042927 [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 Tiller738c3342015-01-12 14:28:33 -080057# SimpleConfig: just compile with CONFIG=config, and run the binary to test
58class SimpleConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080059
murgatroid99132ce6a2015-03-04 17:29:14 -080060 def __init__(self, config, environ=None):
61 if environ is None:
62 environ = {}
Craig Tiller738c3342015-01-12 14:28:33 -080063 self.build_config = config
Craig Tillerc7449162015-01-16 14:42:10 -080064 self.allow_hashing = (config != 'gcov')
Craig Tiller547db2b2015-01-30 14:08:39 -080065 self.environ = environ
murgatroid99132ce6a2015-03-04 17:29:14 -080066 self.environ['CONFIG'] = config
Craig Tiller738c3342015-01-12 14:28:33 -080067
Craig Tiller4fc90032015-05-21 10:39:52 -070068 def job_spec(self, cmdline, hash_targets, shortname=None, environ={}):
Craig Tiller49f61322015-03-03 13:02:11 -080069 """Construct a jobset.JobSpec for a test under this config
70
71 Args:
72 cmdline: a list of strings specifying the command line the test
73 would like to run
74 hash_targets: either None (don't do caching of test results), or
75 a list of strings specifying files to include in a
76 binary hash to check if a test has changed
77 -- if used, all artifacts needed to run the test must
78 be listed
79 """
Craig Tiller4fc90032015-05-21 10:39:52 -070080 actual_environ = self.environ.copy()
81 for k, v in environ.iteritems():
82 actual_environ[k] = v
Craig Tiller49f61322015-03-03 13:02:11 -080083 return jobset.JobSpec(cmdline=cmdline,
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -070084 shortname=shortname,
Craig Tiller4fc90032015-05-21 10:39:52 -070085 environ=actual_environ,
Craig Tiller547db2b2015-01-30 14:08:39 -080086 hash_targets=hash_targets
87 if self.allow_hashing else None)
Craig Tiller738c3342015-01-12 14:28:33 -080088
89
90# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
91class ValgrindConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080092
murgatroid99132ce6a2015-03-04 17:29:14 -080093 def __init__(self, config, tool, args=None):
94 if args is None:
95 args = []
Craig Tiller738c3342015-01-12 14:28:33 -080096 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080097 self.tool = tool
Craig Tiller1a305b12015-02-18 13:37:06 -080098 self.args = args
Craig Tillerc7449162015-01-16 14:42:10 -080099 self.allow_hashing = False
Craig Tiller738c3342015-01-12 14:28:33 -0800100
Craig Tiller49f61322015-03-03 13:02:11 -0800101 def job_spec(self, cmdline, hash_targets):
Craig Tiller1a305b12015-02-18 13:37:06 -0800102 return jobset.JobSpec(cmdline=['valgrind', '--tool=%s' % self.tool] +
Craig Tiller49f61322015-03-03 13:02:11 -0800103 self.args + cmdline,
Craig Tiller71ec6cb2015-06-03 00:51:11 -0700104 shortname='valgrind %s' % cmdline[0],
Craig Tiller1a305b12015-02-18 13:37:06 -0800105 hash_targets=None)
Craig Tiller738c3342015-01-12 14:28:33 -0800106
107
Craig Tillerc7449162015-01-16 14:42:10 -0800108class CLanguage(object):
109
Craig Tillere9c959d2015-01-18 10:23:26 -0800110 def __init__(self, make_target, test_lang):
Craig Tillerc7449162015-01-16 14:42:10 -0800111 self.make_target = make_target
Craig Tillerd625d812015-04-08 15:52:35 -0700112 if platform.system() == 'Windows':
113 plat = 'windows'
114 else:
115 plat = 'posix'
Nicolas Noblee1445362015-05-11 17:40:26 -0700116 self.platform = plat
Craig Tillere9c959d2015-01-18 10:23:26 -0800117 with open('tools/run_tests/tests.json') as f:
Craig Tiller06b4ff22015-01-18 11:01:25 -0800118 js = json.load(f)
Craig Tillerd625d812015-04-08 15:52:35 -0700119 self.binaries = [tgt
120 for tgt in js
121 if tgt['language'] == test_lang and
122 plat in tgt['platforms']]
Craig Tillerc7449162015-01-16 14:42:10 -0800123
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100124 def test_specs(self, config, travis):
Craig Tiller547db2b2015-01-30 14:08:39 -0800125 out = []
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100126 for target in self.binaries:
127 if travis and target['flaky']:
128 continue
Nicolas Noblee1445362015-05-11 17:40:26 -0700129 if self.platform == 'windows':
Jan Tattermusch12e8a042015-06-15 17:07:14 -0700130 binary = 'vsprojects/test_bin/%s.exe' % (target['name'])
Nicolas Noblee1445362015-05-11 17:40:26 -0700131 else:
132 binary = 'bins/%s/%s' % (config.build_config, target['name'])
Craig Tiller49f61322015-03-03 13:02:11 -0800133 out.append(config.job_spec([binary], [binary]))
Nicolas Noblee1445362015-05-11 17:40:26 -0700134 return sorted(out)
Craig Tillerc7449162015-01-16 14:42:10 -0800135
136 def make_targets(self):
Craig Tiller7552f0f2015-06-19 17:46:20 -0700137 return ['buildtests_%s' % self.make_target, 'tools_%s' % self.make_target]
Craig Tillerc7449162015-01-16 14:42:10 -0800138
139 def build_steps(self):
140 return []
141
murgatroid99132ce6a2015-03-04 17:29:14 -0800142 def supports_multi_config(self):
143 return True
144
145 def __str__(self):
146 return self.make_target
147
Craig Tiller99775822015-01-30 13:07:16 -0800148
murgatroid992c8d5162015-01-26 10:41:21 -0800149class NodeLanguage(object):
150
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100151 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700152 return [config.job_spec(['tools/run_tests/run_node.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700153 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid992c8d5162015-01-26 10:41:21 -0800154
155 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700156 return ['static_c', 'shared_c']
murgatroid992c8d5162015-01-26 10:41:21 -0800157
158 def build_steps(self):
159 return [['tools/run_tests/build_node.sh']]
Craig Tillerc7449162015-01-16 14:42:10 -0800160
murgatroid99132ce6a2015-03-04 17:29:14 -0800161 def supports_multi_config(self):
162 return False
163
164 def __str__(self):
165 return 'node'
166
Craig Tiller99775822015-01-30 13:07:16 -0800167
Craig Tillerc7449162015-01-16 14:42:10 -0800168class PhpLanguage(object):
169
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100170 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700171 return [config.job_spec(['src/php/bin/run_tests.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700172 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
Craig Tillerc7449162015-01-16 14:42:10 -0800173
174 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700175 return ['static_c', 'shared_c']
Craig Tillerc7449162015-01-16 14:42:10 -0800176
177 def build_steps(self):
178 return [['tools/run_tests/build_php.sh']]
179
murgatroid99132ce6a2015-03-04 17:29:14 -0800180 def supports_multi_config(self):
181 return False
182
183 def __str__(self):
184 return 'php'
185
Craig Tillerc7449162015-01-16 14:42:10 -0800186
Nathaniel Manista840615e2015-01-22 20:31:47 +0000187class PythonLanguage(object):
188
Craig Tiller49f61322015-03-03 13:02:11 -0800189 def __init__(self):
190 with open('tools/run_tests/python_tests.json') as f:
191 self._tests = json.load(f)
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700192 self._build_python_versions = set([
193 python_version
194 for test in self._tests
195 for python_version in test['pythonVersions']])
196 self._has_python_versions = []
Craig Tiller49f61322015-03-03 13:02:11 -0800197
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100198 def test_specs(self, config, travis):
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700199 job_specifications = []
200 for test in self._tests:
201 command = None
202 short_name = None
203 if 'module' in test:
204 command = ['tools/run_tests/run_python.sh', '-m', test['module']]
205 short_name = test['module']
206 elif 'file' in test:
207 command = ['tools/run_tests/run_python.sh', test['file']]
208 short_name = test['file']
209 else:
210 raise ValueError('expected input to be a module or file to run '
211 'unittests from')
212 for python_version in test['pythonVersions']:
213 if python_version in self._has_python_versions:
214 environment = dict(_FORCE_ENVIRON_FOR_WRAPPERS)
215 environment['PYVER'] = python_version
216 job_specifications.append(config.job_spec(
217 command, None, environ=environment, shortname=short_name))
218 else:
219 jobset.message(
220 'WARNING',
221 'Could not find Python {}; skipping test'.format(python_version),
222 '{}\n'.format(command), do_newline=True)
223 return job_specifications
Nathaniel Manista840615e2015-01-22 20:31:47 +0000224
225 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700226 return ['static_c', 'grpc_python_plugin', 'shared_c']
Nathaniel Manista840615e2015-01-22 20:31:47 +0000227
228 def build_steps(self):
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700229 commands = []
230 for python_version in self._build_python_versions:
231 try:
232 with open(os.devnull, 'w') as output:
233 subprocess.check_call(['which', 'python' + python_version],
234 stdout=output, stderr=output)
235 commands.append(['tools/run_tests/build_python.sh', python_version])
236 self._has_python_versions.append(python_version)
237 except:
238 jobset.message('WARNING', 'Missing Python ' + python_version,
239 do_newline=True)
240 return commands
Nathaniel Manista840615e2015-01-22 20:31:47 +0000241
murgatroid99132ce6a2015-03-04 17:29:14 -0800242 def supports_multi_config(self):
243 return False
244
245 def __str__(self):
246 return 'python'
247
Craig Tillerd625d812015-04-08 15:52:35 -0700248
murgatroid996a4c4fa2015-02-27 12:08:57 -0800249class RubyLanguage(object):
250
251 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700252 return [config.job_spec(['tools/run_tests/run_ruby.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700253 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid996a4c4fa2015-02-27 12:08:57 -0800254
255 def make_targets(self):
Nicolas "Pixel" Noblecbd9c8b2015-05-14 06:22:26 +0200256 return ['run_dep_checks']
murgatroid996a4c4fa2015-02-27 12:08:57 -0800257
258 def build_steps(self):
259 return [['tools/run_tests/build_ruby.sh']]
260
murgatroid99132ce6a2015-03-04 17:29:14 -0800261 def supports_multi_config(self):
262 return False
263
264 def __str__(self):
265 return 'ruby'
266
Craig Tillerd625d812015-04-08 15:52:35 -0700267
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800268class CSharpLanguage(object):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700269 def __init__(self):
270 if platform.system() == 'Windows':
271 plat = 'windows'
272 else:
273 plat = 'posix'
274 self.platform = plat
275
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800276 def test_specs(self, config, travis):
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700277 assemblies = ['Grpc.Core.Tests',
278 'Grpc.Examples.Tests',
279 'Grpc.IntegrationTesting']
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700280 if self.platform == 'windows':
281 cmd = 'tools\\run_tests\\run_csharp.bat'
282 else:
283 cmd = 'tools/run_tests/run_csharp.sh'
284 return [config.job_spec([cmd, assembly],
Craig Tiller4fc90032015-05-21 10:39:52 -0700285 None, shortname=assembly,
Craig Tiller06805272015-06-11 14:46:47 -0700286 environ=_FORCE_ENVIRON_FOR_WRAPPERS)
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700287 for assembly in assemblies ]
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800288
289 def make_targets(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700290 # For Windows, this target doesn't really build anything,
291 # everything is build by buildall script later.
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800292 return ['grpc_csharp_ext']
293
294 def build_steps(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700295 if self.platform == 'windows':
296 return [['src\\csharp\\buildall.bat']]
297 else:
298 return [['tools/run_tests/build_csharp.sh']]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000299
murgatroid99132ce6a2015-03-04 17:29:14 -0800300 def supports_multi_config(self):
301 return False
302
303 def __str__(self):
304 return 'csharp'
305
Craig Tillerd625d812015-04-08 15:52:35 -0700306
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700307class ObjCLanguage(object):
308
309 def test_specs(self, config, travis):
310 return [config.job_spec(['src/objective-c/tests/run_tests.sh'], None,
311 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
312
313 def make_targets(self):
314 return []
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 'objc'
324
325
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100326class Sanity(object):
327
328 def test_specs(self, config, travis):
Craig Tillerf75fc122015-06-25 06:58:00 -0700329 return [config.job_spec('tools/run_tests/run_sanity.sh', None),
330 config.job_spec('tools/run_tests/check_sources_and_headers.py', None)]
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100331
332 def make_targets(self):
333 return ['run_dep_checks']
334
335 def build_steps(self):
336 return []
337
338 def supports_multi_config(self):
339 return False
340
341 def __str__(self):
342 return 'sanity'
343
Nicolas "Pixel" Noblee55cd7f2015-04-14 17:59:13 +0200344
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100345class Build(object):
346
347 def test_specs(self, config, travis):
348 return []
349
350 def make_targets(self):
Nicolas "Pixel" Noblec23827b2015-04-23 06:17:55 +0200351 return ['static']
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100352
353 def build_steps(self):
354 return []
355
356 def supports_multi_config(self):
357 return True
358
359 def __str__(self):
360 return self.make_target
361
362
Craig Tiller738c3342015-01-12 14:28:33 -0800363# different configurations we can run under
364_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -0800365 'dbg': SimpleConfig('dbg'),
366 'opt': SimpleConfig('opt'),
David Klempner1d0302d2015-02-04 16:08:01 -0800367 'tsan': SimpleConfig('tsan', environ={
Craig Tiller69cd2372015-06-11 09:38:09 -0700368 'TSAN_OPTIONS': 'suppressions=tools/tsan_suppressions.txt:halt_on_error=1'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800369 'msan': SimpleConfig('msan'),
Craig Tiller96bd5f62015-02-13 09:04:13 -0800370 'ubsan': SimpleConfig('ubsan'),
Craig Tiller547db2b2015-01-30 14:08:39 -0800371 'asan': SimpleConfig('asan', environ={
Craig Tillerd4b13622015-05-29 09:10:10 -0700372 'ASAN_OPTIONS': 'detect_leaks=1:color=always:suppressions=tools/tsan_suppressions.txt',
373 'LSAN_OPTIONS': 'report_objects=1'}),
Craig Tiller810725c2015-05-12 09:44:41 -0700374 'asan-noleaks': SimpleConfig('asan', environ={
375 'ASAN_OPTIONS': 'detect_leaks=0:color=always:suppressions=tools/tsan_suppressions.txt'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800376 'gcov': SimpleConfig('gcov'),
Craig Tiller1a305b12015-02-18 13:37:06 -0800377 'memcheck': ValgrindConfig('valgrind', 'memcheck', ['--leak-check=full']),
Craig Tillerb50d1662015-01-15 17:28:21 -0800378 'helgrind': ValgrindConfig('dbg', 'helgrind')
379 }
Craig Tiller738c3342015-01-12 14:28:33 -0800380
381
Nicolas "Pixel" Noble1fb5e822015-03-16 06:20:37 +0100382_DEFAULT = ['opt']
Craig Tillerc7449162015-01-16 14:42:10 -0800383_LANGUAGES = {
Craig Tillere9c959d2015-01-18 10:23:26 -0800384 'c++': CLanguage('cxx', 'c++'),
385 'c': CLanguage('c', 'c'),
murgatroid992c8d5162015-01-26 10:41:21 -0800386 'node': NodeLanguage(),
Nathaniel Manista840615e2015-01-22 20:31:47 +0000387 'php': PhpLanguage(),
388 'python': PythonLanguage(),
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800389 'ruby': RubyLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100390 'csharp': CSharpLanguage(),
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700391 'objc' : ObjCLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100392 'sanity': Sanity(),
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100393 'build': Build(),
Craig Tillereb272bc2015-01-30 13:13:14 -0800394 }
Nicolas Nobleddef2462015-01-06 18:08:25 -0800395
396# parse command line
397argp = argparse.ArgumentParser(description='Run grpc tests.')
398argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -0800399 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -0800400 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -0800401 default=_DEFAULT)
David Garcia Quintase90cd372015-05-31 18:15:26 -0700402
403def runs_per_test_type(arg_str):
404 """Auxilary function to parse the "runs_per_test" flag.
405
406 Returns:
407 A positive integer or 0, the latter indicating an infinite number of
408 runs.
409
410 Raises:
411 argparse.ArgumentTypeError: Upon invalid input.
412 """
413 if arg_str == 'inf':
414 return 0
415 try:
416 n = int(arg_str)
417 if n <= 0: raise ValueError
Craig Tiller50e53e22015-06-01 20:18:21 -0700418 return n
David Garcia Quintase90cd372015-05-31 18:15:26 -0700419 except:
420 msg = "'{}' isn't a positive integer or 'inf'".format(arg_str)
421 raise argparse.ArgumentTypeError(msg)
422argp.add_argument('-n', '--runs_per_test', default=1, type=runs_per_test_type,
423 help='A positive integer or "inf". If "inf", all tests will run in an '
424 'infinite loop. Especially useful in combination with "-f"')
Craig Tillerfe406ec2015-02-24 13:55:12 -0800425argp.add_argument('-r', '--regex', default='.*', type=str)
Craig Tiller83762ac2015-05-22 14:04:06 -0700426argp.add_argument('-j', '--jobs', default=2 * multiprocessing.cpu_count(), type=int)
Craig Tiller8451e872015-02-27 09:25:51 -0800427argp.add_argument('-s', '--slowdown', default=1.0, type=float)
ctiller3040cb72015-01-07 12:13:17 -0800428argp.add_argument('-f', '--forever',
429 default=False,
430 action='store_const',
431 const=True)
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100432argp.add_argument('-t', '--travis',
433 default=False,
434 action='store_const',
435 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800436argp.add_argument('--newline_on_success',
437 default=False,
438 action='store_const',
439 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -0800440argp.add_argument('-l', '--language',
Craig Tiller60f15e62015-05-13 09:05:17 -0700441 choices=['all'] + sorted(_LANGUAGES.keys()),
Craig Tiller686fb262015-01-15 07:39:09 -0800442 nargs='+',
Craig Tiller60f15e62015-05-13 09:05:17 -0700443 default=['all'])
Craig Tillercd43da82015-05-29 08:41:29 -0700444argp.add_argument('-S', '--stop_on_failure',
445 default=False,
446 action='store_const',
447 const=True)
Craig Tiller234b6e72015-05-23 10:12:40 -0700448argp.add_argument('-a', '--antagonists', default=0, type=int)
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200449argp.add_argument('-x', '--xml_report', default=None, type=str,
450 help='Generates a JUnit-compatible XML report')
Nicolas Nobleddef2462015-01-06 18:08:25 -0800451args = argp.parse_args()
452
453# grab config
Craig Tiller738c3342015-01-12 14:28:33 -0800454run_configs = set(_CONFIGS[cfg]
455 for cfg in itertools.chain.from_iterable(
456 _CONFIGS.iterkeys() if x == 'all' else [x]
457 for x in args.config))
458build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tillerf1973b02015-01-16 12:32:13 -0800459
Craig Tiller06805272015-06-11 14:46:47 -0700460if args.travis:
461 _FORCE_ENVIRON_FOR_WRAPPERS = {'GRPC_TRACE': 'surface,batch'}
462
Craig Tillerc7449162015-01-16 14:42:10 -0800463make_targets = []
Craig Tiller60f15e62015-05-13 09:05:17 -0700464languages = set(_LANGUAGES[l]
465 for l in itertools.chain.from_iterable(
466 _LANGUAGES.iterkeys() if x == 'all' else [x]
467 for x in args.language))
murgatroid99132ce6a2015-03-04 17:29:14 -0800468
469if len(build_configs) > 1:
470 for language in languages:
471 if not language.supports_multi_config():
472 print language, 'does not support multiple build configurations'
473 sys.exit(1)
474
Craig Tiller5058c692015-04-08 09:42:04 -0700475if platform.system() == 'Windows':
476 def make_jobspec(cfg, targets):
Jan Tattermusche8243592015-04-17 14:14:01 -0700477 return jobset.JobSpec(['make.bat', 'CONFIG=%s' % cfg] + targets,
478 cwd='vsprojects', shell=True)
Craig Tiller5058c692015-04-08 09:42:04 -0700479else:
480 def make_jobspec(cfg, targets):
481 return jobset.JobSpec(['make',
482 '-j', '%d' % (multiprocessing.cpu_count() + 1),
Craig Tiller533b1a22015-05-29 08:41:29 -0700483 'EXTRA_DEFINES=GRPC_TEST_SLOWDOWN_MACHINE_FACTOR=%f' %
Craig Tiller5058c692015-04-08 09:42:04 -0700484 args.slowdown,
485 'CONFIG=%s' % cfg] + targets)
486
Craig Tiller533b1a22015-05-29 08:41:29 -0700487build_steps = [make_jobspec(cfg,
Craig Tiller5058c692015-04-08 09:42:04 -0700488 list(set(itertools.chain.from_iterable(
489 l.make_targets() for l in languages))))
490 for cfg in build_configs]
491build_steps.extend(set(
murgatroid99132ce6a2015-03-04 17:29:14 -0800492 jobset.JobSpec(cmdline, environ={'CONFIG': cfg})
493 for cfg in build_configs
Craig Tiller547db2b2015-01-30 14:08:39 -0800494 for l in languages
Craig Tiller533b1a22015-05-29 08:41:29 -0700495 for cmdline in l.build_steps()))
Craig Tiller547db2b2015-01-30 14:08:39 -0800496one_run = set(
497 spec
498 for config in run_configs
Craig Tiller60f15e62015-05-13 09:05:17 -0700499 for language in languages
500 for spec in language.test_specs(config, args.travis)
Craig Tillerfe406ec2015-02-24 13:55:12 -0800501 if re.search(args.regex, spec.shortname))
Craig Tillerf1973b02015-01-16 12:32:13 -0800502
Nicolas Nobleddef2462015-01-06 18:08:25 -0800503runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -0800504forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -0800505
Nicolas Nobleddef2462015-01-06 18:08:25 -0800506
Craig Tiller71735182015-01-15 17:07:13 -0800507class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800508 """Cache for running tests."""
509
David Klempner25739582015-02-11 15:57:32 -0800510 def __init__(self, use_cache_results):
Craig Tiller71735182015-01-15 17:07:13 -0800511 self._last_successful_run = {}
David Klempner25739582015-02-11 15:57:32 -0800512 self._use_cache_results = use_cache_results
Craig Tiller69cd2372015-06-11 09:38:09 -0700513 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800514
515 def should_run(self, cmdline, bin_hash):
Craig Tiller71735182015-01-15 17:07:13 -0800516 if cmdline not in self._last_successful_run:
517 return True
518 if self._last_successful_run[cmdline] != bin_hash:
519 return True
David Klempner25739582015-02-11 15:57:32 -0800520 if not self._use_cache_results:
521 return True
Craig Tiller71735182015-01-15 17:07:13 -0800522 return False
523
524 def finished(self, cmdline, bin_hash):
Craig Tiller547db2b2015-01-30 14:08:39 -0800525 self._last_successful_run[cmdline] = bin_hash
Craig Tiller69cd2372015-06-11 09:38:09 -0700526 if time.time() - self._last_save > 1:
527 self.save()
Craig Tiller71735182015-01-15 17:07:13 -0800528
529 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800530 return [{'cmdline': k, 'hash': v}
531 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800532
533 def parse(self, exdump):
534 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
535
536 def save(self):
537 with open('.run_tests_cache', 'w') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800538 f.write(json.dumps(self.dump()))
Craig Tiller69cd2372015-06-11 09:38:09 -0700539 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800540
Craig Tiller1cc11db2015-01-15 22:50:50 -0800541 def maybe_load(self):
542 if os.path.exists('.run_tests_cache'):
543 with open('.run_tests_cache') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800544 self.parse(json.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800545
546
Craig Tillerbb309712015-06-28 16:04:47 -0700547def _build_and_run(check_cancelled, newline_on_success, travis, cache, xml_report=None):
ctiller3040cb72015-01-07 12:13:17 -0800548 """Do one pass of building & running tests."""
murgatroid99666450e2015-01-26 13:03:31 -0800549 # build latest sequentially
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100550 if not jobset.run(build_steps, maxjobs=1,
551 newline_on_success=newline_on_success, travis=travis):
Craig Tillerd86a3942015-01-14 12:48:54 -0800552 return 1
ctiller3040cb72015-01-07 12:13:17 -0800553
Craig Tiller234b6e72015-05-23 10:12:40 -0700554 # start antagonists
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700555 antagonists = [subprocess.Popen(['tools/run_tests/antagonist.py'])
Craig Tiller234b6e72015-05-23 10:12:40 -0700556 for _ in range(0, args.antagonists)]
557 try:
David Garcia Quintase90cd372015-05-31 18:15:26 -0700558 infinite_runs = runs_per_test == 0
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700559 # When running on travis, we want out test runs to be as similar as possible
560 # for reproducibility purposes.
561 if travis:
562 massaged_one_run = sorted(one_run, key=lambda x: x.shortname)
563 else:
564 # whereas otherwise, we want to shuffle things up to give all tests a
565 # chance to run.
566 massaged_one_run = list(one_run) # random.shuffle needs an indexable seq.
567 random.shuffle(massaged_one_run) # which it modifies in-place.
Craig Tillerf7b7c892015-06-22 14:33:25 -0700568 if infinite_runs:
569 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 -0700570 runs_sequence = (itertools.repeat(massaged_one_run) if infinite_runs
571 else itertools.repeat(massaged_one_run, runs_per_test))
David Garcia Quintase90cd372015-05-31 18:15:26 -0700572 all_runs = itertools.chain.from_iterable(runs_sequence)
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200573
574 root = ET.Element('testsuites') if xml_report else None
575 testsuite = ET.SubElement(root, 'testsuite', id='1', package='grpc', name='tests') if xml_report else None
576
Craig Tiller234b6e72015-05-23 10:12:40 -0700577 if not jobset.run(all_runs, check_cancelled,
578 newline_on_success=newline_on_success, travis=travis,
David Garcia Quintase90cd372015-05-31 18:15:26 -0700579 infinite_runs=infinite_runs,
Craig Tillerda2220a2015-05-27 07:50:53 -0700580 maxjobs=args.jobs,
Craig Tillercd43da82015-05-29 08:41:29 -0700581 stop_on_failure=args.stop_on_failure,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200582 cache=cache if not xml_report else None,
583 xml_report=testsuite):
Craig Tiller234b6e72015-05-23 10:12:40 -0700584 return 2
585 finally:
586 for antagonist in antagonists:
587 antagonist.kill()
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200588 if xml_report:
589 tree = ET.ElementTree(root)
590 tree.write(xml_report, encoding='UTF-8')
Craig Tillerd86a3942015-01-14 12:48:54 -0800591
Craig Tiller69cd2372015-06-11 09:38:09 -0700592 if cache: cache.save()
593
Craig Tillerd86a3942015-01-14 12:48:54 -0800594 return 0
ctiller3040cb72015-01-07 12:13:17 -0800595
596
David Klempner25739582015-02-11 15:57:32 -0800597test_cache = TestCache(runs_per_test == 1)
Craig Tiller547db2b2015-01-30 14:08:39 -0800598test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800599
ctiller3040cb72015-01-07 12:13:17 -0800600if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800601 success = True
ctiller3040cb72015-01-07 12:13:17 -0800602 while True:
Craig Tiller42bc87c2015-02-23 08:50:19 -0800603 dw = watch_dirs.DirWatcher(['src', 'include', 'test', 'examples'])
ctiller3040cb72015-01-07 12:13:17 -0800604 initial_time = dw.most_recent_change()
605 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800606 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800607 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800608 newline_on_success=False,
Craig Tiller9a5a9402015-04-16 10:39:50 -0700609 travis=args.travis,
Craig Tiller71735182015-01-15 17:07:13 -0800610 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800611 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800612 jobset.message('SUCCESS',
613 'All tests are now passing properly',
614 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800615 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800616 while not have_files_changed():
617 time.sleep(1)
618else:
Craig Tiller71735182015-01-15 17:07:13 -0800619 result = _build_and_run(check_cancelled=lambda: False,
620 newline_on_success=args.newline_on_success,
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100621 travis=args.travis,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200622 cache=test_cache,
623 xml_report=args.xml_report)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800624 if result == 0:
625 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
626 else:
627 jobset.message('FAILED', 'Some tests failed', do_newline=True)
628 sys.exit(result)