blob: eaba699ddfb34c8629762d26c9c802756a79efea [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
murgatroid99132ce6a2015-03-04 17:29:14 -080073 def __init__(self, config, environ=None):
74 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 Tiller738c3342015-01-12 14:28:33 -080080
Craig Tiller4fc90032015-05-21 10:39:52 -070081 def job_spec(self, cmdline, hash_targets, shortname=None, environ={}):
Craig Tiller49f61322015-03-03 13:02:11 -080082 """Construct a jobset.JobSpec for a test under this config
83
84 Args:
85 cmdline: a list of strings specifying the command line the test
86 would like to run
87 hash_targets: either None (don't do caching of test results), or
88 a list of strings specifying files to include in a
89 binary hash to check if a test has changed
90 -- if used, all artifacts needed to run the test must
91 be listed
92 """
Craig Tiller4fc90032015-05-21 10:39:52 -070093 actual_environ = self.environ.copy()
94 for k, v in environ.iteritems():
95 actual_environ[k] = v
Craig Tiller49f61322015-03-03 13:02:11 -080096 return jobset.JobSpec(cmdline=cmdline,
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -070097 shortname=shortname,
Craig Tiller4fc90032015-05-21 10:39:52 -070098 environ=actual_environ,
Craig Tiller547db2b2015-01-30 14:08:39 -080099 hash_targets=hash_targets
100 if self.allow_hashing else None)
Craig Tiller738c3342015-01-12 14:28:33 -0800101
102
103# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
104class ValgrindConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800105
murgatroid99132ce6a2015-03-04 17:29:14 -0800106 def __init__(self, config, tool, args=None):
107 if args is None:
108 args = []
Craig Tiller738c3342015-01-12 14:28:33 -0800109 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -0800110 self.tool = tool
Craig Tiller1a305b12015-02-18 13:37:06 -0800111 self.args = args
Craig Tillerc7449162015-01-16 14:42:10 -0800112 self.allow_hashing = False
Craig Tiller738c3342015-01-12 14:28:33 -0800113
Craig Tiller49f61322015-03-03 13:02:11 -0800114 def job_spec(self, cmdline, hash_targets):
Craig Tiller1a305b12015-02-18 13:37:06 -0800115 return jobset.JobSpec(cmdline=['valgrind', '--tool=%s' % self.tool] +
Craig Tiller49f61322015-03-03 13:02:11 -0800116 self.args + cmdline,
Craig Tiller71ec6cb2015-06-03 00:51:11 -0700117 shortname='valgrind %s' % cmdline[0],
Craig Tiller1a305b12015-02-18 13:37:06 -0800118 hash_targets=None)
Craig Tiller738c3342015-01-12 14:28:33 -0800119
120
Craig Tillerc7449162015-01-16 14:42:10 -0800121class CLanguage(object):
122
Craig Tillere9c959d2015-01-18 10:23:26 -0800123 def __init__(self, make_target, test_lang):
Craig Tillerc7449162015-01-16 14:42:10 -0800124 self.make_target = make_target
Craig Tillerd50993d2015-08-05 08:04:36 -0700125 self.platform = platform_string()
Craig Tillere9c959d2015-01-18 10:23:26 -0800126 with open('tools/run_tests/tests.json') as f:
Craig Tiller06b4ff22015-01-18 11:01:25 -0800127 js = json.load(f)
Craig Tillerd625d812015-04-08 15:52:35 -0700128 self.binaries = [tgt
129 for tgt in js
130 if tgt['language'] == test_lang and
Craig Tiller9429b612015-08-05 08:06:37 -0700131 platform_string() in tgt['platforms']]
Craig Tillerc85357e2015-08-07 07:33:04 -0700132 self.ci_binaries = [tgt
133 for tgt in js
134 if tgt['language'] == test_lang and
135 platform_string() in tgt['ci_platforms']]
Craig Tillerc7449162015-01-16 14:42:10 -0800136
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100137 def test_specs(self, config, travis):
Craig Tiller547db2b2015-01-30 14:08:39 -0800138 out = []
Craig Tillerc85357e2015-08-07 07:33:04 -0700139 for target in (self.ci_binaries if travis else self.binaries):
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100140 if travis and target['flaky']:
141 continue
Nicolas Noblee1445362015-05-11 17:40:26 -0700142 if self.platform == 'windows':
Jan Tattermusch12e8a042015-06-15 17:07:14 -0700143 binary = 'vsprojects/test_bin/%s.exe' % (target['name'])
Nicolas Noblee1445362015-05-11 17:40:26 -0700144 else:
145 binary = 'bins/%s/%s' % (config.build_config, target['name'])
yang-g6c1fdc62015-08-18 11:57:42 -0700146 if os.path.isfile(binary):
147 out.append(config.job_spec([binary], [binary]))
148 else:
149 print "\nWARNING: binary not found, skipping", binary
Nicolas Noblee1445362015-05-11 17:40:26 -0700150 return sorted(out)
Craig Tillerc7449162015-01-16 14:42:10 -0800151
152 def make_targets(self):
Craig Tiller7552f0f2015-06-19 17:46:20 -0700153 return ['buildtests_%s' % self.make_target, 'tools_%s' % self.make_target]
Craig Tillerc7449162015-01-16 14:42:10 -0800154
155 def build_steps(self):
156 return []
157
murgatroid99132ce6a2015-03-04 17:29:14 -0800158 def supports_multi_config(self):
159 return True
160
161 def __str__(self):
162 return self.make_target
163
Craig Tiller99775822015-01-30 13:07:16 -0800164
murgatroid992c8d5162015-01-26 10:41:21 -0800165class NodeLanguage(object):
166
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100167 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700168 return [config.job_spec(['tools/run_tests/run_node.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700169 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid992c8d5162015-01-26 10:41:21 -0800170
171 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700172 return ['static_c', 'shared_c']
murgatroid992c8d5162015-01-26 10:41:21 -0800173
174 def build_steps(self):
175 return [['tools/run_tests/build_node.sh']]
Craig Tillerc7449162015-01-16 14:42:10 -0800176
murgatroid99132ce6a2015-03-04 17:29:14 -0800177 def supports_multi_config(self):
178 return False
179
180 def __str__(self):
181 return 'node'
182
Craig Tiller99775822015-01-30 13:07:16 -0800183
Craig Tillerc7449162015-01-16 14:42:10 -0800184class PhpLanguage(object):
185
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100186 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700187 return [config.job_spec(['src/php/bin/run_tests.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700188 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
Craig Tillerc7449162015-01-16 14:42:10 -0800189
190 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700191 return ['static_c', 'shared_c']
Craig Tillerc7449162015-01-16 14:42:10 -0800192
193 def build_steps(self):
194 return [['tools/run_tests/build_php.sh']]
195
murgatroid99132ce6a2015-03-04 17:29:14 -0800196 def supports_multi_config(self):
197 return False
198
199 def __str__(self):
200 return 'php'
201
Craig Tillerc7449162015-01-16 14:42:10 -0800202
Nathaniel Manista840615e2015-01-22 20:31:47 +0000203class PythonLanguage(object):
204
Craig Tiller49f61322015-03-03 13:02:11 -0800205 def __init__(self):
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700206 self._build_python_versions = ['2.7']
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700207 self._has_python_versions = []
Craig Tiller49f61322015-03-03 13:02:11 -0800208
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100209 def test_specs(self, config, travis):
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700210 environment = dict(_FORCE_ENVIRON_FOR_WRAPPERS)
211 environment['PYVER'] = '2.7'
212 return [config.job_spec(
213 ['tools/run_tests/run_python.sh'],
214 None,
215 environ=environment,
216 shortname='py.test',
217 )]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000218
219 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700220 return ['static_c', 'grpc_python_plugin', 'shared_c']
Nathaniel Manista840615e2015-01-22 20:31:47 +0000221
222 def build_steps(self):
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700223 commands = []
224 for python_version in self._build_python_versions:
225 try:
226 with open(os.devnull, 'w') as output:
227 subprocess.check_call(['which', 'python' + python_version],
228 stdout=output, stderr=output)
229 commands.append(['tools/run_tests/build_python.sh', python_version])
230 self._has_python_versions.append(python_version)
231 except:
232 jobset.message('WARNING', 'Missing Python ' + python_version,
233 do_newline=True)
234 return commands
Nathaniel Manista840615e2015-01-22 20:31:47 +0000235
murgatroid99132ce6a2015-03-04 17:29:14 -0800236 def supports_multi_config(self):
237 return False
238
239 def __str__(self):
240 return 'python'
241
Craig Tillerd625d812015-04-08 15:52:35 -0700242
murgatroid996a4c4fa2015-02-27 12:08:57 -0800243class RubyLanguage(object):
244
245 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700246 return [config.job_spec(['tools/run_tests/run_ruby.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700247 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid996a4c4fa2015-02-27 12:08:57 -0800248
249 def make_targets(self):
murgatroid99a43c14f2015-07-30 13:31:23 -0700250 return ['static_c']
murgatroid996a4c4fa2015-02-27 12:08:57 -0800251
252 def build_steps(self):
253 return [['tools/run_tests/build_ruby.sh']]
254
murgatroid99132ce6a2015-03-04 17:29:14 -0800255 def supports_multi_config(self):
256 return False
257
258 def __str__(self):
259 return 'ruby'
260
Craig Tillerd625d812015-04-08 15:52:35 -0700261
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800262class CSharpLanguage(object):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700263 def __init__(self):
Craig Tillerd50993d2015-08-05 08:04:36 -0700264 self.platform = platform_string()
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700265
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800266 def test_specs(self, config, travis):
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700267 assemblies = ['Grpc.Core.Tests',
268 'Grpc.Examples.Tests',
Jan Tattermusch9d67d8d2015-08-01 20:39:16 -0700269 'Grpc.HealthCheck.Tests',
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700270 'Grpc.IntegrationTesting']
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700271 if self.platform == 'windows':
272 cmd = 'tools\\run_tests\\run_csharp.bat'
273 else:
274 cmd = 'tools/run_tests/run_csharp.sh'
275 return [config.job_spec([cmd, assembly],
Craig Tiller4fc90032015-05-21 10:39:52 -0700276 None, shortname=assembly,
Craig Tiller06805272015-06-11 14:46:47 -0700277 environ=_FORCE_ENVIRON_FOR_WRAPPERS)
Craig Tillerd50993d2015-08-05 08:04:36 -0700278 for assembly in assemblies]
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800279
280 def make_targets(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700281 # For Windows, this target doesn't really build anything,
282 # everything is build by buildall script later.
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800283 return ['grpc_csharp_ext']
284
285 def build_steps(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700286 if self.platform == 'windows':
287 return [['src\\csharp\\buildall.bat']]
288 else:
289 return [['tools/run_tests/build_csharp.sh']]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000290
murgatroid99132ce6a2015-03-04 17:29:14 -0800291 def supports_multi_config(self):
292 return False
293
294 def __str__(self):
295 return 'csharp'
296
Craig Tillerd625d812015-04-08 15:52:35 -0700297
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700298class ObjCLanguage(object):
299
300 def test_specs(self, config, travis):
301 return [config.job_spec(['src/objective-c/tests/run_tests.sh'], None,
302 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
303
304 def make_targets(self):
Jorge Canizalesd0b32e92015-07-30 23:08:43 -0700305 return ['grpc_objective_c_plugin', 'interop_server']
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700306
307 def build_steps(self):
Jorge Canizalesd0b32e92015-07-30 23:08:43 -0700308 return [['src/objective-c/tests/build_tests.sh']]
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700309
310 def supports_multi_config(self):
311 return False
312
313 def __str__(self):
314 return 'objc'
315
316
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100317class Sanity(object):
318
319 def test_specs(self, config, travis):
Craig Tillerf75fc122015-06-25 06:58:00 -0700320 return [config.job_spec('tools/run_tests/run_sanity.sh', None),
321 config.job_spec('tools/run_tests/check_sources_and_headers.py', None)]
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100322
323 def make_targets(self):
324 return ['run_dep_checks']
325
326 def build_steps(self):
327 return []
328
329 def supports_multi_config(self):
330 return False
331
332 def __str__(self):
333 return 'sanity'
334
Nicolas "Pixel" Noblee55cd7f2015-04-14 17:59:13 +0200335
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100336class Build(object):
337
338 def test_specs(self, config, travis):
339 return []
340
341 def make_targets(self):
Nicolas "Pixel" Noblec23827b2015-04-23 06:17:55 +0200342 return ['static']
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100343
344 def build_steps(self):
345 return []
346
347 def supports_multi_config(self):
348 return True
349
350 def __str__(self):
351 return self.make_target
352
353
Craig Tiller738c3342015-01-12 14:28:33 -0800354# different configurations we can run under
355_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -0800356 'dbg': SimpleConfig('dbg'),
357 'opt': SimpleConfig('opt'),
David Klempner1d0302d2015-02-04 16:08:01 -0800358 'tsan': SimpleConfig('tsan', environ={
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700359 'TSAN_OPTIONS': 'suppressions=tools/tsan_suppressions.txt:halt_on_error=1:second_deadlock_stack=1'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800360 'msan': SimpleConfig('msan'),
Craig Tiller96bd5f62015-02-13 09:04:13 -0800361 'ubsan': SimpleConfig('ubsan'),
Craig Tiller547db2b2015-01-30 14:08:39 -0800362 'asan': SimpleConfig('asan', environ={
Craig Tillerd4b13622015-05-29 09:10:10 -0700363 'ASAN_OPTIONS': 'detect_leaks=1:color=always:suppressions=tools/tsan_suppressions.txt',
364 'LSAN_OPTIONS': 'report_objects=1'}),
Craig Tiller810725c2015-05-12 09:44:41 -0700365 'asan-noleaks': SimpleConfig('asan', environ={
366 'ASAN_OPTIONS': 'detect_leaks=0:color=always:suppressions=tools/tsan_suppressions.txt'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800367 'gcov': SimpleConfig('gcov'),
Craig Tiller1a305b12015-02-18 13:37:06 -0800368 'memcheck': ValgrindConfig('valgrind', 'memcheck', ['--leak-check=full']),
Craig Tillerb50d1662015-01-15 17:28:21 -0800369 'helgrind': ValgrindConfig('dbg', 'helgrind')
370 }
Craig Tiller738c3342015-01-12 14:28:33 -0800371
372
Nicolas "Pixel" Noble1fb5e822015-03-16 06:20:37 +0100373_DEFAULT = ['opt']
Craig Tillerc7449162015-01-16 14:42:10 -0800374_LANGUAGES = {
Craig Tillere9c959d2015-01-18 10:23:26 -0800375 'c++': CLanguage('cxx', 'c++'),
376 'c': CLanguage('c', 'c'),
murgatroid992c8d5162015-01-26 10:41:21 -0800377 'node': NodeLanguage(),
Nathaniel Manista840615e2015-01-22 20:31:47 +0000378 'php': PhpLanguage(),
379 'python': PythonLanguage(),
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800380 'ruby': RubyLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100381 'csharp': CSharpLanguage(),
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700382 'objc' : ObjCLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100383 'sanity': Sanity(),
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100384 'build': Build(),
Craig Tillereb272bc2015-01-30 13:13:14 -0800385 }
Nicolas Nobleddef2462015-01-06 18:08:25 -0800386
387# parse command line
388argp = argparse.ArgumentParser(description='Run grpc tests.')
389argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -0800390 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -0800391 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -0800392 default=_DEFAULT)
David Garcia Quintase90cd372015-05-31 18:15:26 -0700393
394def runs_per_test_type(arg_str):
395 """Auxilary function to parse the "runs_per_test" flag.
396
397 Returns:
398 A positive integer or 0, the latter indicating an infinite number of
399 runs.
400
401 Raises:
402 argparse.ArgumentTypeError: Upon invalid input.
403 """
404 if arg_str == 'inf':
405 return 0
406 try:
407 n = int(arg_str)
408 if n <= 0: raise ValueError
Craig Tiller50e53e22015-06-01 20:18:21 -0700409 return n
David Garcia Quintase90cd372015-05-31 18:15:26 -0700410 except:
411 msg = "'{}' isn't a positive integer or 'inf'".format(arg_str)
412 raise argparse.ArgumentTypeError(msg)
413argp.add_argument('-n', '--runs_per_test', default=1, type=runs_per_test_type,
414 help='A positive integer or "inf". If "inf", all tests will run in an '
415 'infinite loop. Especially useful in combination with "-f"')
Craig Tillerfe406ec2015-02-24 13:55:12 -0800416argp.add_argument('-r', '--regex', default='.*', type=str)
Craig Tiller83762ac2015-05-22 14:04:06 -0700417argp.add_argument('-j', '--jobs', default=2 * multiprocessing.cpu_count(), type=int)
Craig Tiller8451e872015-02-27 09:25:51 -0800418argp.add_argument('-s', '--slowdown', default=1.0, type=float)
ctiller3040cb72015-01-07 12:13:17 -0800419argp.add_argument('-f', '--forever',
420 default=False,
421 action='store_const',
422 const=True)
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100423argp.add_argument('-t', '--travis',
424 default=False,
425 action='store_const',
426 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800427argp.add_argument('--newline_on_success',
428 default=False,
429 action='store_const',
430 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -0800431argp.add_argument('-l', '--language',
Craig Tiller60f15e62015-05-13 09:05:17 -0700432 choices=['all'] + sorted(_LANGUAGES.keys()),
Craig Tiller686fb262015-01-15 07:39:09 -0800433 nargs='+',
Craig Tiller60f15e62015-05-13 09:05:17 -0700434 default=['all'])
Craig Tillercd43da82015-05-29 08:41:29 -0700435argp.add_argument('-S', '--stop_on_failure',
436 default=False,
437 action='store_const',
438 const=True)
Craig Tiller234b6e72015-05-23 10:12:40 -0700439argp.add_argument('-a', '--antagonists', default=0, type=int)
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200440argp.add_argument('-x', '--xml_report', default=None, type=str,
441 help='Generates a JUnit-compatible XML report')
Nicolas Nobleddef2462015-01-06 18:08:25 -0800442args = argp.parse_args()
443
444# grab config
Craig Tiller738c3342015-01-12 14:28:33 -0800445run_configs = set(_CONFIGS[cfg]
446 for cfg in itertools.chain.from_iterable(
447 _CONFIGS.iterkeys() if x == 'all' else [x]
448 for x in args.config))
449build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tillerf1973b02015-01-16 12:32:13 -0800450
Craig Tiller06805272015-06-11 14:46:47 -0700451if args.travis:
452 _FORCE_ENVIRON_FOR_WRAPPERS = {'GRPC_TRACE': 'surface,batch'}
453
Craig Tillerc7449162015-01-16 14:42:10 -0800454make_targets = []
Craig Tiller60f15e62015-05-13 09:05:17 -0700455languages = set(_LANGUAGES[l]
456 for l in itertools.chain.from_iterable(
457 _LANGUAGES.iterkeys() if x == 'all' else [x]
458 for x in args.language))
murgatroid99132ce6a2015-03-04 17:29:14 -0800459
460if len(build_configs) > 1:
461 for language in languages:
462 if not language.supports_multi_config():
463 print language, 'does not support multiple build configurations'
464 sys.exit(1)
465
Craig Tiller5058c692015-04-08 09:42:04 -0700466if platform.system() == 'Windows':
467 def make_jobspec(cfg, targets):
Jan Tattermusche8243592015-04-17 14:14:01 -0700468 return jobset.JobSpec(['make.bat', 'CONFIG=%s' % cfg] + targets,
469 cwd='vsprojects', shell=True)
Craig Tiller5058c692015-04-08 09:42:04 -0700470else:
471 def make_jobspec(cfg, targets):
Nicolas "Pixel" Noble4243ca82015-07-23 23:47:56 +0200472 return jobset.JobSpec([os.getenv('MAKE', 'make'),
Craig Tiller5058c692015-04-08 09:42:04 -0700473 '-j', '%d' % (multiprocessing.cpu_count() + 1),
Craig Tiller533b1a22015-05-29 08:41:29 -0700474 'EXTRA_DEFINES=GRPC_TEST_SLOWDOWN_MACHINE_FACTOR=%f' %
Craig Tiller5058c692015-04-08 09:42:04 -0700475 args.slowdown,
Craig Tillerdb0d2342015-08-05 07:47:35 -0700476 'CONFIG=%s' % cfg] + targets,
477 timeout_seconds=30*60)
Craig Tiller5058c692015-04-08 09:42:04 -0700478
Craig Tiller533b1a22015-05-29 08:41:29 -0700479build_steps = [make_jobspec(cfg,
Craig Tiller5058c692015-04-08 09:42:04 -0700480 list(set(itertools.chain.from_iterable(
481 l.make_targets() for l in languages))))
482 for cfg in build_configs]
483build_steps.extend(set(
murgatroid99132ce6a2015-03-04 17:29:14 -0800484 jobset.JobSpec(cmdline, environ={'CONFIG': cfg})
485 for cfg in build_configs
Craig Tiller547db2b2015-01-30 14:08:39 -0800486 for l in languages
Craig Tiller533b1a22015-05-29 08:41:29 -0700487 for cmdline in l.build_steps()))
Craig Tillerf1973b02015-01-16 12:32:13 -0800488
Nicolas Nobleddef2462015-01-06 18:08:25 -0800489runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -0800490forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -0800491
Nicolas Nobleddef2462015-01-06 18:08:25 -0800492
Craig Tiller71735182015-01-15 17:07:13 -0800493class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800494 """Cache for running tests."""
495
David Klempner25739582015-02-11 15:57:32 -0800496 def __init__(self, use_cache_results):
Craig Tiller71735182015-01-15 17:07:13 -0800497 self._last_successful_run = {}
David Klempner25739582015-02-11 15:57:32 -0800498 self._use_cache_results = use_cache_results
Craig Tiller69cd2372015-06-11 09:38:09 -0700499 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800500
501 def should_run(self, cmdline, bin_hash):
Craig Tiller71735182015-01-15 17:07:13 -0800502 if cmdline not in self._last_successful_run:
503 return True
504 if self._last_successful_run[cmdline] != bin_hash:
505 return True
David Klempner25739582015-02-11 15:57:32 -0800506 if not self._use_cache_results:
507 return True
Craig Tiller71735182015-01-15 17:07:13 -0800508 return False
509
510 def finished(self, cmdline, bin_hash):
Craig Tiller547db2b2015-01-30 14:08:39 -0800511 self._last_successful_run[cmdline] = bin_hash
Craig Tiller69cd2372015-06-11 09:38:09 -0700512 if time.time() - self._last_save > 1:
513 self.save()
Craig Tiller71735182015-01-15 17:07:13 -0800514
515 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800516 return [{'cmdline': k, 'hash': v}
517 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800518
519 def parse(self, exdump):
520 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
521
522 def save(self):
523 with open('.run_tests_cache', 'w') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800524 f.write(json.dumps(self.dump()))
Craig Tiller69cd2372015-06-11 09:38:09 -0700525 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800526
Craig Tiller1cc11db2015-01-15 22:50:50 -0800527 def maybe_load(self):
528 if os.path.exists('.run_tests_cache'):
529 with open('.run_tests_cache') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800530 self.parse(json.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800531
532
Craig Tillerf53d9c82015-08-04 14:19:43 -0700533def _start_port_server(port_server_port):
534 # check if a compatible port server is running
535 # if incompatible (version mismatch) ==> start a new one
536 # if not running ==> start a new one
537 # otherwise, leave it up
538 try:
Craig Tilleref125592015-08-05 07:41:35 -0700539 version = urllib2.urlopen('http://localhost:%d/version' % port_server_port).read()
Craig Tillerf53d9c82015-08-04 14:19:43 -0700540 running = True
541 except Exception:
542 running = False
543 if running:
544 with open('tools/run_tests/port_server.py') as f:
545 current_version = hashlib.sha1(f.read()).hexdigest()
546 running = (version == current_version)
547 if not running:
Craig Tilleref125592015-08-05 07:41:35 -0700548 urllib2.urlopen('http://localhost:%d/quit' % port_server_port).read()
549 time.sleep(1)
Craig Tillerf53d9c82015-08-04 14:19:43 -0700550 if not running:
551 port_log = open('portlog.txt', 'w')
552 port_server = subprocess.Popen(
Craig Tiller9a0c10e2015-08-06 15:47:32 -0700553 ['python', 'tools/run_tests/port_server.py', '-p', '%d' % port_server_port],
Craig Tillerf53d9c82015-08-04 14:19:43 -0700554 stderr=subprocess.STDOUT,
555 stdout=port_log)
556 # ensure port server is up
557 while True:
558 try:
559 urllib2.urlopen('http://localhost:%d/get' % port_server_port).read()
560 break
561 except urllib2.URLError:
562 time.sleep(0.5)
563 except:
564 port_server.kill()
565 raise
566
567
568def _build_and_run(
569 check_cancelled, newline_on_success, travis, cache, xml_report=None):
ctiller3040cb72015-01-07 12:13:17 -0800570 """Do one pass of building & running tests."""
murgatroid99666450e2015-01-26 13:03:31 -0800571 # build latest sequentially
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100572 if not jobset.run(build_steps, maxjobs=1,
573 newline_on_success=newline_on_success, travis=travis):
Craig Tillerd86a3942015-01-14 12:48:54 -0800574 return 1
ctiller3040cb72015-01-07 12:13:17 -0800575
Craig Tiller234b6e72015-05-23 10:12:40 -0700576 # start antagonists
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700577 antagonists = [subprocess.Popen(['tools/run_tests/antagonist.py'])
Craig Tiller234b6e72015-05-23 10:12:40 -0700578 for _ in range(0, args.antagonists)]
Craig Tillerf53d9c82015-08-04 14:19:43 -0700579 port_server_port = 9999
580 _start_port_server(port_server_port)
Craig Tiller234b6e72015-05-23 10:12:40 -0700581 try:
David Garcia Quintase90cd372015-05-31 18:15:26 -0700582 infinite_runs = runs_per_test == 0
yang-g6c1fdc62015-08-18 11:57:42 -0700583 one_run = set(
584 spec
585 for config in run_configs
586 for language in languages
587 for spec in language.test_specs(config, args.travis)
588 if re.search(args.regex, spec.shortname))
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700589 # When running on travis, we want out test runs to be as similar as possible
590 # for reproducibility purposes.
591 if travis:
592 massaged_one_run = sorted(one_run, key=lambda x: x.shortname)
593 else:
594 # whereas otherwise, we want to shuffle things up to give all tests a
595 # chance to run.
596 massaged_one_run = list(one_run) # random.shuffle needs an indexable seq.
597 random.shuffle(massaged_one_run) # which it modifies in-place.
Craig Tillerf7b7c892015-06-22 14:33:25 -0700598 if infinite_runs:
599 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 -0700600 runs_sequence = (itertools.repeat(massaged_one_run) if infinite_runs
601 else itertools.repeat(massaged_one_run, runs_per_test))
David Garcia Quintase90cd372015-05-31 18:15:26 -0700602 all_runs = itertools.chain.from_iterable(runs_sequence)
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200603
604 root = ET.Element('testsuites') if xml_report else None
605 testsuite = ET.SubElement(root, 'testsuite', id='1', package='grpc', name='tests') if xml_report else None
606
Craig Tiller234b6e72015-05-23 10:12:40 -0700607 if not jobset.run(all_runs, check_cancelled,
608 newline_on_success=newline_on_success, travis=travis,
David Garcia Quintase90cd372015-05-31 18:15:26 -0700609 infinite_runs=infinite_runs,
Craig Tillerda2220a2015-05-27 07:50:53 -0700610 maxjobs=args.jobs,
Craig Tillercd43da82015-05-29 08:41:29 -0700611 stop_on_failure=args.stop_on_failure,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200612 cache=cache if not xml_report else None,
Craig Tillerf53d9c82015-08-04 14:19:43 -0700613 xml_report=testsuite,
614 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port}):
Craig Tiller234b6e72015-05-23 10:12:40 -0700615 return 2
616 finally:
617 for antagonist in antagonists:
618 antagonist.kill()
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200619 if xml_report:
620 tree = ET.ElementTree(root)
621 tree.write(xml_report, encoding='UTF-8')
Craig Tillerd86a3942015-01-14 12:48:54 -0800622
Craig Tiller69cd2372015-06-11 09:38:09 -0700623 if cache: cache.save()
624
Craig Tillerd86a3942015-01-14 12:48:54 -0800625 return 0
ctiller3040cb72015-01-07 12:13:17 -0800626
627
David Klempner25739582015-02-11 15:57:32 -0800628test_cache = TestCache(runs_per_test == 1)
Craig Tiller547db2b2015-01-30 14:08:39 -0800629test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800630
ctiller3040cb72015-01-07 12:13:17 -0800631if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800632 success = True
ctiller3040cb72015-01-07 12:13:17 -0800633 while True:
Craig Tiller42bc87c2015-02-23 08:50:19 -0800634 dw = watch_dirs.DirWatcher(['src', 'include', 'test', 'examples'])
ctiller3040cb72015-01-07 12:13:17 -0800635 initial_time = dw.most_recent_change()
636 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800637 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800638 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800639 newline_on_success=False,
Craig Tiller9a5a9402015-04-16 10:39:50 -0700640 travis=args.travis,
Craig Tiller71735182015-01-15 17:07:13 -0800641 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800642 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800643 jobset.message('SUCCESS',
644 'All tests are now passing properly',
645 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800646 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800647 while not have_files_changed():
648 time.sleep(1)
649else:
Craig Tiller71735182015-01-15 17:07:13 -0800650 result = _build_and_run(check_cancelled=lambda: False,
651 newline_on_success=args.newline_on_success,
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100652 travis=args.travis,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200653 cache=test_cache,
654 xml_report=args.xml_report)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800655 if result == 0:
656 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
657 else:
658 jobset.message('FAILED', 'Some tests failed', do_newline=True)
659 sys.exit(result)