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/.
15 from mozrunner
.utils
import get_stack_fixer_function
17 log
= mozlog
.unstructured
.getLogger("gtest")
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
35 Run a single C++ unit test program.
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
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.
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
):
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
):
65 print(stack_fixer(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
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"],
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.
97 if GTests
.run_gtest
.timed_out
:
99 result
= proc
.returncode
== 0
101 log
.testFail("gtest | test failed with return code %d", proc
.returncode
)
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"
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"
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":
152 env
[pathvar
] = "%s%s%s" % (self
.xre_path
, os
.pathsep
, env
[pathvar
])
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
]
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
)
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"
184 class gtestOptions(argparse
.ArgumentParser
):
186 super(gtestOptions
, self
).__init
__()
192 help="absolute path to directory from which " "to run the binary",
198 help="absolute path to directory containing XRE " "(probably xulrunner)",
204 help="absolute path to directory containing breakpad "
205 "symbols, or the URL of a zip file containing "
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__
)))
221 while path
!= os
.path
.expanduser("~"):
225 path
= os
.path
.split(path
)[0]
226 mozinfo
.find_and_update_from_json(*dirs
)
230 parser
= gtestOptions()
231 options
= parser
.parse_args()
234 print("Usage: %s <binary>" % sys
.argv
[0])
236 if not options
.xre_path
:
237 print("Error: --xre-path is required")
239 if not options
.utility_path
:
240 print("Warning: --utility-path is required to process assertion stacks")
243 prog
= os
.path
.abspath(args
[0])
244 options
.xre_path
= os
.path
.abspath(options
.xre_path
)
247 result
= tester
.run_gtest(
251 symbols_path
=options
.symbols_path
,
252 utility_path
=options
.utility_path
,
254 except Exception as e
:
257 exit_code
= 0 if result
else 1
258 log
.info("rungtests.py exits with code %s" % exit_code
)
262 if __name__
== "__main__":