Mechanical rename of tracing includes for /chrome
[chromium-blink-merge.git] / testing / test_env.py
blob5f94326a9d198edd2cadc49e74e996931732f201
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 os
9 import stat
10 import subprocess
11 import sys
13 # This is hardcoded to be src/ relative to this script.
14 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
16 CHROME_SANDBOX_ENV = 'CHROME_DEVEL_SANDBOX'
17 CHROME_SANDBOX_PATH = '/opt/chromium/chrome_sandbox'
20 def get_sandbox_env(env):
21 """Returns the environment flags needed for the SUID sandbox to work."""
22 extra_env = {}
23 chrome_sandbox_path = env.get(CHROME_SANDBOX_ENV, CHROME_SANDBOX_PATH)
24 # The above would silently disable the SUID sandbox if the env value were
25 # an empty string. We don't want to allow that. http://crbug.com/245376
26 # TODO(jln): Remove this check once it's no longer possible to disable the
27 # sandbox that way.
28 if not chrome_sandbox_path:
29 chrome_sandbox_path = CHROME_SANDBOX_PATH
30 extra_env[CHROME_SANDBOX_ENV] = chrome_sandbox_path
32 return extra_env
35 def trim_cmd(cmd):
36 """Removes internal flags from cmd since they're just used to communicate from
37 the host machine to this script running on the swarm slaves."""
38 internal_flags = frozenset(['--asan=0', '--asan=1', '--lsan=0', '--lsan=1'])
39 return [i for i in cmd if i not in internal_flags]
42 def fix_python_path(cmd):
43 """Returns the fixed command line to call the right python executable."""
44 out = cmd[:]
45 if out[0] == 'python':
46 out[0] = sys.executable
47 elif out[0].endswith('.py'):
48 out.insert(0, sys.executable)
49 return out
52 def get_asan_env(cmd, lsan):
53 """Returns the envirnoment flags needed for ASan and LSan."""
55 extra_env = {}
57 # Instruct GTK to use malloc while running ASan or LSan tests.
58 extra_env['G_SLICE'] = 'always-malloc'
60 extra_env['NSS_DISABLE_ARENA_FREE_LIST'] = '1'
61 extra_env['NSS_DISABLE_UNLOAD'] = '1'
63 # TODO(glider): remove the symbolizer path once
64 # https://code.google.com/p/address-sanitizer/issues/detail?id=134 is fixed.
65 symbolizer_path = os.path.abspath(os.path.join(ROOT_DIR, 'third_party',
66 'llvm-build', 'Release+Asserts', 'bin', 'llvm-symbolizer'))
68 asan_options = []
69 if lsan:
70 asan_options.append('detect_leaks=1')
71 if sys.platform == 'linux2':
72 # Use the debug version of libstdc++ under LSan. If we don't, there will
73 # be a lot of incomplete stack traces in the reports.
74 extra_env['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu/debug:'
76 # LSan is not sandbox-compatible, so we can use online symbolization. In
77 # fact, it needs symbolization to be able to apply suppressions.
78 symbolization_options = ['symbolize=1',
79 'external_symbolizer_path=%s' % symbolizer_path]
81 suppressions_file = os.path.join(ROOT_DIR, 'tools', 'lsan',
82 'suppressions.txt')
83 lsan_options = ['suppressions=%s' % suppressions_file,
84 'print_suppressions=1']
85 extra_env['LSAN_OPTIONS'] = ' '.join(lsan_options)
86 else:
87 # ASan uses a script for offline symbolization.
88 # Important note: when running ASan with leak detection enabled, we must use
89 # the LSan symbolization options above.
90 symbolization_options = ['symbolize=0']
91 # Set the path to llvm-symbolizer to be used by asan_symbolize.py
92 extra_env['LLVM_SYMBOLIZER_PATH'] = symbolizer_path
94 asan_options.extend(symbolization_options)
96 extra_env['ASAN_OPTIONS'] = ' '.join(asan_options)
98 if sys.platform == 'darwin':
99 isolate_output_dir = os.path.abspath(os.path.dirname(cmd[0]))
100 # This is needed because the test binary has @executable_path embedded in it
101 # it that the OS tries to resolve to the cache directory and not the mapped
102 # directory.
103 extra_env['DYLD_LIBRARY_PATH'] = str(isolate_output_dir)
105 return extra_env
108 def run_executable(cmd, env):
109 """Runs an executable with:
110 - environment variable CR_SOURCE_ROOT set to the root directory.
111 - environment variable LANGUAGE to en_US.UTF-8.
112 - environment variable CHROME_DEVEL_SANDBOX set
113 - Reuses sys.executable automatically.
115 extra_env = {}
116 # Many tests assume a English interface...
117 extra_env['LANG'] = 'en_US.UTF-8'
118 # Used by base/base_paths_linux.cc as an override. Just make sure the default
119 # logic is used.
120 env.pop('CR_SOURCE_ROOT', None)
121 extra_env.update(get_sandbox_env(env))
123 # Copy logic from tools/build/scripts/slave/runtest.py.
124 asan = '--asan=1' in cmd
125 lsan = '--lsan=1' in cmd
127 if asan:
128 extra_env.update(get_asan_env(cmd, lsan))
129 # ASan is not yet sandbox-friendly on Windows (http://crbug.com/382867).
130 if sys.platform == 'win32':
131 cmd.append('--no-sandbox')
132 if lsan:
133 cmd.append('--no-sandbox')
135 cmd = trim_cmd(cmd)
137 # Ensure paths are correctly separated on windows.
138 cmd[0] = cmd[0].replace('/', os.path.sep)
139 cmd = fix_python_path(cmd)
141 print('Additional test environment:\n%s\n'
142 'Command: %s\n' % (
143 '\n'.join(' %s=%s' %
144 (k, v) for k, v in sorted(extra_env.iteritems())),
145 ' '.join(cmd)))
146 env.update(extra_env or {})
147 try:
148 # See above comment regarding offline symbolization.
149 if asan and not lsan:
150 # Need to pipe to the symbolizer script.
151 p1 = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE,
152 stderr=sys.stdout)
153 p2 = subprocess.Popen([sys.executable,
154 "../tools/valgrind/asan/asan_symbolize.py"],
155 env=env, stdin=p1.stdout)
156 p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
157 p1.wait()
158 p2.wait()
159 return p1.returncode
160 else:
161 return subprocess.call(cmd, env=env)
162 except OSError:
163 print >> sys.stderr, 'Failed to start %s' % cmd
164 raise
167 def main():
168 return run_executable(sys.argv[1:], os.environ.copy())
171 if __name__ == '__main__':
172 sys.exit(main())