2012-04-27 11:44:59 -07:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
# You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2014-05-28 06:00:36 -07:00
|
|
|
import datetime
|
2013-03-18 07:01:06 -07:00
|
|
|
import mozcrash
|
2012-06-05 15:17:26 -07:00
|
|
|
import threading
|
2012-04-27 11:44:59 -07:00
|
|
|
import os
|
2014-05-22 12:47:52 -07:00
|
|
|
import posixpath
|
2012-06-05 15:17:26 -07:00
|
|
|
import Queue
|
2012-04-27 11:44:59 -07:00
|
|
|
import re
|
|
|
|
import shutil
|
2014-05-28 06:00:36 -07:00
|
|
|
import signal
|
2012-04-27 11:44:59 -07:00
|
|
|
import tempfile
|
|
|
|
import time
|
2013-03-26 06:50:00 -07:00
|
|
|
import traceback
|
2015-06-26 13:45:18 -07:00
|
|
|
import zipfile
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
from automation import Automation
|
2015-07-16 07:38:40 -07:00
|
|
|
from mozlog import get_default_logger
|
2012-06-05 15:17:26 -07:00
|
|
|
from mozprocess import ProcessHandlerMixin
|
|
|
|
|
|
|
|
|
2012-08-08 17:29:26 -07:00
|
|
|
class StdOutProc(ProcessHandlerMixin):
|
|
|
|
"""Process handler for b2g which puts all output in a Queue.
|
2012-06-05 15:17:26 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, cmd, queue, **kwargs):
|
|
|
|
self.queue = queue
|
|
|
|
kwargs.setdefault('processOutputLine', []).append(self.handle_output)
|
|
|
|
ProcessHandlerMixin.__init__(self, cmd, **kwargs)
|
|
|
|
|
|
|
|
def handle_output(self, line):
|
|
|
|
self.queue.put_nowait(line)
|
|
|
|
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
class B2GRemoteAutomation(Automation):
|
|
|
|
_devicemanager = None
|
|
|
|
|
|
|
|
def __init__(self, deviceManager, appName='', remoteLog=None,
|
2012-08-10 11:25:20 -07:00
|
|
|
marionette=None, context_chrome=True):
|
2012-04-27 11:44:59 -07:00
|
|
|
self._devicemanager = deviceManager
|
|
|
|
self._appName = appName
|
|
|
|
self._remoteProfile = None
|
|
|
|
self._remoteLog = remoteLog
|
|
|
|
self.marionette = marionette
|
2012-08-10 11:25:20 -07:00
|
|
|
self.context_chrome = context_chrome
|
2012-06-05 15:17:26 -07:00
|
|
|
self._is_emulator = False
|
2012-08-15 12:26:31 -07:00
|
|
|
self.test_script = None
|
|
|
|
self.test_script_args = None
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
# Default our product to b2g
|
|
|
|
self._product = "b2g"
|
2012-11-03 10:10:59 -07:00
|
|
|
self.lastTestSeen = "b2gautomation.py"
|
2012-08-10 11:25:20 -07:00
|
|
|
# Default log finish to mochitest standard
|
2013-01-04 10:41:34 -08:00
|
|
|
self.logFinish = 'INFO SimpleTest FINISHED'
|
2012-04-27 11:44:59 -07:00
|
|
|
Automation.__init__(self)
|
|
|
|
|
2012-06-05 15:17:26 -07:00
|
|
|
def setEmulator(self, is_emulator):
|
|
|
|
self._is_emulator = is_emulator
|
|
|
|
|
2012-04-27 11:44:59 -07:00
|
|
|
def setDeviceManager(self, deviceManager):
|
|
|
|
self._devicemanager = deviceManager
|
|
|
|
|
|
|
|
def setAppName(self, appName):
|
|
|
|
self._appName = appName
|
|
|
|
|
|
|
|
def setRemoteProfile(self, remoteProfile):
|
|
|
|
self._remoteProfile = remoteProfile
|
|
|
|
|
|
|
|
def setProduct(self, product):
|
|
|
|
self._product = product
|
|
|
|
|
|
|
|
def setRemoteLog(self, logfile):
|
|
|
|
self._remoteLog = logfile
|
|
|
|
|
2015-06-26 13:45:18 -07:00
|
|
|
def getExtensionIDFromRDF(self, rdfSource):
|
|
|
|
"""
|
|
|
|
Retrieves the extension id from an install.rdf file (or string).
|
|
|
|
"""
|
|
|
|
from xml.dom.minidom import parse, parseString, Node
|
|
|
|
|
|
|
|
if isinstance(rdfSource, file):
|
|
|
|
document = parse(rdfSource)
|
|
|
|
else:
|
|
|
|
document = parseString(rdfSource)
|
|
|
|
|
|
|
|
# Find the <em:id> element. There can be multiple <em:id> tags
|
|
|
|
# within <em:targetApplication> tags, so we have to check this way.
|
|
|
|
for rdfChild in document.documentElement.childNodes:
|
|
|
|
if rdfChild.nodeType == Node.ELEMENT_NODE and rdfChild.tagName == "Description":
|
|
|
|
for descChild in rdfChild.childNodes:
|
|
|
|
if descChild.nodeType == Node.ELEMENT_NODE and descChild.tagName == "em:id":
|
|
|
|
return descChild.childNodes[0].data
|
|
|
|
return None
|
|
|
|
|
2013-01-10 06:49:12 -08:00
|
|
|
def installExtension(self, extensionSource, profileDir, extensionID=None):
|
|
|
|
# Bug 827504 - installing special-powers extension separately causes problems in B2G
|
|
|
|
if extensionID != "special-powers@mozilla.org":
|
2015-06-26 13:45:18 -07:00
|
|
|
if not os.path.isdir(profileDir):
|
|
|
|
self.log.info("INFO | automation.py | Cannot install extension, invalid profileDir at: %s", profileDir)
|
|
|
|
return
|
|
|
|
|
|
|
|
installRDFFilename = "install.rdf"
|
|
|
|
|
|
|
|
extensionsRootDir = os.path.join(profileDir, "extensions", "staged")
|
|
|
|
if not os.path.isdir(extensionsRootDir):
|
|
|
|
os.makedirs(extensionsRootDir)
|
|
|
|
|
|
|
|
if os.path.isfile(extensionSource):
|
|
|
|
reader = zipfile.ZipFile(extensionSource, "r")
|
|
|
|
|
|
|
|
for filename in reader.namelist():
|
|
|
|
# Sanity check the zip file.
|
|
|
|
if os.path.isabs(filename):
|
|
|
|
self.log.info("INFO | automation.py | Cannot install extension, bad files in xpi")
|
|
|
|
return
|
|
|
|
|
|
|
|
# We may need to dig the extensionID out of the zip file...
|
|
|
|
if extensionID is None and filename == installRDFFilename:
|
|
|
|
extensionID = self.getExtensionIDFromRDF(reader.read(filename))
|
|
|
|
|
|
|
|
# We must know the extensionID now.
|
|
|
|
if extensionID is None:
|
|
|
|
self.log.info("INFO | automation.py | Cannot install extension, missing extensionID")
|
|
|
|
return
|
|
|
|
|
|
|
|
# Make the extension directory.
|
|
|
|
extensionDir = os.path.join(extensionsRootDir, extensionID)
|
|
|
|
os.mkdir(extensionDir)
|
|
|
|
|
|
|
|
# Extract all files.
|
|
|
|
reader.extractall(extensionDir)
|
|
|
|
|
|
|
|
elif os.path.isdir(extensionSource):
|
|
|
|
if extensionID is None:
|
|
|
|
filename = os.path.join(extensionSource, installRDFFilename)
|
|
|
|
if os.path.isfile(filename):
|
|
|
|
with open(filename, "r") as installRDF:
|
|
|
|
extensionID = self.getExtensionIDFromRDF(installRDF)
|
|
|
|
|
|
|
|
if extensionID is None:
|
|
|
|
self.log.info("INFO | automation.py | Cannot install extension, missing extensionID")
|
|
|
|
return
|
|
|
|
|
|
|
|
# Copy extension tree into its own directory.
|
|
|
|
# "destination directory must not already exist".
|
|
|
|
shutil.copytree(extensionSource, os.path.join(extensionsRootDir, extensionID))
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.log.info("INFO | automation.py | Cannot install extension, invalid extensionSource at: %s", extensionSource)
|
2013-01-10 06:49:12 -08:00
|
|
|
|
2012-04-27 11:44:59 -07:00
|
|
|
# Set up what we need for the remote environment
|
2014-03-07 12:38:48 -08:00
|
|
|
def environment(self, env=None, xrePath=None, crashreporter=True, debugger=False):
|
2012-04-27 11:44:59 -07:00
|
|
|
# Because we are running remote, we don't want to mimic the local env
|
|
|
|
# so no copying of os.environ
|
|
|
|
if env is None:
|
|
|
|
env = {}
|
|
|
|
|
2013-03-18 07:01:06 -07:00
|
|
|
if crashreporter:
|
|
|
|
env['MOZ_CRASHREPORTER'] = '1'
|
|
|
|
env['MOZ_CRASHREPORTER_NO_REPORT'] = '1'
|
|
|
|
|
2012-06-05 15:17:26 -07:00
|
|
|
# We always hide the results table in B2G; it's much slower if we don't.
|
|
|
|
env['MOZ_HIDE_RESULTS_TABLE'] = '1'
|
2012-04-27 11:44:59 -07:00
|
|
|
return env
|
|
|
|
|
2013-01-04 10:41:34 -08:00
|
|
|
def waitForNet(self):
|
2012-08-08 17:29:26 -07:00
|
|
|
active = False
|
|
|
|
time_out = 0
|
|
|
|
while not active and time_out < 40:
|
2012-10-02 15:43:19 -07:00
|
|
|
data = self._devicemanager._runCmd(['shell', '/system/bin/netcfg']).stdout.readlines()
|
2012-08-08 17:29:26 -07:00
|
|
|
data.pop(0)
|
|
|
|
for line in data:
|
|
|
|
if (re.search(r'UP\s+(?:[0-9]{1,3}\.){3}[0-9]{1,3}', line)):
|
|
|
|
active = True
|
|
|
|
break
|
|
|
|
time_out += 1
|
|
|
|
time.sleep(1)
|
|
|
|
return active
|
|
|
|
|
2012-04-27 11:44:59 -07:00
|
|
|
def checkForCrashes(self, directory, symbolsPath):
|
2013-03-18 07:01:06 -07:00
|
|
|
crashed = False
|
|
|
|
remote_dump_dir = self._remoteProfile + '/minidumps'
|
2013-03-26 06:50:00 -07:00
|
|
|
print "checking for crashes in '%s'" % remote_dump_dir
|
2013-03-18 07:01:06 -07:00
|
|
|
if self._devicemanager.dirExists(remote_dump_dir):
|
|
|
|
local_dump_dir = tempfile.mkdtemp()
|
|
|
|
self._devicemanager.getDirectory(remote_dump_dir, local_dump_dir)
|
2013-02-08 08:32:07 -08:00
|
|
|
try:
|
2014-09-23 17:33:31 -07:00
|
|
|
logger = get_default_logger()
|
|
|
|
if logger is not None:
|
|
|
|
crashed = mozcrash.log_crashes(logger, local_dump_dir, symbolsPath, test=self.lastTestSeen)
|
|
|
|
else:
|
|
|
|
crashed = mozcrash.check_for_crashes(local_dump_dir, symbolsPath, test_name=self.lastTestSeen)
|
2013-03-26 06:50:00 -07:00
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
2013-03-18 07:01:06 -07:00
|
|
|
finally:
|
|
|
|
shutil.rmtree(local_dump_dir)
|
|
|
|
self._devicemanager.removeDir(remote_dump_dir)
|
2012-11-20 07:24:28 -08:00
|
|
|
return crashed
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
def buildCommandLine(self, app, debuggerInfo, profileDir, testURL, extraArgs):
|
|
|
|
# if remote profile is specified, use that instead
|
|
|
|
if (self._remoteProfile):
|
|
|
|
profileDir = self._remoteProfile
|
|
|
|
|
|
|
|
cmd, args = Automation.buildCommandLine(self, app, debuggerInfo, profileDir, testURL, extraArgs)
|
|
|
|
|
|
|
|
return app, args
|
|
|
|
|
|
|
|
def waitForFinish(self, proc, utilityPath, timeout, maxTime, startTime,
|
2016-02-05 12:44:20 -08:00
|
|
|
debuggerInfo, symbolsPath, outputHandler=None):
|
2012-08-10 11:25:20 -07:00
|
|
|
""" Wait for tests to finish (as evidenced by a signature string
|
2012-04-27 11:44:59 -07:00
|
|
|
in logcat), or for a given amount of time to elapse with no
|
|
|
|
output.
|
|
|
|
"""
|
|
|
|
timeout = timeout or 120
|
|
|
|
while True:
|
2016-02-05 12:44:20 -08:00
|
|
|
lines = proc.getStdoutLines(timeout)
|
|
|
|
if lines:
|
|
|
|
currentlog = '\n'.join(lines)
|
|
|
|
|
|
|
|
if outputHandler:
|
|
|
|
for line in lines:
|
|
|
|
outputHandler(line)
|
|
|
|
else:
|
|
|
|
print(currentlog)
|
|
|
|
|
2012-11-03 10:10:59 -07:00
|
|
|
# Match the test filepath from the last TEST-START line found in the new
|
|
|
|
# log content. These lines are in the form:
|
|
|
|
# ... INFO TEST-START | /filepath/we/wish/to/capture.html\n
|
|
|
|
testStartFilenames = re.findall(r"TEST-START \| ([^\s]*)", currentlog)
|
|
|
|
if testStartFilenames:
|
|
|
|
self.lastTestSeen = testStartFilenames[-1]
|
2016-02-05 12:44:20 -08:00
|
|
|
if (outputHandler and outputHandler.suite_finished) or (
|
|
|
|
hasattr(self, 'logFinish') and self.logFinish in currentlog):
|
2012-04-27 11:44:59 -07:00
|
|
|
return 0
|
|
|
|
else:
|
2014-02-11 10:43:33 -08:00
|
|
|
self.log.info("TEST-UNEXPECTED-FAIL | %s | application timed "
|
|
|
|
"out after %d seconds with no output",
|
|
|
|
self.lastTestSeen, int(timeout))
|
2014-05-28 06:00:36 -07:00
|
|
|
self._devicemanager.killProcess('/system/b2g/b2g', sig=signal.SIGABRT)
|
|
|
|
|
|
|
|
timeout = 10 # seconds
|
|
|
|
starttime = datetime.datetime.now()
|
|
|
|
while datetime.datetime.now() - starttime < datetime.timedelta(seconds=timeout):
|
|
|
|
if not self._devicemanager.processExist('/system/b2g/b2g'):
|
|
|
|
break
|
|
|
|
time.sleep(1)
|
|
|
|
else:
|
|
|
|
print "timed out after %d seconds waiting for b2g process to exit" % timeout
|
|
|
|
return 1
|
|
|
|
|
|
|
|
self.checkForCrashes(None, symbolsPath)
|
2014-02-11 10:43:33 -08:00
|
|
|
return 1
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
def getDeviceStatus(self, serial=None):
|
|
|
|
# Get the current status of the device. If we know the device
|
|
|
|
# serial number, we look for that, otherwise we use the (presumably
|
|
|
|
# only) device shown in 'adb devices'.
|
2013-02-21 11:51:36 -08:00
|
|
|
serial = serial or self._devicemanager._deviceSerial
|
2012-04-27 11:44:59 -07:00
|
|
|
status = 'unknown'
|
|
|
|
|
2012-10-02 15:43:19 -07:00
|
|
|
for line in self._devicemanager._runCmd(['devices']).stdout.readlines():
|
2013-01-04 10:41:34 -08:00
|
|
|
result = re.match('(.*?)\t(.*)', line)
|
2012-04-27 11:44:59 -07:00
|
|
|
if result:
|
|
|
|
thisSerial = result.group(1)
|
|
|
|
if not serial or thisSerial == serial:
|
|
|
|
serial = thisSerial
|
|
|
|
status = result.group(2)
|
|
|
|
|
|
|
|
return (serial, status)
|
|
|
|
|
2012-06-05 15:17:26 -07:00
|
|
|
def restartB2G(self):
|
2012-08-10 11:25:20 -07:00
|
|
|
# TODO hangs in subprocess.Popen without this delay
|
|
|
|
time.sleep(5)
|
2012-10-05 13:06:02 -07:00
|
|
|
self._devicemanager._checkCmd(['shell', 'stop', 'b2g'])
|
2012-06-05 15:17:26 -07:00
|
|
|
# Wait for a bit to make sure B2G has completely shut down.
|
2012-06-18 10:43:59 -07:00
|
|
|
time.sleep(10)
|
2012-10-05 13:06:02 -07:00
|
|
|
self._devicemanager._checkCmd(['shell', 'start', 'b2g'])
|
2012-06-05 15:17:26 -07:00
|
|
|
if self._is_emulator:
|
2014-06-19 11:17:26 -07:00
|
|
|
self.marionette.emulator.wait_for_port(self.marionette.port)
|
2012-06-05 15:17:26 -07:00
|
|
|
|
2012-04-27 11:44:59 -07:00
|
|
|
def rebootDevice(self):
|
|
|
|
# find device's current status and serial number
|
|
|
|
serial, status = self.getDeviceStatus()
|
|
|
|
|
|
|
|
# reboot!
|
2012-10-02 15:43:19 -07:00
|
|
|
self._devicemanager._runCmd(['shell', '/system/bin/reboot'])
|
2012-08-08 17:29:26 -07:00
|
|
|
|
|
|
|
# The above command can return while adb still thinks the device is
|
|
|
|
# connected, so wait a little bit for it to disconnect from adb.
|
|
|
|
time.sleep(10)
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
# wait for device to come back to previous status
|
|
|
|
print 'waiting for device to come back online after reboot'
|
|
|
|
start = time.time()
|
|
|
|
rserial, rstatus = self.getDeviceStatus(serial)
|
|
|
|
while rstatus != 'device':
|
|
|
|
if time.time() - start > 120:
|
|
|
|
# device hasn't come back online in 2 minutes, something's wrong
|
|
|
|
raise Exception("Device %s (status: %s) not back online after reboot" % (serial, rstatus))
|
|
|
|
time.sleep(5)
|
|
|
|
rserial, rstatus = self.getDeviceStatus(serial)
|
|
|
|
print 'device:', serial, 'status:', rstatus
|
|
|
|
|
|
|
|
def Process(self, cmd, stdout=None, stderr=None, env=None, cwd=None):
|
|
|
|
# On a desktop or fennec run, the Process method invokes a gecko
|
2012-08-10 11:25:20 -07:00
|
|
|
# process in which to the tests. For B2G, we simply
|
2012-04-27 11:44:59 -07:00
|
|
|
# reboot the device (which was configured with a test profile
|
|
|
|
# already), wait for B2G to start up, and then navigate to the
|
|
|
|
# test url using Marionette. There doesn't seem to be any way
|
2012-08-10 11:25:20 -07:00
|
|
|
# to pass env variables into the B2G process, but this doesn't
|
2012-04-27 11:44:59 -07:00
|
|
|
# seem to matter.
|
|
|
|
|
|
|
|
# reboot device so it starts up with the mochitest profile
|
|
|
|
# XXX: We could potentially use 'stop b2g' + 'start b2g' to achieve
|
|
|
|
# a similar effect; will see which is more stable while attempting
|
|
|
|
# to bring up the continuous integration.
|
2012-08-08 17:29:26 -07:00
|
|
|
if not self._is_emulator:
|
2012-06-05 15:17:26 -07:00
|
|
|
self.rebootDevice()
|
2012-08-08 17:29:26 -07:00
|
|
|
time.sleep(5)
|
2013-01-04 10:41:34 -08:00
|
|
|
#wait for wlan to come up
|
2012-08-08 17:29:26 -07:00
|
|
|
if not self.waitForNet():
|
2013-01-04 10:41:34 -08:00
|
|
|
raise Exception("network did not come up, please configure the network" +
|
2012-08-08 17:29:26 -07:00
|
|
|
" prior to running before running the automation framework")
|
|
|
|
|
|
|
|
# stop b2g
|
2012-10-02 15:43:19 -07:00
|
|
|
self._devicemanager._runCmd(['shell', 'stop', 'b2g'])
|
2012-08-08 17:29:26 -07:00
|
|
|
time.sleep(5)
|
2012-04-27 11:44:59 -07:00
|
|
|
|
2014-05-22 12:47:52 -07:00
|
|
|
# For some reason user.js in the profile doesn't get picked up.
|
|
|
|
# Manually copy it over to prefs.js. See bug 1009730 for more details.
|
|
|
|
self._devicemanager.moveTree(posixpath.join(self._remoteProfile, 'user.js'),
|
|
|
|
posixpath.join(self._remoteProfile, 'prefs.js'))
|
|
|
|
|
2012-08-08 17:29:26 -07:00
|
|
|
# relaunch b2g inside b2g instance
|
2013-03-06 13:53:48 -08:00
|
|
|
instance = self.B2GInstance(self._devicemanager, env=env)
|
2012-08-08 17:29:26 -07:00
|
|
|
|
|
|
|
time.sleep(5)
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
# Set up port forwarding again for Marionette, since any that
|
|
|
|
# existed previously got wiped out by the reboot.
|
2012-06-05 15:17:26 -07:00
|
|
|
if not self._is_emulator:
|
2012-10-05 13:06:02 -07:00
|
|
|
self._devicemanager._checkCmd(['forward',
|
|
|
|
'tcp:%s' % self.marionette.port,
|
|
|
|
'tcp:%s' % self.marionette.port])
|
2012-04-27 11:44:59 -07:00
|
|
|
|
2012-08-09 12:04:28 -07:00
|
|
|
if self._is_emulator:
|
2014-06-19 11:17:26 -07:00
|
|
|
self.marionette.emulator.wait_for_port(self.marionette.port)
|
2012-08-09 12:04:28 -07:00
|
|
|
else:
|
|
|
|
time.sleep(5)
|
2012-08-08 17:29:26 -07:00
|
|
|
|
2012-04-27 11:44:59 -07:00
|
|
|
# start a marionette session
|
|
|
|
session = self.marionette.start_session()
|
|
|
|
if 'b2g' not in session:
|
|
|
|
raise Exception("bad session value %s returned by start_session" % session)
|
|
|
|
|
2015-05-18 16:36:15 -07:00
|
|
|
self.marionette.set_context(self.marionette.CONTEXT_CHROME)
|
|
|
|
self.marionette.execute_script("""
|
|
|
|
let SECURITY_PREF = "security.turn_off_all_security_so_that_viruses_can_take_over_this_computer";
|
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
Services.prefs.setBoolPref(SECURITY_PREF, true);
|
|
|
|
|
|
|
|
if (!testUtils.hasOwnProperty("specialPowersObserver")) {
|
|
|
|
let loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
|
|
|
|
.getService(Components.interfaces.mozIJSSubScriptLoader);
|
2015-11-05 07:00:59 -08:00
|
|
|
loader.loadSubScript("chrome://specialpowers/content/SpecialPowersObserver.jsm",
|
2015-05-18 16:36:15 -07:00
|
|
|
testUtils);
|
|
|
|
testUtils.specialPowersObserver = new testUtils.SpecialPowersObserver();
|
|
|
|
testUtils.specialPowersObserver.init();
|
|
|
|
}
|
|
|
|
""")
|
|
|
|
|
|
|
|
if not self.context_chrome:
|
2012-10-31 06:37:22 -07:00
|
|
|
self.marionette.set_context(self.marionette.CONTEXT_CONTENT)
|
2012-08-10 11:25:20 -07:00
|
|
|
|
|
|
|
# run the script that starts the tests
|
2012-10-31 06:37:22 -07:00
|
|
|
if self.test_script:
|
2012-08-15 12:26:31 -07:00
|
|
|
if os.path.isfile(self.test_script):
|
|
|
|
script = open(self.test_script, 'r')
|
|
|
|
self.marionette.execute_script(script.read(), script_args=self.test_script_args)
|
2012-08-10 11:25:20 -07:00
|
|
|
script.close()
|
2012-10-31 06:37:22 -07:00
|
|
|
elif isinstance(self.test_script, basestring):
|
2012-08-15 12:26:31 -07:00
|
|
|
self.marionette.execute_script(self.test_script, script_args=self.test_script_args)
|
2012-08-10 11:25:20 -07:00
|
|
|
else:
|
|
|
|
# assumes the tests are started on startup automatically
|
|
|
|
pass
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
return instance
|
|
|
|
|
|
|
|
# be careful here as this inner class doesn't have access to outer class members
|
|
|
|
class B2GInstance(object):
|
|
|
|
"""Represents a B2G instance running on a device, and exposes
|
|
|
|
some process-like methods/properties that are expected by the
|
|
|
|
automation.
|
|
|
|
"""
|
|
|
|
|
2013-03-06 13:53:48 -08:00
|
|
|
def __init__(self, dm, env=None):
|
2012-04-27 11:44:59 -07:00
|
|
|
self.dm = dm
|
2013-03-06 13:53:48 -08:00
|
|
|
self.env = env or {}
|
2012-08-08 17:29:26 -07:00
|
|
|
self.stdout_proc = None
|
2012-06-05 15:17:26 -07:00
|
|
|
self.queue = Queue.Queue()
|
|
|
|
|
2012-08-08 17:29:26 -07:00
|
|
|
# Launch b2g in a separate thread, and dump all output lines
|
2012-06-05 15:17:26 -07:00
|
|
|
# into a queue. The lines in this queue are
|
|
|
|
# retrieved and returned by accessing the stdout property of
|
|
|
|
# this class.
|
2012-11-14 09:49:04 -08:00
|
|
|
cmd = [self.dm._adbPath]
|
|
|
|
if self.dm._deviceSerial:
|
|
|
|
cmd.extend(['-s', self.dm._deviceSerial])
|
2012-08-08 17:29:26 -07:00
|
|
|
cmd.append('shell')
|
2013-03-06 13:53:48 -08:00
|
|
|
for k, v in self.env.iteritems():
|
|
|
|
cmd.append("%s=%s" % (k, v))
|
2012-08-08 17:29:26 -07:00
|
|
|
cmd.append('/system/bin/b2g.sh')
|
|
|
|
proc = threading.Thread(target=self._save_stdout_proc, args=(cmd, self.queue))
|
2012-06-05 15:17:26 -07:00
|
|
|
proc.daemon = True
|
|
|
|
proc.start()
|
|
|
|
|
2012-08-08 17:29:26 -07:00
|
|
|
def _save_stdout_proc(self, cmd, queue):
|
|
|
|
self.stdout_proc = StdOutProc(cmd, queue)
|
|
|
|
self.stdout_proc.run()
|
|
|
|
if hasattr(self.stdout_proc, 'processOutput'):
|
|
|
|
self.stdout_proc.processOutput()
|
2013-03-28 13:42:49 -07:00
|
|
|
self.stdout_proc.wait()
|
2012-08-08 17:29:26 -07:00
|
|
|
self.stdout_proc = None
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def pid(self):
|
|
|
|
# a dummy value to make the automation happy
|
|
|
|
return 0
|
|
|
|
|
2014-02-11 10:43:33 -08:00
|
|
|
def getStdoutLines(self, timeout):
|
2012-06-05 15:17:26 -07:00
|
|
|
# Return any lines in the queue used by the
|
2012-08-08 17:29:26 -07:00
|
|
|
# b2g process handler.
|
2012-06-05 15:17:26 -07:00
|
|
|
lines = []
|
2014-02-11 10:43:33 -08:00
|
|
|
# get all of the lines that are currently available
|
2012-06-05 15:17:26 -07:00
|
|
|
while True:
|
2012-04-27 11:44:59 -07:00
|
|
|
try:
|
2012-06-05 15:17:26 -07:00
|
|
|
lines.append(self.queue.get_nowait())
|
|
|
|
except Queue.Empty:
|
2012-04-27 11:44:59 -07:00
|
|
|
break
|
2014-02-11 10:43:33 -08:00
|
|
|
|
|
|
|
# wait 'timeout' for any additional lines
|
2016-02-05 12:44:20 -08:00
|
|
|
if not lines:
|
|
|
|
try:
|
|
|
|
lines.append(self.queue.get(True, timeout))
|
|
|
|
except Queue.Empty:
|
|
|
|
pass
|
|
|
|
return lines
|
2012-04-27 11:44:59 -07:00
|
|
|
|
2013-01-04 10:41:34 -08:00
|
|
|
def wait(self, timeout=None):
|
2012-04-27 11:44:59 -07:00
|
|
|
# this should never happen
|
|
|
|
raise Exception("'wait' called on B2GInstance")
|
|
|
|
|
|
|
|
def kill(self):
|
|
|
|
# this should never happen
|
|
|
|
raise Exception("'kill' called on B2GInstance")
|
2013-01-04 10:41:34 -08:00
|
|
|
|