test.py revision 4f3ca9071c95d5da11676e118e54f8871cbefa1e
1#!/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
10import os
11import gradle
12import optparse
13import subprocess
14import sys
15import utils
16import uuid
17
18ALL_ART_VMS = ["default", "7.0.0", "6.0.1", "5.1.1"]
19BUCKET = 'r8-test-results'
20
21def ParseOptions():
22  result = optparse.OptionParser()
23  result.add_option('--no_internal',
24      help='Do not run Google internal tests.',
25      default=False, action='store_true')
26  result.add_option('--archive_failures',
27      help='Upload test results to cloud storage on failure.',
28      default=False, action='store_true')
29  result.add_option('--only_internal',
30      help='Only run Google internal tests.',
31      default=False, action='store_true')
32  result.add_option('--all_tests',
33      help='Run tests in all configurations.',
34      default=False, action='store_true')
35  result.add_option('-v', '--verbose',
36      help='Print test stdout to, well, stdout.',
37      default=False, action='store_true')
38  result.add_option('--dex_vm',
39      help='The android version of the vm to use. "all" will run the tests on '
40           'all art vm versions (stopping after first failed execution)',
41      default="default",
42      choices=ALL_ART_VMS + ["all"])
43  result.add_option('--one_line_per_test',
44      help='Print a line before a tests starts and after it ends to stdout.',
45      default=False, action='store_true')
46  result.add_option('--tool',
47      help='Tool to run ART tests with: "r8" (default) or "d8". Ignored if "--all_tests" enabled.',
48      default=None, choices=["r8", "d8"])
49  result.add_option('--jctf',
50      help='Run JCTF tests with: "r8" (default) or "d8".',
51      default=False, action='store_true')
52  result.add_option('--only_jctf',
53      help='Run only JCTF tests with: "r8" (default) or "d8".',
54      default=False, action='store_true')
55  result.add_option('--jctf_compile_only',
56      help="Don't run, only compile JCTF tests.",
57      default=False, action='store_true')
58  result.add_option('--disable_assertions',
59      help="Disable assertions when running tests.",
60      default=False, action='store_true')
61
62  return result.parse_args()
63
64def archive_failures():
65  upload_dir = os.path.join(utils.REPO_ROOT, 'build', 'reports', 'tests')
66  u_dir = uuid.uuid4()
67  destination = 'gs://%s/%s' % (BUCKET, u_dir)
68  utils.upload_html_to_cloud_storage(upload_dir, destination)
69  url = 'http://storage.googleapis.com/%s/%s/index.html' % (BUCKET, u_dir)
70  print 'Test results available at: %s' % url
71
72def run_bot_debugging():
73  subprocess.check_call(['gsutil.py', 'cp', 'gs://r8-test-results/hello.dex', '/tmp/hello.dex'])
74  subprocess.check_call(['bash', 'tools/linux/art/bin/art', '-cp', '/tmp/hello.dex', 'hello.Hello'])
75  subprocess.check_call(['cat', '/proc/meminfo'])
76  subprocess.check_call(['ps', 'aux'])
77  print 'ulimit -s'
78  subprocess.check_call('ulimit -s', shell=True)
79
80def Main():
81  user = os.environ.get('USER', 'foobar')
82  bot_name = os.environ.get('BUILDBOT_BUILDERNAME')
83  if user == 'chrome-bot' and (bot_name == 'linux-jctf' or bot_name == 'd8-linux-jctf'):
84    print 'Running temporarily disabled for %s' % user
85    run_bot_debugging()
86    return 0
87  (options, args) = ParseOptions()
88  gradle_args = ['cleanTest', 'test']
89  if len(args) > 1:
90    print("test.py takes at most one argument, the pattern for tests to run")
91    return -1
92  if options.verbose:
93    gradle_args.append('-Pprint_test_stdout')
94  if options.no_internal:
95    gradle_args.append('-Pno_internal')
96  if options.only_internal:
97    gradle_args.append('-Ponly_internal')
98  if options.all_tests:
99    gradle_args.append('-Pall_tests')
100  if options.tool:
101    gradle_args.append('-Ptool=%s' % options.tool)
102  if options.one_line_per_test:
103    gradle_args.append('-Pone_line_per_test')
104  if options.jctf:
105    gradle_args.append('-Pjctf')
106  if options.only_jctf:
107    gradle_args.append('-Ponly_jctf')
108  if options.jctf_compile_only:
109    gradle_args.append('-Pjctf_compile_only')
110  if options.disable_assertions:
111    gradle_args.append('-Pdisable_assertions')
112  if len(args) > 0:
113    gradle_args.append('--tests')
114    gradle_args.append(args[0])
115  if os.name == 'nt':
116    # temporary hack
117    gradle_args.append('-Pno_internal')
118    gradle_args.append('-x')
119    gradle_args.append('createJctfTests')
120    gradle_args.append('-x')
121    gradle_args.append('jctfCommonJar')
122    gradle_args.append('-x')
123    gradle_args.append('jctfTestsClasses')
124  vms_to_test = [options.dex_vm] if options.dex_vm != "all" else ALL_ART_VMS
125  for art_vm in vms_to_test:
126    return_code = gradle.RunGradle(gradle_args + ['-Pdex_vm=%s' % art_vm],
127                                   throw_on_failure=False)
128    if return_code != 0:
129      if options.archive_failures:
130        archive_failures()
131      return return_code
132
133if __name__ == '__main__':
134  sys.exit(Main())
135