Fix some lint errors in url_request_context_adapter
[chromium-blink-merge.git] / testing / test_env.py
blob2c39508c438d2e9074c10aad29f4c845c380464b
1 #!/usr/bin/env python
2 # Copyright (c) 2012 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.
6 """Sets environment variables needed to run a chromium unit test."""
8 import collections
9 import os
10 import stat
11 import subprocess
12 import sys
14 # This is hardcoded to be src/ relative to this script.
15 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17 CHROME_SANDBOX_ENV = 'CHROME_DEVEL_SANDBOX'
18 CHROME_SANDBOX_PATH = '/opt/chromium/chrome_sandbox'
21 def should_enable_sandbox(cmd, sandbox_path):
22 """Return a boolean indicating that the current slave is capable of using the
23 sandbox and should enable it. This should return True iff the slave is a
24 Linux host with the sandbox file present and configured correctly."""
25 if not (sys.platform.startswith('linux') and
26 os.path.exists(sandbox_path)):
27 return False
29 # Copy the check in tools/build/scripts/slave/runtest.py.
30 if '--lsan=1' in cmd:
31 return False
33 sandbox_stat = os.stat(sandbox_path)
34 if ((sandbox_stat.st_mode & stat.S_ISUID) and
35 (sandbox_stat.st_mode & stat.S_IRUSR) and
36 (sandbox_stat.st_mode & stat.S_IXUSR) and
37 (sandbox_stat.st_uid == 0)):
38 return True
39 return False
42 def enable_sandbox_if_required(cmd, env, verbose=False):
43 """Checks enables the sandbox if it is required, otherwise it disables it."""
44 chrome_sandbox_path = env.get(CHROME_SANDBOX_ENV, CHROME_SANDBOX_PATH)
46 if should_enable_sandbox(cmd, chrome_sandbox_path):
47 if verbose:
48 print 'Enabling sandbox. Setting environment variable:'
49 print ' %s="%s"' % (CHROME_SANDBOX_ENV, chrome_sandbox_path)
50 env[CHROME_SANDBOX_ENV] = chrome_sandbox_path
51 else:
52 if verbose:
53 print 'Disabling sandbox. Setting environment variable:'
54 print ' CHROME_DEVEL_SANDBOX=""'
55 env['CHROME_DEVEL_SANDBOX'] = ''
58 def fix_python_path(cmd):
59 """Returns the fixed command line to call the right python executable."""
60 out = cmd[:]
61 if out[0] == 'python':
62 out[0] = sys.executable
63 elif out[0].endswith('.py'):
64 out.insert(0, sys.executable)
65 return out
68 def run_executable(cmd, env):
69 """Runs an executable with:
70 - environment variable CR_SOURCE_ROOT set to the root directory.
71 - environment variable LANGUAGE to en_US.UTF-8.
72 - environment variable CHROME_DEVEL_SANDBOX set if need
73 - Reuses sys.executable automatically.
74 """
75 env = collections.defaultdict(str, env)
76 # Many tests assume a English interface...
77 env['LANG'] = 'en_US.UTF-8'
78 # Used by base/base_paths_linux.cc as an override. Just make sure the default
79 # logic is used.
80 env.pop('CR_SOURCE_ROOT', None)
81 enable_sandbox_if_required(cmd, env)
83 # Copy logic from tools/build/scripts/slave/runtest.py.
84 asan = '--asan=1' in cmd
85 lsan = '--lsan=1' in cmd
86 if lsan and sys.platform == 'linux2':
87 # Use the debug version of libstdc++ under LSan. If we don't, there will
88 # be a lot of incomplete stack traces in the reports.
89 env['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu/debug:'
91 if asan and sys.platform == 'darwin':
92 isolate_output_dir = os.path.abspath(os.path.dirname(cmd[0]))
93 # This is needed because the test binary has @executable_path embedded in it
94 # that the OS tries to resolve to the cache directory and not the mapped
95 # directory.
96 env['DYLD_LIBRARY_PATH'] = str(isolate_output_dir)
98 # Ensure paths are correctly separated on windows.
99 cmd[0] = cmd[0].replace('/', os.path.sep)
100 cmd = fix_python_path(cmd)
101 try:
102 if asan:
103 # Need to pipe to the symbolizer script.
104 p1 = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE,
105 stderr=sys.stdout)
106 p2 = subprocess.Popen(["../tools/valgrind/asan/asan_symbolize.py"],
107 stdin=p1.stdout)
108 p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
109 p2.wait()
110 return p2.returncode
111 else:
112 return subprocess.call(cmd, env=env)
113 except OSError:
114 print >> sys.stderr, 'Failed to start %s' % cmd
115 raise
118 def main():
119 return run_executable(sys.argv[1:], os.environ.copy())
122 if __name__ == '__main__':
123 sys.exit(main())