1#!/usr/bin/env vpython3 2# Copyright 2020 The Chromium Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5"""Runs an isolated non-Telemetry ANGLE test. 6 7The main contract is that the caller passes the arguments: 8 9 --isolated-script-test-output=[FILENAME] 10json is written to that file in the format: 11https://chromium.googlesource.com/chromium/src/+/main/docs/testing/json_test_results_format.md 12 13Optional argument: 14 15 --isolated-script-test-filter=[TEST_NAMES] 16 17is a double-colon-separated ("::") list of test names, to run just that subset 18of tests. This list is parsed by this harness and sent down via the 19--gtest_filter argument. 20 21This script is intended to be the base command invoked by the isolate, 22followed by a subsequent non-python executable. For a similar script see 23run_performance_test.py. 24""" 25 26import argparse 27import json 28import os 29import shutil 30import sys 31import tempfile 32import traceback 33 34 35def _AddToPathIfNeeded(path): 36 if path not in sys.path: 37 sys.path.insert(0, path) 38 39 40_AddToPathIfNeeded( 41 os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'src', 'tests', 'py_utils'))) 42import angle_path_util 43 44angle_path_util.AddDepsDirToPath('testing/scripts') 45import common 46import xvfb 47import test_env 48 49# Unfortunately we need to copy these variables from 50# //src/testing/scripts/test_env.py. Importing it and using its 51# get_sandbox_env breaks test runs on Linux (it seems to unset DISPLAY). 52CHROME_SANDBOX_ENV = 'CHROME_DEVEL_SANDBOX' 53CHROME_SANDBOX_PATH = '/opt/chromium/chrome_sandbox' 54 55 56def IsWindows(): 57 return sys.platform == 'cygwin' or sys.platform.startswith('win') 58 59 60def main(): 61 parser = argparse.ArgumentParser() 62 parser.add_argument('executable', help='Test executable.') 63 parser.add_argument('--isolated-script-test-output', type=str) 64 parser.add_argument('--isolated-script-test-filter', type=str) 65 parser.add_argument('--xvfb', help='Start xvfb.', action='store_true') 66 67 # Kept for compatiblity. 68 # TODO(jmadill): Remove when removed from the recipes. http://crbug.com/954415 69 parser.add_argument('--isolated-script-test-perf-output', type=str) 70 71 args, extra_flags = parser.parse_known_args() 72 73 env = os.environ.copy() 74 75 if 'GTEST_TOTAL_SHARDS' in env: 76 extra_flags += ['--shard-count=' + env['GTEST_TOTAL_SHARDS']] 77 env.pop('GTEST_TOTAL_SHARDS') 78 if 'GTEST_SHARD_INDEX' in env: 79 extra_flags += ['--shard-index=' + env['GTEST_SHARD_INDEX']] 80 env.pop('GTEST_SHARD_INDEX') 81 if 'ISOLATED_OUTDIR' in env: 82 extra_flags += ['--isolated-outdir=' + env['ISOLATED_OUTDIR']] 83 env.pop('ISOLATED_OUTDIR') 84 85 # Assume we want to set up the sandbox environment variables all the 86 # time; doing so is harmless on non-Linux platforms and is needed 87 # all the time on Linux. 88 env[CHROME_SANDBOX_ENV] = CHROME_SANDBOX_PATH 89 90 rc = 0 91 try: 92 # Consider adding stdio control flags. 93 if args.isolated_script_test_output: 94 extra_flags.append('--isolated-script-test-output=%s' % 95 args.isolated_script_test_output) 96 97 if args.isolated_script_test_filter: 98 filter_list = common.extract_filter_list(args.isolated_script_test_filter) 99 extra_flags.append('--gtest_filter=' + ':'.join(filter_list)) 100 101 if IsWindows(): 102 args.executable = '.\\%s.exe' % args.executable 103 else: 104 args.executable = './%s' % args.executable 105 with common.temporary_file() as tempfile_path: 106 env['CHROME_HEADLESS'] = '1' 107 cmd = [args.executable] + extra_flags 108 109 if args.xvfb: 110 rc = xvfb.run_executable(cmd, env, stdoutfile=tempfile_path) 111 else: 112 rc = test_env.run_command_with_output(cmd, env=env, stdoutfile=tempfile_path) 113 114 except Exception: 115 traceback.print_exc() 116 rc = 1 117 118 return rc 119 120 121# This is not really a "script test" so does not need to manually add 122# any additional compile targets. 123def main_compile_targets(args): 124 json.dump([], args.output) 125 126 127if __name__ == '__main__': 128 # Conform minimally to the protocol defined by ScriptTest. 129 if 'compile_targets' in sys.argv: 130 funcs = { 131 'run': None, 132 'compile_targets': main_compile_targets, 133 } 134 sys.exit(common.run_script(sys.argv[1:], funcs)) 135 sys.exit(main()) 136