blob: fa749498d2ff946615cb495f29dc3b46b34ca374 [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):
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700190 self._build_python_versions = ['2.7']
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700191 self._has_python_versions = []
Craig Tiller49f61322015-03-03 13:02:11 -0800192
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100193 def test_specs(self, config, travis):
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700194 environment = dict(_FORCE_ENVIRON_FOR_WRAPPERS)
195 environment['PYVER'] = '2.7'
196 return [config.job_spec(
197 ['tools/run_tests/run_python.sh'],
198 None,
199 environ=environment,
200 shortname='py.test',
201 )]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000202
203 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700204 return ['static_c', 'grpc_python_plugin', 'shared_c']
Nathaniel Manista840615e2015-01-22 20:31:47 +0000205
206 def build_steps(self):
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700207 commands = []
208 for python_version in self._build_python_versions:
209 try:
210 with open(os.devnull, 'w') as output:
211 subprocess.check_call(['which', 'python' + python_version],
212 stdout=output, stderr=output)
213 commands.append(['tools/run_tests/build_python.sh', python_version])
214 self._has_python_versions.append(python_version)
215 except:
216 jobset.message('WARNING', 'Missing Python ' + python_version,
217 do_newline=True)
218 return commands
Nathaniel Manista840615e2015-01-22 20:31:47 +0000219
murgatroid99132ce6a2015-03-04 17:29:14 -0800220 def supports_multi_config(self):
221 return False
222
223 def __str__(self):
224 return 'python'
225
Craig Tillerd625d812015-04-08 15:52:35 -0700226
murgatroid996a4c4fa2015-02-27 12:08:57 -0800227class RubyLanguage(object):
228
229 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700230 return [config.job_spec(['tools/run_tests/run_ruby.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700231 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid996a4c4fa2015-02-27 12:08:57 -0800232
233 def make_targets(self):
murgatroid99a43c14f2015-07-30 13:31:23 -0700234 return ['static_c']
murgatroid996a4c4fa2015-02-27 12:08:57 -0800235
236 def build_steps(self):
237 return [['tools/run_tests/build_ruby.sh']]
238
murgatroid99132ce6a2015-03-04 17:29:14 -0800239 def supports_multi_config(self):
240 return False
241
242 def __str__(self):
243 return 'ruby'
244
Craig Tillerd625d812015-04-08 15:52:35 -0700245
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800246class CSharpLanguage(object):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700247 def __init__(self):
248 if platform.system() == 'Windows':
249 plat = 'windows'
250 else:
251 plat = 'posix'
252 self.platform = plat
253
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800254 def test_specs(self, config, travis):
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700255 assemblies = ['Grpc.Core.Tests',
256 'Grpc.Examples.Tests',
257 'Grpc.IntegrationTesting']
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700258 if self.platform == 'windows':
259 cmd = 'tools\\run_tests\\run_csharp.bat'
260 else:
261 cmd = 'tools/run_tests/run_csharp.sh'
262 return [config.job_spec([cmd, assembly],
Craig Tiller4fc90032015-05-21 10:39:52 -0700263 None, shortname=assembly,
Craig Tiller06805272015-06-11 14:46:47 -0700264 environ=_FORCE_ENVIRON_FOR_WRAPPERS)
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700265 for assembly in assemblies ]
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800266
267 def make_targets(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700268 # For Windows, this target doesn't really build anything,
269 # everything is build by buildall script later.
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800270 return ['grpc_csharp_ext']
271
272 def build_steps(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700273 if self.platform == 'windows':
274 return [['src\\csharp\\buildall.bat']]
275 else:
276 return [['tools/run_tests/build_csharp.sh']]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000277
murgatroid99132ce6a2015-03-04 17:29:14 -0800278 def supports_multi_config(self):
279 return False
280
281 def __str__(self):
282 return 'csharp'
283
Craig Tillerd625d812015-04-08 15:52:35 -0700284
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700285class ObjCLanguage(object):
286
287 def test_specs(self, config, travis):
288 return [config.job_spec(['src/objective-c/tests/run_tests.sh'], None,
289 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
290
291 def make_targets(self):
Jorge Canizalesd0b32e92015-07-30 23:08:43 -0700292 return ['grpc_objective_c_plugin', 'interop_server']
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700293
294 def build_steps(self):
Jorge Canizalesd0b32e92015-07-30 23:08:43 -0700295 return [['src/objective-c/tests/build_tests.sh']]
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700296
297 def supports_multi_config(self):
298 return False
299
300 def __str__(self):
301 return 'objc'
302
303
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100304class Sanity(object):
305
306 def test_specs(self, config, travis):
Craig Tillerf75fc122015-06-25 06:58:00 -0700307 return [config.job_spec('tools/run_tests/run_sanity.sh', None),
308 config.job_spec('tools/run_tests/check_sources_and_headers.py', None)]
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100309
310 def make_targets(self):
311 return ['run_dep_checks']
312
313 def build_steps(self):
314 return []
315
316 def supports_multi_config(self):
317 return False
318
319 def __str__(self):
320 return 'sanity'
321
Nicolas "Pixel" Noblee55cd7f2015-04-14 17:59:13 +0200322
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100323class Build(object):
324
325 def test_specs(self, config, travis):
326 return []
327
328 def make_targets(self):
Nicolas "Pixel" Noblec23827b2015-04-23 06:17:55 +0200329 return ['static']
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100330
331 def build_steps(self):
332 return []
333
334 def supports_multi_config(self):
335 return True
336
337 def __str__(self):
338 return self.make_target
339
340
Craig Tiller738c3342015-01-12 14:28:33 -0800341# different configurations we can run under
342_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -0800343 'dbg': SimpleConfig('dbg'),
344 'opt': SimpleConfig('opt'),
David Klempner1d0302d2015-02-04 16:08:01 -0800345 'tsan': SimpleConfig('tsan', environ={
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700346 'TSAN_OPTIONS': 'suppressions=tools/tsan_suppressions.txt:halt_on_error=1:second_deadlock_stack=1'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800347 'msan': SimpleConfig('msan'),
Craig Tiller96bd5f62015-02-13 09:04:13 -0800348 'ubsan': SimpleConfig('ubsan'),
Craig Tiller547db2b2015-01-30 14:08:39 -0800349 'asan': SimpleConfig('asan', environ={
Craig Tillerd4b13622015-05-29 09:10:10 -0700350 'ASAN_OPTIONS': 'detect_leaks=1:color=always:suppressions=tools/tsan_suppressions.txt',
351 'LSAN_OPTIONS': 'report_objects=1'}),
Craig Tiller810725c2015-05-12 09:44:41 -0700352 'asan-noleaks': SimpleConfig('asan', environ={
353 'ASAN_OPTIONS': 'detect_leaks=0:color=always:suppressions=tools/tsan_suppressions.txt'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800354 'gcov': SimpleConfig('gcov'),
Craig Tiller1a305b12015-02-18 13:37:06 -0800355 'memcheck': ValgrindConfig('valgrind', 'memcheck', ['--leak-check=full']),
Craig Tillerb50d1662015-01-15 17:28:21 -0800356 'helgrind': ValgrindConfig('dbg', 'helgrind')
357 }
Craig Tiller738c3342015-01-12 14:28:33 -0800358
359
Nicolas "Pixel" Noble1fb5e822015-03-16 06:20:37 +0100360_DEFAULT = ['opt']
Craig Tillerc7449162015-01-16 14:42:10 -0800361_LANGUAGES = {
Craig Tillere9c959d2015-01-18 10:23:26 -0800362 'c++': CLanguage('cxx', 'c++'),
363 'c': CLanguage('c', 'c'),
murgatroid992c8d5162015-01-26 10:41:21 -0800364 'node': NodeLanguage(),
Nathaniel Manista840615e2015-01-22 20:31:47 +0000365 'php': PhpLanguage(),
366 'python': PythonLanguage(),
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800367 'ruby': RubyLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100368 'csharp': CSharpLanguage(),
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700369 'objc' : ObjCLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100370 'sanity': Sanity(),
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100371 'build': Build(),
Craig Tillereb272bc2015-01-30 13:13:14 -0800372 }
Nicolas Nobleddef2462015-01-06 18:08:25 -0800373
374# parse command line
375argp = argparse.ArgumentParser(description='Run grpc tests.')
376argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -0800377 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -0800378 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -0800379 default=_DEFAULT)
David Garcia Quintase90cd372015-05-31 18:15:26 -0700380
381def runs_per_test_type(arg_str):
382 """Auxilary function to parse the "runs_per_test" flag.
383
384 Returns:
385 A positive integer or 0, the latter indicating an infinite number of
386 runs.
387
388 Raises:
389 argparse.ArgumentTypeError: Upon invalid input.
390 """
391 if arg_str == 'inf':
392 return 0
393 try:
394 n = int(arg_str)
395 if n <= 0: raise ValueError
Craig Tiller50e53e22015-06-01 20:18:21 -0700396 return n
David Garcia Quintase90cd372015-05-31 18:15:26 -0700397 except:
398 msg = "'{}' isn't a positive integer or 'inf'".format(arg_str)
399 raise argparse.ArgumentTypeError(msg)
400argp.add_argument('-n', '--runs_per_test', default=1, type=runs_per_test_type,
401 help='A positive integer or "inf". If "inf", all tests will run in an '
402 'infinite loop. Especially useful in combination with "-f"')
Craig Tillerfe406ec2015-02-24 13:55:12 -0800403argp.add_argument('-r', '--regex', default='.*', type=str)
Craig Tiller83762ac2015-05-22 14:04:06 -0700404argp.add_argument('-j', '--jobs', default=2 * multiprocessing.cpu_count(), type=int)
Craig Tiller8451e872015-02-27 09:25:51 -0800405argp.add_argument('-s', '--slowdown', default=1.0, type=float)
ctiller3040cb72015-01-07 12:13:17 -0800406argp.add_argument('-f', '--forever',
407 default=False,
408 action='store_const',
409 const=True)
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100410argp.add_argument('-t', '--travis',
411 default=False,
412 action='store_const',
413 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800414argp.add_argument('--newline_on_success',
415 default=False,
416 action='store_const',
417 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -0800418argp.add_argument('-l', '--language',
Craig Tiller60f15e62015-05-13 09:05:17 -0700419 choices=['all'] + sorted(_LANGUAGES.keys()),
Craig Tiller686fb262015-01-15 07:39:09 -0800420 nargs='+',
Craig Tiller60f15e62015-05-13 09:05:17 -0700421 default=['all'])
Craig Tillercd43da82015-05-29 08:41:29 -0700422argp.add_argument('-S', '--stop_on_failure',
423 default=False,
424 action='store_const',
425 const=True)
Craig Tiller234b6e72015-05-23 10:12:40 -0700426argp.add_argument('-a', '--antagonists', default=0, type=int)
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200427argp.add_argument('-x', '--xml_report', default=None, type=str,
428 help='Generates a JUnit-compatible XML report')
Nicolas Nobleddef2462015-01-06 18:08:25 -0800429args = argp.parse_args()
430
431# grab config
Craig Tiller738c3342015-01-12 14:28:33 -0800432run_configs = set(_CONFIGS[cfg]
433 for cfg in itertools.chain.from_iterable(
434 _CONFIGS.iterkeys() if x == 'all' else [x]
435 for x in args.config))
436build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tillerf1973b02015-01-16 12:32:13 -0800437
Craig Tiller06805272015-06-11 14:46:47 -0700438if args.travis:
439 _FORCE_ENVIRON_FOR_WRAPPERS = {'GRPC_TRACE': 'surface,batch'}
440
Craig Tillerc7449162015-01-16 14:42:10 -0800441make_targets = []
Craig Tiller60f15e62015-05-13 09:05:17 -0700442languages = set(_LANGUAGES[l]
443 for l in itertools.chain.from_iterable(
444 _LANGUAGES.iterkeys() if x == 'all' else [x]
445 for x in args.language))
murgatroid99132ce6a2015-03-04 17:29:14 -0800446
447if len(build_configs) > 1:
448 for language in languages:
449 if not language.supports_multi_config():
450 print language, 'does not support multiple build configurations'
451 sys.exit(1)
452
Craig Tiller5058c692015-04-08 09:42:04 -0700453if platform.system() == 'Windows':
454 def make_jobspec(cfg, targets):
Jan Tattermusche8243592015-04-17 14:14:01 -0700455 return jobset.JobSpec(['make.bat', 'CONFIG=%s' % cfg] + targets,
456 cwd='vsprojects', shell=True)
Craig Tiller5058c692015-04-08 09:42:04 -0700457else:
458 def make_jobspec(cfg, targets):
Nicolas "Pixel" Noble4243ca82015-07-23 23:47:56 +0200459 return jobset.JobSpec([os.getenv('MAKE', 'make'),
Craig Tiller5058c692015-04-08 09:42:04 -0700460 '-j', '%d' % (multiprocessing.cpu_count() + 1),
Craig Tiller533b1a22015-05-29 08:41:29 -0700461 'EXTRA_DEFINES=GRPC_TEST_SLOWDOWN_MACHINE_FACTOR=%f' %
Craig Tiller5058c692015-04-08 09:42:04 -0700462 args.slowdown,
463 'CONFIG=%s' % cfg] + targets)
464
Craig Tiller533b1a22015-05-29 08:41:29 -0700465build_steps = [make_jobspec(cfg,
Craig Tiller5058c692015-04-08 09:42:04 -0700466 list(set(itertools.chain.from_iterable(
467 l.make_targets() for l in languages))))
468 for cfg in build_configs]
469build_steps.extend(set(
murgatroid99132ce6a2015-03-04 17:29:14 -0800470 jobset.JobSpec(cmdline, environ={'CONFIG': cfg})
471 for cfg in build_configs
Craig Tiller547db2b2015-01-30 14:08:39 -0800472 for l in languages
Craig Tiller533b1a22015-05-29 08:41:29 -0700473 for cmdline in l.build_steps()))
Craig Tiller547db2b2015-01-30 14:08:39 -0800474one_run = set(
475 spec
476 for config in run_configs
Craig Tiller60f15e62015-05-13 09:05:17 -0700477 for language in languages
478 for spec in language.test_specs(config, args.travis)
Craig Tillerfe406ec2015-02-24 13:55:12 -0800479 if re.search(args.regex, spec.shortname))
Craig Tillerf1973b02015-01-16 12:32:13 -0800480
Nicolas Nobleddef2462015-01-06 18:08:25 -0800481runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -0800482forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -0800483
Nicolas Nobleddef2462015-01-06 18:08:25 -0800484
Craig Tiller71735182015-01-15 17:07:13 -0800485class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800486 """Cache for running tests."""
487
David Klempner25739582015-02-11 15:57:32 -0800488 def __init__(self, use_cache_results):
Craig Tiller71735182015-01-15 17:07:13 -0800489 self._last_successful_run = {}
David Klempner25739582015-02-11 15:57:32 -0800490 self._use_cache_results = use_cache_results
Craig Tiller69cd2372015-06-11 09:38:09 -0700491 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800492
493 def should_run(self, cmdline, bin_hash):
Craig Tiller71735182015-01-15 17:07:13 -0800494 if cmdline not in self._last_successful_run:
495 return True
496 if self._last_successful_run[cmdline] != bin_hash:
497 return True
David Klempner25739582015-02-11 15:57:32 -0800498 if not self._use_cache_results:
499 return True
Craig Tiller71735182015-01-15 17:07:13 -0800500 return False
501
502 def finished(self, cmdline, bin_hash):
Craig Tiller547db2b2015-01-30 14:08:39 -0800503 self._last_successful_run[cmdline] = bin_hash
Craig Tiller69cd2372015-06-11 09:38:09 -0700504 if time.time() - self._last_save > 1:
505 self.save()
Craig Tiller71735182015-01-15 17:07:13 -0800506
507 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800508 return [{'cmdline': k, 'hash': v}
509 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800510
511 def parse(self, exdump):
512 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
513
514 def save(self):
515 with open('.run_tests_cache', 'w') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800516 f.write(json.dumps(self.dump()))
Craig Tiller69cd2372015-06-11 09:38:09 -0700517 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800518
Craig Tiller1cc11db2015-01-15 22:50:50 -0800519 def maybe_load(self):
520 if os.path.exists('.run_tests_cache'):
521 with open('.run_tests_cache') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800522 self.parse(json.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800523
524
Craig Tillerbb309712015-06-28 16:04:47 -0700525def _build_and_run(check_cancelled, newline_on_success, travis, cache, xml_report=None):
ctiller3040cb72015-01-07 12:13:17 -0800526 """Do one pass of building & running tests."""
murgatroid99666450e2015-01-26 13:03:31 -0800527 # build latest sequentially
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100528 if not jobset.run(build_steps, maxjobs=1,
529 newline_on_success=newline_on_success, travis=travis):
Craig Tillerd86a3942015-01-14 12:48:54 -0800530 return 1
ctiller3040cb72015-01-07 12:13:17 -0800531
Craig Tiller234b6e72015-05-23 10:12:40 -0700532 # start antagonists
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700533 antagonists = [subprocess.Popen(['tools/run_tests/antagonist.py'])
Craig Tiller234b6e72015-05-23 10:12:40 -0700534 for _ in range(0, args.antagonists)]
535 try:
David Garcia Quintase90cd372015-05-31 18:15:26 -0700536 infinite_runs = runs_per_test == 0
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700537 # When running on travis, we want out test runs to be as similar as possible
538 # for reproducibility purposes.
539 if travis:
540 massaged_one_run = sorted(one_run, key=lambda x: x.shortname)
541 else:
542 # whereas otherwise, we want to shuffle things up to give all tests a
543 # chance to run.
544 massaged_one_run = list(one_run) # random.shuffle needs an indexable seq.
545 random.shuffle(massaged_one_run) # which it modifies in-place.
Craig Tillerf7b7c892015-06-22 14:33:25 -0700546 if infinite_runs:
547 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 -0700548 runs_sequence = (itertools.repeat(massaged_one_run) if infinite_runs
549 else itertools.repeat(massaged_one_run, runs_per_test))
David Garcia Quintase90cd372015-05-31 18:15:26 -0700550 all_runs = itertools.chain.from_iterable(runs_sequence)
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200551
552 root = ET.Element('testsuites') if xml_report else None
553 testsuite = ET.SubElement(root, 'testsuite', id='1', package='grpc', name='tests') if xml_report else None
554
Craig Tiller234b6e72015-05-23 10:12:40 -0700555 if not jobset.run(all_runs, check_cancelled,
556 newline_on_success=newline_on_success, travis=travis,
David Garcia Quintase90cd372015-05-31 18:15:26 -0700557 infinite_runs=infinite_runs,
Craig Tillerda2220a2015-05-27 07:50:53 -0700558 maxjobs=args.jobs,
Craig Tillercd43da82015-05-29 08:41:29 -0700559 stop_on_failure=args.stop_on_failure,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200560 cache=cache if not xml_report else None,
561 xml_report=testsuite):
Craig Tiller234b6e72015-05-23 10:12:40 -0700562 return 2
563 finally:
564 for antagonist in antagonists:
565 antagonist.kill()
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200566 if xml_report:
567 tree = ET.ElementTree(root)
568 tree.write(xml_report, encoding='UTF-8')
Craig Tillerd86a3942015-01-14 12:48:54 -0800569
Craig Tiller69cd2372015-06-11 09:38:09 -0700570 if cache: cache.save()
571
Craig Tillerd86a3942015-01-14 12:48:54 -0800572 return 0
ctiller3040cb72015-01-07 12:13:17 -0800573
574
David Klempner25739582015-02-11 15:57:32 -0800575test_cache = TestCache(runs_per_test == 1)
Craig Tiller547db2b2015-01-30 14:08:39 -0800576test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800577
ctiller3040cb72015-01-07 12:13:17 -0800578if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800579 success = True
ctiller3040cb72015-01-07 12:13:17 -0800580 while True:
Craig Tiller42bc87c2015-02-23 08:50:19 -0800581 dw = watch_dirs.DirWatcher(['src', 'include', 'test', 'examples'])
ctiller3040cb72015-01-07 12:13:17 -0800582 initial_time = dw.most_recent_change()
583 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800584 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800585 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800586 newline_on_success=False,
Craig Tiller9a5a9402015-04-16 10:39:50 -0700587 travis=args.travis,
Craig Tiller71735182015-01-15 17:07:13 -0800588 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800589 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800590 jobset.message('SUCCESS',
591 'All tests are now passing properly',
592 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800593 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800594 while not have_files_changed():
595 time.sleep(1)
596else:
Craig Tiller71735182015-01-15 17:07:13 -0800597 result = _build_and_run(check_cancelled=lambda: False,
598 newline_on_success=args.newline_on_success,
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100599 travis=args.travis,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200600 cache=test_cache,
601 xml_report=args.xml_report)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800602 if result == 0:
603 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
604 else:
605 jobset.message('FAILED', 'Some tests failed', do_newline=True)
606 sys.exit(result)