Bug 1613457 [wpt PR 21606] - Update interfaces/cookie-store.idl, a=testonly
[gecko.git] / testing / gtest / rungtests.py
blobd6501671ab0111bb6228e02f236b93c9c2e42628
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 from __future__ import with_statement
9 from optparse import OptionParser
10 import os
11 import sys
13 import mozcrash
14 import mozinfo
15 import mozlog
16 import mozprocess
17 from mozrunner.utils import get_stack_fixer_function
19 log = mozlog.unstructured.getLogger('gtest')
22 class GTests(object):
23 # Time (seconds) to wait for test process to complete
24 TEST_PROC_TIMEOUT = 2400
25 # Time (seconds) in which process will be killed if it produces no output.
26 TEST_PROC_NO_OUTPUT_TIMEOUT = 300
28 def run_gtest(self, prog, xre_path, cwd, symbols_path=None,
29 utility_path=None, enable_webrender=False):
30 """
31 Run a single C++ unit test program.
33 Arguments:
34 * prog: The path to the test program to run.
35 * env: The environment to use for running the program.
36 * cwd: The directory to run tests from (support files will be found
37 in this direcotry).
38 * symbols_path: A path to a directory containing Breakpad-formatted
39 symbol files for producing stack traces on crash.
40 * utility_path: A path to a directory containing utility programs.
41 currently used to locate a stack fixer to provide
42 symbols symbols for assertion stacks.
44 Return True if the program exits with a zero status, False otherwise.
45 """
46 self.xre_path = xre_path
47 env = self.build_environment(enable_webrender)
48 log.info("Running gtest")
50 if cwd and not os.path.isdir(cwd):
51 os.makedirs(cwd)
53 stream_output = mozprocess.StreamOutput(sys.stdout)
54 process_output = stream_output
55 if utility_path:
56 stack_fixer = get_stack_fixer_function(utility_path, symbols_path)
57 if stack_fixer:
58 def f(line): return stream_output(stack_fixer(line))
59 process_output = f
61 proc = mozprocess.ProcessHandler([prog, "-unittest",
62 "--gtest_death_test_style=threadsafe"],
63 cwd=cwd,
64 env=env,
65 processOutputLine=process_output)
66 # TODO: After bug 811320 is fixed, don't let .run() kill the process,
67 # instead use a timeout in .wait() and then kill to get a stack.
68 proc.run(timeout=GTests.TEST_PROC_TIMEOUT,
69 outputTimeout=GTests.TEST_PROC_NO_OUTPUT_TIMEOUT)
70 proc.wait()
71 log.info("gtest | process wait complete, returncode=%s" % proc.proc.returncode)
72 if proc.timedOut:
73 if proc.outputTimedOut:
74 log.testFail("gtest | timed out after %d seconds without output",
75 GTests.TEST_PROC_NO_OUTPUT_TIMEOUT)
76 else:
77 log.testFail("gtest | timed out after %d seconds",
78 GTests.TEST_PROC_TIMEOUT)
79 return False
80 if mozcrash.check_for_crashes(cwd, symbols_path, test_name="gtest"):
81 # mozcrash will output the log failure line for us.
82 return False
83 result = proc.proc.returncode == 0
84 if not result:
85 log.testFail("gtest | test failed with return code %d", proc.proc.returncode)
86 return result
88 def build_core_environment(self, env={}):
89 """
90 Add environment variables likely to be used across all platforms, including remote systems.
91 """
92 env["MOZ_XRE_DIR"] = self.xre_path
93 env["MOZ_GMP_PATH"] = os.pathsep.join(
94 os.path.join(self.xre_path, p, "1.0")
95 for p in ('gmp-fake', 'gmp-fakeopenh264')
97 env["XPCOM_DEBUG_BREAK"] = "stack-and-abort"
98 env["MOZ_CRASHREPORTER_NO_REPORT"] = "1"
99 env["MOZ_CRASHREPORTER"] = "1"
100 env["MOZ_DISABLE_NONLOCAL_CONNECTIONS"] = "1"
101 env["MOZ_RUN_GTEST"] = "1"
102 # Normally we run with GTest default output, override this to use the TBPL test format.
103 env["MOZ_TBPL_PARSER"] = "1"
105 if not mozinfo.has_sandbox:
106 # Bug 1082193 - This is horrible. Our linux build boxes run CentOS 6,
107 # which is too old to support sandboxing. Disable sandbox for gtests
108 # on machines which don't support sandboxing until they can be
109 # upgraded, or gtests are run on test machines instead.
110 env["MOZ_DISABLE_GMP_SANDBOX"] = "1"
112 return env
114 def build_environment(self, enable_webrender):
116 Create and return a dictionary of all the appropriate env variables
117 and values. On a remote system, we overload this to set different
118 values and are missing things like os.environ and PATH.
120 if not os.path.isdir(self.xre_path):
121 raise Exception("xre_path does not exist: %s", self.xre_path)
122 env = dict(os.environ)
123 env = self.build_core_environment(env)
124 env["PERFHERDER_ALERTING_ENABLED"] = "1"
125 pathvar = ""
126 if mozinfo.os == "linux":
127 pathvar = "LD_LIBRARY_PATH"
128 # disable alerts for unstable tests (Bug 1369807)
129 del env["PERFHERDER_ALERTING_ENABLED"]
130 elif mozinfo.os == "mac":
131 pathvar = "DYLD_LIBRARY_PATH"
132 elif mozinfo.os == "win":
133 pathvar = "PATH"
134 if pathvar:
135 if pathvar in env:
136 env[pathvar] = "%s%s%s" % (self.xre_path, os.pathsep, env[pathvar])
137 else:
138 env[pathvar] = self.xre_path
140 # ASan specific environment stuff
141 if mozinfo.info["asan"]:
142 # Symbolizer support
143 llvmsym = os.path.join(
144 self.xre_path,
145 "llvm-symbolizer" + mozinfo.info["bin_suffix"].encode('ascii'))
146 if os.path.isfile(llvmsym):
147 env["ASAN_SYMBOLIZER_PATH"] = llvmsym
148 log.info("gtest | ASan using symbolizer at %s", llvmsym)
149 else:
150 # This should be |testFail| instead of |info|. See bug 1050891.
151 log.info("gtest | Failed to find ASan symbolizer at %s", llvmsym)
153 if enable_webrender:
154 env["MOZ_WEBRENDER"] = "1"
155 env["MOZ_ACCELERATED"] = "1"
156 else:
157 env["MOZ_WEBRENDER"] = "0"
159 return env
162 class gtestOptions(OptionParser):
163 def __init__(self):
164 OptionParser.__init__(self)
165 self.add_option("--cwd",
166 dest="cwd",
167 default=os.getcwd(),
168 help="absolute path to directory from which "
169 "to run the binary")
170 self.add_option("--xre-path",
171 dest="xre_path",
172 default=None,
173 help="absolute path to directory containing XRE "
174 "(probably xulrunner)")
175 self.add_option("--symbols-path",
176 dest="symbols_path",
177 default=None,
178 help="absolute path to directory containing breakpad "
179 "symbols, or the URL of a zip file containing "
180 "symbols")
181 self.add_option("--utility-path",
182 dest="utility_path",
183 default=None,
184 help="path to a directory containing utility program binaries")
185 self.add_option("--enable-webrender",
186 action="store_true",
187 dest="enable_webrender",
188 default=False,
189 help="Enable the WebRender compositor in Gecko.")
192 def update_mozinfo():
193 """walk up directories to find mozinfo.json update the info"""
194 path = os.path.abspath(os.path.realpath(os.path.dirname(__file__)))
195 dirs = set()
196 while path != os.path.expanduser('~'):
197 if path in dirs:
198 break
199 dirs.add(path)
200 path = os.path.split(path)[0]
201 mozinfo.find_and_update_from_json(*dirs)
204 def main():
205 parser = gtestOptions()
206 options, args = parser.parse_args()
207 if not args:
208 print >>sys.stderr, """Usage: %s <binary>""" % sys.argv[0]
209 sys.exit(1)
210 if not options.xre_path:
211 print >>sys.stderr, """Error: --xre-path is required"""
212 sys.exit(1)
213 if not options.utility_path:
214 print >>sys.stderr, """Warning: --utility-path is required to process assertion stacks"""
216 update_mozinfo()
217 prog = os.path.abspath(args[0])
218 options.xre_path = os.path.abspath(options.xre_path)
219 tester = GTests()
220 try:
221 result = tester.run_gtest(prog, options.xre_path,
222 options.cwd,
223 symbols_path=options.symbols_path,
224 utility_path=options.utility_path,
225 enable_webrender=options.enable_webrender)
226 except Exception as e:
227 log.error(str(e))
228 result = False
229 exit_code = 0 if result else 1
230 log.info("rungtests.py exits with code %s" % exit_code)
231 sys.exit(exit_code)
234 if __name__ == '__main__':
235 main()