blob: 157bd281caea26888a9b8cd511303e907198e300 [file] [log] [blame]
Mads Ager418d1ca2017-05-22 09:35:49 +02001#!/usr/bin/env python
2# Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5
6# Convenience script for running tests. If no argument is given run all tests,
7# if an argument is given, run only tests with that pattern. This script will
8# force the tests to run, even if no input changed.
9
Ian Zerny019e7782021-04-20 15:34:01 +020010import archive_desugar_jdk_libs
11import notify
Mads Ager418d1ca2017-05-22 09:35:49 +020012import optparse
Ian Zerny5fffb0a2019-02-11 13:54:22 +010013import os
Ian Zerny24398bc2019-02-22 11:59:18 +010014import shutil
Rico Windf65a1d62017-06-30 09:41:56 +020015import subprocess
Mads Ager418d1ca2017-05-22 09:35:49 +020016import sys
Rico Windda6836e2018-12-07 12:32:03 +010017import thread
18import time
Rico Wind06487ac2018-12-10 09:09:19 +010019import uuid
Ian Zerny5fffb0a2019-02-11 13:54:22 +010020
21import gradle
Ian Zerny5fffb0a2019-02-11 13:54:22 +010022import utils
Jean-Marie Henaffce162f32017-10-04 10:39:27 +020023
Søren Gjessefe7c0112018-12-03 12:33:12 +010024ALL_ART_VMS = [
25 "default",
clementbera97d5cce2019-11-22 15:09:27 +010026 "10.0.0",
Søren Gjessefe7c0112018-12-03 12:33:12 +010027 "9.0.0",
28 "8.1.0",
29 "7.0.0",
30 "6.0.1",
31 "5.1.1",
32 "4.4.4",
33 "4.0.4"]
Mads Ager418d1ca2017-05-22 09:35:49 +020034
Rico Windda6836e2018-12-07 12:32:03 +010035# How often do we check for progress on the bots:
36# Should be long enough that a normal run would always have med progress
37# Should be short enough that we ensure that two calls are close enough
38# to happen before bot times out.
39# A false positiv, i.e., printing the stacks of non hanging processes
40# is not a problem, no harm done except some logging in the stdout.
41TIMEOUT_HANDLER_PERIOD = 60 * 18
42
Rico Wind06487ac2018-12-10 09:09:19 +010043BUCKET = 'r8-test-results'
44
Ian Zerny24398bc2019-02-22 11:59:18 +010045NUMBER_OF_TEST_REPORTS = 5
46REPORTS_PATH = os.path.join(utils.BUILD, 'reports')
47REPORT_INDEX = ['tests', 'test', 'index.html']
Ian Zernybcbd6c52019-10-29 15:27:04 +010048VALID_RUNTIMES = [
49 'none',
50 'jdk8',
51 'jdk9',
52 'jdk11',
53] + [ 'dex-%s' % dexvm for dexvm in ALL_ART_VMS ]
Ian Zerny24398bc2019-02-22 11:59:18 +010054
Mads Ager418d1ca2017-05-22 09:35:49 +020055def ParseOptions():
56 result = optparse.OptionParser()
Søren Gjesse77527982018-10-05 12:58:49 +020057 result.add_option('--no-internal', '--no_internal',
Mads Ager418d1ca2017-05-22 09:35:49 +020058 help='Do not run Google internal tests.',
59 default=False, action='store_true')
Søren Gjesse77527982018-10-05 12:58:49 +020060 result.add_option('--archive-failures', '--archive_failures',
Rico Winda94f01c2017-06-27 10:32:34 +020061 help='Upload test results to cloud storage on failure.',
62 default=False, action='store_true')
Søren Gjesse77527982018-10-05 12:58:49 +020063 result.add_option('--only-internal', '--only_internal',
Mads Ager418d1ca2017-05-22 09:35:49 +020064 help='Only run Google internal tests.',
65 default=False, action='store_true')
Søren Gjesse77527982018-10-05 12:58:49 +020066 result.add_option('--all-tests', '--all_tests',
Mads Ager418d1ca2017-05-22 09:35:49 +020067 help='Run tests in all configurations.',
68 default=False, action='store_true')
Christoffer Quist Adamsen748e4662019-08-23 14:53:49 +020069 result.add_option('--slow-tests', '--slow_tests',
70 help='Also run slow tests.',
71 default=False, action='store_true')
Mads Ager418d1ca2017-05-22 09:35:49 +020072 result.add_option('-v', '--verbose',
73 help='Print test stdout to, well, stdout.',
74 default=False, action='store_true')
Søren Gjesse77527982018-10-05 12:58:49 +020075 result.add_option('--dex-vm', '--dex_vm',
Mads Ager418d1ca2017-05-22 09:35:49 +020076 help='The android version of the vm to use. "all" will run the tests on '
77 'all art vm versions (stopping after first failed execution)',
78 default="default",
79 choices=ALL_ART_VMS + ["all"])
Søren Gjesse77527982018-10-05 12:58:49 +020080 result.add_option('--dex-vm-kind', '--dex_vm_kind',
Jean-Marie Henaffce162f32017-10-04 10:39:27 +020081 help='Whether to use host or target version of runtime',
82 default="host",
83 nargs=1,
84 choices=["host", "target"])
Søren Gjesse77527982018-10-05 12:58:49 +020085 result.add_option('--one-line-per-test', '--one_line_per_test',
Mads Ager418d1ca2017-05-22 09:35:49 +020086 help='Print a line before a tests starts and after it ends to stdout.',
87 default=False, action='store_true')
88 result.add_option('--tool',
Tamas Kenezcfb2c052018-10-12 11:03:57 +020089 help='Tool to run ART tests with: "r8" (default) or "d8" or "r8cf"'
90 ' (r8 w/CF-backend). Ignored if "--all_tests" enabled.',
91 default=None, choices=["r8", "d8", "r8cf"])
Mads Ager418d1ca2017-05-22 09:35:49 +020092 result.add_option('--jctf',
Tamas Kenezcfb2c052018-10-12 11:03:57 +020093 help='Run JCTF tests with: "r8" (default) or "d8" or "r8cf".',
Mads Ager418d1ca2017-05-22 09:35:49 +020094 default=False, action='store_true')
Søren Gjesse77527982018-10-05 12:58:49 +020095 result.add_option('--only-jctf', '--only_jctf',
Tamas Kenezcfb2c052018-10-12 11:03:57 +020096 help='Run only JCTF tests with: "r8" (default) or "d8" or "r8cf".',
Mads Ager418d1ca2017-05-22 09:35:49 +020097 default=False, action='store_true')
Søren Gjesse77527982018-10-05 12:58:49 +020098 result.add_option('--jctf-compile-only', '--jctf_compile_only',
Mads Ager418d1ca2017-05-22 09:35:49 +020099 help="Don't run, only compile JCTF tests.",
100 default=False, action='store_true')
Søren Gjesse77527982018-10-05 12:58:49 +0200101 result.add_option('--disable-assertions', '--disable_assertions',
Tamas Kenezb77b7d82017-08-17 14:05:16 +0200102 help='Disable assertions when running tests.',
Søren Gjesseaf1c5e22017-06-15 12:24:03 +0200103 default=False, action='store_true')
Søren Gjesse77527982018-10-05 12:58:49 +0200104 result.add_option('--with-code-coverage', '--with_code_coverage',
Tamas Kenezb77b7d82017-08-17 14:05:16 +0200105 help='Enable code coverage with Jacoco.',
Sebastien Hertze2687b62017-07-25 11:16:04 +0200106 default=False, action='store_true')
Søren Gjesse77527982018-10-05 12:58:49 +0200107 result.add_option('--test-dir', '--test_dir',
Tamas Kenezb77b7d82017-08-17 14:05:16 +0200108 help='Use a custom directory for the test artifacts instead of a'
109 ' temporary (which is automatically removed after the test).'
110 ' Note that the directory will not be cleared before the test.')
Søren Gjesse77527982018-10-05 12:58:49 +0200111 result.add_option('--java-home', '--java_home',
Mikaël Peltier5c0a3232017-10-18 09:14:40 +0200112 help='Use a custom java version to run tests.')
Ian Zerny5fffb0a2019-02-11 13:54:22 +0100113 result.add_option('--java-max-memory-size', '--java_max_memory_size',
Rico Wind97b0a992019-08-30 11:09:15 +0200114 help='Set memory for running tests, default 4G',
115 default='4G')
Rico Windba151112020-10-01 08:16:33 +0200116 result.add_option('--test-namespace', '--test_namespace',
117 help='Only run tests in this namespace. The namespace is relative to '
118 'com/android/tools/r8, e.g., desugar/desugaredlibrary',
119 default=None)
Rico Wind8e2f7e42019-02-21 10:13:21 +0100120 result.add_option('--shard-count', '--shard_count',
121 help='We are running this many shards.')
122 result.add_option('--shard-number', '--shard_number',
123 help='We are running this shard.')
Søren Gjesse77527982018-10-05 12:58:49 +0200124 result.add_option('--generate-golden-files-to', '--generate_golden_files_to',
Jean-Marie Henaff7a64eec2018-05-31 15:30:35 +0200125 help='Store dex files produced by tests in the specified directory.'
126 ' It is aimed to be read on platforms with no host runtime available'
127 ' for comparison.')
Søren Gjesse77527982018-10-05 12:58:49 +0200128 result.add_option('--use-golden-files-in', '--use_golden_files_in',
Jean-Marie Henaff7a64eec2018-05-31 15:30:35 +0200129 help='Download golden files hierarchy for this commit in the specified'
130 ' location and use them instead of executing on host runtime.')
Søren Gjesse1956bdb2019-02-19 08:37:52 +0100131 result.add_option('--no-r8lib', '--no_r8lib',
132 default=False, action='store_true',
Morten Krogh-Jespersen2243b162019-01-14 08:40:53 +0100133 help='Run the tests on R8 full with relocated dependencies.')
Søren Gjesse1956bdb2019-02-19 08:37:52 +0100134 result.add_option('--r8lib-no-deps', '--r8lib_no_deps',
135 default=False, action='store_true',
Morten Krogh-Jespersen807b15f2018-12-17 14:24:22 +0100136 help='Run the tests on r8lib without relocated dependencies.')
Ian Zerny24398bc2019-02-22 11:59:18 +0100137 result.add_option('--failed',
138 default=False, action='store_true',
139 help='Run the tests that failed last execution.')
140 result.add_option('--fail-fast', '--fail_fast',
141 default=False, action='store_true',
142 help='Stop on first failure. Passes --fail-fast to gradle test runner.')
Morten Krogh-Jespersen89d005b2019-10-15 13:32:23 +0200143 result.add_option('--worktree',
144 default=False, action='store_true',
145 help='Tests are run in worktree and should not use gradle user home.')
Ian Zernybcbd6c52019-10-29 15:27:04 +0100146 result.add_option('--runtimes',
147 default=None,
148 help='Test parameter runtimes to use, separated by : (eg, none:jdk9).'
149 ' Special values include: all (for all runtimes)'
150 ' and empty (for no runtimes).')
Morten Krogh-Jespersendcb8d452020-07-09 15:07:50 +0200151 result.add_option('--print-hanging-stacks', '--print_hanging_stacks',
152 default=-1, type="int", help='Print hanging stacks after timeout in seconds')
Christoffer Quist Adamsen7bf60342020-11-09 14:00:27 +0100153 result.add_option('--print-full-stacktraces', '--print_full_stacktraces',
154 default=False, action='store_true',
155 help='Print the full stacktraces without any filtering applied')
156 result.add_option(
157 '--print-obfuscated-stacktraces', '--print_obfuscated_stacktraces',
158 default=False, action='store_true',
159 help='Print the obfuscated stacktraces')
Morten Krogh-Jespersen2d3aca62020-11-30 12:48:11 +0100160 result.add_option(
161 '--debug-agent', '--debug_agent',
162 help='Enable Java debug agent and suspend compilation (default disabled)',
163 default=False,
164 action='store_true')
Rico Winde1393092021-03-21 12:57:01 +0100165 result.add_option('--desugared-library-configuration',
166 '--desugared_library-configuration',
Søren Gjesseef195772021-03-11 16:04:42 +0100167 help='Use alternative desugared library configuration.')
Søren Gjesse4a45f9b2021-02-11 14:05:29 +0100168 result.add_option('--desugared-library', '--desugared_library',
169 help='Build and use desugared library from GitHub.')
Rico Windf2f4c292021-04-23 07:06:13 +0200170 result.add_option('--print-times', '--print_times',
171 help='Print the execution time of the slowest tests..',
172 default=False, action='store_true')
Ian Zerny9a0e96a2021-04-28 12:35:49 +0200173 result.add_option(
Ian Zerny27ea4c72021-04-29 22:35:49 +0200174 '--with-testing-state',
175 help='Run/resume tests using testing state.',
176 default=False, action='store_true')
177 result.add_option(
178 '--reset-testing-state',
179 help='Clean the testing state and rerun tests (implies --with-testing-state).',
Ian Zerny9a0e96a2021-04-28 12:35:49 +0200180 default=False, action='store_true')
181 result.add_option(
182 '--stacktrace',
183 help='Pass --stacktrace to the gradle run',
184 default=False, action='store_true')
Mads Ager418d1ca2017-05-22 09:35:49 +0200185 return result.parse_args()
186
Rico Wind06487ac2018-12-10 09:09:19 +0100187def archive_failures():
188 upload_dir = os.path.join(utils.REPO_ROOT, 'build', 'reports', 'tests')
189 u_dir = uuid.uuid4()
190 destination = 'gs://%s/%s' % (BUCKET, u_dir)
191 utils.upload_dir_to_cloud_storage(upload_dir, destination, is_html=True)
Rico Wind70d614f2020-01-31 08:45:21 +0100192 url = 'https://storage.googleapis.com/%s/%s/test/index.html' % (BUCKET, u_dir)
Rico Wind06487ac2018-12-10 09:09:19 +0100193 print 'Test results available at: %s' % url
194 print '@@@STEP_LINK@Test failures@%s@@@' % url
195
Mads Ager418d1ca2017-05-22 09:35:49 +0200196def Main():
197 (options, args) = ParseOptions()
Rico Wind3f82c202019-11-25 07:19:23 +0100198
Rico Windda6836e2018-12-07 12:32:03 +0100199 if utils.is_bot():
Rico Wind22707fc2019-03-15 13:19:57 +0100200 gradle.RunGradle(['--no-daemon', 'clean'])
Sebastien Hertze2687b62017-07-25 11:16:04 +0200201
Søren Gjesseef195772021-03-11 16:04:42 +0100202 desugar_jdk_json_dir = None
203 if options.desugared_library_configuration:
204 if options.desugared_library_configuration != 'jdk11':
205 print("Only value supported for --desugared-library is 'jdk11'")
206 exit(1)
207 desugar_jdk_json_dir = 'src/library_desugar/jdk11'
208
Søren Gjesse4a45f9b2021-02-11 14:05:29 +0100209 desugar_jdk_libs = None
210 if options.desugared_library:
211 if options.desugared_library != 'HEAD':
212 print("Only value supported for --desugared-library is 'HEAD'")
213 exit(1)
214 desugar_jdk_libs_dir = 'build/desugar_jdk_libs'
215 shutil.rmtree(desugar_jdk_libs_dir, ignore_errors=True)
216 os.makedirs(desugar_jdk_libs_dir)
217 print('Building desugared library.')
218 with utils.TempDir() as checkout_dir:
219 archive_desugar_jdk_libs.CloneDesugaredLibrary('google', checkout_dir)
Rico Winde4b43a32021-03-02 09:29:52 +0100220 # Make sure bazel is extracted in third_party.
221 utils.DownloadFromGoogleCloudStorage(utils.BAZEL_SHA_FILE)
222 utils.DownloadFromGoogleCloudStorage(utils.JAVA8_SHA_FILE)
Søren Gjesseef195772021-03-11 16:04:42 +0100223 utils.DownloadFromGoogleCloudStorage(utils.JAVA11_SHA_FILE)
224 (library_jar, maven_zip) = archive_desugar_jdk_libs.BuildDesugaredLibrary(checkout_dir, 'jdk11' if options.desugared_library_configuration == 'jdk11' else 'jdk8')
Søren Gjesse4a45f9b2021-02-11 14:05:29 +0100225 desugar_jdk_libs = os.path.join(desugar_jdk_libs_dir, os.path.basename(library_jar))
226 shutil.copyfile(library_jar, desugar_jdk_libs)
227 print('Desugared library for test in ' + desugar_jdk_libs)
228
Ian Zerny9a0e96a2021-04-28 12:35:49 +0200229 gradle_args = []
230
231 if options.stacktrace or utils.is_bot():
232 gradle_args.append('--stacktrace')
233
Rico Wind22707fc2019-03-15 13:19:57 +0100234 if utils.is_bot():
Rico Wind24777b92020-06-19 22:44:14 +0200235 # Bots don't like dangling processes.
Rico Wind22707fc2019-03-15 13:19:57 +0100236 gradle_args.append('--no-daemon')
237
Sebastien Hertze2687b62017-07-25 11:16:04 +0200238 # Set all necessary Gradle properties and options first.
Rico Wind8e2f7e42019-02-21 10:13:21 +0100239 if options.shard_count:
240 assert options.shard_number
241 gradle_args.append('-Pshard_count=%s' % options.shard_count)
242 gradle_args.append('-Pshard_number=%s' % options.shard_number)
Mads Ager418d1ca2017-05-22 09:35:49 +0200243 if options.verbose:
244 gradle_args.append('-Pprint_test_stdout')
245 if options.no_internal:
246 gradle_args.append('-Pno_internal')
247 if options.only_internal:
248 gradle_args.append('-Ponly_internal')
249 if options.all_tests:
250 gradle_args.append('-Pall_tests')
Christoffer Quist Adamsen748e4662019-08-23 14:53:49 +0200251 if options.slow_tests:
252 gradle_args.append('-Pslow_tests=1')
Mads Ager418d1ca2017-05-22 09:35:49 +0200253 if options.tool:
254 gradle_args.append('-Ptool=%s' % options.tool)
255 if options.one_line_per_test:
256 gradle_args.append('-Pone_line_per_test')
257 if options.jctf:
258 gradle_args.append('-Pjctf')
259 if options.only_jctf:
260 gradle_args.append('-Ponly_jctf')
Rico Windba151112020-10-01 08:16:33 +0200261 if options.test_namespace:
262 gradle_args.append('-Ptest_namespace=%s' % options.test_namespace)
Mads Ager418d1ca2017-05-22 09:35:49 +0200263 if options.jctf_compile_only:
264 gradle_args.append('-Pjctf_compile_only')
Søren Gjesseaf1c5e22017-06-15 12:24:03 +0200265 if options.disable_assertions:
266 gradle_args.append('-Pdisable_assertions')
Sebastien Hertze2687b62017-07-25 11:16:04 +0200267 if options.with_code_coverage:
268 gradle_args.append('-Pwith_code_coverage')
Christoffer Quist Adamsen7bf60342020-11-09 14:00:27 +0100269 if options.print_full_stacktraces:
270 gradle_args.append('-Pprint_full_stacktraces')
271 if options.print_obfuscated_stacktraces:
272 gradle_args.append('-Pprint_obfuscated_stacktraces')
Jean-Marie Henaff7b424e92017-06-15 11:02:56 +0200273 if os.name == 'nt':
274 # temporary hack
275 gradle_args.append('-Pno_internal')
276 gradle_args.append('-x')
277 gradle_args.append('createJctfTests')
278 gradle_args.append('-x')
279 gradle_args.append('jctfCommonJar')
280 gradle_args.append('-x')
281 gradle_args.append('jctfTestsClasses')
Tamas Kenezb77b7d82017-08-17 14:05:16 +0200282 if options.test_dir:
283 gradle_args.append('-Ptest_dir=' + options.test_dir)
284 if not os.path.exists(options.test_dir):
285 os.makedirs(options.test_dir)
Mikaël Peltier5c0a3232017-10-18 09:14:40 +0200286 if options.java_home:
287 gradle_args.append('-Dorg.gradle.java.home=' + options.java_home)
Ian Zerny5fffb0a2019-02-11 13:54:22 +0100288 if options.java_max_memory_size:
Rico Wind97b0a992019-08-30 11:09:15 +0200289 gradle_args.append('-Ptest_xmx=' + options.java_max_memory_size)
Jean-Marie Henaff7a64eec2018-05-31 15:30:35 +0200290 if options.generate_golden_files_to:
291 gradle_args.append('-Pgenerate_golden_files_to=' + options.generate_golden_files_to)
292 if not os.path.exists(options.generate_golden_files_to):
293 os.makedirs(options.generate_golden_files_to)
294 gradle_args.append('-PHEAD_sha1=' + utils.get_HEAD_sha1())
295 if options.use_golden_files_in:
296 gradle_args.append('-Puse_golden_files_in=' + options.use_golden_files_in)
297 if not os.path.exists(options.use_golden_files_in):
298 os.makedirs(options.use_golden_files_in)
299 gradle_args.append('-PHEAD_sha1=' + utils.get_HEAD_sha1())
Morten Krogh-Jespersen432dd912019-01-14 13:26:35 +0100300 if (not options.no_r8lib) and options.r8lib_no_deps:
Morten Krogh-Jespersen2243b162019-01-14 08:40:53 +0100301 print('Cannot run tests on r8lib with and without deps. R8lib is now default target.')
Morten Krogh-Jespersen807b15f2018-12-17 14:24:22 +0100302 exit(1)
Morten Krogh-Jespersen2243b162019-01-14 08:40:53 +0100303 if not options.no_r8lib:
Morten Krogh-Jespersen807b15f2018-12-17 14:24:22 +0100304 gradle_args.append('-Pr8lib')
Morten Krogh-Jespersen54f196e2019-01-14 16:10:08 +0100305 # Force gradle to build a version of r8lib without dependencies for
306 # BootstrapCurrentEqualityTest.
307 gradle_args.append('R8LibNoDeps')
Morten Krogh-Jespersen0d7a96f2021-01-11 17:48:12 +0100308 gradle_args.append('R8Retrace')
Morten Krogh-Jespersen807b15f2018-12-17 14:24:22 +0100309 if options.r8lib_no_deps:
310 gradle_args.append('-Pr8lib_no_deps')
Morten Krogh-Jespersen89d005b2019-10-15 13:32:23 +0200311 if options.worktree:
312 gradle_args.append('-g=' + os.path.join(utils.REPO_ROOT, ".gradle_user_home"))
Ian Zernya97ccc42020-02-10 14:18:07 +0100313 gradle_args.append('--no-daemon')
Morten Krogh-Jespersen2d3aca62020-11-30 12:48:11 +0100314 if options.debug_agent:
315 gradle_args.append('--no-daemon')
Søren Gjesseef195772021-03-11 16:04:42 +0100316 if desugar_jdk_json_dir:
317 gradle_args.append('-Pdesugar_jdk_json_dir=' + desugar_jdk_json_dir)
Søren Gjesse4a45f9b2021-02-11 14:05:29 +0100318 if desugar_jdk_libs:
319 gradle_args.append('-Pdesugar_jdk_libs=' + desugar_jdk_libs)
Ian Zerny27ea4c72021-04-29 22:35:49 +0200320 if options.reset_testing_state:
321 gradle_args.append('-Ptesting-state')
322 gradle_args.append('-Preset-testing-state')
323 elif options.with_testing_state:
324 gradle_args.append('-Ptesting-state')
Morten Krogh-Jespersen807b15f2018-12-17 14:24:22 +0100325
Morten Krogh-Jespersen54f196e2019-01-14 16:10:08 +0100326 # Build an R8 with dependencies for bootstrapping tests before adding test sources.
Morten Krogh-Jespersen47393d92020-05-01 12:39:38 +0200327 gradle_args.append('r8WithDeps')
328 gradle_args.append('r8WithDeps11')
Morten Krogh-Jespersen54f196e2019-01-14 16:10:08 +0100329 gradle_args.append('r8WithRelocatedDeps')
clementbera55e84822019-06-06 16:08:11 +0200330 gradle_args.append('r8WithRelocatedDeps11')
Morten Krogh-Jespersen54f196e2019-01-14 16:10:08 +0100331
Sebastien Hertze2687b62017-07-25 11:16:04 +0200332 # Add Gradle tasks
333 gradle_args.append('cleanTest')
334 gradle_args.append('test')
Morten Krogh-Jespersen2d3aca62020-11-30 12:48:11 +0100335 if options.debug_agent:
336 gradle_args.append('--debug-jvm')
Ian Zerny24398bc2019-02-22 11:59:18 +0100337 if options.fail_fast:
338 gradle_args.append('--fail-fast')
339 if options.failed:
340 args = compute_failed_tests(args)
341 if args is None:
342 return 1
343 if len(args) == 0:
344 print "No failing tests"
345 return 0
Sebastien Hertz0f4e7fb2017-10-02 11:33:45 +0200346 # Test filtering. Must always follow the 'test' task.
347 for testFilter in args:
Sebastien Hertze2687b62017-07-25 11:16:04 +0200348 gradle_args.append('--tests')
Sebastien Hertz0f4e7fb2017-10-02 11:33:45 +0200349 gradle_args.append(testFilter)
Sebastien Hertze2687b62017-07-25 11:16:04 +0200350 if options.with_code_coverage:
351 # Create Jacoco report after tests.
352 gradle_args.append('jacocoTestReport')
353
Jean-Marie Henaff7a64eec2018-05-31 15:30:35 +0200354 if options.use_golden_files_in:
355 sha1 = '%s' % utils.get_HEAD_sha1()
356 with utils.ChangedWorkingDirectory(options.use_golden_files_in):
357 utils.download_file_from_cloud_storage(
358 'gs://r8-test-results/golden-files/%s.tar.gz' % sha1,
359 '%s.tar.gz' % sha1)
360 utils.unpack_archive('%s.tar.gz' % sha1)
361
Morten Krogh-Jespersendcb8d452020-07-09 15:07:50 +0200362 print_stacks_timeout = options.print_hanging_stacks
363 if (utils.is_bot() and not utils.IsWindows()) or print_stacks_timeout > -1:
Rico Windda6836e2018-12-07 12:32:03 +0100364 timestamp_file = os.path.join(utils.BUILD, 'last_test_time')
365 if os.path.exists(timestamp_file):
366 os.remove(timestamp_file)
367 gradle_args.append('-Pupdate_test_timestamp=' + timestamp_file)
Morten Krogh-Jespersendcb8d452020-07-09 15:07:50 +0200368 print_stacks_timeout = (print_stacks_timeout
369 if print_stacks_timeout != -1
370 else TIMEOUT_HANDLER_PERIOD)
371 thread.start_new_thread(timeout_handler, (timestamp_file, print_stacks_timeout,))
Ian Zerny24398bc2019-02-22 11:59:18 +0100372 rotate_test_reports()
373
Ian Zerny324d7612019-03-20 10:52:28 +0100374 if options.only_jctf:
375 # Note: not setting -Pruntimes will run with all available runtimes.
376 return_code = gradle.RunGradle(gradle_args, throw_on_failure=False)
Ian Zerny17561362019-05-27 15:16:26 +0200377 return archive_and_return(return_code, options)
Ian Zerny324d7612019-03-20 10:52:28 +0100378
Sebastien Hertze2687b62017-07-25 11:16:04 +0200379 # Now run tests on selected runtime(s).
Ian Zernybcbd6c52019-10-29 15:27:04 +0100380 if options.runtimes:
381 if options.dex_vm != 'default':
382 print 'Unexpected runtimes and dex_vm argument: ' + options.dex_vm
383 sys.exit(1)
384 if options.runtimes == 'empty':
385 # Set runtimes with no content will configure no runtimes.
386 gradle_args.append('-Pruntimes=')
387 elif options.runtimes == 'all':
388 # An unset runtimes will configure all runtimes
389 pass
390 else:
391 prefixes = [prefix.strip() for prefix in options.runtimes.split(':')]
392 runtimes = []
393 for prefix in prefixes:
394 matches = [ rt for rt in VALID_RUNTIMES if rt.startswith(prefix) ]
395 if len(matches) == 0:
396 print "Invalid runtime prefix '%s'." % prefix
397 print "Must be just 'all', 'empty'," \
398 " or a prefix of %s" % ', '.join(VALID_RUNTIMES)
399 sys.exit(1)
400 runtimes.extend(matches)
401 gradle_args.append('-Pruntimes=%s' % ':'.join(runtimes))
402
403 return_code = gradle.RunGradle(gradle_args, throw_on_failure=False)
404 return archive_and_return(return_code, options)
405
406 # Legacy testing populates the runtimes based on dex_vm.
Mads Ager418d1ca2017-05-22 09:35:49 +0200407 vms_to_test = [options.dex_vm] if options.dex_vm != "all" else ALL_ART_VMS
Rico Wind124b2712019-02-28 13:49:25 +0100408
Rico Windf2f4c292021-04-23 07:06:13 +0200409 if options.print_times:
410 gradle_args.append('-Pprint_times=true')
Mads Ager418d1ca2017-05-22 09:35:49 +0200411 for art_vm in vms_to_test:
Ian Zerny4dfd5a52019-03-12 07:56:11 +0100412 vm_suffix = "_" + options.dex_vm_kind if art_vm != "default" else ""
Ian Zerny324d7612019-03-20 10:52:28 +0100413 runtimes = ['dex-' + art_vm]
Ian Zerny019e7782021-04-20 15:34:01 +0200414 # Append the "none" runtime and default JVM if running the "default" DEX VM.
Ian Zerny324d7612019-03-20 10:52:28 +0100415 if art_vm == "default":
Morten Krogh-Jespersen69358942020-10-09 11:07:09 +0200416 runtimes.extend(['jdk11', 'none'])
Ian Zerny4dfd5a52019-03-12 07:56:11 +0100417 return_code = gradle.RunGradle(
418 gradle_args + [
419 '-Pdex_vm=%s' % art_vm + vm_suffix,
Ian Zerny324d7612019-03-20 10:52:28 +0100420 '-Pruntimes=%s' % ':'.join(runtimes),
421 ],
Ian Zerny4dfd5a52019-03-12 07:56:11 +0100422 throw_on_failure=False)
Jean-Marie Henaff7a64eec2018-05-31 15:30:35 +0200423 if options.generate_golden_files_to:
424 sha1 = '%s' % utils.get_HEAD_sha1()
425 with utils.ChangedWorkingDirectory(options.generate_golden_files_to):
426 archive = utils.create_archive(sha1)
427 utils.upload_file_to_cloud_storage(archive,
428 'gs://r8-test-results/golden-files/' + archive)
429
Rico Winda94f01c2017-06-27 10:32:34 +0200430 if return_code != 0:
Ian Zerny17561362019-05-27 15:16:26 +0200431 return archive_and_return(return_code, options)
Mads Ager418d1ca2017-05-22 09:35:49 +0200432
Jinseong Jeon9749d172017-09-19 00:25:01 -0700433 return 0
434
Rico Windda6836e2018-12-07 12:32:03 +0100435
Ian Zerny17561362019-05-27 15:16:26 +0200436def archive_and_return(return_code, options):
Rico Windc58a20e2019-05-23 09:43:19 +0200437 if return_code != 0:
438 if options.archive_failures and os.name != 'nt':
439 archive_failures()
440 return return_code
441
Rico Windda6836e2018-12-07 12:32:03 +0100442def print_jstacks():
443 processes = subprocess.check_output(['ps', 'aux'])
444 for l in processes.splitlines():
445 if 'java' in l and 'openjdk' in l:
446 # Example line:
447 # ricow 184313 2.6 0.0 36839068 31808 ? Sl 09:53 0:00 /us..
448 columns = l.split()
449 pid = columns[1]
450 return_value = subprocess.call(['jstack', pid])
451 if return_value:
452 print('Could not jstack %s' % l)
Rico Windda6836e2018-12-07 12:32:03 +0100453
454def get_time_from_file(timestamp_file):
455 if os.path.exists(timestamp_file):
456 timestamp = os.stat(timestamp_file).st_mtime
457 print('TIMEOUT HANDLER timestamp: %s' % (timestamp))
Rico Windda6836e2018-12-07 12:32:03 +0100458 sys.stdout.flush()
459 return timestamp
460 else:
461 print('TIMEOUT HANDLER no timestamp file yet')
Rico Windda6836e2018-12-07 12:32:03 +0100462 sys.stdout.flush()
463 return None
464
Morten Krogh-Jespersendcb8d452020-07-09 15:07:50 +0200465def timeout_handler(timestamp_file, timeout_handler_period):
Rico Windda6836e2018-12-07 12:32:03 +0100466 last_timestamp = None
467 while True:
Morten Krogh-Jespersendcb8d452020-07-09 15:07:50 +0200468 time.sleep(timeout_handler_period)
Rico Windda6836e2018-12-07 12:32:03 +0100469 new_timestamp = get_time_from_file(timestamp_file)
470 if last_timestamp and new_timestamp == last_timestamp:
471 print_jstacks()
472 last_timestamp = new_timestamp
473
Ian Zerny24398bc2019-02-22 11:59:18 +0100474def report_dir_path(index):
475 if index is 0:
476 return REPORTS_PATH
477 return '%s%d' % (REPORTS_PATH, index)
478
479def report_index_path(index):
480 return os.path.join(report_dir_path(index), *REPORT_INDEX)
481
482# Rotate test results so previous results are still accessible.
483def rotate_test_reports():
484 if not os.path.exists(report_dir_path(0)):
485 return
486 i = 1
487 while i < NUMBER_OF_TEST_REPORTS and os.path.exists(report_dir_path(i)):
488 i += 1
489 if i == NUMBER_OF_TEST_REPORTS and os.path.exists(report_dir_path(i)):
490 shutil.rmtree(report_dir_path(i))
491 while i > 0:
492 shutil.move(report_dir_path(i - 1), report_dir_path(i))
493 i -= 1
494
495def compute_failed_tests(args):
496 if len(args) > 1:
497 print "Running with --failed can take an optional path to a report index (or report number)."
498 return None
499 report = report_index_path(0)
500 # If the default report does not exist, fall back to the previous report as it may be a failed
501 # gradle run which has already moved the report to report1, but did not produce a new report.
502 if not os.path.exists(report):
503 report1 = report_index_path(1)
504 if os.path.exists(report1):
505 report = report1
506 if len(args) == 1:
507 try:
508 # try to parse the arg as a report index.
509 index = int(args[0])
510 report = report_index_path(index)
511 except ValueError:
512 # if integer parsing failed assume it is a report file path.
513 report = args[0]
514 if not os.path.exists(report):
515 print "Can't re-run failing, no report at:", report
516 return None
517 print "Reading failed tests in", report
518 failing = set()
519 inFailedSection = False
520 for line in file(report):
521 l = line.strip()
522 if l == "<h2>Failed tests</h2>":
523 inFailedSection = True
524 elif l.startswith("<h2>"):
525 inFailedSection = False
526 prefix = '<a href="classes/'
527 if inFailedSection and l.startswith(prefix):
528 href = l[len(prefix):l.index('">')]
529 # Ignore enties ending with .html which are test classes, not test methods.
530 if not href.endswith('.html'):
531 # Remove the .html and anchor separateor, also, a classMethod test is the static
532 # setup failing so rerun the full class of tests.
533 test = href.replace('.html','').replace('#', '.').replace('.classMethod', '')
534 failing.add(test)
535 return list(failing)
Mads Ager418d1ca2017-05-22 09:35:49 +0200536if __name__ == '__main__':
Stephan Herhutd24b1b72017-08-24 15:09:36 +0200537 return_code = Main()
538 if return_code != 0:
539 notify.notify("Tests failed.")
540 else:
541 notify.notify("Tests passed.")
542 sys.exit(return_code)