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
17 from mozrunner
.utils
import get_stack_fixer_function
19 log
= mozlog
.unstructured
.getLogger('gtest')
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):
31 Run a single C++ unit test program.
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
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.
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
):
53 stream_output
= mozprocess
.StreamOutput(sys
.stdout
)
54 process_output
= stream_output
56 stack_fixer
= get_stack_fixer_function(utility_path
, symbols_path
)
58 def f(line
): return stream_output(stack_fixer(line
))
61 proc
= mozprocess
.ProcessHandler([prog
, "-unittest",
62 "--gtest_death_test_style=threadsafe"],
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
)
71 log
.info("gtest | process wait complete, returncode=%s" % proc
.proc
.returncode
)
73 if proc
.outputTimedOut
:
74 log
.testFail("gtest | timed out after %d seconds without output",
75 GTests
.TEST_PROC_NO_OUTPUT_TIMEOUT
)
77 log
.testFail("gtest | timed out after %d seconds",
78 GTests
.TEST_PROC_TIMEOUT
)
80 if mozcrash
.check_for_crashes(cwd
, symbols_path
, test_name
="gtest"):
81 # mozcrash will output the log failure line for us.
83 result
= proc
.proc
.returncode
== 0
85 log
.testFail("gtest | test failed with return code %d", proc
.proc
.returncode
)
88 def build_core_environment(self
, env
={}):
90 Add environment variables likely to be used across all platforms, including remote systems.
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"
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"
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":
136 env
[pathvar
] = "%s%s%s" % (self
.xre_path
, os
.pathsep
, env
[pathvar
])
138 env
[pathvar
] = self
.xre_path
140 # ASan specific environment stuff
141 if mozinfo
.info
["asan"]:
143 llvmsym
= os
.path
.join(
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
)
150 # This should be |testFail| instead of |info|. See bug 1050891.
151 log
.info("gtest | Failed to find ASan symbolizer at %s", llvmsym
)
154 env
["MOZ_WEBRENDER"] = "1"
155 env
["MOZ_ACCELERATED"] = "1"
157 env
["MOZ_WEBRENDER"] = "0"
162 class gtestOptions(OptionParser
):
164 OptionParser
.__init
__(self
)
165 self
.add_option("--cwd",
168 help="absolute path to directory from which "
170 self
.add_option("--xre-path",
173 help="absolute path to directory containing XRE "
174 "(probably xulrunner)")
175 self
.add_option("--symbols-path",
178 help="absolute path to directory containing breakpad "
179 "symbols, or the URL of a zip file containing "
181 self
.add_option("--utility-path",
184 help="path to a directory containing utility program binaries")
185 self
.add_option("--enable-webrender",
187 dest
="enable_webrender",
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__
)))
196 while path
!= os
.path
.expanduser('~'):
200 path
= os
.path
.split(path
)[0]
201 mozinfo
.find_and_update_from_json(*dirs
)
205 parser
= gtestOptions()
206 options
, args
= parser
.parse_args()
208 print >>sys
.stderr
, """Usage: %s <binary>""" % sys
.argv
[0]
210 if not options
.xre_path
:
211 print >>sys
.stderr
, """Error: --xre-path is required"""
213 if not options
.utility_path
:
214 print >>sys
.stderr
, """Warning: --utility-path is required to process assertion stacks"""
217 prog
= os
.path
.abspath(args
[0])
218 options
.xre_path
= os
.path
.abspath(options
.xre_path
)
221 result
= tester
.run_gtest(prog
, options
.xre_path
,
223 symbols_path
=options
.symbols_path
,
224 utility_path
=options
.utility_path
,
225 enable_webrender
=options
.enable_webrender
)
226 except Exception as e
:
229 exit_code
= 0 if result
else 1
230 log
.info("rungtests.py exits with code %s" % exit_code
)
234 if __name__
== '__main__':