blob: 4f8aad493163411f1a5c04178a80bd965c267d2d [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:
murgatroid9989899b12015-09-22 09:14:48 -0700127 js = json.load(f)
murgatroid99cf08daf2015-09-21 15:33:16 -0700128 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
murgatroid99fafeeb32015-09-22 09:13:03 -0700135
Craig Tillerc7449162015-01-16 14:42:10 -0800136class CLanguage(object):
137
Craig Tillere9c959d2015-01-18 10:23:26 -0800138 def __init__(self, make_target, test_lang):
Craig Tillerc7449162015-01-16 14:42:10 -0800139 self.make_target = make_target
Craig Tillerd50993d2015-08-05 08:04:36 -0700140 self.platform = platform_string()
Craig Tiller711bbe62015-08-19 12:35:16 -0700141 self.test_lang = test_lang
Craig Tillerc7449162015-01-16 14:42:10 -0800142
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100143 def test_specs(self, config, travis):
Craig Tiller547db2b2015-01-30 14:08:39 -0800144 out = []
murgatroid99cf08daf2015-09-21 15:33:16 -0700145 binaries = get_c_tests(travis, self.test_lang)
Craig Tiller711bbe62015-08-19 12:35:16 -0700146 for target in binaries:
murgatroid99cf08daf2015-09-21 15:33:16 -0700147 if config.build_config in tgt['exclude_configs']:
murgatroid99fafeeb32015-09-22 09:13:03 -0700148 continue
Nicolas Noblee1445362015-05-11 17:40:26 -0700149 if self.platform == 'windows':
Craig Tillerf4182602015-09-01 12:23:16 -0700150 binary = 'vsprojects/%s/%s.exe' % (
151 _WINDOWS_CONFIG[config.build_config], target['name'])
Nicolas Noblee1445362015-05-11 17:40:26 -0700152 else:
153 binary = 'bins/%s/%s' % (config.build_config, target['name'])
yang-g6c1fdc62015-08-18 11:57:42 -0700154 if os.path.isfile(binary):
155 out.append(config.job_spec([binary], [binary]))
156 else:
157 print "\nWARNING: binary not found, skipping", binary
Nicolas Noblee1445362015-05-11 17:40:26 -0700158 return sorted(out)
Craig Tillerc7449162015-01-16 14:42:10 -0800159
160 def make_targets(self):
Craig Tiller7bb3efd2015-09-01 08:04:03 -0700161 if platform_string() == 'windows':
162 # don't build tools on windows just yet
163 return ['buildtests_%s' % self.make_target]
Craig Tiller7552f0f2015-06-19 17:46:20 -0700164 return ['buildtests_%s' % self.make_target, 'tools_%s' % self.make_target]
Craig Tillerc7449162015-01-16 14:42:10 -0800165
murgatroid99256d3df2015-09-21 16:58:02 -0700166 def pre_build_steps(self):
167 return []
168
Craig Tillerc7449162015-01-16 14:42:10 -0800169 def build_steps(self):
170 return []
171
murgatroid99132ce6a2015-03-04 17:29:14 -0800172 def supports_multi_config(self):
173 return True
174
175 def __str__(self):
176 return self.make_target
177
murgatroid99fafeeb32015-09-22 09:13:03 -0700178
murgatroid99cf08daf2015-09-21 15:33:16 -0700179def gyp_test_paths(travis, config=None):
180 binaries = get_c_tests(travis, 'c')
181 out = []
182 for target in binaries:
murgatroid99fddac962015-09-22 09:20:11 -0700183 if config is not None and config.build_config in target['exclude_configs']:
murgatroid99cf08daf2015-09-21 15:33:16 -0700184 continue
185 binary = 'out/Debug/%s' % target['name']
186 out.append(binary)
187 return sorted(out)
188
murgatroid99fafeeb32015-09-22 09:13:03 -0700189
murgatroid99cf08daf2015-09-21 15:33:16 -0700190class GYPCLanguage(object):
191
192 def test_specs(self, config, travis):
193 return [config.job_spec([binary], [binary])
194 for binary in gyp_test_paths(travis, config)]
195
murgatroid99256d3df2015-09-21 16:58:02 -0700196 def pre_build_steps(self):
197 return [['gyp', '--depth=.', 'grpc.gyp']]
198
murgatroid99cf08daf2015-09-21 15:33:16 -0700199 def make_targets(self):
200 return gyp_test_paths(False)
201
202 def build_steps(self):
203 return []
204
205 def supports_multi_config(self):
206 return False
207
208 def __str__(self):
209 return 'gyp'
Craig Tiller99775822015-01-30 13:07:16 -0800210
murgatroid99fafeeb32015-09-22 09:13:03 -0700211
murgatroid992c8d5162015-01-26 10:41:21 -0800212class NodeLanguage(object):
213
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100214 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700215 return [config.job_spec(['tools/run_tests/run_node.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700216 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid992c8d5162015-01-26 10:41:21 -0800217
murgatroid99256d3df2015-09-21 16:58:02 -0700218 def pre_build_steps(self):
219 return []
220
murgatroid992c8d5162015-01-26 10:41:21 -0800221 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700222 return ['static_c', 'shared_c']
murgatroid992c8d5162015-01-26 10:41:21 -0800223
224 def build_steps(self):
225 return [['tools/run_tests/build_node.sh']]
Craig Tillerc7449162015-01-16 14:42:10 -0800226
murgatroid99132ce6a2015-03-04 17:29:14 -0800227 def supports_multi_config(self):
228 return False
229
230 def __str__(self):
231 return 'node'
232
Craig Tiller99775822015-01-30 13:07:16 -0800233
Craig Tillerc7449162015-01-16 14:42:10 -0800234class PhpLanguage(object):
235
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100236 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700237 return [config.job_spec(['src/php/bin/run_tests.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700238 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
Craig Tillerc7449162015-01-16 14:42:10 -0800239
murgatroid99256d3df2015-09-21 16:58:02 -0700240 def pre_build_steps(self):
241 return []
242
Craig Tillerc7449162015-01-16 14:42:10 -0800243 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700244 return ['static_c', 'shared_c']
Craig Tillerc7449162015-01-16 14:42:10 -0800245
246 def build_steps(self):
247 return [['tools/run_tests/build_php.sh']]
248
murgatroid99132ce6a2015-03-04 17:29:14 -0800249 def supports_multi_config(self):
250 return False
251
252 def __str__(self):
253 return 'php'
254
Craig Tillerc7449162015-01-16 14:42:10 -0800255
Nathaniel Manista840615e2015-01-22 20:31:47 +0000256class PythonLanguage(object):
257
Craig Tiller49f61322015-03-03 13:02:11 -0800258 def __init__(self):
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700259 self._build_python_versions = ['2.7']
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700260 self._has_python_versions = []
Craig Tiller49f61322015-03-03 13:02:11 -0800261
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100262 def test_specs(self, config, travis):
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700263 environment = dict(_FORCE_ENVIRON_FOR_WRAPPERS)
264 environment['PYVER'] = '2.7'
265 return [config.job_spec(
266 ['tools/run_tests/run_python.sh'],
267 None,
268 environ=environment,
269 shortname='py.test',
270 )]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000271
murgatroid99256d3df2015-09-21 16:58:02 -0700272 def pre_build_steps(self):
273 return []
274
Nathaniel Manista840615e2015-01-22 20:31:47 +0000275 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700276 return ['static_c', 'grpc_python_plugin', 'shared_c']
Nathaniel Manista840615e2015-01-22 20:31:47 +0000277
278 def build_steps(self):
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700279 commands = []
280 for python_version in self._build_python_versions:
281 try:
282 with open(os.devnull, 'w') as output:
283 subprocess.check_call(['which', 'python' + python_version],
284 stdout=output, stderr=output)
285 commands.append(['tools/run_tests/build_python.sh', python_version])
286 self._has_python_versions.append(python_version)
287 except:
288 jobset.message('WARNING', 'Missing Python ' + python_version,
289 do_newline=True)
290 return commands
Nathaniel Manista840615e2015-01-22 20:31:47 +0000291
murgatroid99132ce6a2015-03-04 17:29:14 -0800292 def supports_multi_config(self):
293 return False
294
295 def __str__(self):
296 return 'python'
297
Craig Tillerd625d812015-04-08 15:52:35 -0700298
murgatroid996a4c4fa2015-02-27 12:08:57 -0800299class RubyLanguage(object):
300
301 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700302 return [config.job_spec(['tools/run_tests/run_ruby.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700303 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid996a4c4fa2015-02-27 12:08:57 -0800304
murgatroid99256d3df2015-09-21 16:58:02 -0700305 def pre_build_steps(self):
306 return []
307
murgatroid996a4c4fa2015-02-27 12:08:57 -0800308 def make_targets(self):
murgatroid99a43c14f2015-07-30 13:31:23 -0700309 return ['static_c']
murgatroid996a4c4fa2015-02-27 12:08:57 -0800310
311 def build_steps(self):
312 return [['tools/run_tests/build_ruby.sh']]
313
murgatroid99132ce6a2015-03-04 17:29:14 -0800314 def supports_multi_config(self):
315 return False
316
317 def __str__(self):
318 return 'ruby'
319
Craig Tillerd625d812015-04-08 15:52:35 -0700320
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800321class CSharpLanguage(object):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700322 def __init__(self):
Craig Tillerd50993d2015-08-05 08:04:36 -0700323 self.platform = platform_string()
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700324
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800325 def test_specs(self, config, travis):
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700326 assemblies = ['Grpc.Core.Tests',
327 'Grpc.Examples.Tests',
Jan Tattermusch9d67d8d2015-08-01 20:39:16 -0700328 'Grpc.HealthCheck.Tests',
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700329 'Grpc.IntegrationTesting']
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700330 if self.platform == 'windows':
331 cmd = 'tools\\run_tests\\run_csharp.bat'
332 else:
333 cmd = 'tools/run_tests/run_csharp.sh'
334 return [config.job_spec([cmd, assembly],
Craig Tiller4fc90032015-05-21 10:39:52 -0700335 None, shortname=assembly,
Craig Tiller06805272015-06-11 14:46:47 -0700336 environ=_FORCE_ENVIRON_FOR_WRAPPERS)
Craig Tillerd50993d2015-08-05 08:04:36 -0700337 for assembly in assemblies]
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800338
murgatroid99256d3df2015-09-21 16:58:02 -0700339 def pre_build_steps(self):
340 return []
341
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800342 def make_targets(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700343 # For Windows, this target doesn't really build anything,
344 # everything is build by buildall script later.
Craig Tillerd5904822015-08-31 21:30:58 -0700345 if self.platform == 'windows':
346 return []
347 else:
348 return ['grpc_csharp_ext']
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800349
350 def build_steps(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700351 if self.platform == 'windows':
352 return [['src\\csharp\\buildall.bat']]
353 else:
354 return [['tools/run_tests/build_csharp.sh']]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000355
murgatroid99132ce6a2015-03-04 17:29:14 -0800356 def supports_multi_config(self):
357 return False
358
359 def __str__(self):
360 return 'csharp'
361
Craig Tillerd625d812015-04-08 15:52:35 -0700362
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700363class ObjCLanguage(object):
364
365 def test_specs(self, config, travis):
366 return [config.job_spec(['src/objective-c/tests/run_tests.sh'], None,
367 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
368
murgatroid99256d3df2015-09-21 16:58:02 -0700369 def pre_build_steps(self):
370 return []
371
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700372 def make_targets(self):
Jorge Canizalesd0b32e92015-07-30 23:08:43 -0700373 return ['grpc_objective_c_plugin', 'interop_server']
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700374
375 def build_steps(self):
Jorge Canizalesd0b32e92015-07-30 23:08:43 -0700376 return [['src/objective-c/tests/build_tests.sh']]
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700377
378 def supports_multi_config(self):
379 return False
380
381 def __str__(self):
382 return 'objc'
383
384
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100385class Sanity(object):
386
387 def test_specs(self, config, travis):
Craig Tillerf75fc122015-06-25 06:58:00 -0700388 return [config.job_spec('tools/run_tests/run_sanity.sh', None),
389 config.job_spec('tools/run_tests/check_sources_and_headers.py', None)]
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100390
murgatroid99256d3df2015-09-21 16:58:02 -0700391 def pre_build_steps(self):
392 return []
393
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100394 def make_targets(self):
395 return ['run_dep_checks']
396
397 def build_steps(self):
398 return []
399
400 def supports_multi_config(self):
401 return False
402
403 def __str__(self):
404 return 'sanity'
405
Nicolas "Pixel" Noblee55cd7f2015-04-14 17:59:13 +0200406
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100407class Build(object):
408
409 def test_specs(self, config, travis):
410 return []
411
murgatroid99256d3df2015-09-21 16:58:02 -0700412 def pre_build_steps(self):
413 return []
414
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100415 def make_targets(self):
Nicolas "Pixel" Noblec23827b2015-04-23 06:17:55 +0200416 return ['static']
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100417
418 def build_steps(self):
419 return []
420
421 def supports_multi_config(self):
422 return True
423
424 def __str__(self):
425 return self.make_target
426
427
Craig Tiller738c3342015-01-12 14:28:33 -0800428# different configurations we can run under
429_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -0800430 'dbg': SimpleConfig('dbg'),
431 'opt': SimpleConfig('opt'),
Craig Tillerb2ea0b92015-08-26 13:06:53 -0700432 'tsan': SimpleConfig('tsan', timeout_seconds=10*60, environ={
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700433 'TSAN_OPTIONS': 'suppressions=tools/tsan_suppressions.txt:halt_on_error=1:second_deadlock_stack=1'}),
Craig Tiller4a71ce22015-08-26 15:47:55 -0700434 'msan': SimpleConfig('msan', timeout_seconds=7*60),
Craig Tiller96bd5f62015-02-13 09:04:13 -0800435 'ubsan': SimpleConfig('ubsan'),
Craig Tillerb2ea0b92015-08-26 13:06:53 -0700436 'asan': SimpleConfig('asan', timeout_seconds=7*60, environ={
Craig Tillerd4b13622015-05-29 09:10:10 -0700437 'ASAN_OPTIONS': 'detect_leaks=1:color=always:suppressions=tools/tsan_suppressions.txt',
438 'LSAN_OPTIONS': 'report_objects=1'}),
Craig Tiller810725c2015-05-12 09:44:41 -0700439 'asan-noleaks': SimpleConfig('asan', environ={
440 'ASAN_OPTIONS': 'detect_leaks=0:color=always:suppressions=tools/tsan_suppressions.txt'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800441 'gcov': SimpleConfig('gcov'),
Craig Tiller1a305b12015-02-18 13:37:06 -0800442 'memcheck': ValgrindConfig('valgrind', 'memcheck', ['--leak-check=full']),
Craig Tillerb50d1662015-01-15 17:28:21 -0800443 'helgrind': ValgrindConfig('dbg', 'helgrind')
444 }
Craig Tiller738c3342015-01-12 14:28:33 -0800445
446
Nicolas "Pixel" Noble1fb5e822015-03-16 06:20:37 +0100447_DEFAULT = ['opt']
Craig Tillerc7449162015-01-16 14:42:10 -0800448_LANGUAGES = {
Craig Tillere9c959d2015-01-18 10:23:26 -0800449 'c++': CLanguage('cxx', 'c++'),
450 'c': CLanguage('c', 'c'),
murgatroid99cf08daf2015-09-21 15:33:16 -0700451 'gyp': GYPCLanguage(),
murgatroid992c8d5162015-01-26 10:41:21 -0800452 'node': NodeLanguage(),
Nathaniel Manista840615e2015-01-22 20:31:47 +0000453 'php': PhpLanguage(),
454 'python': PythonLanguage(),
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800455 'ruby': RubyLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100456 'csharp': CSharpLanguage(),
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700457 'objc' : ObjCLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100458 'sanity': Sanity(),
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100459 'build': Build(),
Craig Tillereb272bc2015-01-30 13:13:14 -0800460 }
Nicolas Nobleddef2462015-01-06 18:08:25 -0800461
Craig Tiller7bb3efd2015-09-01 08:04:03 -0700462_WINDOWS_CONFIG = {
463 'dbg': 'Debug',
464 'opt': 'Release',
465 }
466
Nicolas Nobleddef2462015-01-06 18:08:25 -0800467# parse command line
468argp = argparse.ArgumentParser(description='Run grpc tests.')
469argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -0800470 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -0800471 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -0800472 default=_DEFAULT)
David Garcia Quintase90cd372015-05-31 18:15:26 -0700473
474def runs_per_test_type(arg_str):
475 """Auxilary function to parse the "runs_per_test" flag.
476
477 Returns:
478 A positive integer or 0, the latter indicating an infinite number of
479 runs.
480
481 Raises:
482 argparse.ArgumentTypeError: Upon invalid input.
483 """
484 if arg_str == 'inf':
485 return 0
486 try:
487 n = int(arg_str)
488 if n <= 0: raise ValueError
Craig Tiller50e53e22015-06-01 20:18:21 -0700489 return n
David Garcia Quintase90cd372015-05-31 18:15:26 -0700490 except:
491 msg = "'{}' isn't a positive integer or 'inf'".format(arg_str)
492 raise argparse.ArgumentTypeError(msg)
493argp.add_argument('-n', '--runs_per_test', default=1, type=runs_per_test_type,
494 help='A positive integer or "inf". If "inf", all tests will run in an '
495 'infinite loop. Especially useful in combination with "-f"')
Craig Tillerfe406ec2015-02-24 13:55:12 -0800496argp.add_argument('-r', '--regex', default='.*', type=str)
Craig Tiller83762ac2015-05-22 14:04:06 -0700497argp.add_argument('-j', '--jobs', default=2 * multiprocessing.cpu_count(), type=int)
Craig Tiller8451e872015-02-27 09:25:51 -0800498argp.add_argument('-s', '--slowdown', default=1.0, type=float)
ctiller3040cb72015-01-07 12:13:17 -0800499argp.add_argument('-f', '--forever',
500 default=False,
501 action='store_const',
502 const=True)
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100503argp.add_argument('-t', '--travis',
504 default=False,
505 action='store_const',
506 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800507argp.add_argument('--newline_on_success',
508 default=False,
509 action='store_const',
510 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -0800511argp.add_argument('-l', '--language',
Craig Tiller60f15e62015-05-13 09:05:17 -0700512 choices=['all'] + sorted(_LANGUAGES.keys()),
Craig Tiller686fb262015-01-15 07:39:09 -0800513 nargs='+',
Craig Tiller60f15e62015-05-13 09:05:17 -0700514 default=['all'])
Craig Tillercd43da82015-05-29 08:41:29 -0700515argp.add_argument('-S', '--stop_on_failure',
516 default=False,
517 action='store_const',
518 const=True)
Craig Tiller234b6e72015-05-23 10:12:40 -0700519argp.add_argument('-a', '--antagonists', default=0, type=int)
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200520argp.add_argument('-x', '--xml_report', default=None, type=str,
521 help='Generates a JUnit-compatible XML report')
Nicolas Nobleddef2462015-01-06 18:08:25 -0800522args = argp.parse_args()
523
524# grab config
Craig Tiller738c3342015-01-12 14:28:33 -0800525run_configs = set(_CONFIGS[cfg]
526 for cfg in itertools.chain.from_iterable(
527 _CONFIGS.iterkeys() if x == 'all' else [x]
528 for x in args.config))
529build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tillerf1973b02015-01-16 12:32:13 -0800530
Craig Tiller06805272015-06-11 14:46:47 -0700531if args.travis:
532 _FORCE_ENVIRON_FOR_WRAPPERS = {'GRPC_TRACE': 'surface,batch'}
533
Craig Tiller60f15e62015-05-13 09:05:17 -0700534languages = set(_LANGUAGES[l]
535 for l in itertools.chain.from_iterable(
536 _LANGUAGES.iterkeys() if x == 'all' else [x]
537 for x in args.language))
murgatroid99132ce6a2015-03-04 17:29:14 -0800538
539if len(build_configs) > 1:
540 for language in languages:
541 if not language.supports_multi_config():
542 print language, 'does not support multiple build configurations'
543 sys.exit(1)
544
Craig Tiller5058c692015-04-08 09:42:04 -0700545if platform.system() == 'Windows':
546 def make_jobspec(cfg, targets):
Craig Tillerfc3c0c42015-09-01 16:47:54 -0700547 extra_args = []
Craig Tillerb5391e12015-09-03 14:35:18 -0700548 # better do parallel compilation
549 extra_args.extend(["/m"])
550 # disable PDB generation: it's broken, and we don't need it during CI
551 extra_args.extend(["/p:GenerateDebugInformation=false", "/p:DebugInformationFormat=None"])
Craig Tiller6fd23842015-09-01 07:36:31 -0700552 return [
murgatroid99cf08daf2015-09-21 15:33:16 -0700553 jobset.JobSpec(['vsprojects\\build.bat',
554 'vsprojects\\%s.sln' % target,
Craig Tillerfc3c0c42015-09-01 16:47:54 -0700555 '/p:Configuration=%s' % _WINDOWS_CONFIG[cfg]] +
556 extra_args,
Craig Tillerdfc3eee2015-09-01 16:32:16 -0700557 shell=True, timeout_seconds=90*60)
Craig Tiller6fd23842015-09-01 07:36:31 -0700558 for target in targets]
Craig Tiller5058c692015-04-08 09:42:04 -0700559else:
560 def make_jobspec(cfg, targets):
Craig Tiller6fd23842015-09-01 07:36:31 -0700561 return [jobset.JobSpec([os.getenv('MAKE', 'make'),
562 '-j', '%d' % (multiprocessing.cpu_count() + 1),
563 'EXTRA_DEFINES=GRPC_TEST_SLOWDOWN_MACHINE_FACTOR=%f' %
564 args.slowdown,
565 'CONFIG=%s' % cfg] + targets,
566 timeout_seconds=30*60)]
Craig Tiller5058c692015-04-08 09:42:04 -0700567
Craig Tillerbd4e3782015-09-01 06:48:55 -0700568make_targets = list(set(itertools.chain.from_iterable(
569 l.make_targets() for l in languages)))
murgatroid99fddac962015-09-22 09:20:11 -0700570build_steps = list(set(
murgatroid99256d3df2015-09-21 16:58:02 -0700571 jobset.JobSpec(cmdline, environ={'CONFIG': cfg})
572 for cfg in build_configs
573 for l in languages
574 for cmdline in l.pre_build_steps()))
Craig Tillerbd4e3782015-09-01 06:48:55 -0700575if make_targets:
Craig Tiller6fd23842015-09-01 07:36:31 -0700576 make_commands = itertools.chain.from_iterable(make_jobspec(cfg, make_targets) for cfg in build_configs)
577 build_steps.extend(set(make_commands))
Craig Tiller5058c692015-04-08 09:42:04 -0700578build_steps.extend(set(
murgatroid99132ce6a2015-03-04 17:29:14 -0800579 jobset.JobSpec(cmdline, environ={'CONFIG': cfg})
580 for cfg in build_configs
Craig Tiller547db2b2015-01-30 14:08:39 -0800581 for l in languages
Craig Tiller533b1a22015-05-29 08:41:29 -0700582 for cmdline in l.build_steps()))
Craig Tillerf1973b02015-01-16 12:32:13 -0800583
Nicolas Nobleddef2462015-01-06 18:08:25 -0800584runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -0800585forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -0800586
Nicolas Nobleddef2462015-01-06 18:08:25 -0800587
Craig Tiller71735182015-01-15 17:07:13 -0800588class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800589 """Cache for running tests."""
590
David Klempner25739582015-02-11 15:57:32 -0800591 def __init__(self, use_cache_results):
Craig Tiller71735182015-01-15 17:07:13 -0800592 self._last_successful_run = {}
David Klempner25739582015-02-11 15:57:32 -0800593 self._use_cache_results = use_cache_results
Craig Tiller69cd2372015-06-11 09:38:09 -0700594 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800595
596 def should_run(self, cmdline, bin_hash):
Craig Tiller71735182015-01-15 17:07:13 -0800597 if cmdline not in self._last_successful_run:
598 return True
599 if self._last_successful_run[cmdline] != bin_hash:
600 return True
David Klempner25739582015-02-11 15:57:32 -0800601 if not self._use_cache_results:
602 return True
Craig Tiller71735182015-01-15 17:07:13 -0800603 return False
604
605 def finished(self, cmdline, bin_hash):
Craig Tiller547db2b2015-01-30 14:08:39 -0800606 self._last_successful_run[cmdline] = bin_hash
Craig Tiller69cd2372015-06-11 09:38:09 -0700607 if time.time() - self._last_save > 1:
608 self.save()
Craig Tiller71735182015-01-15 17:07:13 -0800609
610 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800611 return [{'cmdline': k, 'hash': v}
612 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800613
614 def parse(self, exdump):
615 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
616
617 def save(self):
618 with open('.run_tests_cache', 'w') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800619 f.write(json.dumps(self.dump()))
Craig Tiller69cd2372015-06-11 09:38:09 -0700620 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800621
Craig Tiller1cc11db2015-01-15 22:50:50 -0800622 def maybe_load(self):
623 if os.path.exists('.run_tests_cache'):
624 with open('.run_tests_cache') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800625 self.parse(json.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800626
627
Craig Tillerf53d9c82015-08-04 14:19:43 -0700628def _start_port_server(port_server_port):
629 # check if a compatible port server is running
630 # if incompatible (version mismatch) ==> start a new one
631 # if not running ==> start a new one
632 # otherwise, leave it up
633 try:
Craig Tillerabd37fd2015-08-26 07:54:01 -0700634 version = urllib2.urlopen('http://localhost:%d/version' % port_server_port,
635 timeout=1).read()
Craig Tillerf53d9c82015-08-04 14:19:43 -0700636 running = True
637 except Exception:
638 running = False
639 if running:
640 with open('tools/run_tests/port_server.py') as f:
641 current_version = hashlib.sha1(f.read()).hexdigest()
642 running = (version == current_version)
643 if not running:
Craig Tilleref125592015-08-05 07:41:35 -0700644 urllib2.urlopen('http://localhost:%d/quit' % port_server_port).read()
645 time.sleep(1)
Craig Tillerf53d9c82015-08-04 14:19:43 -0700646 if not running:
647 port_log = open('portlog.txt', 'w')
648 port_server = subprocess.Popen(
Craig Tiller9a0c10e2015-08-06 15:47:32 -0700649 ['python', 'tools/run_tests/port_server.py', '-p', '%d' % port_server_port],
Craig Tillerf53d9c82015-08-04 14:19:43 -0700650 stderr=subprocess.STDOUT,
651 stdout=port_log)
Craig Tiller8b5f4dc2015-08-26 08:02:01 -0700652 # ensure port server is up
Craig Tillerabd37fd2015-08-26 07:54:01 -0700653 waits = 0
Craig Tillerf53d9c82015-08-04 14:19:43 -0700654 while True:
Craig Tillerabd37fd2015-08-26 07:54:01 -0700655 if waits > 10:
656 port_server.kill()
657 print "port_server failed to start"
658 sys.exit(1)
Craig Tillerf53d9c82015-08-04 14:19:43 -0700659 try:
Craig Tillerabd37fd2015-08-26 07:54:01 -0700660 urllib2.urlopen('http://localhost:%d/get' % port_server_port,
661 timeout=1).read()
Craig Tillerf53d9c82015-08-04 14:19:43 -0700662 break
663 except urllib2.URLError:
Craig Tillerabd37fd2015-08-26 07:54:01 -0700664 print "waiting for port_server"
Craig Tillerf53d9c82015-08-04 14:19:43 -0700665 time.sleep(0.5)
Craig Tillerabd37fd2015-08-26 07:54:01 -0700666 waits += 1
Craig Tillerf53d9c82015-08-04 14:19:43 -0700667 except:
668 port_server.kill()
669 raise
670
671
672def _build_and_run(
673 check_cancelled, newline_on_success, travis, cache, xml_report=None):
ctiller3040cb72015-01-07 12:13:17 -0800674 """Do one pass of building & running tests."""
murgatroid99666450e2015-01-26 13:03:31 -0800675 # build latest sequentially
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100676 if not jobset.run(build_steps, maxjobs=1,
677 newline_on_success=newline_on_success, travis=travis):
Craig Tillerd86a3942015-01-14 12:48:54 -0800678 return 1
ctiller3040cb72015-01-07 12:13:17 -0800679
Craig Tiller234b6e72015-05-23 10:12:40 -0700680 # start antagonists
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700681 antagonists = [subprocess.Popen(['tools/run_tests/antagonist.py'])
Craig Tiller234b6e72015-05-23 10:12:40 -0700682 for _ in range(0, args.antagonists)]
Craig Tillerf53d9c82015-08-04 14:19:43 -0700683 port_server_port = 9999
684 _start_port_server(port_server_port)
Craig Tiller234b6e72015-05-23 10:12:40 -0700685 try:
David Garcia Quintase90cd372015-05-31 18:15:26 -0700686 infinite_runs = runs_per_test == 0
yang-g6c1fdc62015-08-18 11:57:42 -0700687 one_run = set(
688 spec
689 for config in run_configs
690 for language in languages
691 for spec in language.test_specs(config, args.travis)
692 if re.search(args.regex, spec.shortname))
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700693 # When running on travis, we want out test runs to be as similar as possible
694 # for reproducibility purposes.
695 if travis:
696 massaged_one_run = sorted(one_run, key=lambda x: x.shortname)
697 else:
698 # whereas otherwise, we want to shuffle things up to give all tests a
699 # chance to run.
700 massaged_one_run = list(one_run) # random.shuffle needs an indexable seq.
701 random.shuffle(massaged_one_run) # which it modifies in-place.
Craig Tillerf7b7c892015-06-22 14:33:25 -0700702 if infinite_runs:
703 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 -0700704 runs_sequence = (itertools.repeat(massaged_one_run) if infinite_runs
705 else itertools.repeat(massaged_one_run, runs_per_test))
David Garcia Quintase90cd372015-05-31 18:15:26 -0700706 all_runs = itertools.chain.from_iterable(runs_sequence)
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200707
708 root = ET.Element('testsuites') if xml_report else None
709 testsuite = ET.SubElement(root, 'testsuite', id='1', package='grpc', name='tests') if xml_report else None
710
Craig Tiller234b6e72015-05-23 10:12:40 -0700711 if not jobset.run(all_runs, check_cancelled,
712 newline_on_success=newline_on_success, travis=travis,
David Garcia Quintase90cd372015-05-31 18:15:26 -0700713 infinite_runs=infinite_runs,
Craig Tillerda2220a2015-05-27 07:50:53 -0700714 maxjobs=args.jobs,
Craig Tillercd43da82015-05-29 08:41:29 -0700715 stop_on_failure=args.stop_on_failure,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200716 cache=cache if not xml_report else None,
Craig Tillerf53d9c82015-08-04 14:19:43 -0700717 xml_report=testsuite,
718 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port}):
Craig Tiller234b6e72015-05-23 10:12:40 -0700719 return 2
720 finally:
721 for antagonist in antagonists:
722 antagonist.kill()
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200723 if xml_report:
724 tree = ET.ElementTree(root)
725 tree.write(xml_report, encoding='UTF-8')
Craig Tillerd86a3942015-01-14 12:48:54 -0800726
Craig Tiller69cd2372015-06-11 09:38:09 -0700727 if cache: cache.save()
728
Craig Tillerd86a3942015-01-14 12:48:54 -0800729 return 0
ctiller3040cb72015-01-07 12:13:17 -0800730
731
David Klempner25739582015-02-11 15:57:32 -0800732test_cache = TestCache(runs_per_test == 1)
Craig Tiller547db2b2015-01-30 14:08:39 -0800733test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800734
ctiller3040cb72015-01-07 12:13:17 -0800735if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800736 success = True
ctiller3040cb72015-01-07 12:13:17 -0800737 while True:
Craig Tiller42bc87c2015-02-23 08:50:19 -0800738 dw = watch_dirs.DirWatcher(['src', 'include', 'test', 'examples'])
ctiller3040cb72015-01-07 12:13:17 -0800739 initial_time = dw.most_recent_change()
740 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800741 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800742 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800743 newline_on_success=False,
Craig Tiller9a5a9402015-04-16 10:39:50 -0700744 travis=args.travis,
Craig Tiller71735182015-01-15 17:07:13 -0800745 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800746 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800747 jobset.message('SUCCESS',
748 'All tests are now passing properly',
749 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800750 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800751 while not have_files_changed():
752 time.sleep(1)
753else:
Craig Tiller71735182015-01-15 17:07:13 -0800754 result = _build_and_run(check_cancelled=lambda: False,
755 newline_on_success=args.newline_on_success,
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100756 travis=args.travis,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200757 cache=test_cache,
758 xml_report=args.xml_report)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800759 if result == 0:
760 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
761 else:
762 jobset.message('FAILED', 'Some tests failed', do_newline=True)
763 sys.exit(result)