automation.py.in 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. #
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. from __future__ import with_statement
  6. import logging
  7. import os
  8. import re
  9. import select
  10. import signal
  11. import subprocess
  12. import sys
  13. import tempfile
  14. from datetime import datetime, timedelta
  15. SCRIPT_DIR = os.path.abspath(os.path.realpath(os.path.dirname(sys.argv[0])))
  16. sys.path.insert(0, SCRIPT_DIR)
  17. # --------------------------------------------------------------
  18. # TODO: this is a hack for mozbase without virtualenv, remove with bug 849900
  19. # These paths refer to relative locations to test.zip, not the OBJDIR or SRCDIR
  20. here = os.path.dirname(os.path.realpath(__file__))
  21. mozbase = os.path.realpath(os.path.join(os.path.dirname(here), 'mozbase'))
  22. if os.path.isdir(mozbase):
  23. for package in os.listdir(mozbase):
  24. package_path = os.path.join(mozbase, package)
  25. if package_path not in sys.path:
  26. sys.path.append(package_path)
  27. import mozcrash
  28. from mozscreenshot import printstatus, dump_screen
  29. # ---------------------------------------------------------------
  30. _DEFAULT_PREFERENCE_FILE = os.path.join(SCRIPT_DIR, 'prefs_general.js')
  31. _DEFAULT_APPS_FILE = os.path.join(SCRIPT_DIR, 'webapps_mochitest.json')
  32. _DEFAULT_WEB_SERVER = "127.0.0.1"
  33. _DEFAULT_HTTP_PORT = 8888
  34. _DEFAULT_SSL_PORT = 4443
  35. _DEFAULT_WEBSOCKET_PORT = 9988
  36. # from nsIPrincipal.idl
  37. _APP_STATUS_NOT_INSTALLED = 0
  38. _APP_STATUS_INSTALLED = 1
  39. _APP_STATUS_PRIVILEGED = 2
  40. _APP_STATUS_CERTIFIED = 3
  41. #expand _DIST_BIN = __XPC_BIN_PATH__
  42. #expand _IS_WIN32 = len("__WIN32__") != 0
  43. #expand _IS_MAC = __IS_MAC__ != 0
  44. #expand _IS_LINUX = __IS_LINUX__ != 0
  45. #ifdef IS_CYGWIN
  46. #expand _IS_CYGWIN = __IS_CYGWIN__ == 1
  47. #else
  48. _IS_CYGWIN = False
  49. #endif
  50. #expand _BIN_SUFFIX = __BIN_SUFFIX__
  51. #expand _DEFAULT_APP = "./" + __BROWSER_PATH__
  52. #expand _CERTS_SRC_DIR = __CERTS_SRC_DIR__
  53. #expand _IS_TEST_BUILD = __IS_TEST_BUILD__
  54. #expand _IS_DEBUG_BUILD = __IS_DEBUG_BUILD__
  55. #expand _CRASHREPORTER = __CRASHREPORTER__ == 1
  56. #expand _IS_ASAN = __IS_ASAN__ == 1
  57. if _IS_WIN32:
  58. import ctypes, ctypes.wintypes, time, msvcrt
  59. else:
  60. import errno
  61. def resetGlobalLog(log):
  62. while _log.handlers:
  63. _log.removeHandler(_log.handlers[0])
  64. handler = logging.StreamHandler(log)
  65. _log.setLevel(logging.INFO)
  66. _log.addHandler(handler)
  67. # We use the logging system here primarily because it'll handle multiple
  68. # threads, which is needed to process the output of the server and application
  69. # processes simultaneously.
  70. _log = logging.getLogger()
  71. resetGlobalLog(sys.stdout)
  72. #################
  73. # PROFILE SETUP #
  74. #################
  75. class Automation(object):
  76. """
  77. Runs the browser from a script, and provides useful utilities
  78. for setting up the browser environment.
  79. """
  80. DIST_BIN = _DIST_BIN
  81. IS_WIN32 = _IS_WIN32
  82. IS_MAC = _IS_MAC
  83. IS_LINUX = _IS_LINUX
  84. IS_CYGWIN = _IS_CYGWIN
  85. BIN_SUFFIX = _BIN_SUFFIX
  86. UNIXISH = not IS_WIN32 and not IS_MAC
  87. DEFAULT_APP = _DEFAULT_APP
  88. CERTS_SRC_DIR = _CERTS_SRC_DIR
  89. IS_TEST_BUILD = _IS_TEST_BUILD
  90. IS_DEBUG_BUILD = _IS_DEBUG_BUILD
  91. CRASHREPORTER = _CRASHREPORTER
  92. IS_ASAN = _IS_ASAN
  93. # timeout, in seconds
  94. DEFAULT_TIMEOUT = 60.0
  95. DEFAULT_WEB_SERVER = _DEFAULT_WEB_SERVER
  96. DEFAULT_HTTP_PORT = _DEFAULT_HTTP_PORT
  97. DEFAULT_SSL_PORT = _DEFAULT_SSL_PORT
  98. DEFAULT_WEBSOCKET_PORT = _DEFAULT_WEBSOCKET_PORT
  99. def __init__(self):
  100. self.log = _log
  101. self.lastTestSeen = "automation.py"
  102. self.haveDumpedScreen = False
  103. def setServerInfo(self,
  104. webServer = _DEFAULT_WEB_SERVER,
  105. httpPort = _DEFAULT_HTTP_PORT,
  106. sslPort = _DEFAULT_SSL_PORT,
  107. webSocketPort = _DEFAULT_WEBSOCKET_PORT):
  108. self.webServer = webServer
  109. self.httpPort = httpPort
  110. self.sslPort = sslPort
  111. self.webSocketPort = webSocketPort
  112. @property
  113. def __all__(self):
  114. return [
  115. "UNIXISH",
  116. "IS_WIN32",
  117. "IS_MAC",
  118. "log",
  119. "runApp",
  120. "Process",
  121. "DIST_BIN",
  122. "DEFAULT_APP",
  123. "CERTS_SRC_DIR",
  124. "environment",
  125. "IS_TEST_BUILD",
  126. "IS_DEBUG_BUILD",
  127. "DEFAULT_TIMEOUT",
  128. ]
  129. class Process(subprocess.Popen):
  130. """
  131. Represents our view of a subprocess.
  132. It adds a kill() method which allows it to be stopped explicitly.
  133. """
  134. def __init__(self,
  135. args,
  136. bufsize=0,
  137. executable=None,
  138. stdin=None,
  139. stdout=None,
  140. stderr=None,
  141. preexec_fn=None,
  142. close_fds=False,
  143. shell=False,
  144. cwd=None,
  145. env=None,
  146. universal_newlines=False,
  147. startupinfo=None,
  148. creationflags=0):
  149. _log.info("INFO | automation.py | Launching: %s", subprocess.list2cmdline(args))
  150. subprocess.Popen.__init__(self, args, bufsize, executable,
  151. stdin, stdout, stderr,
  152. preexec_fn, close_fds,
  153. shell, cwd, env,
  154. universal_newlines, startupinfo, creationflags)
  155. self.log = _log
  156. def kill(self):
  157. if Automation().IS_WIN32:
  158. import platform
  159. pid = "%i" % self.pid
  160. if platform.release() == "2000":
  161. # Windows 2000 needs 'kill.exe' from the
  162. #'Windows 2000 Resource Kit tools'. (See bug 475455.)
  163. try:
  164. subprocess.Popen(["kill", "-f", pid]).wait()
  165. except:
  166. self.log.info("TEST-UNEXPECTED-FAIL | automation.py | Missing 'kill' utility to kill process with pid=%s. Kill it manually!", pid)
  167. else:
  168. # Windows XP and later.
  169. subprocess.Popen(["taskkill", "/F", "/PID", pid]).wait()
  170. else:
  171. os.kill(self.pid, signal.SIGKILL)
  172. def environment(self, env=None, xrePath=None, crashreporter=True, debugger=False, dmdPath=None, lsanPath=None):
  173. if xrePath == None:
  174. xrePath = self.DIST_BIN
  175. if env == None:
  176. env = dict(os.environ)
  177. ldLibraryPath = os.path.abspath(os.path.join(SCRIPT_DIR, xrePath))
  178. dmdLibrary = None
  179. preloadEnvVar = None
  180. if self.UNIXISH or self.IS_MAC:
  181. envVar = "LD_LIBRARY_PATH"
  182. preloadEnvVar = "LD_PRELOAD"
  183. if self.IS_MAC:
  184. envVar = "DYLD_LIBRARY_PATH"
  185. dmdLibrary = "libdmd.dylib"
  186. else: # unixish
  187. env['MOZILLA_FIVE_HOME'] = xrePath
  188. dmdLibrary = "libdmd.so"
  189. if envVar in env:
  190. ldLibraryPath = ldLibraryPath + ":" + env[envVar]
  191. env[envVar] = ldLibraryPath
  192. elif self.IS_WIN32:
  193. env["PATH"] = env["PATH"] + ";" + str(ldLibraryPath)
  194. dmdLibrary = "dmd.dll"
  195. preloadEnvVar = "MOZ_REPLACE_MALLOC_LIB"
  196. if dmdPath and dmdLibrary and preloadEnvVar:
  197. env[preloadEnvVar] = os.path.join(dmdPath, dmdLibrary)
  198. env['MOZ_CRASHREPORTER_DISABLE'] = '1'
  199. # Crash on non-local network connections by default.
  200. # MOZ_DISABLE_NONLOCAL_CONNECTIONS can be set to "0" to temporarily
  201. # enable non-local connections for the purposes of local testing. Don't
  202. # override the user's choice here. See bug 1049688.
  203. env.setdefault('MOZ_DISABLE_NONLOCAL_CONNECTIONS', '1')
  204. env['GNOME_DISABLE_CRASH_DIALOG'] = '1'
  205. env['XRE_NO_WINDOWS_CRASH_DIALOG'] = '1'
  206. # Set WebRTC logging in case it is not set yet
  207. env.setdefault('MOZ_LOG', 'signaling:3,mtransport:4,DataChannel:4,jsep:4,MediaPipelineFactory:4')
  208. env.setdefault('R_LOG_LEVEL', '6')
  209. env.setdefault('R_LOG_DESTINATION', 'stderr')
  210. env.setdefault('R_LOG_VERBOSE', '1')
  211. # ASan specific environment stuff
  212. if self.IS_ASAN and (self.IS_LINUX or self.IS_MAC):
  213. # Symbolizer support
  214. llvmsym = os.path.join(xrePath, "llvm-symbolizer")
  215. if os.path.isfile(llvmsym):
  216. env["ASAN_SYMBOLIZER_PATH"] = llvmsym
  217. self.log.info("INFO | automation.py | ASan using symbolizer at %s", llvmsym)
  218. else:
  219. self.log.info("TEST-UNEXPECTED-FAIL | automation.py | Failed to find ASan symbolizer at %s", llvmsym)
  220. try:
  221. totalMemory = int(os.popen("free").readlines()[1].split()[1])
  222. # Only 4 GB RAM or less available? Use custom ASan options to reduce
  223. # the amount of resources required to do the tests. Standard options
  224. # will otherwise lead to OOM conditions on the current test slaves.
  225. if totalMemory <= 1024 * 1024 * 4:
  226. self.log.info("INFO | automation.py | ASan running in low-memory configuration")
  227. env["ASAN_OPTIONS"] = "quarantine_size=50331648:malloc_context_size=5"
  228. else:
  229. self.log.info("INFO | automation.py | ASan running in default memory configuration")
  230. except OSError,err:
  231. self.log.info("Failed determine available memory, disabling ASan low-memory configuration: %s", err.strerror)
  232. except:
  233. self.log.info("Failed determine available memory, disabling ASan low-memory configuration")
  234. return env
  235. def killPid(self, pid):
  236. try:
  237. os.kill(pid, getattr(signal, "SIGKILL", signal.SIGTERM))
  238. except WindowsError:
  239. self.log.info("Failed to kill process %d." % pid)
  240. if IS_WIN32:
  241. PeekNamedPipe = ctypes.windll.kernel32.PeekNamedPipe
  242. GetLastError = ctypes.windll.kernel32.GetLastError
  243. def readWithTimeout(self, f, timeout):
  244. """
  245. Try to read a line of output from the file object |f|. |f| must be a
  246. pipe, like the |stdout| member of a subprocess.Popen object created
  247. with stdout=PIPE. Returns a tuple (line, did_timeout), where |did_timeout|
  248. is True if the read timed out, and False otherwise. If no output is
  249. received within |timeout| seconds, returns a blank line.
  250. """
  251. if timeout is None:
  252. timeout = 0
  253. x = msvcrt.get_osfhandle(f.fileno())
  254. l = ctypes.c_long()
  255. done = time.time() + timeout
  256. buffer = ""
  257. while timeout == 0 or time.time() < done:
  258. if self.PeekNamedPipe(x, None, 0, None, ctypes.byref(l), None) == 0:
  259. err = self.GetLastError()
  260. if err == 38 or err == 109: # ERROR_HANDLE_EOF || ERROR_BROKEN_PIPE
  261. return ('', False)
  262. else:
  263. self.log.error("readWithTimeout got error: %d", err)
  264. # read a character at a time, checking for eol. Return once we get there.
  265. index = 0
  266. while index < l.value:
  267. char = f.read(1)
  268. buffer += char
  269. if char == '\n':
  270. return (buffer, False)
  271. index = index + 1
  272. time.sleep(0.01)
  273. return (buffer, True)
  274. def isPidAlive(self, pid):
  275. STILL_ACTIVE = 259
  276. PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
  277. pHandle = ctypes.windll.kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid)
  278. if not pHandle:
  279. return False
  280. pExitCode = ctypes.wintypes.DWORD()
  281. ctypes.windll.kernel32.GetExitCodeProcess(pHandle, ctypes.byref(pExitCode))
  282. ctypes.windll.kernel32.CloseHandle(pHandle)
  283. return pExitCode.value == STILL_ACTIVE
  284. else:
  285. def readWithTimeout(self, f, timeout):
  286. """Try to read a line of output from the file object |f|. If no output
  287. is received within |timeout| seconds, return a blank line.
  288. Returns a tuple (line, did_timeout), where |did_timeout| is True
  289. if the read timed out, and False otherwise."""
  290. (r, w, e) = select.select([f], [], [], timeout)
  291. if len(r) == 0:
  292. return ('', True)
  293. return (f.readline(), False)
  294. def isPidAlive(self, pid):
  295. try:
  296. # kill(pid, 0) checks for a valid PID without actually sending a signal
  297. # The method throws OSError if the PID is invalid, which we catch below.
  298. os.kill(pid, 0)
  299. # Wait on it to see if it's a zombie. This can throw OSError.ECHILD if
  300. # the process terminates before we get to this point.
  301. wpid, wstatus = os.waitpid(pid, os.WNOHANG)
  302. return wpid == 0
  303. except OSError, err:
  304. # Catch the errors we might expect from os.kill/os.waitpid,
  305. # and re-raise any others
  306. if err.errno == errno.ESRCH or err.errno == errno.ECHILD:
  307. return False
  308. raise
  309. def dumpScreen(self, utilityPath):
  310. if self.haveDumpedScreen:
  311. self.log.info("Not taking screenshot here: see the one that was previously logged")
  312. return
  313. self.haveDumpedScreen = True;
  314. dump_screen(utilityPath, self.log)
  315. def killAndGetStack(self, processPID, utilityPath, debuggerInfo):
  316. """Kill the process, preferrably in a way that gets us a stack trace.
  317. Also attempts to obtain a screenshot before killing the process."""
  318. if not debuggerInfo:
  319. self.dumpScreen(utilityPath)
  320. self.killAndGetStackNoScreenshot(processPID, utilityPath, debuggerInfo)
  321. def killAndGetStackNoScreenshot(self, processPID, utilityPath, debuggerInfo):
  322. """Kill the process, preferrably in a way that gets us a stack trace."""
  323. if self.CRASHREPORTER and not debuggerInfo:
  324. if not self.IS_WIN32:
  325. # ABRT will get picked up by Breakpad's signal handler
  326. os.kill(processPID, signal.SIGABRT)
  327. return
  328. else:
  329. # We should have a "crashinject" program in our utility path
  330. crashinject = os.path.normpath(os.path.join(utilityPath, "crashinject.exe"))
  331. if os.path.exists(crashinject):
  332. status = subprocess.Popen([crashinject, str(processPID)]).wait()
  333. printstatus("crashinject", status)
  334. if status == 0:
  335. return
  336. self.log.info("Can't trigger Breakpad, just killing process")
  337. self.killPid(processPID)
  338. def waitForFinish(self, proc, utilityPath, timeout, maxTime, startTime, debuggerInfo, symbolsPath, outputHandler=None):
  339. """ Look for timeout or crashes and return the status after the process terminates """
  340. stackFixerFunction = None
  341. didTimeout = False
  342. hitMaxTime = False
  343. if proc.stdout is None:
  344. self.log.info("TEST-INFO: Not logging stdout or stderr due to debugger connection")
  345. else:
  346. logsource = proc.stdout
  347. if self.IS_DEBUG_BUILD and symbolsPath and os.path.exists(symbolsPath):
  348. # Run each line through a function in fix_stack_using_bpsyms.py (uses breakpad symbol files)
  349. # This method is preferred for Tinderbox builds, since native symbols may have been stripped.
  350. sys.path.insert(0, utilityPath)
  351. import fix_stack_using_bpsyms as stackFixerModule
  352. stackFixerFunction = lambda line: stackFixerModule.fixSymbols(line, symbolsPath)
  353. del sys.path[0]
  354. elif self.IS_DEBUG_BUILD and self.IS_MAC:
  355. # Run each line through a function in fix_macosx_stack.py (uses atos)
  356. sys.path.insert(0, utilityPath)
  357. import fix_macosx_stack as stackFixerModule
  358. stackFixerFunction = lambda line: stackFixerModule.fixSymbols(line)
  359. del sys.path[0]
  360. elif self.IS_DEBUG_BUILD and self.IS_LINUX:
  361. # Run each line through a function in fix_linux_stack.py (uses addr2line)
  362. # This method is preferred for developer machines, so we don't have to run "make buildsymbols".
  363. sys.path.insert(0, utilityPath)
  364. import fix_linux_stack as stackFixerModule
  365. stackFixerFunction = lambda line: stackFixerModule.fixSymbols(line)
  366. del sys.path[0]
  367. # With metro browser runs this script launches the metro test harness which launches the browser.
  368. # The metro test harness hands back the real browser process id via log output which we need to
  369. # pick up on and parse out. This variable tracks the real browser process id if we find it.
  370. browserProcessId = -1
  371. (line, didTimeout) = self.readWithTimeout(logsource, timeout)
  372. while line != "" and not didTimeout:
  373. if stackFixerFunction:
  374. line = stackFixerFunction(line)
  375. if outputHandler is None:
  376. self.log.info(line.rstrip().decode("UTF-8", "ignore"))
  377. else:
  378. outputHandler(line)
  379. if "TEST-START" in line and "|" in line:
  380. self.lastTestSeen = line.split("|")[1].strip()
  381. if not debuggerInfo and "TEST-UNEXPECTED-FAIL" in line and "Test timed out" in line:
  382. self.dumpScreen(utilityPath)
  383. (line, didTimeout) = self.readWithTimeout(logsource, timeout)
  384. if not hitMaxTime and maxTime and datetime.now() - startTime > timedelta(seconds = maxTime):
  385. # Kill the application.
  386. hitMaxTime = True
  387. self.log.info("TEST-UNEXPECTED-FAIL | %s | application ran for longer than allowed maximum time of %d seconds", self.lastTestSeen, int(maxTime))
  388. self.log.error("Force-terminating active process(es).");
  389. self.killAndGetStack(proc.pid, utilityPath, debuggerInfo)
  390. if didTimeout:
  391. if line:
  392. self.log.info(line.rstrip().decode("UTF-8", "ignore"))
  393. self.log.info("TEST-UNEXPECTED-FAIL | %s | application timed out after %d seconds with no output", self.lastTestSeen, int(timeout))
  394. self.log.error("Force-terminating active process(es).");
  395. if browserProcessId == -1:
  396. browserProcessId = proc.pid
  397. self.killAndGetStack(browserProcessId, utilityPath, debuggerInfo)
  398. status = proc.wait()
  399. printstatus("Main app process", status)
  400. if status == 0:
  401. self.lastTestSeen = "Main app process exited normally"
  402. if status != 0 and not didTimeout and not hitMaxTime:
  403. self.log.info("TEST-UNEXPECTED-FAIL | %s | Exited with code %d during test run", self.lastTestSeen, status)
  404. return status
  405. def buildCommandLine(self, app, debuggerInfo, profileDir, testURL, extraArgs):
  406. """ build the application command line """
  407. cmd = os.path.abspath(app)
  408. if self.IS_MAC and os.path.exists(cmd + "-bin"):
  409. # Prefer 'app-bin' in case 'app' is a shell script.
  410. # We can remove this hack once bug 673899 etc are fixed.
  411. cmd += "-bin"
  412. args = []
  413. if debuggerInfo:
  414. args.extend(debuggerInfo.args)
  415. args.append(cmd)
  416. cmd = os.path.abspath(debuggerInfo.path)
  417. if self.IS_MAC:
  418. args.append("-foreground")
  419. if self.IS_CYGWIN:
  420. profileDirectory = commands.getoutput("cygpath -w \"" + profileDir + "/\"")
  421. else:
  422. profileDirectory = profileDir + "/"
  423. args.extend(("-no-remote", "-profile", profileDirectory))
  424. if testURL is not None:
  425. args.append((testURL))
  426. args.extend(extraArgs)
  427. return cmd, args
  428. def checkForZombies(self, processLog, utilityPath, debuggerInfo):
  429. """ Look for hung processes """
  430. if not os.path.exists(processLog):
  431. self.log.info('Automation Error: PID log not found: %s', processLog)
  432. # Whilst no hung process was found, the run should still display as a failure
  433. return True
  434. foundZombie = False
  435. self.log.info('INFO | zombiecheck | Reading PID log: %s', processLog)
  436. processList = []
  437. pidRE = re.compile(r'launched child process (\d+)$')
  438. processLogFD = open(processLog)
  439. for line in processLogFD:
  440. self.log.info(line.rstrip())
  441. m = pidRE.search(line)
  442. if m:
  443. processList.append(int(m.group(1)))
  444. processLogFD.close()
  445. for processPID in processList:
  446. self.log.info("INFO | zombiecheck | Checking for orphan process with PID: %d", processPID)
  447. if self.isPidAlive(processPID):
  448. foundZombie = True
  449. self.log.info("TEST-UNEXPECTED-FAIL | zombiecheck | child process %d still alive after shutdown", processPID)
  450. self.killAndGetStack(processPID, utilityPath, debuggerInfo)
  451. return foundZombie
  452. def checkForCrashes(self, minidumpDir, symbolsPath):
  453. return mozcrash.check_for_crashes(minidumpDir, symbolsPath, test_name=self.lastTestSeen)
  454. def runApp(self, testURL, env, app, profileDir, extraArgs, utilityPath = None,
  455. xrePath = None, certPath = None,
  456. debuggerInfo = None, symbolsPath = None,
  457. timeout = -1, maxTime = None, onLaunch = None,
  458. detectShutdownLeaks = False, screenshotOnFail=False, testPath=None, bisectChunk=None,
  459. valgrindPath=None, valgrindArgs=None, valgrindSuppFiles=None, outputHandler=None):
  460. """
  461. Run the app, log the duration it took to execute, return the status code.
  462. Kills the app if it runs for longer than |maxTime| seconds, or outputs nothing for |timeout| seconds.
  463. """
  464. if utilityPath == None:
  465. utilityPath = self.DIST_BIN
  466. if xrePath == None:
  467. xrePath = self.DIST_BIN
  468. if certPath == None:
  469. certPath = self.CERTS_SRC_DIR
  470. if timeout == -1:
  471. timeout = self.DEFAULT_TIMEOUT
  472. # copy env so we don't munge the caller's environment
  473. env = dict(env);
  474. env["NO_EM_RESTART"] = "1"
  475. tmpfd, processLog = tempfile.mkstemp(suffix='pidlog')
  476. os.close(tmpfd)
  477. env["MOZ_PROCESS_LOG"] = processLog
  478. cmd, args = self.buildCommandLine(app, debuggerInfo, profileDir, testURL, extraArgs)
  479. startTime = datetime.now()
  480. if debuggerInfo and debuggerInfo.interactive:
  481. # If an interactive debugger is attached, don't redirect output,
  482. # don't use timeouts, and don't capture ctrl-c.
  483. timeout = None
  484. maxTime = None
  485. outputPipe = None
  486. signal.signal(signal.SIGINT, lambda sigid, frame: None)
  487. else:
  488. outputPipe = subprocess.PIPE
  489. self.lastTestSeen = "automation.py"
  490. proc = self.Process([cmd] + args,
  491. env = self.environment(env, xrePath = xrePath,
  492. crashreporter = not debuggerInfo),
  493. stdout = outputPipe,
  494. stderr = subprocess.STDOUT)
  495. self.log.info("INFO | automation.py | Application pid: %d", proc.pid)
  496. if onLaunch is not None:
  497. # Allow callers to specify an onLaunch callback to be fired after the
  498. # app is launched.
  499. onLaunch()
  500. status = self.waitForFinish(proc, utilityPath, timeout, maxTime, startTime, debuggerInfo, symbolsPath,
  501. outputHandler=outputHandler)
  502. self.log.info("INFO | automation.py | Application ran for: %s", str(datetime.now() - startTime))
  503. # Do a final check for zombie child processes.
  504. zombieProcesses = self.checkForZombies(processLog, utilityPath, debuggerInfo)
  505. crashed = self.checkForCrashes(os.path.join(profileDir, "minidumps"), symbolsPath)
  506. if crashed or zombieProcesses:
  507. status = 1
  508. if os.path.exists(processLog):
  509. os.unlink(processLog)
  510. return status
  511. def elf_arm(self, filename):
  512. data = open(filename, 'rb').read(20)
  513. return data[:4] == "\x7fELF" and ord(data[18]) == 40 # EM_ARM