blob: de644b5946796a07d4b3425845c4785e5047780c [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
Craig Tillerf53d9c82015-08-04 14:19:43 -070035import hashlib
Nicolas Nobleddef2462015-01-06 18:08:25 -080036import itertools
Craig Tiller261dd982015-01-16 16:41:45 -080037import json
Nicolas Nobleddef2462015-01-06 18:08:25 -080038import multiprocessing
Craig Tiller1cc11db2015-01-15 22:50:50 -080039import os
David Garcia Quintas79e389f2015-06-02 17:49:42 -070040import platform
41import random
Craig Tillerfe406ec2015-02-24 13:55:12 -080042import re
David Garcia Quintas79e389f2015-06-02 17:49:42 -070043import subprocess
Nicolas Nobleddef2462015-01-06 18:08:25 -080044import sys
ctiller3040cb72015-01-07 12:13:17 -080045import time
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +020046import xml.etree.cElementTree as ET
Craig Tillerf53d9c82015-08-04 14:19:43 -070047import urllib2
Nicolas Nobleddef2462015-01-06 18:08:25 -080048
49import jobset
ctiller3040cb72015-01-07 12:13:17 -080050import watch_dirs
Nicolas Nobleddef2462015-01-06 18:08:25 -080051
Craig Tiller2cc2b842015-02-27 11:38:31 -080052ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
53os.chdir(ROOT)
54
55
Craig Tiller06805272015-06-11 14:46:47 -070056_FORCE_ENVIRON_FOR_WRAPPERS = {}
57
58
Craig Tillerd50993d2015-08-05 08:04:36 -070059def platform_string():
60 if platform.system() == 'Windows':
61 return 'windows'
62 elif platform.system() == 'Darwin':
63 return 'mac'
64 elif platform.system() == 'Linux':
65 return 'linux'
66 else:
67 return 'posix'
68
69
Craig Tiller738c3342015-01-12 14:28:33 -080070# SimpleConfig: just compile with CONFIG=config, and run the binary to test
71class SimpleConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080072
Craig Tillerb2ea0b92015-08-26 13:06:53 -070073 def __init__(self, config, environ=None, timeout_seconds=5*60):
murgatroid99132ce6a2015-03-04 17:29:14 -080074 if environ is None:
75 environ = {}
Craig Tiller738c3342015-01-12 14:28:33 -080076 self.build_config = config
Craig Tillerc7449162015-01-16 14:42:10 -080077 self.allow_hashing = (config != 'gcov')
Craig Tiller547db2b2015-01-30 14:08:39 -080078 self.environ = environ
murgatroid99132ce6a2015-03-04 17:29:14 -080079 self.environ['CONFIG'] = config
Craig Tillerb2ea0b92015-08-26 13:06:53 -070080 self.timeout_seconds = timeout_seconds
Craig Tiller738c3342015-01-12 14:28:33 -080081
Craig Tiller4fc90032015-05-21 10:39:52 -070082 def job_spec(self, cmdline, hash_targets, shortname=None, environ={}):
Craig Tiller49f61322015-03-03 13:02:11 -080083 """Construct a jobset.JobSpec for a test under this config
84
85 Args:
86 cmdline: a list of strings specifying the command line the test
87 would like to run
88 hash_targets: either None (don't do caching of test results), or
89 a list of strings specifying files to include in a
90 binary hash to check if a test has changed
91 -- if used, all artifacts needed to run the test must
92 be listed
93 """
Craig Tiller4fc90032015-05-21 10:39:52 -070094 actual_environ = self.environ.copy()
95 for k, v in environ.iteritems():
96 actual_environ[k] = v
Craig Tiller49f61322015-03-03 13:02:11 -080097 return jobset.JobSpec(cmdline=cmdline,
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -070098 shortname=shortname,
Craig Tiller4fc90032015-05-21 10:39:52 -070099 environ=actual_environ,
Craig Tillerb2ea0b92015-08-26 13:06:53 -0700100 timeout_seconds=self.timeout_seconds,
Craig Tiller547db2b2015-01-30 14:08:39 -0800101 hash_targets=hash_targets
102 if self.allow_hashing else None)
Craig Tiller738c3342015-01-12 14:28:33 -0800103
104
105# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
106class ValgrindConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800107
murgatroid99132ce6a2015-03-04 17:29:14 -0800108 def __init__(self, config, tool, args=None):
109 if args is None:
110 args = []
Craig Tiller738c3342015-01-12 14:28:33 -0800111 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -0800112 self.tool = tool
Craig Tiller1a305b12015-02-18 13:37:06 -0800113 self.args = args
Craig Tillerc7449162015-01-16 14:42:10 -0800114 self.allow_hashing = False
Craig Tiller738c3342015-01-12 14:28:33 -0800115
Craig Tiller49f61322015-03-03 13:02:11 -0800116 def job_spec(self, cmdline, hash_targets):
Craig Tiller1a305b12015-02-18 13:37:06 -0800117 return jobset.JobSpec(cmdline=['valgrind', '--tool=%s' % self.tool] +
Craig Tiller49f61322015-03-03 13:02:11 -0800118 self.args + cmdline,
Craig Tiller71ec6cb2015-06-03 00:51:11 -0700119 shortname='valgrind %s' % cmdline[0],
Craig Tiller1a305b12015-02-18 13:37:06 -0800120 hash_targets=None)
Craig Tiller738c3342015-01-12 14:28:33 -0800121
122
murgatroid99cf08daf2015-09-21 15:33:16 -0700123def get_c_tests(travis, test_lang) :
124 out = []
125 platforms_str = 'ci_platforms' if travis else 'platforms'
126 with open('tools/run_tests/tests.json') as f:
127 js = json.load(f);
128 binaries = [tgt
129 for tgt in js
130 if tgt['language'] == test_lang and
131 platform_string() in tgt[platforms_str] and
132 not (travis and tgt['flaky'])]
133 return binaries
134
Craig Tillerc7449162015-01-16 14:42:10 -0800135class CLanguage(object):
136
Craig Tillere9c959d2015-01-18 10:23:26 -0800137 def __init__(self, make_target, test_lang):
Craig Tillerc7449162015-01-16 14:42:10 -0800138 self.make_target = make_target
Craig Tillerd50993d2015-08-05 08:04:36 -0700139 self.platform = platform_string()
Craig Tiller711bbe62015-08-19 12:35:16 -0700140 self.test_lang = test_lang
Craig Tillerc7449162015-01-16 14:42:10 -0800141
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100142 def test_specs(self, config, travis):
Craig Tiller547db2b2015-01-30 14:08:39 -0800143 out = []
murgatroid99cf08daf2015-09-21 15:33:16 -0700144 binaries = get_c_tests(travis, self.test_lang)
Craig Tiller711bbe62015-08-19 12:35:16 -0700145 for target in binaries:
murgatroid99cf08daf2015-09-21 15:33:16 -0700146 if config.build_config in tgt['exclude_configs']:
147 continue;
Nicolas Noblee1445362015-05-11 17:40:26 -0700148 if self.platform == 'windows':
Craig Tillerf4182602015-09-01 12:23:16 -0700149 binary = 'vsprojects/%s/%s.exe' % (
150 _WINDOWS_CONFIG[config.build_config], target['name'])
Nicolas Noblee1445362015-05-11 17:40:26 -0700151 else:
152 binary = 'bins/%s/%s' % (config.build_config, target['name'])
yang-g6c1fdc62015-08-18 11:57:42 -0700153 if os.path.isfile(binary):
154 out.append(config.job_spec([binary], [binary]))
155 else:
156 print "\nWARNING: binary not found, skipping", binary
Nicolas Noblee1445362015-05-11 17:40:26 -0700157 return sorted(out)
Craig Tillerc7449162015-01-16 14:42:10 -0800158
159 def make_targets(self):
Craig Tiller7bb3efd2015-09-01 08:04:03 -0700160 if platform_string() == 'windows':
161 # don't build tools on windows just yet
162 return ['buildtests_%s' % self.make_target]
Craig Tiller7552f0f2015-06-19 17:46:20 -0700163 return ['buildtests_%s' % self.make_target, 'tools_%s' % self.make_target]
Craig Tillerc7449162015-01-16 14:42:10 -0800164
murgatroid99256d3df2015-09-21 16:58:02 -0700165 def pre_build_steps(self):
166 return []
167
Craig Tillerc7449162015-01-16 14:42:10 -0800168 def build_steps(self):
169 return []
170
murgatroid99132ce6a2015-03-04 17:29:14 -0800171 def supports_multi_config(self):
172 return True
173
174 def __str__(self):
175 return self.make_target
176
murgatroid99cf08daf2015-09-21 15:33:16 -0700177def gyp_test_paths(travis, config=None):
178 binaries = get_c_tests(travis, 'c')
179 out = []
180 for target in binaries:
181 if config is not None:
182 if config.build_config in target['exclude_configs']:
183 continue
184 binary = 'out/Debug/%s' % target['name']
185 out.append(binary)
186 return sorted(out)
187
188class GYPCLanguage(object):
189
190 def test_specs(self, config, travis):
191 return [config.job_spec([binary], [binary])
192 for binary in gyp_test_paths(travis, config)]
193
murgatroid99256d3df2015-09-21 16:58:02 -0700194 def pre_build_steps(self):
195 return [['gyp', '--depth=.', 'grpc.gyp']]
196
murgatroid99cf08daf2015-09-21 15:33:16 -0700197 def make_targets(self):
198 return gyp_test_paths(False)
199
200 def build_steps(self):
201 return []
202
203 def supports_multi_config(self):
204 return False
205
206 def __str__(self):
207 return 'gyp'
Craig Tiller99775822015-01-30 13:07:16 -0800208
murgatroid992c8d5162015-01-26 10:41:21 -0800209class NodeLanguage(object):
210
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100211 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700212 return [config.job_spec(['tools/run_tests/run_node.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700213 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid992c8d5162015-01-26 10:41:21 -0800214
murgatroid99256d3df2015-09-21 16:58:02 -0700215 def pre_build_steps(self):
216 return []
217
murgatroid992c8d5162015-01-26 10:41:21 -0800218 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700219 return ['static_c', 'shared_c']
murgatroid992c8d5162015-01-26 10:41:21 -0800220
221 def build_steps(self):
222 return [['tools/run_tests/build_node.sh']]
Craig Tillerc7449162015-01-16 14:42:10 -0800223
murgatroid99132ce6a2015-03-04 17:29:14 -0800224 def supports_multi_config(self):
225 return False
226
227 def __str__(self):
228 return 'node'
229
Craig Tiller99775822015-01-30 13:07:16 -0800230
Craig Tillerc7449162015-01-16 14:42:10 -0800231class PhpLanguage(object):
232
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100233 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700234 return [config.job_spec(['src/php/bin/run_tests.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700235 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
Craig Tillerc7449162015-01-16 14:42:10 -0800236
murgatroid99256d3df2015-09-21 16:58:02 -0700237 def pre_build_steps(self):
238 return []
239
Craig Tillerc7449162015-01-16 14:42:10 -0800240 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700241 return ['static_c', 'shared_c']
Craig Tillerc7449162015-01-16 14:42:10 -0800242
243 def build_steps(self):
244 return [['tools/run_tests/build_php.sh']]
245
murgatroid99132ce6a2015-03-04 17:29:14 -0800246 def supports_multi_config(self):
247 return False
248
249 def __str__(self):
250 return 'php'
251
Craig Tillerc7449162015-01-16 14:42:10 -0800252
Nathaniel Manista840615e2015-01-22 20:31:47 +0000253class PythonLanguage(object):
254
Craig Tiller49f61322015-03-03 13:02:11 -0800255 def __init__(self):
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700256 self._build_python_versions = ['2.7']
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700257 self._has_python_versions = []
Craig Tiller49f61322015-03-03 13:02:11 -0800258
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100259 def test_specs(self, config, travis):
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700260 environment = dict(_FORCE_ENVIRON_FOR_WRAPPERS)
261 environment['PYVER'] = '2.7'
262 return [config.job_spec(
263 ['tools/run_tests/run_python.sh'],
264 None,
265 environ=environment,
266 shortname='py.test',
267 )]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000268
murgatroid99256d3df2015-09-21 16:58:02 -0700269 def pre_build_steps(self):
270 return []
271
Nathaniel Manista840615e2015-01-22 20:31:47 +0000272 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700273 return ['static_c', 'grpc_python_plugin', 'shared_c']
Nathaniel Manista840615e2015-01-22 20:31:47 +0000274
275 def build_steps(self):
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700276 commands = []
277 for python_version in self._build_python_versions:
278 try:
279 with open(os.devnull, 'w') as output:
280 subprocess.check_call(['which', 'python' + python_version],
281 stdout=output, stderr=output)
282 commands.append(['tools/run_tests/build_python.sh', python_version])
283 self._has_python_versions.append(python_version)
284 except:
285 jobset.message('WARNING', 'Missing Python ' + python_version,
286 do_newline=True)
287 return commands
Nathaniel Manista840615e2015-01-22 20:31:47 +0000288
murgatroid99132ce6a2015-03-04 17:29:14 -0800289 def supports_multi_config(self):
290 return False
291
292 def __str__(self):
293 return 'python'
294
Craig Tillerd625d812015-04-08 15:52:35 -0700295
murgatroid996a4c4fa2015-02-27 12:08:57 -0800296class RubyLanguage(object):
297
298 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700299 return [config.job_spec(['tools/run_tests/run_ruby.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700300 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid996a4c4fa2015-02-27 12:08:57 -0800301
murgatroid99256d3df2015-09-21 16:58:02 -0700302 def pre_build_steps(self):
303 return []
304
murgatroid996a4c4fa2015-02-27 12:08:57 -0800305 def make_targets(self):
murgatroid99a43c14f2015-07-30 13:31:23 -0700306 return ['static_c']
murgatroid996a4c4fa2015-02-27 12:08:57 -0800307
308 def build_steps(self):
309 return [['tools/run_tests/build_ruby.sh']]
310
murgatroid99132ce6a2015-03-04 17:29:14 -0800311 def supports_multi_config(self):
312 return False
313
314 def __str__(self):
315 return 'ruby'
316
Craig Tillerd625d812015-04-08 15:52:35 -0700317
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800318class CSharpLanguage(object):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700319 def __init__(self):
Craig Tillerd50993d2015-08-05 08:04:36 -0700320 self.platform = platform_string()
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700321
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800322 def test_specs(self, config, travis):
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700323 assemblies = ['Grpc.Core.Tests',
324 'Grpc.Examples.Tests',
Jan Tattermusch9d67d8d2015-08-01 20:39:16 -0700325 'Grpc.HealthCheck.Tests',
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700326 'Grpc.IntegrationTesting']
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700327 if self.platform == 'windows':
328 cmd = 'tools\\run_tests\\run_csharp.bat'
329 else:
330 cmd = 'tools/run_tests/run_csharp.sh'
331 return [config.job_spec([cmd, assembly],
Craig Tiller4fc90032015-05-21 10:39:52 -0700332 None, shortname=assembly,
Craig Tiller06805272015-06-11 14:46:47 -0700333 environ=_FORCE_ENVIRON_FOR_WRAPPERS)
Craig Tillerd50993d2015-08-05 08:04:36 -0700334 for assembly in assemblies]
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800335
murgatroid99256d3df2015-09-21 16:58:02 -0700336 def pre_build_steps(self):
337 return []
338
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800339 def make_targets(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700340 # For Windows, this target doesn't really build anything,
341 # everything is build by buildall script later.
Craig Tillerd5904822015-08-31 21:30:58 -0700342 if self.platform == 'windows':
343 return []
344 else:
345 return ['grpc_csharp_ext']
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800346
347 def build_steps(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700348 if self.platform == 'windows':
349 return [['src\\csharp\\buildall.bat']]
350 else:
351 return [['tools/run_tests/build_csharp.sh']]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000352
murgatroid99132ce6a2015-03-04 17:29:14 -0800353 def supports_multi_config(self):
354 return False
355
356 def __str__(self):
357 return 'csharp'
358
Craig Tillerd625d812015-04-08 15:52:35 -0700359
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700360class ObjCLanguage(object):
361
362 def test_specs(self, config, travis):
363 return [config.job_spec(['src/objective-c/tests/run_tests.sh'], None,
364 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
365
murgatroid99256d3df2015-09-21 16:58:02 -0700366 def pre_build_steps(self):
367 return []
368
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700369 def make_targets(self):
Jorge Canizalesd0b32e92015-07-30 23:08:43 -0700370 return ['grpc_objective_c_plugin', 'interop_server']
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700371
372 def build_steps(self):
Jorge Canizalesd0b32e92015-07-30 23:08:43 -0700373 return [['src/objective-c/tests/build_tests.sh']]
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700374
375 def supports_multi_config(self):
376 return False
377
378 def __str__(self):
379 return 'objc'
380
381
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100382class Sanity(object):
383
384 def test_specs(self, config, travis):
Craig Tillerf75fc122015-06-25 06:58:00 -0700385 return [config.job_spec('tools/run_tests/run_sanity.sh', None),
386 config.job_spec('tools/run_tests/check_sources_and_headers.py', None)]
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100387
murgatroid99256d3df2015-09-21 16:58:02 -0700388 def pre_build_steps(self):
389 return []
390
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100391 def make_targets(self):
392 return ['run_dep_checks']
393
394 def build_steps(self):
395 return []
396
397 def supports_multi_config(self):
398 return False
399
400 def __str__(self):
401 return 'sanity'
402
Nicolas "Pixel" Noblee55cd7f2015-04-14 17:59:13 +0200403
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100404class Build(object):
405
406 def test_specs(self, config, travis):
407 return []
408
murgatroid99256d3df2015-09-21 16:58:02 -0700409 def pre_build_steps(self):
410 return []
411
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100412 def make_targets(self):
Nicolas "Pixel" Noblec23827b2015-04-23 06:17:55 +0200413 return ['static']
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100414
415 def build_steps(self):
416 return []
417
418 def supports_multi_config(self):
419 return True
420
421 def __str__(self):
422 return self.make_target
423
424
Craig Tiller738c3342015-01-12 14:28:33 -0800425# different configurations we can run under
426_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -0800427 'dbg': SimpleConfig('dbg'),
428 'opt': SimpleConfig('opt'),
Craig Tillerb2ea0b92015-08-26 13:06:53 -0700429 'tsan': SimpleConfig('tsan', timeout_seconds=10*60, environ={
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700430 'TSAN_OPTIONS': 'suppressions=tools/tsan_suppressions.txt:halt_on_error=1:second_deadlock_stack=1'}),
Craig Tiller4a71ce22015-08-26 15:47:55 -0700431 'msan': SimpleConfig('msan', timeout_seconds=7*60),
Craig Tiller96bd5f62015-02-13 09:04:13 -0800432 'ubsan': SimpleConfig('ubsan'),
Craig Tillerb2ea0b92015-08-26 13:06:53 -0700433 'asan': SimpleConfig('asan', timeout_seconds=7*60, environ={
Craig Tillerd4b13622015-05-29 09:10:10 -0700434 'ASAN_OPTIONS': 'detect_leaks=1:color=always:suppressions=tools/tsan_suppressions.txt',
435 'LSAN_OPTIONS': 'report_objects=1'}),
Craig Tiller810725c2015-05-12 09:44:41 -0700436 'asan-noleaks': SimpleConfig('asan', environ={
437 'ASAN_OPTIONS': 'detect_leaks=0:color=always:suppressions=tools/tsan_suppressions.txt'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800438 'gcov': SimpleConfig('gcov'),
Craig Tiller1a305b12015-02-18 13:37:06 -0800439 'memcheck': ValgrindConfig('valgrind', 'memcheck', ['--leak-check=full']),
Craig Tillerb50d1662015-01-15 17:28:21 -0800440 'helgrind': ValgrindConfig('dbg', 'helgrind')
441 }
Craig Tiller738c3342015-01-12 14:28:33 -0800442
443
Nicolas "Pixel" Noble1fb5e822015-03-16 06:20:37 +0100444_DEFAULT = ['opt']
Craig Tillerc7449162015-01-16 14:42:10 -0800445_LANGUAGES = {
Craig Tillere9c959d2015-01-18 10:23:26 -0800446 'c++': CLanguage('cxx', 'c++'),
447 'c': CLanguage('c', 'c'),
murgatroid99cf08daf2015-09-21 15:33:16 -0700448 'gyp': GYPCLanguage(),
murgatroid992c8d5162015-01-26 10:41:21 -0800449 'node': NodeLanguage(),
Nathaniel Manista840615e2015-01-22 20:31:47 +0000450 'php': PhpLanguage(),
451 'python': PythonLanguage(),
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800452 'ruby': RubyLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100453 'csharp': CSharpLanguage(),
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700454 'objc' : ObjCLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100455 'sanity': Sanity(),
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100456 'build': Build(),
Craig Tillereb272bc2015-01-30 13:13:14 -0800457 }
Nicolas Nobleddef2462015-01-06 18:08:25 -0800458
Craig Tiller7bb3efd2015-09-01 08:04:03 -0700459_WINDOWS_CONFIG = {
460 'dbg': 'Debug',
461 'opt': 'Release',
462 }
463
Nicolas Nobleddef2462015-01-06 18:08:25 -0800464# parse command line
465argp = argparse.ArgumentParser(description='Run grpc tests.')
466argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -0800467 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -0800468 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -0800469 default=_DEFAULT)
David Garcia Quintase90cd372015-05-31 18:15:26 -0700470
471def runs_per_test_type(arg_str):
472 """Auxilary function to parse the "runs_per_test" flag.
473
474 Returns:
475 A positive integer or 0, the latter indicating an infinite number of
476 runs.
477
478 Raises:
479 argparse.ArgumentTypeError: Upon invalid input.
480 """
481 if arg_str == 'inf':
482 return 0
483 try:
484 n = int(arg_str)
485 if n <= 0: raise ValueError
Craig Tiller50e53e22015-06-01 20:18:21 -0700486 return n
David Garcia Quintase90cd372015-05-31 18:15:26 -0700487 except:
488 msg = "'{}' isn't a positive integer or 'inf'".format(arg_str)
489 raise argparse.ArgumentTypeError(msg)
490argp.add_argument('-n', '--runs_per_test', default=1, type=runs_per_test_type,
491 help='A positive integer or "inf". If "inf", all tests will run in an '
492 'infinite loop. Especially useful in combination with "-f"')
Craig Tillerfe406ec2015-02-24 13:55:12 -0800493argp.add_argument('-r', '--regex', default='.*', type=str)
Craig Tiller83762ac2015-05-22 14:04:06 -0700494argp.add_argument('-j', '--jobs', default=2 * multiprocessing.cpu_count(), type=int)
Craig Tiller8451e872015-02-27 09:25:51 -0800495argp.add_argument('-s', '--slowdown', default=1.0, type=float)
ctiller3040cb72015-01-07 12:13:17 -0800496argp.add_argument('-f', '--forever',
497 default=False,
498 action='store_const',
499 const=True)
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100500argp.add_argument('-t', '--travis',
501 default=False,
502 action='store_const',
503 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800504argp.add_argument('--newline_on_success',
505 default=False,
506 action='store_const',
507 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -0800508argp.add_argument('-l', '--language',
Craig Tiller60f15e62015-05-13 09:05:17 -0700509 choices=['all'] + sorted(_LANGUAGES.keys()),
Craig Tiller686fb262015-01-15 07:39:09 -0800510 nargs='+',
Craig Tiller60f15e62015-05-13 09:05:17 -0700511 default=['all'])
Craig Tillercd43da82015-05-29 08:41:29 -0700512argp.add_argument('-S', '--stop_on_failure',
513 default=False,
514 action='store_const',
515 const=True)
Craig Tiller234b6e72015-05-23 10:12:40 -0700516argp.add_argument('-a', '--antagonists', default=0, type=int)
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200517argp.add_argument('-x', '--xml_report', default=None, type=str,
518 help='Generates a JUnit-compatible XML report')
Nicolas Nobleddef2462015-01-06 18:08:25 -0800519args = argp.parse_args()
520
521# grab config
Craig Tiller738c3342015-01-12 14:28:33 -0800522run_configs = set(_CONFIGS[cfg]
523 for cfg in itertools.chain.from_iterable(
524 _CONFIGS.iterkeys() if x == 'all' else [x]
525 for x in args.config))
526build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tillerf1973b02015-01-16 12:32:13 -0800527
Craig Tiller06805272015-06-11 14:46:47 -0700528if args.travis:
529 _FORCE_ENVIRON_FOR_WRAPPERS = {'GRPC_TRACE': 'surface,batch'}
530
Craig Tiller60f15e62015-05-13 09:05:17 -0700531languages = set(_LANGUAGES[l]
532 for l in itertools.chain.from_iterable(
533 _LANGUAGES.iterkeys() if x == 'all' else [x]
534 for x in args.language))
murgatroid99132ce6a2015-03-04 17:29:14 -0800535
536if len(build_configs) > 1:
537 for language in languages:
538 if not language.supports_multi_config():
539 print language, 'does not support multiple build configurations'
540 sys.exit(1)
541
Craig Tiller5058c692015-04-08 09:42:04 -0700542if platform.system() == 'Windows':
543 def make_jobspec(cfg, targets):
Craig Tillerfc3c0c42015-09-01 16:47:54 -0700544 extra_args = []
Craig Tillerb5391e12015-09-03 14:35:18 -0700545 # better do parallel compilation
546 extra_args.extend(["/m"])
547 # disable PDB generation: it's broken, and we don't need it during CI
548 extra_args.extend(["/p:GenerateDebugInformation=false", "/p:DebugInformationFormat=None"])
Craig Tiller6fd23842015-09-01 07:36:31 -0700549 return [
murgatroid99cf08daf2015-09-21 15:33:16 -0700550 jobset.JobSpec(['vsprojects\\build.bat',
551 'vsprojects\\%s.sln' % target,
Craig Tillerfc3c0c42015-09-01 16:47:54 -0700552 '/p:Configuration=%s' % _WINDOWS_CONFIG[cfg]] +
553 extra_args,
Craig Tillerdfc3eee2015-09-01 16:32:16 -0700554 shell=True, timeout_seconds=90*60)
Craig Tiller6fd23842015-09-01 07:36:31 -0700555 for target in targets]
Craig Tiller5058c692015-04-08 09:42:04 -0700556else:
557 def make_jobspec(cfg, targets):
Craig Tiller6fd23842015-09-01 07:36:31 -0700558 return [jobset.JobSpec([os.getenv('MAKE', 'make'),
559 '-j', '%d' % (multiprocessing.cpu_count() + 1),
560 'EXTRA_DEFINES=GRPC_TEST_SLOWDOWN_MACHINE_FACTOR=%f' %
561 args.slowdown,
562 'CONFIG=%s' % cfg] + targets,
563 timeout_seconds=30*60)]
Craig Tiller5058c692015-04-08 09:42:04 -0700564
Craig Tillerbd4e3782015-09-01 06:48:55 -0700565make_targets = list(set(itertools.chain.from_iterable(
566 l.make_targets() for l in languages)))
567build_steps = []
murgatroid99256d3df2015-09-21 16:58:02 -0700568build_steps.extend(set(
569 jobset.JobSpec(cmdline, environ={'CONFIG': cfg})
570 for cfg in build_configs
571 for l in languages
572 for cmdline in l.pre_build_steps()))
Craig Tillerbd4e3782015-09-01 06:48:55 -0700573if make_targets:
Craig Tiller6fd23842015-09-01 07:36:31 -0700574 make_commands = itertools.chain.from_iterable(make_jobspec(cfg, make_targets) for cfg in build_configs)
575 build_steps.extend(set(make_commands))
Craig Tiller5058c692015-04-08 09:42:04 -0700576build_steps.extend(set(
murgatroid99132ce6a2015-03-04 17:29:14 -0800577 jobset.JobSpec(cmdline, environ={'CONFIG': cfg})
578 for cfg in build_configs
Craig Tiller547db2b2015-01-30 14:08:39 -0800579 for l in languages
Craig Tiller533b1a22015-05-29 08:41:29 -0700580 for cmdline in l.build_steps()))
Craig Tillerf1973b02015-01-16 12:32:13 -0800581
Nicolas Nobleddef2462015-01-06 18:08:25 -0800582runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -0800583forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -0800584
Nicolas Nobleddef2462015-01-06 18:08:25 -0800585
Craig Tiller71735182015-01-15 17:07:13 -0800586class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800587 """Cache for running tests."""
588
David Klempner25739582015-02-11 15:57:32 -0800589 def __init__(self, use_cache_results):
Craig Tiller71735182015-01-15 17:07:13 -0800590 self._last_successful_run = {}
David Klempner25739582015-02-11 15:57:32 -0800591 self._use_cache_results = use_cache_results
Craig Tiller69cd2372015-06-11 09:38:09 -0700592 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800593
594 def should_run(self, cmdline, bin_hash):
Craig Tiller71735182015-01-15 17:07:13 -0800595 if cmdline not in self._last_successful_run:
596 return True
597 if self._last_successful_run[cmdline] != bin_hash:
598 return True
David Klempner25739582015-02-11 15:57:32 -0800599 if not self._use_cache_results:
600 return True
Craig Tiller71735182015-01-15 17:07:13 -0800601 return False
602
603 def finished(self, cmdline, bin_hash):
Craig Tiller547db2b2015-01-30 14:08:39 -0800604 self._last_successful_run[cmdline] = bin_hash
Craig Tiller69cd2372015-06-11 09:38:09 -0700605 if time.time() - self._last_save > 1:
606 self.save()
Craig Tiller71735182015-01-15 17:07:13 -0800607
608 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800609 return [{'cmdline': k, 'hash': v}
610 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800611
612 def parse(self, exdump):
613 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
614
615 def save(self):
616 with open('.run_tests_cache', 'w') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800617 f.write(json.dumps(self.dump()))
Craig Tiller69cd2372015-06-11 09:38:09 -0700618 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800619
Craig Tiller1cc11db2015-01-15 22:50:50 -0800620 def maybe_load(self):
621 if os.path.exists('.run_tests_cache'):
622 with open('.run_tests_cache') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800623 self.parse(json.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800624
625
Craig Tillerf53d9c82015-08-04 14:19:43 -0700626def _start_port_server(port_server_port):
627 # check if a compatible port server is running
628 # if incompatible (version mismatch) ==> start a new one
629 # if not running ==> start a new one
630 # otherwise, leave it up
631 try:
Craig Tillerabd37fd2015-08-26 07:54:01 -0700632 version = urllib2.urlopen('http://localhost:%d/version' % port_server_port,
633 timeout=1).read()
Craig Tillerf53d9c82015-08-04 14:19:43 -0700634 running = True
635 except Exception:
636 running = False
637 if running:
638 with open('tools/run_tests/port_server.py') as f:
639 current_version = hashlib.sha1(f.read()).hexdigest()
640 running = (version == current_version)
641 if not running:
Craig Tilleref125592015-08-05 07:41:35 -0700642 urllib2.urlopen('http://localhost:%d/quit' % port_server_port).read()
643 time.sleep(1)
Craig Tillerf53d9c82015-08-04 14:19:43 -0700644 if not running:
645 port_log = open('portlog.txt', 'w')
646 port_server = subprocess.Popen(
Craig Tiller9a0c10e2015-08-06 15:47:32 -0700647 ['python', 'tools/run_tests/port_server.py', '-p', '%d' % port_server_port],
Craig Tillerf53d9c82015-08-04 14:19:43 -0700648 stderr=subprocess.STDOUT,
649 stdout=port_log)
Craig Tiller8b5f4dc2015-08-26 08:02:01 -0700650 # ensure port server is up
Craig Tillerabd37fd2015-08-26 07:54:01 -0700651 waits = 0
Craig Tillerf53d9c82015-08-04 14:19:43 -0700652 while True:
Craig Tillerabd37fd2015-08-26 07:54:01 -0700653 if waits > 10:
654 port_server.kill()
655 print "port_server failed to start"
656 sys.exit(1)
Craig Tillerf53d9c82015-08-04 14:19:43 -0700657 try:
Craig Tillerabd37fd2015-08-26 07:54:01 -0700658 urllib2.urlopen('http://localhost:%d/get' % port_server_port,
659 timeout=1).read()
Craig Tillerf53d9c82015-08-04 14:19:43 -0700660 break
661 except urllib2.URLError:
Craig Tillerabd37fd2015-08-26 07:54:01 -0700662 print "waiting for port_server"
Craig Tillerf53d9c82015-08-04 14:19:43 -0700663 time.sleep(0.5)
Craig Tillerabd37fd2015-08-26 07:54:01 -0700664 waits += 1
Craig Tillerf53d9c82015-08-04 14:19:43 -0700665 except:
666 port_server.kill()
667 raise
668
669
670def _build_and_run(
671 check_cancelled, newline_on_success, travis, cache, xml_report=None):
ctiller3040cb72015-01-07 12:13:17 -0800672 """Do one pass of building & running tests."""
murgatroid99666450e2015-01-26 13:03:31 -0800673 # build latest sequentially
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100674 if not jobset.run(build_steps, maxjobs=1,
675 newline_on_success=newline_on_success, travis=travis):
Craig Tillerd86a3942015-01-14 12:48:54 -0800676 return 1
ctiller3040cb72015-01-07 12:13:17 -0800677
Craig Tiller234b6e72015-05-23 10:12:40 -0700678 # start antagonists
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700679 antagonists = [subprocess.Popen(['tools/run_tests/antagonist.py'])
Craig Tiller234b6e72015-05-23 10:12:40 -0700680 for _ in range(0, args.antagonists)]
Craig Tillerf53d9c82015-08-04 14:19:43 -0700681 port_server_port = 9999
682 _start_port_server(port_server_port)
Craig Tiller234b6e72015-05-23 10:12:40 -0700683 try:
David Garcia Quintase90cd372015-05-31 18:15:26 -0700684 infinite_runs = runs_per_test == 0
yang-g6c1fdc62015-08-18 11:57:42 -0700685 one_run = set(
686 spec
687 for config in run_configs
688 for language in languages
689 for spec in language.test_specs(config, args.travis)
690 if re.search(args.regex, spec.shortname))
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700691 # When running on travis, we want out test runs to be as similar as possible
692 # for reproducibility purposes.
693 if travis:
694 massaged_one_run = sorted(one_run, key=lambda x: x.shortname)
695 else:
696 # whereas otherwise, we want to shuffle things up to give all tests a
697 # chance to run.
698 massaged_one_run = list(one_run) # random.shuffle needs an indexable seq.
699 random.shuffle(massaged_one_run) # which it modifies in-place.
Craig Tillerf7b7c892015-06-22 14:33:25 -0700700 if infinite_runs:
701 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 -0700702 runs_sequence = (itertools.repeat(massaged_one_run) if infinite_runs
703 else itertools.repeat(massaged_one_run, runs_per_test))
David Garcia Quintase90cd372015-05-31 18:15:26 -0700704 all_runs = itertools.chain.from_iterable(runs_sequence)
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200705
706 root = ET.Element('testsuites') if xml_report else None
707 testsuite = ET.SubElement(root, 'testsuite', id='1', package='grpc', name='tests') if xml_report else None
708
Craig Tiller234b6e72015-05-23 10:12:40 -0700709 if not jobset.run(all_runs, check_cancelled,
710 newline_on_success=newline_on_success, travis=travis,
David Garcia Quintase90cd372015-05-31 18:15:26 -0700711 infinite_runs=infinite_runs,
Craig Tillerda2220a2015-05-27 07:50:53 -0700712 maxjobs=args.jobs,
Craig Tillercd43da82015-05-29 08:41:29 -0700713 stop_on_failure=args.stop_on_failure,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200714 cache=cache if not xml_report else None,
Craig Tillerf53d9c82015-08-04 14:19:43 -0700715 xml_report=testsuite,
716 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port}):
Craig Tiller234b6e72015-05-23 10:12:40 -0700717 return 2
718 finally:
719 for antagonist in antagonists:
720 antagonist.kill()
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200721 if xml_report:
722 tree = ET.ElementTree(root)
723 tree.write(xml_report, encoding='UTF-8')
Craig Tillerd86a3942015-01-14 12:48:54 -0800724
Craig Tiller69cd2372015-06-11 09:38:09 -0700725 if cache: cache.save()
726
Craig Tillerd86a3942015-01-14 12:48:54 -0800727 return 0
ctiller3040cb72015-01-07 12:13:17 -0800728
729
David Klempner25739582015-02-11 15:57:32 -0800730test_cache = TestCache(runs_per_test == 1)
Craig Tiller547db2b2015-01-30 14:08:39 -0800731test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800732
ctiller3040cb72015-01-07 12:13:17 -0800733if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800734 success = True
ctiller3040cb72015-01-07 12:13:17 -0800735 while True:
Craig Tiller42bc87c2015-02-23 08:50:19 -0800736 dw = watch_dirs.DirWatcher(['src', 'include', 'test', 'examples'])
ctiller3040cb72015-01-07 12:13:17 -0800737 initial_time = dw.most_recent_change()
738 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800739 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800740 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800741 newline_on_success=False,
Craig Tiller9a5a9402015-04-16 10:39:50 -0700742 travis=args.travis,
Craig Tiller71735182015-01-15 17:07:13 -0800743 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800744 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800745 jobset.message('SUCCESS',
746 'All tests are now passing properly',
747 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800748 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800749 while not have_files_changed():
750 time.sleep(1)
751else:
Craig Tiller71735182015-01-15 17:07:13 -0800752 result = _build_and_run(check_cancelled=lambda: False,
753 newline_on_success=args.newline_on_success,
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100754 travis=args.travis,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200755 cache=test_cache,
756 xml_report=args.xml_report)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800757 if result == 0:
758 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
759 else:
760 jobset.message('FAILED', 'Some tests failed', do_newline=True)
761 sys.exit(result)