blob: b1f886f319c6d82b6362453acb96fe126b76b14b [file] [log] [blame]
Nathaniel Manistaae4fbcd2015-09-23 16:29:44 +00001#!/usr/bin/env python2.7
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
Craig Tiller82875232015-09-25 13:57:34 -070043import socket
David Garcia Quintas79e389f2015-06-02 17:49:42 -070044import subprocess
Nicolas Nobleddef2462015-01-06 18:08:25 -080045import sys
Craig Tillerf0a293e2015-10-12 10:05:50 -070046import tempfile
47import traceback
ctiller3040cb72015-01-07 12:13:17 -080048import time
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +020049import xml.etree.cElementTree as ET
Craig Tillerf53d9c82015-08-04 14:19:43 -070050import urllib2
Nicolas Nobleddef2462015-01-06 18:08:25 -080051
52import jobset
ctiller3040cb72015-01-07 12:13:17 -080053import watch_dirs
Nicolas Nobleddef2462015-01-06 18:08:25 -080054
Craig Tiller2cc2b842015-02-27 11:38:31 -080055ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
56os.chdir(ROOT)
57
58
Craig Tiller06805272015-06-11 14:46:47 -070059_FORCE_ENVIRON_FOR_WRAPPERS = {}
60
61
Craig Tillerd50993d2015-08-05 08:04:36 -070062def platform_string():
63 if platform.system() == 'Windows':
64 return 'windows'
65 elif platform.system() == 'Darwin':
66 return 'mac'
67 elif platform.system() == 'Linux':
68 return 'linux'
69 else:
70 return 'posix'
71
72
Craig Tiller738c3342015-01-12 14:28:33 -080073# SimpleConfig: just compile with CONFIG=config, and run the binary to test
74class SimpleConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080075
Masood Malekghassemi26ea9e22015-10-09 15:19:17 -070076 def __init__(self, config, environ=None, timeout_multiplier=1):
murgatroid99132ce6a2015-03-04 17:29:14 -080077 if environ is None:
78 environ = {}
Craig Tiller738c3342015-01-12 14:28:33 -080079 self.build_config = config
Craig Tillerc7449162015-01-16 14:42:10 -080080 self.allow_hashing = (config != 'gcov')
Craig Tiller547db2b2015-01-30 14:08:39 -080081 self.environ = environ
murgatroid99132ce6a2015-03-04 17:29:14 -080082 self.environ['CONFIG'] = config
Masood Malekghassemi26ea9e22015-10-09 15:19:17 -070083 self.timeout_multiplier = timeout_multiplier
Craig Tiller738c3342015-01-12 14:28:33 -080084
Masood Malekghassemi26ea9e22015-10-09 15:19:17 -070085 def job_spec(self, cmdline, hash_targets, timeout_seconds=5*60,
86 shortname=None, environ={}):
Craig Tiller49f61322015-03-03 13:02:11 -080087 """Construct a jobset.JobSpec for a test under this config
88
89 Args:
90 cmdline: a list of strings specifying the command line the test
91 would like to run
92 hash_targets: either None (don't do caching of test results), or
93 a list of strings specifying files to include in a
94 binary hash to check if a test has changed
95 -- if used, all artifacts needed to run the test must
96 be listed
97 """
Craig Tiller4fc90032015-05-21 10:39:52 -070098 actual_environ = self.environ.copy()
99 for k, v in environ.iteritems():
100 actual_environ[k] = v
Craig Tiller49f61322015-03-03 13:02:11 -0800101 return jobset.JobSpec(cmdline=cmdline,
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700102 shortname=shortname,
Craig Tiller4fc90032015-05-21 10:39:52 -0700103 environ=actual_environ,
Masood Malekghassemi26ea9e22015-10-09 15:19:17 -0700104 timeout_seconds=self.timeout_multiplier * timeout_seconds,
Craig Tiller547db2b2015-01-30 14:08:39 -0800105 hash_targets=hash_targets
Craig Tillerd4509a12015-09-28 09:18:40 -0700106 if self.allow_hashing else None,
Craig Tiller35505de2015-10-08 13:31:33 -0700107 flake_retries=5 if args.allow_flakes else 0,
108 timeout_retries=3 if args.allow_flakes else 0)
Craig Tiller738c3342015-01-12 14:28:33 -0800109
110
111# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
112class ValgrindConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800113
murgatroid99132ce6a2015-03-04 17:29:14 -0800114 def __init__(self, config, tool, args=None):
115 if args is None:
116 args = []
Craig Tiller738c3342015-01-12 14:28:33 -0800117 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -0800118 self.tool = tool
Craig Tiller1a305b12015-02-18 13:37:06 -0800119 self.args = args
Craig Tillerc7449162015-01-16 14:42:10 -0800120 self.allow_hashing = False
Craig Tiller738c3342015-01-12 14:28:33 -0800121
Craig Tiller49f61322015-03-03 13:02:11 -0800122 def job_spec(self, cmdline, hash_targets):
Craig Tiller1a305b12015-02-18 13:37:06 -0800123 return jobset.JobSpec(cmdline=['valgrind', '--tool=%s' % self.tool] +
Craig Tiller49f61322015-03-03 13:02:11 -0800124 self.args + cmdline,
Craig Tiller71ec6cb2015-06-03 00:51:11 -0700125 shortname='valgrind %s' % cmdline[0],
Craig Tillerd4509a12015-09-28 09:18:40 -0700126 hash_targets=None,
Craig Tiller95cc07b2015-09-28 13:41:30 -0700127 flake_retries=5 if args.allow_flakes else 0,
Craig Tiller35505de2015-10-08 13:31:33 -0700128 timeout_retries=3 if args.allow_flakes else 0)
Craig Tiller738c3342015-01-12 14:28:33 -0800129
130
murgatroid99cf08daf2015-09-21 15:33:16 -0700131def get_c_tests(travis, test_lang) :
132 out = []
133 platforms_str = 'ci_platforms' if travis else 'platforms'
134 with open('tools/run_tests/tests.json') as f:
murgatroid9989899b12015-09-22 09:14:48 -0700135 js = json.load(f)
murgatroid99a3e244f2015-09-22 11:25:53 -0700136 return [tgt
137 for tgt in js
138 if tgt['language'] == test_lang and
139 platform_string() in tgt[platforms_str] and
140 not (travis and tgt['flaky'])]
murgatroid99cf08daf2015-09-21 15:33:16 -0700141
murgatroid99fafeeb32015-09-22 09:13:03 -0700142
Craig Tillerc7449162015-01-16 14:42:10 -0800143class CLanguage(object):
144
Craig Tillere9c959d2015-01-18 10:23:26 -0800145 def __init__(self, make_target, test_lang):
Craig Tillerc7449162015-01-16 14:42:10 -0800146 self.make_target = make_target
Craig Tillerd50993d2015-08-05 08:04:36 -0700147 self.platform = platform_string()
Craig Tiller711bbe62015-08-19 12:35:16 -0700148 self.test_lang = test_lang
Craig Tillerc7449162015-01-16 14:42:10 -0800149
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100150 def test_specs(self, config, travis):
Craig Tiller547db2b2015-01-30 14:08:39 -0800151 out = []
murgatroid99cf08daf2015-09-21 15:33:16 -0700152 binaries = get_c_tests(travis, self.test_lang)
Craig Tiller711bbe62015-08-19 12:35:16 -0700153 for target in binaries:
murgatroid99a3e244f2015-09-22 11:25:53 -0700154 if config.build_config in target['exclude_configs']:
murgatroid99fafeeb32015-09-22 09:13:03 -0700155 continue
Nicolas Noblee1445362015-05-11 17:40:26 -0700156 if self.platform == 'windows':
Craig Tillerf4182602015-09-01 12:23:16 -0700157 binary = 'vsprojects/%s/%s.exe' % (
158 _WINDOWS_CONFIG[config.build_config], target['name'])
Nicolas Noblee1445362015-05-11 17:40:26 -0700159 else:
160 binary = 'bins/%s/%s' % (config.build_config, target['name'])
yang-g6c1fdc62015-08-18 11:57:42 -0700161 if os.path.isfile(binary):
162 out.append(config.job_spec([binary], [binary]))
163 else:
Adele Zhoue4c35612015-10-16 15:34:23 -0700164 print '\nWARNING: binary not found, skipping', binary
Nicolas Noblee1445362015-05-11 17:40:26 -0700165 return sorted(out)
Craig Tillerc7449162015-01-16 14:42:10 -0800166
167 def make_targets(self):
Craig Tiller7bb3efd2015-09-01 08:04:03 -0700168 if platform_string() == 'windows':
169 # don't build tools on windows just yet
170 return ['buildtests_%s' % self.make_target]
Craig Tiller7552f0f2015-06-19 17:46:20 -0700171 return ['buildtests_%s' % self.make_target, 'tools_%s' % self.make_target]
Craig Tillerc7449162015-01-16 14:42:10 -0800172
murgatroid99256d3df2015-09-21 16:58:02 -0700173 def pre_build_steps(self):
Jan Tattermusch874aec02015-10-07 19:26:19 -0700174 if self.platform == 'windows':
175 return [['tools\\run_tests\\pre_build_c.bat']]
176 else:
177 return []
murgatroid99256d3df2015-09-21 16:58:02 -0700178
Craig Tillerc7449162015-01-16 14:42:10 -0800179 def build_steps(self):
180 return []
181
Nicolas "Pixel" Noble3fcd3bf2015-10-10 02:30:38 +0200182 def post_tests_steps(self):
183 if self.platform == 'windows':
184 return []
185 else:
186 return [['tools/run_tests/post_tests_c.sh']]
187
murgatroid99a3e244f2015-09-22 11:25:53 -0700188 def makefile_name(self):
189 return 'Makefile'
190
murgatroid99132ce6a2015-03-04 17:29:14 -0800191 def supports_multi_config(self):
192 return True
193
194 def __str__(self):
195 return self.make_target
196
murgatroid992c8d5162015-01-26 10:41:21 -0800197class NodeLanguage(object):
198
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100199 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700200 return [config.job_spec(['tools/run_tests/run_node.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700201 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid992c8d5162015-01-26 10:41:21 -0800202
murgatroid99256d3df2015-09-21 16:58:02 -0700203 def pre_build_steps(self):
murgatroid99ae369de2015-10-09 15:40:48 -0700204 # Default to 1 week cache expiration
murgatroid9993758952015-10-14 11:51:05 -0700205 return [['tools/run_tests/pre_build_node.sh']]
murgatroid99256d3df2015-09-21 16:58:02 -0700206
murgatroid992c8d5162015-01-26 10:41:21 -0800207 def make_targets(self):
murgatroid99db5b1602015-10-01 13:20:11 -0700208 return []
murgatroid992c8d5162015-01-26 10:41:21 -0800209
210 def build_steps(self):
211 return [['tools/run_tests/build_node.sh']]
Craig Tillerc7449162015-01-16 14:42:10 -0800212
Nicolas "Pixel" Noble3fcd3bf2015-10-10 02:30:38 +0200213 def post_tests_steps(self):
214 return []
215
murgatroid99a3e244f2015-09-22 11:25:53 -0700216 def makefile_name(self):
217 return 'Makefile'
218
murgatroid99132ce6a2015-03-04 17:29:14 -0800219 def supports_multi_config(self):
220 return False
221
222 def __str__(self):
223 return 'node'
224
Craig Tiller99775822015-01-30 13:07:16 -0800225
Craig Tillerc7449162015-01-16 14:42:10 -0800226class PhpLanguage(object):
227
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100228 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700229 return [config.job_spec(['src/php/bin/run_tests.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700230 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
Craig Tillerc7449162015-01-16 14:42:10 -0800231
murgatroid99256d3df2015-09-21 16:58:02 -0700232 def pre_build_steps(self):
233 return []
234
Craig Tillerc7449162015-01-16 14:42:10 -0800235 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700236 return ['static_c', 'shared_c']
Craig Tillerc7449162015-01-16 14:42:10 -0800237
238 def build_steps(self):
239 return [['tools/run_tests/build_php.sh']]
240
Nicolas "Pixel" Noble3fcd3bf2015-10-10 02:30:38 +0200241 def post_tests_steps(self):
242 return []
243
murgatroid99a3e244f2015-09-22 11:25:53 -0700244 def makefile_name(self):
245 return 'Makefile'
246
murgatroid99132ce6a2015-03-04 17:29:14 -0800247 def supports_multi_config(self):
248 return False
249
250 def __str__(self):
251 return 'php'
252
Craig Tillerc7449162015-01-16 14:42:10 -0800253
Nathaniel Manista840615e2015-01-22 20:31:47 +0000254class PythonLanguage(object):
255
Craig Tiller49f61322015-03-03 13:02:11 -0800256 def __init__(self):
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700257 self._build_python_versions = ['2.7']
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700258 self._has_python_versions = []
Craig Tiller49f61322015-03-03 13:02:11 -0800259
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100260 def test_specs(self, config, travis):
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700261 environment = dict(_FORCE_ENVIRON_FOR_WRAPPERS)
262 environment['PYVER'] = '2.7'
263 return [config.job_spec(
264 ['tools/run_tests/run_python.sh'],
265 None,
266 environ=environment,
267 shortname='py.test',
Masood Malekghassemi26ea9e22015-10-09 15:19:17 -0700268 timeout_seconds=15*60
Masood Malekghassemi2b841622015-07-28 17:39:02 -0700269 )]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000270
murgatroid99256d3df2015-09-21 16:58:02 -0700271 def pre_build_steps(self):
272 return []
273
Nathaniel Manista840615e2015-01-22 20:31:47 +0000274 def make_targets(self):
Craig Tilleraf7cf542015-05-22 10:07:34 -0700275 return ['static_c', 'grpc_python_plugin', 'shared_c']
Nathaniel Manista840615e2015-01-22 20:31:47 +0000276
277 def build_steps(self):
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700278 commands = []
279 for python_version in self._build_python_versions:
280 try:
281 with open(os.devnull, 'w') as output:
282 subprocess.check_call(['which', 'python' + python_version],
283 stdout=output, stderr=output)
284 commands.append(['tools/run_tests/build_python.sh', python_version])
285 self._has_python_versions.append(python_version)
286 except:
287 jobset.message('WARNING', 'Missing Python ' + python_version,
288 do_newline=True)
289 return commands
Nathaniel Manista840615e2015-01-22 20:31:47 +0000290
Nicolas "Pixel" Noble3fcd3bf2015-10-10 02:30:38 +0200291 def post_tests_steps(self):
292 return []
293
murgatroid99a3e244f2015-09-22 11:25:53 -0700294 def makefile_name(self):
295 return 'Makefile'
296
murgatroid99132ce6a2015-03-04 17:29:14 -0800297 def supports_multi_config(self):
298 return False
299
300 def __str__(self):
301 return 'python'
302
Craig Tillerd625d812015-04-08 15:52:35 -0700303
murgatroid996a4c4fa2015-02-27 12:08:57 -0800304class RubyLanguage(object):
305
306 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700307 return [config.job_spec(['tools/run_tests/run_ruby.sh'], None,
Craig Tiller06805272015-06-11 14:46:47 -0700308 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
murgatroid996a4c4fa2015-02-27 12:08:57 -0800309
murgatroid99256d3df2015-09-21 16:58:02 -0700310 def pre_build_steps(self):
Nicolas "Pixel" Noblebcf988f2015-10-08 03:00:42 +0200311 return [['tools/run_tests/pre_build_ruby.sh']]
murgatroid99256d3df2015-09-21 16:58:02 -0700312
murgatroid996a4c4fa2015-02-27 12:08:57 -0800313 def make_targets(self):
murgatroid99a43c14f2015-07-30 13:31:23 -0700314 return ['static_c']
murgatroid996a4c4fa2015-02-27 12:08:57 -0800315
316 def build_steps(self):
317 return [['tools/run_tests/build_ruby.sh']]
318
Nicolas "Pixel" Noble3fcd3bf2015-10-10 02:30:38 +0200319 def post_tests_steps(self):
320 return []
321
murgatroid99a3e244f2015-09-22 11:25:53 -0700322 def makefile_name(self):
323 return 'Makefile'
324
murgatroid99132ce6a2015-03-04 17:29:14 -0800325 def supports_multi_config(self):
326 return False
327
328 def __str__(self):
329 return 'ruby'
330
Craig Tillerd625d812015-04-08 15:52:35 -0700331
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800332class CSharpLanguage(object):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700333 def __init__(self):
Craig Tillerd50993d2015-08-05 08:04:36 -0700334 self.platform = platform_string()
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700335
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800336 def test_specs(self, config, travis):
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700337 assemblies = ['Grpc.Core.Tests',
338 'Grpc.Examples.Tests',
Jan Tattermusch9d67d8d2015-08-01 20:39:16 -0700339 'Grpc.HealthCheck.Tests',
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700340 'Grpc.IntegrationTesting']
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700341 if self.platform == 'windows':
342 cmd = 'tools\\run_tests\\run_csharp.bat'
343 else:
344 cmd = 'tools/run_tests/run_csharp.sh'
Jan Tattermuschbf3b1532015-10-26 10:24:42 -0700345
346 if not config.build_config == 'gcov':
347 return [config.job_spec([cmd, assembly],
348 None, shortname=assembly,
349 environ=_FORCE_ENVIRON_FOR_WRAPPERS)
350 for assembly in assemblies]
351 else:
352 # For code coverage we need to run all tests in one suite.
353 return [config.job_spec([cmd], None,
354 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800355
murgatroid99256d3df2015-09-21 16:58:02 -0700356 def pre_build_steps(self):
Jan Tattermusch48423fc2015-10-07 18:59:16 -0700357 if self.platform == 'windows':
Jan Tattermusch874aec02015-10-07 19:26:19 -0700358 return [['tools\\run_tests\\pre_build_csharp.bat']]
Jan Tattermusch48423fc2015-10-07 18:59:16 -0700359 else:
360 return [['tools/run_tests/pre_build_csharp.sh']]
murgatroid99256d3df2015-09-21 16:58:02 -0700361
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800362 def make_targets(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700363 # For Windows, this target doesn't really build anything,
364 # everything is build by buildall script later.
Craig Tillerd5904822015-08-31 21:30:58 -0700365 if self.platform == 'windows':
366 return []
367 else:
368 return ['grpc_csharp_ext']
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800369
370 def build_steps(self):
Jan Tattermuschb00aa672015-06-01 15:48:03 -0700371 if self.platform == 'windows':
372 return [['src\\csharp\\buildall.bat']]
373 else:
374 return [['tools/run_tests/build_csharp.sh']]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000375
Nicolas "Pixel" Noble3fcd3bf2015-10-10 02:30:38 +0200376 def post_tests_steps(self):
377 return []
378
murgatroid99a3e244f2015-09-22 11:25:53 -0700379 def makefile_name(self):
380 return 'Makefile'
381
murgatroid99132ce6a2015-03-04 17:29:14 -0800382 def supports_multi_config(self):
383 return False
384
385 def __str__(self):
386 return 'csharp'
387
Craig Tillerd625d812015-04-08 15:52:35 -0700388
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700389class ObjCLanguage(object):
390
391 def test_specs(self, config, travis):
392 return [config.job_spec(['src/objective-c/tests/run_tests.sh'], None,
393 environ=_FORCE_ENVIRON_FOR_WRAPPERS)]
394
murgatroid99256d3df2015-09-21 16:58:02 -0700395 def pre_build_steps(self):
396 return []
397
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700398 def make_targets(self):
Jorge Canizalesd0b32e92015-07-30 23:08:43 -0700399 return ['grpc_objective_c_plugin', 'interop_server']
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700400
401 def build_steps(self):
Jorge Canizalesd0b32e92015-07-30 23:08:43 -0700402 return [['src/objective-c/tests/build_tests.sh']]
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700403
Nicolas "Pixel" Noble3fcd3bf2015-10-10 02:30:38 +0200404 def post_tests_steps(self):
405 return []
406
murgatroid99a3e244f2015-09-22 11:25:53 -0700407 def makefile_name(self):
408 return 'Makefile'
409
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700410 def supports_multi_config(self):
411 return False
412
413 def __str__(self):
414 return 'objc'
415
416
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100417class Sanity(object):
418
419 def test_specs(self, config, travis):
Craig Tillerf75fc122015-06-25 06:58:00 -0700420 return [config.job_spec('tools/run_tests/run_sanity.sh', None),
421 config.job_spec('tools/run_tests/check_sources_and_headers.py', None)]
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100422
murgatroid99256d3df2015-09-21 16:58:02 -0700423 def pre_build_steps(self):
424 return []
425
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100426 def make_targets(self):
427 return ['run_dep_checks']
428
429 def build_steps(self):
430 return []
431
Nicolas "Pixel" Noble87879b32015-10-12 23:28:53 +0200432 def post_tests_steps(self):
433 return []
434
murgatroid99a3e244f2015-09-22 11:25:53 -0700435 def makefile_name(self):
436 return 'Makefile'
437
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100438 def supports_multi_config(self):
439 return False
440
441 def __str__(self):
442 return 'sanity'
443
Nicolas "Pixel" Noblee55cd7f2015-04-14 17:59:13 +0200444
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100445class Build(object):
446
447 def test_specs(self, config, travis):
448 return []
449
murgatroid99256d3df2015-09-21 16:58:02 -0700450 def pre_build_steps(self):
451 return []
452
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100453 def make_targets(self):
Nicolas "Pixel" Noblec23827b2015-04-23 06:17:55 +0200454 return ['static']
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100455
456 def build_steps(self):
457 return []
458
murgatroid99a3e244f2015-09-22 11:25:53 -0700459 def makefile_name(self):
460 return 'Makefile'
461
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100462 def supports_multi_config(self):
463 return True
464
465 def __str__(self):
466 return self.make_target
467
468
Craig Tiller738c3342015-01-12 14:28:33 -0800469# different configurations we can run under
470_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -0800471 'dbg': SimpleConfig('dbg'),
472 'opt': SimpleConfig('opt'),
Masood Malekghassemi26ea9e22015-10-09 15:19:17 -0700473 'tsan': SimpleConfig('tsan', timeout_multiplier=2, environ={
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700474 'TSAN_OPTIONS': 'suppressions=tools/tsan_suppressions.txt:halt_on_error=1:second_deadlock_stack=1'}),
Masood Malekghassemi26ea9e22015-10-09 15:19:17 -0700475 'msan': SimpleConfig('msan', timeout_multiplier=1.5),
Craig Tiller96bd5f62015-02-13 09:04:13 -0800476 'ubsan': SimpleConfig('ubsan'),
Masood Malekghassemi26ea9e22015-10-09 15:19:17 -0700477 'asan': SimpleConfig('asan', timeout_multiplier=1.5, environ={
Craig Tillerd4b13622015-05-29 09:10:10 -0700478 'ASAN_OPTIONS': 'detect_leaks=1:color=always:suppressions=tools/tsan_suppressions.txt',
479 'LSAN_OPTIONS': 'report_objects=1'}),
Craig Tiller810725c2015-05-12 09:44:41 -0700480 'asan-noleaks': SimpleConfig('asan', environ={
481 'ASAN_OPTIONS': 'detect_leaks=0:color=always:suppressions=tools/tsan_suppressions.txt'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800482 'gcov': SimpleConfig('gcov'),
Craig Tiller1a305b12015-02-18 13:37:06 -0800483 'memcheck': ValgrindConfig('valgrind', 'memcheck', ['--leak-check=full']),
Craig Tillerb50d1662015-01-15 17:28:21 -0800484 'helgrind': ValgrindConfig('dbg', 'helgrind')
485 }
Craig Tiller738c3342015-01-12 14:28:33 -0800486
487
Nicolas "Pixel" Noble1fb5e822015-03-16 06:20:37 +0100488_DEFAULT = ['opt']
Craig Tillerc7449162015-01-16 14:42:10 -0800489_LANGUAGES = {
Craig Tillere9c959d2015-01-18 10:23:26 -0800490 'c++': CLanguage('cxx', 'c++'),
491 'c': CLanguage('c', 'c'),
murgatroid992c8d5162015-01-26 10:41:21 -0800492 'node': NodeLanguage(),
Nathaniel Manista840615e2015-01-22 20:31:47 +0000493 'php': PhpLanguage(),
494 'python': PythonLanguage(),
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800495 'ruby': RubyLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100496 'csharp': CSharpLanguage(),
Jorge Canizalesa0b3bfa2015-07-30 19:25:52 -0700497 'objc' : ObjCLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100498 'sanity': Sanity(),
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100499 'build': Build(),
Craig Tillereb272bc2015-01-30 13:13:14 -0800500 }
Nicolas Nobleddef2462015-01-06 18:08:25 -0800501
Craig Tiller7bb3efd2015-09-01 08:04:03 -0700502_WINDOWS_CONFIG = {
503 'dbg': 'Debug',
504 'opt': 'Release',
505 }
506
David Garcia Quintase90cd372015-05-31 18:15:26 -0700507
508def runs_per_test_type(arg_str):
509 """Auxilary function to parse the "runs_per_test" flag.
510
511 Returns:
512 A positive integer or 0, the latter indicating an infinite number of
513 runs.
514
515 Raises:
516 argparse.ArgumentTypeError: Upon invalid input.
517 """
518 if arg_str == 'inf':
519 return 0
520 try:
521 n = int(arg_str)
522 if n <= 0: raise ValueError
Craig Tiller50e53e22015-06-01 20:18:21 -0700523 return n
David Garcia Quintase90cd372015-05-31 18:15:26 -0700524 except:
Adele Zhoue4c35612015-10-16 15:34:23 -0700525 msg = '\'{}\' is not a positive integer or \'inf\''.format(arg_str)
David Garcia Quintase90cd372015-05-31 18:15:26 -0700526 raise argparse.ArgumentTypeError(msg)
Jan Tattermuschc95eead2015-09-18 13:03:50 -0700527
528# parse command line
529argp = argparse.ArgumentParser(description='Run grpc tests.')
530argp.add_argument('-c', '--config',
531 choices=['all'] + sorted(_CONFIGS.keys()),
532 nargs='+',
533 default=_DEFAULT)
David Garcia Quintase90cd372015-05-31 18:15:26 -0700534argp.add_argument('-n', '--runs_per_test', default=1, type=runs_per_test_type,
535 help='A positive integer or "inf". If "inf", all tests will run in an '
536 'infinite loop. Especially useful in combination with "-f"')
Craig Tillerfe406ec2015-02-24 13:55:12 -0800537argp.add_argument('-r', '--regex', default='.*', type=str)
Craig Tiller83762ac2015-05-22 14:04:06 -0700538argp.add_argument('-j', '--jobs', default=2 * multiprocessing.cpu_count(), type=int)
Craig Tiller8451e872015-02-27 09:25:51 -0800539argp.add_argument('-s', '--slowdown', default=1.0, type=float)
ctiller3040cb72015-01-07 12:13:17 -0800540argp.add_argument('-f', '--forever',
541 default=False,
542 action='store_const',
543 const=True)
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100544argp.add_argument('-t', '--travis',
545 default=False,
546 action='store_const',
547 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800548argp.add_argument('--newline_on_success',
549 default=False,
550 action='store_const',
551 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -0800552argp.add_argument('-l', '--language',
Craig Tiller60f15e62015-05-13 09:05:17 -0700553 choices=['all'] + sorted(_LANGUAGES.keys()),
Craig Tiller686fb262015-01-15 07:39:09 -0800554 nargs='+',
Craig Tiller60f15e62015-05-13 09:05:17 -0700555 default=['all'])
Craig Tillercd43da82015-05-29 08:41:29 -0700556argp.add_argument('-S', '--stop_on_failure',
557 default=False,
558 action='store_const',
559 const=True)
Jan Tattermuschc95eead2015-09-18 13:03:50 -0700560argp.add_argument('--use_docker',
561 default=False,
562 action='store_const',
563 const=True,
Adele Zhoue4c35612015-10-16 15:34:23 -0700564 help='Run all the tests under docker. That provides ' +
565 'additional isolation and prevents the need to install ' +
566 'language specific prerequisites. Only available on Linux.')
Craig Tillerd4509a12015-09-28 09:18:40 -0700567argp.add_argument('--allow_flakes',
568 default=False,
569 action='store_const',
570 const=True,
Adele Zhoue4c35612015-10-16 15:34:23 -0700571 help='Allow flaky tests to show as passing (re-runs failed tests up to five times)')
Craig Tiller234b6e72015-05-23 10:12:40 -0700572argp.add_argument('-a', '--antagonists', default=0, type=int)
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200573argp.add_argument('-x', '--xml_report', default=None, type=str,
574 help='Generates a JUnit-compatible XML report')
Nicolas Nobleddef2462015-01-06 18:08:25 -0800575args = argp.parse_args()
576
Jan Tattermuschc96b9eb2015-09-18 16:01:21 -0700577if args.use_docker:
578 if not args.travis:
579 print 'Seen --use_docker flag, will run tests under docker.'
580 print
581 print 'IMPORTANT: The changes you are testing need to be locally committed'
582 print 'because only the committed changes in the current branch will be'
583 print 'copied to the docker environment.'
584 time.sleep(5)
Jan Tattermuschc95eead2015-09-18 13:03:50 -0700585
586 child_argv = [ arg for arg in sys.argv if not arg == '--use_docker' ]
Adele Zhoue4c35612015-10-16 15:34:23 -0700587 run_tests_cmd = 'tools/run_tests/run_tests.py %s' % ' '.join(child_argv[1:])
Jan Tattermuschc95eead2015-09-18 13:03:50 -0700588
589 # TODO(jtattermusch): revisit if we need special handling for arch here
590 # set arch command prefix in case we are working with different arch.
591 arch_env = os.getenv('arch')
592 if arch_env:
593 run_test_cmd = 'arch %s %s' % (arch_env, run_test_cmd)
594
595 env = os.environ.copy()
596 env['RUN_TESTS_COMMAND'] = run_tests_cmd
597 if args.xml_report:
598 env['XML_REPORT'] = args.xml_report
Jan Tattermusch261b58c2015-09-18 17:15:48 -0700599 if not args.travis:
600 env['TTY_FLAG'] = '-t' # enables Ctrl-C when not on Jenkins.
Jan Tattermuschc95eead2015-09-18 13:03:50 -0700601
602 subprocess.check_call(['tools/jenkins/build_docker_and_run_tests.sh'],
603 shell=True,
604 env=env)
605 sys.exit(0)
606
Nicolas Nobleddef2462015-01-06 18:08:25 -0800607# grab config
Craig Tiller738c3342015-01-12 14:28:33 -0800608run_configs = set(_CONFIGS[cfg]
609 for cfg in itertools.chain.from_iterable(
610 _CONFIGS.iterkeys() if x == 'all' else [x]
611 for x in args.config))
612build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tillerf1973b02015-01-16 12:32:13 -0800613
Craig Tiller06805272015-06-11 14:46:47 -0700614if args.travis:
murgatroid99d3b5b7f2015-10-06 17:02:03 -0700615 _FORCE_ENVIRON_FOR_WRAPPERS = {'GRPC_TRACE': 'api'}
Craig Tiller06805272015-06-11 14:46:47 -0700616
Craig Tiller60f15e62015-05-13 09:05:17 -0700617languages = set(_LANGUAGES[l]
618 for l in itertools.chain.from_iterable(
619 _LANGUAGES.iterkeys() if x == 'all' else [x]
620 for x in args.language))
murgatroid99132ce6a2015-03-04 17:29:14 -0800621
622if len(build_configs) > 1:
623 for language in languages:
624 if not language.supports_multi_config():
625 print language, 'does not support multiple build configurations'
626 sys.exit(1)
627
Craig Tiller5058c692015-04-08 09:42:04 -0700628if platform.system() == 'Windows':
murgatroid99a3e244f2015-09-22 11:25:53 -0700629 def make_jobspec(cfg, targets, makefile='Makefile'):
Craig Tillerfc3c0c42015-09-01 16:47:54 -0700630 extra_args = []
Craig Tillerb5391e12015-09-03 14:35:18 -0700631 # better do parallel compilation
Jan Tattermusch47eeb2b2015-10-07 14:09:18 -0700632 # empirically /m:2 gives the best performance/price and should prevent
633 # overloading the windows workers.
Adele Zhoue4c35612015-10-16 15:34:23 -0700634 extra_args.extend(['/m:2'])
Craig Tillerb5391e12015-09-03 14:35:18 -0700635 # disable PDB generation: it's broken, and we don't need it during CI
Adele Zhoue4c35612015-10-16 15:34:23 -0700636 extra_args.extend(['/p:Jenkins=true'])
Craig Tiller6fd23842015-09-01 07:36:31 -0700637 return [
murgatroid99cf08daf2015-09-21 15:33:16 -0700638 jobset.JobSpec(['vsprojects\\build.bat',
639 'vsprojects\\%s.sln' % target,
Craig Tillerfc3c0c42015-09-01 16:47:54 -0700640 '/p:Configuration=%s' % _WINDOWS_CONFIG[cfg]] +
641 extra_args,
Craig Tillerdfc3eee2015-09-01 16:32:16 -0700642 shell=True, timeout_seconds=90*60)
Craig Tiller6fd23842015-09-01 07:36:31 -0700643 for target in targets]
Craig Tiller5058c692015-04-08 09:42:04 -0700644else:
murgatroid99a3e244f2015-09-22 11:25:53 -0700645 def make_jobspec(cfg, targets, makefile='Makefile'):
Craig Tiller6fd23842015-09-01 07:36:31 -0700646 return [jobset.JobSpec([os.getenv('MAKE', 'make'),
murgatroid99a3e244f2015-09-22 11:25:53 -0700647 '-f', makefile,
Craig Tiller6fd23842015-09-01 07:36:31 -0700648 '-j', '%d' % (multiprocessing.cpu_count() + 1),
649 'EXTRA_DEFINES=GRPC_TEST_SLOWDOWN_MACHINE_FACTOR=%f' %
650 args.slowdown,
651 'CONFIG=%s' % cfg] + targets,
652 timeout_seconds=30*60)]
murgatroid99a3e244f2015-09-22 11:25:53 -0700653make_targets = {}
654for l in languages:
655 makefile = l.makefile_name()
656 make_targets[makefile] = make_targets.get(makefile, set()).union(
657 set(l.make_targets()))
Craig Tiller5058c692015-04-08 09:42:04 -0700658
murgatroid99fddac962015-09-22 09:20:11 -0700659build_steps = list(set(
Nicolas "Pixel" Noble351da8d2015-10-08 02:55:08 +0200660 jobset.JobSpec(cmdline, environ={'CONFIG': cfg}, flake_retries=5)
murgatroid99256d3df2015-09-21 16:58:02 -0700661 for cfg in build_configs
662 for l in languages
663 for cmdline in l.pre_build_steps()))
Craig Tillerbd4e3782015-09-01 06:48:55 -0700664if make_targets:
murgatroid99a3e244f2015-09-22 11:25:53 -0700665 make_commands = itertools.chain.from_iterable(make_jobspec(cfg, list(targets), makefile) for cfg in build_configs for (makefile, targets) in make_targets.iteritems())
Craig Tiller6fd23842015-09-01 07:36:31 -0700666 build_steps.extend(set(make_commands))
Craig Tiller5058c692015-04-08 09:42:04 -0700667build_steps.extend(set(
Jan Tattermusch21897192015-10-07 15:42:21 -0700668 jobset.JobSpec(cmdline, environ={'CONFIG': cfg}, timeout_seconds=10*60)
murgatroid99132ce6a2015-03-04 17:29:14 -0800669 for cfg in build_configs
Craig Tiller547db2b2015-01-30 14:08:39 -0800670 for l in languages
Craig Tiller533b1a22015-05-29 08:41:29 -0700671 for cmdline in l.build_steps()))
Craig Tillerf1973b02015-01-16 12:32:13 -0800672
Nicolas "Pixel" Noble3fcd3bf2015-10-10 02:30:38 +0200673post_tests_steps = list(set(
674 jobset.JobSpec(cmdline, environ={'CONFIG': cfg})
675 for cfg in build_configs
676 for l in languages
677 for cmdline in l.post_tests_steps()))
Nicolas Nobleddef2462015-01-06 18:08:25 -0800678runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -0800679forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -0800680
Nicolas Nobleddef2462015-01-06 18:08:25 -0800681
Craig Tiller71735182015-01-15 17:07:13 -0800682class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800683 """Cache for running tests."""
684
David Klempner25739582015-02-11 15:57:32 -0800685 def __init__(self, use_cache_results):
Craig Tiller71735182015-01-15 17:07:13 -0800686 self._last_successful_run = {}
David Klempner25739582015-02-11 15:57:32 -0800687 self._use_cache_results = use_cache_results
Craig Tiller69cd2372015-06-11 09:38:09 -0700688 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800689
690 def should_run(self, cmdline, bin_hash):
Craig Tiller71735182015-01-15 17:07:13 -0800691 if cmdline not in self._last_successful_run:
692 return True
693 if self._last_successful_run[cmdline] != bin_hash:
694 return True
David Klempner25739582015-02-11 15:57:32 -0800695 if not self._use_cache_results:
696 return True
Craig Tiller71735182015-01-15 17:07:13 -0800697 return False
698
699 def finished(self, cmdline, bin_hash):
Craig Tiller547db2b2015-01-30 14:08:39 -0800700 self._last_successful_run[cmdline] = bin_hash
Craig Tiller69cd2372015-06-11 09:38:09 -0700701 if time.time() - self._last_save > 1:
702 self.save()
Craig Tiller71735182015-01-15 17:07:13 -0800703
704 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800705 return [{'cmdline': k, 'hash': v}
706 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800707
708 def parse(self, exdump):
709 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
710
711 def save(self):
712 with open('.run_tests_cache', 'w') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800713 f.write(json.dumps(self.dump()))
Craig Tiller69cd2372015-06-11 09:38:09 -0700714 self._last_save = time.time()
Craig Tiller71735182015-01-15 17:07:13 -0800715
Craig Tiller1cc11db2015-01-15 22:50:50 -0800716 def maybe_load(self):
717 if os.path.exists('.run_tests_cache'):
718 with open('.run_tests_cache') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800719 self.parse(json.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800720
721
Craig Tillerf53d9c82015-08-04 14:19:43 -0700722def _start_port_server(port_server_port):
723 # check if a compatible port server is running
724 # if incompatible (version mismatch) ==> start a new one
725 # if not running ==> start a new one
726 # otherwise, leave it up
727 try:
Craig Tillerfe4939f2015-10-06 12:55:36 -0700728 version = int(urllib2.urlopen(
729 'http://localhost:%d/version_number' % port_server_port,
730 timeout=1).read())
731 print 'detected port server running version %d' % version
Craig Tillerf53d9c82015-08-04 14:19:43 -0700732 running = True
Craig Tillerfe4939f2015-10-06 12:55:36 -0700733 except Exception as e:
Craig Tiller0d6499e2015-10-05 14:00:47 -0700734 print 'failed to detect port server: %s' % sys.exc_info()[0]
Craig Tillerfe4939f2015-10-06 12:55:36 -0700735 print e.strerror
Craig Tillerf53d9c82015-08-04 14:19:43 -0700736 running = False
737 if running:
Craig Tillerfe4939f2015-10-06 12:55:36 -0700738 current_version = int(subprocess.check_output(
739 [sys.executable, 'tools/run_tests/port_server.py', 'dump_version']))
740 print 'my port server is version %d' % current_version
741 running = (version >= current_version)
742 if not running:
743 print 'port_server version mismatch: killing the old one'
744 urllib2.urlopen('http://localhost:%d/quitquitquit' % port_server_port).read()
745 time.sleep(1)
Craig Tillerf53d9c82015-08-04 14:19:43 -0700746 if not running:
Craig Tillerf0a293e2015-10-12 10:05:50 -0700747 fd, logfile = tempfile.mkstemp()
748 os.close(fd)
749 print 'starting port_server, with log file %s' % logfile
Craig Tillerd2c39712015-10-12 11:08:49 -0700750 args = [sys.executable, 'tools/run_tests/port_server.py', '-p', '%d' % port_server_port, '-l', logfile]
Craig Tiller367d41d2015-10-12 13:00:22 -0700751 env = dict(os.environ)
752 env['BUILD_ID'] = 'pleaseDontKillMeJenkins'
Craig Tillerd2c39712015-10-12 11:08:49 -0700753 if platform.system() == 'Windows':
754 port_server = subprocess.Popen(
Craig Tiller367d41d2015-10-12 13:00:22 -0700755 args,
756 env=env,
757 creationflags = 0x00000008, # detached process
758 close_fds=True)
Craig Tillerd2c39712015-10-12 11:08:49 -0700759 else:
760 port_server = subprocess.Popen(
761 args,
Craig Tiller367d41d2015-10-12 13:00:22 -0700762 env=env,
Craig Tillerd2c39712015-10-12 11:08:49 -0700763 preexec_fn=os.setsid,
764 close_fds=True)
Craig Tillerf0a293e2015-10-12 10:05:50 -0700765 time.sleep(1)
Craig Tiller8b5f4dc2015-08-26 08:02:01 -0700766 # ensure port server is up
Craig Tillerabd37fd2015-08-26 07:54:01 -0700767 waits = 0
Craig Tillerf53d9c82015-08-04 14:19:43 -0700768 while True:
Craig Tillerabd37fd2015-08-26 07:54:01 -0700769 if waits > 10:
Craig Tillerf0a293e2015-10-12 10:05:50 -0700770 print 'killing port server due to excessive start up waits'
Craig Tillerabd37fd2015-08-26 07:54:01 -0700771 port_server.kill()
Craig Tillera2f38b02015-09-24 11:19:17 -0700772 if port_server.poll() is not None:
Craig Tiller0d6499e2015-10-05 14:00:47 -0700773 print 'port_server failed to start'
Craig Tillerf0a293e2015-10-12 10:05:50 -0700774 # try one final time: maybe another build managed to start one
775 time.sleep(1)
776 try:
777 urllib2.urlopen('http://localhost:%d/get' % port_server_port,
778 timeout=1).read()
779 print 'last ditch attempt to contact port server succeeded'
780 break
781 except:
782 traceback.print_exc();
783 port_log = open(logfile, 'r').read()
784 print port_log
785 sys.exit(1)
Craig Tillerf53d9c82015-08-04 14:19:43 -0700786 try:
Craig Tillerabd37fd2015-08-26 07:54:01 -0700787 urllib2.urlopen('http://localhost:%d/get' % port_server_port,
788 timeout=1).read()
Craig Tillerf0a293e2015-10-12 10:05:50 -0700789 print 'port server is up and ready'
Craig Tillerf53d9c82015-08-04 14:19:43 -0700790 break
Craig Tiller31fdaa42015-09-25 13:09:59 -0700791 except socket.timeout:
Craig Tiller0d6499e2015-10-05 14:00:47 -0700792 print 'waiting for port_server: timeout'
Craig Tillerf0a293e2015-10-12 10:05:50 -0700793 traceback.print_exc();
794 time.sleep(1)
Craig Tiller31fdaa42015-09-25 13:09:59 -0700795 waits += 1
Craig Tillerf53d9c82015-08-04 14:19:43 -0700796 except urllib2.URLError:
Craig Tiller0d6499e2015-10-05 14:00:47 -0700797 print 'waiting for port_server: urlerror'
Craig Tillerf0a293e2015-10-12 10:05:50 -0700798 traceback.print_exc();
799 time.sleep(1)
Craig Tillerabd37fd2015-08-26 07:54:01 -0700800 waits += 1
Craig Tillerf53d9c82015-08-04 14:19:43 -0700801 except:
Craig Tillerf0a293e2015-10-12 10:05:50 -0700802 traceback.print_exc();
Craig Tillerf53d9c82015-08-04 14:19:43 -0700803 port_server.kill()
804 raise
805
806
807def _build_and_run(
808 check_cancelled, newline_on_success, travis, cache, xml_report=None):
ctiller3040cb72015-01-07 12:13:17 -0800809 """Do one pass of building & running tests."""
murgatroid99666450e2015-01-26 13:03:31 -0800810 # build latest sequentially
Adele Zhoue4c35612015-10-16 15:34:23 -0700811 num_failures, _ = jobset.run(
812 build_steps, maxjobs=1, stop_on_failure=True,
813 newline_on_success=newline_on_success, travis=travis)
814 if num_failures:
Craig Tillerd86a3942015-01-14 12:48:54 -0800815 return 1
ctiller3040cb72015-01-07 12:13:17 -0800816
Craig Tiller234b6e72015-05-23 10:12:40 -0700817 # start antagonists
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700818 antagonists = [subprocess.Popen(['tools/run_tests/antagonist.py'])
Craig Tiller234b6e72015-05-23 10:12:40 -0700819 for _ in range(0, args.antagonists)]
Craig Tillerfe4939f2015-10-06 12:55:36 -0700820 port_server_port = 32767
Craig Tillerf53d9c82015-08-04 14:19:43 -0700821 _start_port_server(port_server_port)
Craig Tiller234b6e72015-05-23 10:12:40 -0700822 try:
David Garcia Quintase90cd372015-05-31 18:15:26 -0700823 infinite_runs = runs_per_test == 0
yang-g6c1fdc62015-08-18 11:57:42 -0700824 one_run = set(
825 spec
826 for config in run_configs
827 for language in languages
828 for spec in language.test_specs(config, args.travis)
829 if re.search(args.regex, spec.shortname))
David Garcia Quintas79e389f2015-06-02 17:49:42 -0700830 # When running on travis, we want out test runs to be as similar as possible
831 # for reproducibility purposes.
832 if travis:
833 massaged_one_run = sorted(one_run, key=lambda x: x.shortname)
834 else:
835 # whereas otherwise, we want to shuffle things up to give all tests a
836 # chance to run.
837 massaged_one_run = list(one_run) # random.shuffle needs an indexable seq.
838 random.shuffle(massaged_one_run) # which it modifies in-place.
Craig Tillerf7b7c892015-06-22 14:33:25 -0700839 if infinite_runs:
840 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 -0700841 runs_sequence = (itertools.repeat(massaged_one_run) if infinite_runs
842 else itertools.repeat(massaged_one_run, runs_per_test))
David Garcia Quintase90cd372015-05-31 18:15:26 -0700843 all_runs = itertools.chain.from_iterable(runs_sequence)
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200844
845 root = ET.Element('testsuites') if xml_report else None
846 testsuite = ET.SubElement(root, 'testsuite', id='1', package='grpc', name='tests') if xml_report else None
847
Adele Zhouf2ca7bc2015-10-23 15:38:00 -0700848 number_failures, _ = jobset.run(
849 all_runs, check_cancelled, newline_on_success=newline_on_success,
850 travis=travis, infinite_runs=infinite_runs, maxjobs=args.jobs,
851 stop_on_failure=args.stop_on_failure,
852 cache=cache if not xml_report else None,
853 xml_report=testsuite,
854 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port})
855 if number_failures:
Craig Tiller234b6e72015-05-23 10:12:40 -0700856 return 2
857 finally:
858 for antagonist in antagonists:
859 antagonist.kill()
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200860 if xml_report:
861 tree = ET.ElementTree(root)
862 tree.write(xml_report, encoding='UTF-8')
Craig Tillerd86a3942015-01-14 12:48:54 -0800863
Adele Zhouf2ca7bc2015-10-23 15:38:00 -0700864 number_failures, _ = jobset.run(
865 post_tests_steps, maxjobs=1, stop_on_failure=True,
866 newline_on_success=newline_on_success, travis=travis)
867 if number_failures:
Nicolas "Pixel" Noble3fcd3bf2015-10-10 02:30:38 +0200868 return 3
869
Craig Tiller69cd2372015-06-11 09:38:09 -0700870 if cache: cache.save()
871
Craig Tillerd86a3942015-01-14 12:48:54 -0800872 return 0
ctiller3040cb72015-01-07 12:13:17 -0800873
874
David Klempner25739582015-02-11 15:57:32 -0800875test_cache = TestCache(runs_per_test == 1)
Craig Tiller547db2b2015-01-30 14:08:39 -0800876test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800877
ctiller3040cb72015-01-07 12:13:17 -0800878if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800879 success = True
ctiller3040cb72015-01-07 12:13:17 -0800880 while True:
Craig Tiller42bc87c2015-02-23 08:50:19 -0800881 dw = watch_dirs.DirWatcher(['src', 'include', 'test', 'examples'])
ctiller3040cb72015-01-07 12:13:17 -0800882 initial_time = dw.most_recent_change()
883 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800884 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800885 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800886 newline_on_success=False,
Craig Tiller9a5a9402015-04-16 10:39:50 -0700887 travis=args.travis,
Craig Tiller71735182015-01-15 17:07:13 -0800888 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800889 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800890 jobset.message('SUCCESS',
891 'All tests are now passing properly',
892 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800893 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800894 while not have_files_changed():
895 time.sleep(1)
896else:
Craig Tiller71735182015-01-15 17:07:13 -0800897 result = _build_and_run(check_cancelled=lambda: False,
898 newline_on_success=args.newline_on_success,
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100899 travis=args.travis,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200900 cache=test_cache,
901 xml_report=args.xml_report)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800902 if result == 0:
903 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
904 else:
905 jobset.message('FAILED', 'Some tests failed', do_newline=True)
906 sys.exit(result)