Update configs. IGNORE BROKEN CHANGESETS CLOSED TREE NO BUG a=release ba=release
[gecko.git] / testing / gtest / rungtests.py
blob212a562abb8d45a710caf794ba7084958adb62a2
1 #!/usr/bin/env python
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 import argparse
8 import os
9 import sys
11 import mozcrash
12 import mozinfo
13 import mozlog
14 import mozprocess
15 from mozrunner.utils import get_stack_fixer_function
17 log = mozlog.unstructured.getLogger("gtest")
20 class GTests(object):
21 # Time (seconds) to wait for test process to complete
22 TEST_PROC_TIMEOUT = 2400
23 # Time (seconds) in which process will be killed if it produces no output.
24 TEST_PROC_NO_OUTPUT_TIMEOUT = 300
26 def run_gtest(
27 self,
28 prog,
29 xre_path,
30 cwd,
31 symbols_path=None,
32 utility_path=None,
34 """
35 Run a single C++ unit test program.
37 Arguments:
38 * prog: The path to the test program to run.
39 * env: The environment to use for running the program.
40 * cwd: The directory to run tests from (support files will be found
41 in this direcotry).
42 * symbols_path: A path to a directory containing Breakpad-formatted
43 symbol files for producing stack traces on crash.
44 * utility_path: A path to a directory containing utility programs.
45 currently used to locate a stack fixer to provide
46 symbols symbols for assertion stacks.
48 Return True if the program exits with a zero status, False otherwise.
49 """
50 self.xre_path = xre_path
51 env = self.build_environment()
52 log.info("Running gtest")
54 if cwd and not os.path.isdir(cwd):
55 os.makedirs(cwd)
57 stack_fixer = None
58 if utility_path:
59 stack_fixer = get_stack_fixer_function(utility_path, symbols_path)
61 GTests.run_gtest.timed_out = False
63 def output_line_handler(proc, line):
64 if stack_fixer:
65 print(stack_fixer(line))
66 else:
67 print(line)
69 def proc_timeout_handler(proc):
70 GTests.run_gtest.timed_out = True
71 log.testFail("gtest | timed out after %d seconds", GTests.TEST_PROC_TIMEOUT)
72 mozcrash.kill_and_get_minidump(proc.pid, cwd, utility_path)
74 def output_timeout_handler(proc):
75 GTests.run_gtest.timed_out = True
76 log.testFail(
77 "gtest | timed out after %d seconds without output",
78 GTests.TEST_PROC_NO_OUTPUT_TIMEOUT,
80 mozcrash.kill_and_get_minidump(proc.pid, cwd, utility_path)
82 proc = mozprocess.run_and_wait(
83 [prog, "-unittest", "--gtest_death_test_style=threadsafe"],
84 cwd=cwd,
85 env=env,
86 output_line_handler=output_line_handler,
87 timeout=GTests.TEST_PROC_TIMEOUT,
88 timeout_handler=proc_timeout_handler,
89 output_timeout=GTests.TEST_PROC_NO_OUTPUT_TIMEOUT,
90 output_timeout_handler=output_timeout_handler,
93 log.info("gtest | process wait complete, returncode=%s" % proc.returncode)
94 if mozcrash.check_for_crashes(cwd, symbols_path, test_name="gtest"):
95 # mozcrash will output the log failure line for us.
96 return False
97 if GTests.run_gtest.timed_out:
98 return False
99 result = proc.returncode == 0
100 if not result:
101 log.testFail("gtest | test failed with return code %d", proc.returncode)
102 return result
104 def build_core_environment(self, env={}):
106 Add environment variables likely to be used across all platforms, including remote systems.
108 env["MOZ_XRE_DIR"] = self.xre_path
109 env["MOZ_GMP_PATH"] = os.pathsep.join(
110 os.path.join(self.xre_path, p, "1.0")
111 for p in ("gmp-fake", "gmp-fakeopenh264")
113 env["XPCOM_DEBUG_BREAK"] = "stack-and-abort"
114 env["MOZ_CRASHREPORTER_NO_REPORT"] = "1"
115 env["MOZ_CRASHREPORTER"] = "1"
116 env["MOZ_DISABLE_NONLOCAL_CONNECTIONS"] = "1"
117 env["MOZ_RUN_GTEST"] = "1"
118 # Normally we run with GTest default output, override this to use the TBPL test format.
119 env["MOZ_TBPL_PARSER"] = "1"
121 if not mozinfo.has_sandbox:
122 # Bug 1082193 - This is horrible. Our linux build boxes run CentOS 6,
123 # which is too old to support sandboxing. Disable sandbox for gtests
124 # on machines which don't support sandboxing until they can be
125 # upgraded, or gtests are run on test machines instead.
126 env["MOZ_DISABLE_GMP_SANDBOX"] = "1"
128 return env
130 def build_environment(self):
132 Create and return a dictionary of all the appropriate env variables
133 and values. On a remote system, we overload this to set different
134 values and are missing things like os.environ and PATH.
136 if not os.path.isdir(self.xre_path):
137 raise Exception("xre_path does not exist: %s", self.xre_path)
138 env = dict(os.environ)
139 env = self.build_core_environment(env)
140 env["PERFHERDER_ALERTING_ENABLED"] = "1"
141 pathvar = ""
142 if mozinfo.os == "linux":
143 pathvar = "LD_LIBRARY_PATH"
144 # disable alerts for unstable tests (Bug 1369807)
145 del env["PERFHERDER_ALERTING_ENABLED"]
146 elif mozinfo.os == "mac":
147 pathvar = "DYLD_LIBRARY_PATH"
148 elif mozinfo.os == "win":
149 pathvar = "PATH"
150 if pathvar:
151 if pathvar in env:
152 env[pathvar] = "%s%s%s" % (self.xre_path, os.pathsep, env[pathvar])
153 else:
154 env[pathvar] = self.xre_path
156 symbolizer_path = None
157 if mozinfo.info["asan"]:
158 symbolizer_path = "ASAN_SYMBOLIZER_PATH"
159 elif mozinfo.info["tsan"]:
160 symbolizer_path = "TSAN_SYMBOLIZER_PATH"
162 if symbolizer_path is not None:
163 # Use llvm-symbolizer for ASan/TSan if available/required
164 if symbolizer_path in env and os.path.isfile(env[symbolizer_path]):
165 llvmsym = env[symbolizer_path]
166 else:
167 llvmsym = os.path.join(
168 self.xre_path, "llvm-symbolizer" + mozinfo.info["bin_suffix"]
170 if os.path.isfile(llvmsym):
171 env[symbolizer_path] = llvmsym
172 log.info("Using LLVM symbolizer at %s", llvmsym)
173 else:
174 # This should be |testFail| instead of |info|. See bug 1050891.
175 log.info("Failed to find LLVM symbolizer at %s", llvmsym)
177 # webrender needs gfx.webrender.all=true, gtest doesn't use prefs
178 env["MOZ_WEBRENDER"] = "1"
179 env["MOZ_ACCELERATED"] = "1"
181 return env
184 class gtestOptions(argparse.ArgumentParser):
185 def __init__(self):
186 super(gtestOptions, self).__init__()
188 self.add_argument(
189 "--cwd",
190 dest="cwd",
191 default=os.getcwd(),
192 help="absolute path to directory from which " "to run the binary",
194 self.add_argument(
195 "--xre-path",
196 dest="xre_path",
197 default=None,
198 help="absolute path to directory containing XRE " "(probably xulrunner)",
200 self.add_argument(
201 "--symbols-path",
202 dest="symbols_path",
203 default=None,
204 help="absolute path to directory containing breakpad "
205 "symbols, or the URL of a zip file containing "
206 "symbols",
208 self.add_argument(
209 "--utility-path",
210 dest="utility_path",
211 default=None,
212 help="path to a directory containing utility program binaries",
214 self.add_argument("args", nargs=argparse.REMAINDER)
217 def update_mozinfo():
218 """walk up directories to find mozinfo.json update the info"""
219 path = os.path.abspath(os.path.realpath(os.path.dirname(__file__)))
220 dirs = set()
221 while path != os.path.expanduser("~"):
222 if path in dirs:
223 break
224 dirs.add(path)
225 path = os.path.split(path)[0]
226 mozinfo.find_and_update_from_json(*dirs)
229 def main():
230 parser = gtestOptions()
231 options = parser.parse_args()
232 args = options.args
233 if not args:
234 print("Usage: %s <binary>" % sys.argv[0])
235 sys.exit(1)
236 if not options.xre_path:
237 print("Error: --xre-path is required")
238 sys.exit(1)
239 if not options.utility_path:
240 print("Warning: --utility-path is required to process assertion stacks")
242 update_mozinfo()
243 prog = os.path.abspath(args[0])
244 options.xre_path = os.path.abspath(options.xre_path)
245 tester = GTests()
246 try:
247 result = tester.run_gtest(
248 prog,
249 options.xre_path,
250 options.cwd,
251 symbols_path=options.symbols_path,
252 utility_path=options.utility_path,
254 except Exception as e:
255 log.error(str(e))
256 result = False
257 exit_code = 0 if result else 1
258 log.info("rungtests.py exits with code %s" % exit_code)
259 sys.exit(exit_code)
262 if __name__ == "__main__":
263 main()