blob: 77b9f4b5f8572f828bca00f609166a70a71ff2e6 [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 Nobleddef2462015-01-06 18:08:25 -080045
46import jobset
ctiller3040cb72015-01-07 12:13:17 -080047import watch_dirs
Nicolas Nobleddef2462015-01-06 18:08:25 -080048
Craig Tiller2cc2b842015-02-27 11:38:31 -080049ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
50os.chdir(ROOT)
51
52
Craig Tiller06805272015-06-11 14:46:47 -070053_FORCE_ENVIRON_FOR_WRAPPERS = {}
54
55
Craig Tiller738c3342015-01-12 14:28:33 -080056# SimpleConfig: just compile with CONFIG=config, and run the binary to test
57class SimpleConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080058
murgatroid99132ce6a2015-03-04 17:29:14 -080059 def __init__(self, config, environ=None):
60 if environ is None:
61 environ = {}
Craig Tiller738c3342015-01-12 14:28:33 -080062 self.build_config = config
Craig Tillerc7449162015-01-16 14:42:10 -080063 self.allow_hashing = (config != 'gcov')
Craig Tiller547db2b2015-01-30 14:08:39 -080064 self.environ = environ
murgatroid99132ce6a2015-03-04 17:29:14 -080065 self.environ['CONFIG'] = config
Craig Tiller738c3342015-01-12 14:28:33 -080066
Craig Tiller4fc90032015-05-21 10:39:52 -070067 def job_spec(self, cmdline, hash_targets, shortname=None, environ={}):
Craig Tiller49f61322015-03-03 13:02:11 -080068 """Construct a jobset.JobSpec for a test under this config
69
70 Args:
71 cmdline: a list of strings specifying the command line the test
72 would like to run
73 hash_targets: either None (don't do caching of test results), or
74 a list of strings specifying files to include in a
75 binary hash to check if a test has changed
76 -- if used, all artifacts needed to run the test must
77 be listed
78 """
Craig Tiller4fc90032015-05-21 10:39:52 -070079 actual_environ = self.environ.copy()
80 for k, v in environ.iteritems():
81 actual_environ[k] = v
Craig Tiller49f61322015-03-03 13:02:11 -080082 return jobset.JobSpec(cmdline=cmdline,
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -070083 shortname=shortname,
Craig Tiller4fc90032015-05-21 10:39:52 -070084 environ=actual_environ,
Craig Tiller547db2b2015-01-30 14:08:39 -080085 hash_targets=hash_targets
86 if self.allow_hashing else None)
Craig Tiller738c3342015-01-12 14:28:33 -080087
88
89# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
90class ValgrindConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080091
murgatroid99132ce6a2015-03-04 17:29:14 -080092 def __init__(self, config, tool, args=None):
93 if args is None:
94 args = []
Craig Tiller738c3342015-01-12 14:28:33 -080095 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080096 self.tool = tool
Craig Tiller1a305b12015-02-18 13:37:06 -080097 self.args = args
Craig Tillerc7449162015-01-16 14:42:10 -080098 self.allow_hashing = False
Craig Tiller738c3342015-01-12 14:28:33 -080099
Craig Tiller49f61322015-03-03 13:02:11 -0800100 def job_spec(self, cmdline, hash_targets):
Craig Tiller1a305b12015-02-18 13:37:06 -0800101 return jobset.JobSpec(cmdline=['valgrind', '--tool=%s' % self.tool] +
Craig Tiller49f61322015-03-03 13:02:11 -0800102 self.args + cmdline,
Craig Tiller71ec6cb2015-06-03 00:51:11 -0700103 shortname='valgrind %s' % cmdline[0],
Craig Tiller1a305b12015-02-18 13:37:06 -0800104 hash_targets=None)
Craig Tiller738c3342015-01-12 14:28:33 -0800105
106
Craig Tillerc7449162015-01-16 14:42:10 -0800107class CLanguage(object):
108
Craig Tillere9c959d2015-01-18 10:23:26 -0800109 def __init__(self, make_target, test_lang):
Craig Tillerc7449162015-01-16 14:42:10 -0800110 self.make_target = make_target
Craig Tillerd625d812015-04-08 15:52:35 -0700111 if platform.system() == 'Windows':
112 plat = 'windows'
113 else:
114 plat = 'posix'
Nicolas Noblee1445362015-05-11 17:40:26 -0700115 self.platform = plat
Craig Tillere9c959d2015-01-18 10:23:26 -0800116 with open('tools/run_tests/tests.json') as f:
Craig Tiller06b4ff22015-01-18 11:01:25 -0800117 js = json.load(f)
Craig Tillerd625d812015-04-08 15:52:35 -0700118 self.binaries = [tgt
119 for tgt in js
120 if tgt['language'] == test_lang and
121 plat in tgt['platforms']]
Craig Tillerc7449162015-01-16 14:42:10 -0800122
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100123 def test_specs(self, config, travis):
Craig Tiller547db2b2015-01-30 14:08:39 -0800124 out = []
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100125 for target in self.binaries:
126 if travis and target['flaky']:
127 continue
Nicolas Noblee1445362015-05-11 17:40:26 -0700128 if self.platform == 'windows':
129 binary = 'vsprojects\\test_bin\\%s.exe' % (target['name'])
130 else:
131 binary = 'bins/%s/%s' % (config.build_config, target['name'])
Craig Tiller49f61322015-03-03 13:02:11 -0800132 out.append(config.job_spec([binary], [binary]))
Nicolas Noblee1445362015-05-11 17:40:26 -0700133 return sorted(out)
Craig Tillerc7449162015-01-16 14:42:10 -0800134
135 def make_targets(self):
136 return ['buildtests_%s' % self.make_target]
137
138 def build_steps(self):
139 return []
140
murgatroid99132ce6a2015-03-04 17:29:14 -0800141 def supports_multi_config(self):
142 return True
143
144 def __str__(self):
145 return self.make_target
146
Craig Tiller99775822015-01-30 13:07:16 -0800147
murgatroid992c8d5162015-01-26 10:41:21 -0800148class NodeLanguage(object):
149
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100150 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700151 return [config.job_spec(['tools/run_tests/run_node.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700152 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid992c8d5162015-01-26 10:41:21 -0800153
154 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700155 return ['static_c', 'shared_c']
murgatroid992c8d5162015-01-26 10:41:21 -0800156
157 def build_steps(self):
158 return [['tools/run_tests/build_node.sh']]
Craig Tillerc7449162015-01-16 14:42:10 -0800159
murgatroid99132ce6a2015-03-04 17:29:14 -0800160 def supports_multi_config(self):
161 return False
162
163 def __str__(self):
164 return 'node'
165
Craig Tiller99775822015-01-30 13:07:16 -0800166
Craig Tillerc7449162015-01-16 14:42:10 -0800167class PhpLanguage(object):
168
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100169 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700170 return [config.job_spec(['src/php/bin/run_tests.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700171 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
Craig Tillerc7449162015-01-16 14:42:10 -0800172
173 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700174 return ['static_c', 'shared_c']
Craig Tillerc7449162015-01-16 14:42:10 -0800175
176 def build_steps(self):
177 return [['tools/run_tests/build_php.sh']]
178
murgatroid99132ce6a2015-03-04 17:29:14 -0800179 def supports_multi_config(self):
180 return False
181
182 def __str__(self):
183 return 'php'
184
Craig Tillerc7449162015-01-16 14:42:10 -0800185
Nathaniel Manista840615e2015-01-22 20:31:47 +0000186class PythonLanguage(object):
187
Craig Tiller49f61322015-03-03 13:02:11 -0800188 def __init__(self):
189 with open('tools/run_tests/python_tests.json') as f:
190 self._tests = json.load(f)
191
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100192 def test_specs(self, config, travis):
Masood Malekghassemib2d4a8d2015-03-05 17:04:18 -0800193 modules = [config.job_spec(['tools/run_tests/run_python.sh', '-m',
Craig Tiller4fc90032015-05-21 10:39:52 -0700194 test['module']],
195 None,
Craig Tiller06805272015-06-11 14:46:47 -0700196 environ=_FORCE_ENVIRON_FOR_WRAPPERS,
Craig Tiller83020252015-05-13 14:46:45 -0700197 shortname=test['module'])
Masood Malekghassemib2d4a8d2015-03-05 17:04:18 -0800198 for test in self._tests if 'module' in test]
199 files = [config.job_spec(['tools/run_tests/run_python.sh',
Craig Tiller4fc90032015-05-21 10:39:52 -0700200 test['file']],
201 None,
Craig Tiller06805272015-06-11 14:46:47 -0700202 environ=_FORCE_ENVIRON_FOR_WRAPPERS,
Craig Tiller83020252015-05-13 14:46:45 -0700203 shortname=test['file'])
Craig Tiller4fc90032015-05-21 10:39:52 -0700204 for test in self._tests if 'file' in test]
Masood Malekghassemib2d4a8d2015-03-05 17:04:18 -0800205 return files + modules
Nathaniel Manista840615e2015-01-22 20:31:47 +0000206
207 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700208 return ['static_c', 'grpc_python_plugin', 'shared_c']
Nathaniel Manista840615e2015-01-22 20:31:47 +0000209
210 def build_steps(self):
211 return [['tools/run_tests/build_python.sh']]
212
murgatroid99132ce6a2015-03-04 17:29:14 -0800213 def supports_multi_config(self):
214 return False
215
216 def __str__(self):
217 return 'python'
218
Craig Tillerd625d812015-04-08 15:52:35 -0700219
murgatroid996a4c4fa2015-02-27 12:08:57 -0800220class RubyLanguage(object):
221
222 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700223 return [config.job_spec(['tools/run_tests/run_ruby.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700224 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid996a4c4fa2015-02-27 12:08:57 -0800225
226 def make_targets(self):
Nicolas "Pixel" Noblecbd9c8b2015-05-14 06:22:26 +0200227 return ['run_dep_checks']
murgatroid996a4c4fa2015-02-27 12:08:57 -0800228
229 def build_steps(self):
230 return [['tools/run_tests/build_ruby.sh']]
231
murgatroid99132ce6a2015-03-04 17:29:14 -0800232 def supports_multi_config(self):
233 return False
234
235 def __str__(self):
236 return 'ruby'
237
Craig Tillerd625d812015-04-08 15:52:35 -0700238
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800239class CSharpLanguage(object):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700240 def __init__(self):
241 if platform.system() == 'Windows':
242 plat = 'windows'
243 else:
244 plat = 'posix'
245 self.platform = plat
246
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800247 def test_specs(self, config, travis):
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700248 assemblies = ['Grpc.Core.Tests',
249 'Grpc.Examples.Tests',
250 'Grpc.IntegrationTesting']
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700251 if self.platform == 'windows':
252 cmd = 'tools\\run_tests\\run_csharp.bat'
253 else:
254 cmd = 'tools/run_tests/run_csharp.sh'
255 return [config.job_spec([cmd, assembly],
Craig Tiller4fc90032015-05-21 10:39:52 -0700256 None, shortname=assembly,
Craig Tiller06805272015-06-11 14:46:47 -0700257 environ=_FORCE_ENVIRON_FOR_WRAPPERS)
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700258 for assembly in assemblies ]
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800259
260 def make_targets(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700261 # For Windows, this target doesn't really build anything,
262 # everything is build by buildall script later.
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800263 return ['grpc_csharp_ext']
264
265 def build_steps(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700266 if self.platform == 'windows':
267 return [['src\\csharp\\buildall.bat']]
268 else:
269 return [['tools/run_tests/build_csharp.sh']]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000270
murgatroid99132ce6a2015-03-04 17:29:14 -0800271 def supports_multi_config(self):
272 return False
273
274 def __str__(self):
275 return 'csharp'
276
Craig Tillerd625d812015-04-08 15:52:35 -0700277
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100278class Sanity(object):
279
280 def test_specs(self, config, travis):
281 return [config.job_spec('tools/run_tests/run_sanity.sh', None)]
282
283 def make_targets(self):
284 return ['run_dep_checks']
285
286 def build_steps(self):
287 return []
288
289 def supports_multi_config(self):
290 return False
291
292 def __str__(self):
293 return 'sanity'
294
Nicolas "Pixel" Noblee55cd7f2015-04-14 17:59:13 +0200295
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100296class Build(object):
297
298 def test_specs(self, config, travis):
299 return []
300
301 def make_targets(self):
Nicolas "Pixel" Noblec23827b2015-04-23 06:17:55 +0200302 return ['static']
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100303
304 def build_steps(self):
305 return []
306
307 def supports_multi_config(self):
308 return True
309
310 def __str__(self):
311 return self.make_target
312
313
Craig Tiller738c3342015-01-12 14:28:33 -0800314# different configurations we can run under
315_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -0800316 'dbg': SimpleConfig('dbg'),
317 'opt': SimpleConfig('opt'),
David Klempner1d0302d2015-02-04 16:08:01 -0800318 'tsan': SimpleConfig('tsan', environ={
Craig Tiller69cd2372015-06-11 09:38:09 -0700319 'TSAN_OPTIONS': 'suppressions=tools/tsan_suppressions.txt:halt_on_error=1'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800320 'msan': SimpleConfig('msan'),
Craig Tiller96bd5f62015-02-13 09:04:13 -0800321 'ubsan': SimpleConfig('ubsan'),
Craig Tiller547db2b2015-01-30 14:08:39 -0800322 'asan': SimpleConfig('asan', environ={
Craig Tillerd4b13622015-05-29 09:10:10 -0700323 'ASAN_OPTIONS': 'detect_leaks=1:color=always:suppressions=tools/tsan_suppressions.txt',
324 'LSAN_OPTIONS': 'report_objects=1'}),
Craig Tiller810725c2015-05-12 09:44:41 -0700325 'asan-noleaks': SimpleConfig('asan', environ={
326 'ASAN_OPTIONS': 'detect_leaks=0:color=always:suppressions=tools/tsan_suppressions.txt'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800327 'gcov': SimpleConfig('gcov'),
Craig Tiller1a305b12015-02-18 13:37:06 -0800328 'memcheck': ValgrindConfig('valgrind', 'memcheck', ['--leak-check=full']),
Craig Tillerb50d1662015-01-15 17:28:21 -0800329 'helgrind': ValgrindConfig('dbg', 'helgrind')
330 }
Craig Tiller738c3342015-01-12 14:28:33 -0800331
332
Nicolas "Pixel" Noble1fb5e822015-03-16 06:20:37 +0100333_DEFAULT = ['opt']
Craig Tillerc7449162015-01-16 14:42:10 -0800334_LANGUAGES = {
Craig Tillere9c959d2015-01-18 10:23:26 -0800335 'c++': CLanguage('cxx', 'c++'),
336 'c': CLanguage('c', 'c'),
murgatroid992c8d5162015-01-26 10:41:21 -0800337 'node': NodeLanguage(),
Nathaniel Manista840615e2015-01-22 20:31:47 +0000338 'php': PhpLanguage(),
339 'python': PythonLanguage(),
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800340 'ruby': RubyLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100341 'csharp': CSharpLanguage(),
342 'sanity': Sanity(),
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100343 'build': Build(),
Craig Tillereb272bc2015-01-30 13:13:14 -0800344 }
Nicolas Nobleddef2462015-01-06 18:08:25 -0800345
346# parse command line
347argp = argparse.ArgumentParser(description='Run grpc tests.')
348argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -0800349 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -0800350 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -0800351 default=_DEFAULT)
David Garcia Quintase90cd372015-05-31 18:15:26 -0700352
353def runs_per_test_type(arg_str):
354 """Auxilary function to parse the "runs_per_test" flag.
355
356 Returns:
357 A positive integer or 0, the latter indicating an infinite number of
358 runs.
359
360 Raises:
361 argparse.ArgumentTypeError: Upon invalid input.
362 """
363 if arg_str == 'inf':
364 return 0
365 try:
366 n = int(arg_str)
367 if n <= 0: raise ValueError
Craig Tiller50e53e22015-06-01 20:18:21 -0700368 return n
David Garcia Quintase90cd372015-05-31 18:15:26 -0700369 except:
370 msg = "'{}' isn't a positive integer or 'inf'".format(arg_str)
371 raise argparse.ArgumentTypeError(msg)
372argp.add_argument('-n', '--runs_per_test', default=1, type=runs_per_test_type,
373 help='A positive integer or "inf". If "inf", all tests will run in an '
374 'infinite loop. Especially useful in combination with "-f"')
Craig Tillerfe406ec2015-02-24 13:55:12 -0800375argp.add_argument('-r', '--regex', default='.*', type=str)
Craig Tiller83762ac2015-05-22 14:04:06 -0700376argp.add_argument('-j', '--jobs', default=2 * multiprocessing.cpu_count(), type=int)
Craig Tiller8451e872015-02-27 09:25:51 -0800377argp.add_argument('-s', '--slowdown', default=1.0, type=float)
ctiller3040cb72015-01-07 12:13:17 -0800378argp.add_argument('-f', '--forever',
379 default=False,
380 action='store_const',
381 const=True)
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100382argp.add_argument('-t', '--travis',
383 default=False,
384 action='store_const',
385 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800386argp.add_argument('--newline_on_success',
387 default=False,
388 action='store_const',
389 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -0800390argp.add_argument('-l', '--language',
Craig Tiller60f15e62015-05-13 09:05:17 -0700391 choices=['all'] + sorted(_LANGUAGES.keys()),
Craig Tiller686fb262015-01-15 07:39:09 -0800392 nargs='+',
Craig Tiller60f15e62015-05-13 09:05:17 -0700393 default=['all'])
Craig Tillercd43da82015-05-29 08:41:29 -0700394argp.add_argument('-S', '--stop_on_failure',
395 default=False,
396 action='store_const',
397 const=True)
Craig Tiller234b6e72015-05-23 10:12:40 -0700398argp.add_argument('-a', '--antagonists', default=0, type=int)
Nicolas Nobleddef2462015-01-06 18:08:25 -0800399args = argp.parse_args()
400
401# grab config
Craig Tiller738c3342015-01-12 14:28:33 -0800402run_configs = set(_CONFIGS[cfg]
403 for cfg in itertools.chain.from_iterable(
404 _CONFIGS.iterkeys() if x == 'all' else [x]
405 for x in args.config))
406build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tillerf1973b02015-01-16 12:32:13 -0800407
Craig Tiller06805272015-06-11 14:46:47 -0700408if args.travis:
409 _FORCE_ENVIRON_FOR_WRAPPERS = {'GRPC_TRACE': 'surface,batch'}
410
Craig Tillerc7449162015-01-16 14:42:10 -0800411make_targets = []
Craig Tiller60f15e62015-05-13 09:05:17 -0700412languages = set(_LANGUAGES[l]
413 for l in itertools.chain.from_iterable(
414 _LANGUAGES.iterkeys() if x == 'all' else [x]
415 for x in args.language))
murgatroid99132ce6a2015-03-04 17:29:14 -0800416
417if len(build_configs) > 1:
418 for language in languages:
419 if not language.supports_multi_config():
420 print language, 'does not support multiple build configurations'
421 sys.exit(1)
422
Craig Tiller5058c692015-04-08 09:42:04 -0700423if platform.system() == 'Windows':
424 def make_jobspec(cfg, targets):
Jan Tattermusche8243592015-04-17 14:14:01 -0700425 return jobset.JobSpec(['make.bat', 'CONFIG=%s' % cfg] + targets,
426 cwd='vsprojects', shell=True)
Craig Tiller5058c692015-04-08 09:42:04 -0700427else:
428 def make_jobspec(cfg, targets):
429 return jobset.JobSpec(['make',
430 '-j', '%d' % (multiprocessing.cpu_count() + 1),
Craig Tiller533b1a22015-05-29 08:41:29 -0700431 'EXTRA_DEFINES=GRPC_TEST_SLOWDOWN_MACHINE_FACTOR=%f' %
Craig Tiller5058c692015-04-08 09:42:04 -0700432 args.slowdown,
433 'CONFIG=%s' % cfg] + targets)
434
Craig Tiller533b1a22015-05-29 08:41:29 -0700435build_steps = [make_jobspec(cfg,
Craig Tiller5058c692015-04-08 09:42:04 -0700436 list(set(itertools.chain.from_iterable(
437 l.make_targets() for l in languages))))
438 for cfg in build_configs]
439build_steps.extend(set(
murgatroid99132ce6a2015-03-04 17:29:14 -0800440 jobset.JobSpec(cmdline, environ={'CONFIG': cfg})
441 for cfg in build_configs
Craig Tiller547db2b2015-01-30 14:08:39 -0800442 for l in languages
Craig Tiller533b1a22015-05-29 08:41:29 -0700443 for cmdline in l.build_steps()))
Craig Tiller547db2b2015-01-30 14:08:39 -0800444one_run = set(
445 spec
446 for config in run_configs
Craig Tiller60f15e62015-05-13 09:05:17 -0700447 for language in languages
448 for spec in language.test_specs(config, args.travis)
Craig Tillerfe406ec2015-02-24 13:55:12 -0800449 if re.search(args.regex, spec.shortname))
Craig Tillerf1973b02015-01-16 12:32:13 -0800450
Nicolas Nobleddef2462015-01-06 18:08:25 -0800451runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -0800452forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -0800453
Nicolas Nobleddef2462015-01-06 18:08:25 -0800454
Craig Tiller71735182015-01-15 17:07:13 -0800455class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800456 """Cache for running tests."""
457
David Klempner25739582015-02-11 15:57:32 -0800458 def __init__(self, use_cache_results):
Craig Tiller71735182015-01-15 17:07:13 -0800459 self._last_successful_run = {}
David Klempner25739582015-02-11 15:57:32 -0800460 self._use_cache_results = use_cache_results
Craig Tiller69cd2372015-06-11 09:38:09 -0700461 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800462
463 def should_run(self, cmdline, bin_hash):
Craig Tiller71735182015-01-15 17:07:13 -0800464 if cmdline not in self._last_successful_run:
465 return True
466 if self._last_successful_run[cmdline] != bin_hash:
467 return True
David Klempner25739582015-02-11 15:57:32 -0800468 if not self._use_cache_results:
469 return True
Craig Tiller71735182015-01-15 17:07:13 -0800470 return False
471
472 def finished(self, cmdline, bin_hash):
Craig Tiller547db2b2015-01-30 14:08:39 -0800473 self._last_successful_run[cmdline] = bin_hash
Craig Tiller69cd2372015-06-11 09:38:09 -0700474 if time.time() - self._last_save > 1:
475 self.save()
Craig Tiller71735182015-01-15 17:07:13 -0800476
477 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800478 return [{'cmdline': k, 'hash': v}
479 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800480
481 def parse(self, exdump):
482 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
483
484 def save(self):
485 with open('.run_tests_cache', 'w') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800486 f.write(json.dumps(self.dump()))
Craig Tiller69cd2372015-06-11 09:38:09 -0700487 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800488
Craig Tiller1cc11db2015-01-15 22:50:50 -0800489 def maybe_load(self):
490 if os.path.exists('.run_tests_cache'):
491 with open('.run_tests_cache') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800492 self.parse(json.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800493
494
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100495def _build_and_run(check_cancelled, newline_on_success, travis, cache):
ctiller3040cb72015-01-07 12:13:17 -0800496 """Do one pass of building & running tests."""
murgatroid99666450e2015-01-26 13:03:31 -0800497 # build latest sequentially
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100498 if not jobset.run(build_steps, maxjobs=1,
499 newline_on_success=newline_on_success, travis=travis):
Craig Tillerd86a3942015-01-14 12:48:54 -0800500 return 1
ctiller3040cb72015-01-07 12:13:17 -0800501
Craig Tiller234b6e72015-05-23 10:12:40 -0700502 # start antagonists
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700503 antagonists = [subprocess.Popen(['tools/run_tests/antagonist.py'])
Craig Tiller234b6e72015-05-23 10:12:40 -0700504 for _ in range(0, args.antagonists)]
505 try:
David Garcia Quintase90cd372015-05-31 18:15:26 -0700506 infinite_runs = runs_per_test == 0
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700507 # When running on travis, we want out test runs to be as similar as possible
508 # for reproducibility purposes.
509 if travis:
510 massaged_one_run = sorted(one_run, key=lambda x: x.shortname)
511 else:
512 # whereas otherwise, we want to shuffle things up to give all tests a
513 # chance to run.
514 massaged_one_run = list(one_run) # random.shuffle needs an indexable seq.
515 random.shuffle(massaged_one_run) # which it modifies in-place.
516 runs_sequence = (itertools.repeat(massaged_one_run) if infinite_runs
517 else itertools.repeat(massaged_one_run, runs_per_test))
David Garcia Quintase90cd372015-05-31 18:15:26 -0700518 all_runs = itertools.chain.from_iterable(runs_sequence)
Craig Tiller234b6e72015-05-23 10:12:40 -0700519 if not jobset.run(all_runs, check_cancelled,
520 newline_on_success=newline_on_success, travis=travis,
David Garcia Quintase90cd372015-05-31 18:15:26 -0700521 infinite_runs=infinite_runs,
Craig Tillerda2220a2015-05-27 07:50:53 -0700522 maxjobs=args.jobs,
Craig Tillercd43da82015-05-29 08:41:29 -0700523 stop_on_failure=args.stop_on_failure,
Craig Tiller234b6e72015-05-23 10:12:40 -0700524 cache=cache):
525 return 2
526 finally:
527 for antagonist in antagonists:
528 antagonist.kill()
Craig Tillerd86a3942015-01-14 12:48:54 -0800529
Craig Tiller69cd2372015-06-11 09:38:09 -0700530 if cache: cache.save()
531
Craig Tillerd86a3942015-01-14 12:48:54 -0800532 return 0
ctiller3040cb72015-01-07 12:13:17 -0800533
534
David Klempner25739582015-02-11 15:57:32 -0800535test_cache = TestCache(runs_per_test == 1)
Craig Tiller547db2b2015-01-30 14:08:39 -0800536test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800537
ctiller3040cb72015-01-07 12:13:17 -0800538if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800539 success = True
ctiller3040cb72015-01-07 12:13:17 -0800540 while True:
Craig Tiller42bc87c2015-02-23 08:50:19 -0800541 dw = watch_dirs.DirWatcher(['src', 'include', 'test', 'examples'])
ctiller3040cb72015-01-07 12:13:17 -0800542 initial_time = dw.most_recent_change()
543 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800544 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800545 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800546 newline_on_success=False,
Craig Tiller9a5a9402015-04-16 10:39:50 -0700547 travis=args.travis,
Craig Tiller71735182015-01-15 17:07:13 -0800548 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800549 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800550 jobset.message('SUCCESS',
551 'All tests are now passing properly',
552 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800553 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800554 while not have_files_changed():
555 time.sleep(1)
556else:
Craig Tiller71735182015-01-15 17:07:13 -0800557 result = _build_and_run(check_cancelled=lambda: False,
558 newline_on_success=args.newline_on_success,
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100559 travis=args.travis,
Craig Tiller71735182015-01-15 17:07:13 -0800560 cache=test_cache)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800561 if result == 0:
562 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
563 else:
564 jobset.message('FAILED', 'Some tests failed', do_newline=True)
565 sys.exit(result)