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/.
|
|
|
|
|
2013-10-01 07:32:58 -07:00
|
|
|
import json
|
2012-04-27 11:44:59 -07:00
|
|
|
import os
|
2013-07-26 11:40:04 -07:00
|
|
|
import posixpath
|
2013-01-04 10:41:34 -08:00
|
|
|
import shutil
|
2012-04-27 11:44:59 -07:00
|
|
|
import sys
|
|
|
|
import tempfile
|
2013-01-04 10:41:34 -08:00
|
|
|
import threading
|
2012-06-15 14:13:04 -07:00
|
|
|
import traceback
|
2012-04-27 11:44:59 -07:00
|
|
|
|
2013-08-26 14:17:51 -07:00
|
|
|
here = os.path.abspath(os.path.dirname(__file__))
|
2013-03-28 13:42:49 -07:00
|
|
|
sys.path.insert(0, here)
|
2012-04-27 11:44:59 -07:00
|
|
|
|
2013-08-14 10:57:43 -07:00
|
|
|
from runtests import Mochitest
|
2013-07-26 11:40:04 -07:00
|
|
|
from runtests import MochitestUtilsMixin
|
2012-04-27 11:44:59 -07:00
|
|
|
from runtests import MochitestOptions
|
|
|
|
from runtests import MochitestServer
|
2013-07-26 11:40:04 -07:00
|
|
|
from mochitest_options import B2GOptions, MochitestOptions
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
from marionette import Marionette
|
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
from mozdevice import DeviceManagerADB
|
2013-08-14 10:57:43 -07:00
|
|
|
from mozprofile import Profile, Preferences
|
2013-07-26 11:40:04 -07:00
|
|
|
from mozrunner import B2GRunner
|
|
|
|
import mozlog
|
|
|
|
import mozinfo
|
|
|
|
import moznetwork
|
|
|
|
|
|
|
|
log = mozlog.getLogger('Mochitest')
|
|
|
|
|
|
|
|
class B2GMochitest(MochitestUtilsMixin):
|
|
|
|
def __init__(self, marionette,
|
|
|
|
out_of_process=True,
|
|
|
|
profile_data_dir=None,
|
|
|
|
locations=os.path.join(here, 'server-locations.txt')):
|
|
|
|
super(B2GMochitest, self).__init__()
|
|
|
|
self.marionette = marionette
|
|
|
|
self.out_of_process = out_of_process
|
2013-03-28 13:42:49 -07:00
|
|
|
self.locations = locations
|
|
|
|
self.preferences = []
|
|
|
|
self.webapps = None
|
2013-07-26 11:40:04 -07:00
|
|
|
self.test_script = os.path.join(here, 'b2g_start_script.js')
|
|
|
|
self.test_script_args = [self.out_of_process]
|
|
|
|
self.product = 'b2g'
|
2013-03-28 13:42:49 -07:00
|
|
|
|
|
|
|
if profile_data_dir:
|
|
|
|
self.preferences = [os.path.join(profile_data_dir, f)
|
|
|
|
for f in os.listdir(profile_data_dir) if f.startswith('pref')]
|
|
|
|
self.webapps = [os.path.join(profile_data_dir, f)
|
|
|
|
for f in os.listdir(profile_data_dir) if f.startswith('webapp')]
|
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
# mozinfo is populated by the parent class
|
|
|
|
if mozinfo.info['debug']:
|
|
|
|
self.SERVER_STARTUP_TIMEOUT = 180
|
2013-01-04 10:41:34 -08:00
|
|
|
else:
|
2013-07-26 11:40:04 -07:00
|
|
|
self.SERVER_STARTUP_TIMEOUT = 90
|
2013-03-22 08:41:07 -07:00
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
def setup_common_options(self, options):
|
|
|
|
test_url = self.buildTestPath(options)
|
|
|
|
if len(self.urlOpts) > 0:
|
|
|
|
test_url += "?" + "&".join(self.urlOpts)
|
|
|
|
self.test_script_args.append(test_url)
|
2013-01-04 10:41:34 -08:00
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
def build_profile(self, options):
|
2013-03-28 13:42:49 -07:00
|
|
|
# preferences
|
|
|
|
prefs = {}
|
|
|
|
for path in self.preferences:
|
|
|
|
prefs.update(Preferences.read_prefs(path))
|
|
|
|
|
|
|
|
for v in options.extraPrefs:
|
|
|
|
thispref = v.split("=", 1)
|
|
|
|
if len(thispref) < 2:
|
|
|
|
print "Error: syntax error in --setpref=" + v
|
|
|
|
sys.exit(1)
|
|
|
|
prefs[thispref[0]] = thispref[1]
|
|
|
|
|
|
|
|
# interpolate the preferences
|
|
|
|
interpolation = { "server": "%s:%s" % (options.webServer, options.httpPort),
|
2013-07-26 11:40:04 -07:00
|
|
|
"OOP": "true" if self.out_of_process else "false" }
|
2013-03-28 13:42:49 -07:00
|
|
|
prefs = json.loads(json.dumps(prefs) % interpolation)
|
2013-04-09 17:31:42 -07:00
|
|
|
for pref in prefs:
|
|
|
|
prefs[pref] = Preferences.cast(prefs[pref])
|
2013-03-28 13:42:49 -07:00
|
|
|
|
2013-04-09 17:13:32 -07:00
|
|
|
kwargs = {
|
|
|
|
'addons': self.getExtensionsToInstall(options),
|
|
|
|
'apps': self.webapps,
|
|
|
|
'locations': self.locations,
|
|
|
|
'preferences': prefs,
|
|
|
|
'proxy': {"remote": options.webServer}
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.profile:
|
|
|
|
self.profile = Profile.clone(options.profile, **kwargs)
|
|
|
|
else:
|
|
|
|
self.profile = Profile(**kwargs)
|
2013-03-28 13:42:49 -07:00
|
|
|
|
|
|
|
options.profilePath = self.profile.profile
|
|
|
|
# TODO bug 839108 - mozprofile should probably handle this
|
|
|
|
manifest = self.addChromeToProfile(options)
|
|
|
|
self.copyExtraFilesToProfile(options)
|
|
|
|
return manifest
|
2013-01-04 10:41:34 -08:00
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
def run_tests(self, options):
|
|
|
|
""" Prepare, configure, run tests and cleanup """
|
2013-01-04 10:41:34 -08:00
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
self.leak_report_file = os.path.join(options.profilePath, "runtests_leaks.log")
|
|
|
|
manifest = self.build_profile(options)
|
2013-01-03 17:37:26 -08:00
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
self.startWebServer(options)
|
|
|
|
self.startWebSocketServer(options, None)
|
|
|
|
self.buildURLOptions(options, {'MOZ_HIDE_RESULTS_TABLE': '1'})
|
|
|
|
|
|
|
|
if options.timeout:
|
|
|
|
timeout = options.timeout + 30
|
|
|
|
elif options.debugger or not options.autorun:
|
|
|
|
timeout = None
|
|
|
|
else:
|
|
|
|
timeout = 330.0 # default JS harness timeout is 300 seconds
|
2012-04-27 11:44:59 -07:00
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
log.info("runtestsb2g.py | Running tests: start.")
|
|
|
|
status = 0
|
|
|
|
try:
|
|
|
|
runner_args = { 'profile': self.profile,
|
|
|
|
'devicemanager': self._dm,
|
|
|
|
'marionette': self.marionette,
|
|
|
|
'remote_test_root': self.remote_test_root,
|
|
|
|
'test_script': self.test_script,
|
|
|
|
'test_script_args': self.test_script_args }
|
|
|
|
self.runner = B2GRunner(**runner_args)
|
|
|
|
self.runner.start(outputTimeout=timeout)
|
2013-09-30 08:05:46 -07:00
|
|
|
status = self.runner.wait()
|
|
|
|
if status is None:
|
|
|
|
# the runner has timed out
|
|
|
|
status = 124
|
2013-07-26 11:40:04 -07:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
log.info("runtests.py | Received keyboard interrupt.\n");
|
|
|
|
status = -1
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
2013-09-19 05:20:17 -07:00
|
|
|
log.error("Automation Error: Received unexpected exception while running application\n")
|
2013-07-26 11:40:04 -07:00
|
|
|
status = 1
|
|
|
|
|
|
|
|
self.stopWebServer(options)
|
|
|
|
self.stopWebSocketServer(options)
|
|
|
|
|
|
|
|
log.info("runtestsb2g.py | Running tests: end.")
|
|
|
|
|
|
|
|
if manifest is not None:
|
|
|
|
self.cleanup(manifest, options)
|
|
|
|
return status
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
|
2013-03-28 13:42:49 -07:00
|
|
|
class B2GDeviceMochitest(B2GMochitest):
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
_dm = None
|
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
def __init__(self, marionette, devicemanager, profile_data_dir,
|
|
|
|
local_binary_dir, remote_test_root=None, remote_log_file=None):
|
|
|
|
B2GMochitest.__init__(self, marionette, out_of_process=True, profile_data_dir=profile_data_dir)
|
|
|
|
self._dm = devicemanager
|
|
|
|
self.remote_test_root = remote_test_root or self._dm.getDeviceRoot()
|
|
|
|
self.remote_profile = posixpath.join(self.remote_test_root, 'profile')
|
|
|
|
self.remote_log = remote_log_file or posixpath.join(self.remote_test_root, 'log', 'mochitest.log')
|
|
|
|
self.local_log = None
|
|
|
|
self.local_binary_dir = local_binary_dir
|
2012-08-08 17:29:26 -07:00
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
if not self._dm.dirExists(posixpath.dirname(self.remote_log)):
|
|
|
|
self._dm.mkDirs(self.remote_log)
|
2012-06-05 15:17:26 -07:00
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
def cleanup(self, manifest, options):
|
|
|
|
if self.local_log:
|
|
|
|
self._dm.getFile(self.remote_log, self.local_log)
|
|
|
|
self._dm.removeFile(self.remote_log)
|
2012-06-05 15:17:26 -07:00
|
|
|
|
2012-04-27 11:44:59 -07:00
|
|
|
if options.pidFile != "":
|
|
|
|
try:
|
|
|
|
os.remove(options.pidFile)
|
|
|
|
os.remove(options.pidFile + ".xpcshell.pid")
|
|
|
|
except:
|
|
|
|
print "Warning: cleaning up pidfile '%s' was unsuccessful from the test harness" % options.pidFile
|
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
# stop and clean up the runner
|
|
|
|
if getattr(self, 'runner', False):
|
|
|
|
self.runner.cleanup()
|
|
|
|
self.runner = None
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
def startWebServer(self, options):
|
|
|
|
""" Create the webserver on the host and start it up """
|
2013-07-26 11:40:04 -07:00
|
|
|
d = vars(options).copy()
|
|
|
|
d['xrePath'] = self.local_binary_dir
|
|
|
|
d['utilityPath'] = self.local_binary_dir
|
|
|
|
d['profilePath'] = tempfile.mkdtemp()
|
|
|
|
if d.get('httpdPath') is None:
|
|
|
|
d['httpdPath'] = os.path.abspath(os.path.join(self.local_binary_dir, 'components'))
|
2013-09-23 07:47:48 -07:00
|
|
|
self.server = MochitestServer(d)
|
2012-04-27 11:44:59 -07:00
|
|
|
self.server.start()
|
|
|
|
|
|
|
|
if (options.pidFile != ""):
|
|
|
|
f = open(options.pidFile + ".xpcshell.pid", 'w')
|
|
|
|
f.write("%s" % self.server._process.pid)
|
|
|
|
f.close()
|
2013-07-26 11:40:04 -07:00
|
|
|
self.server.ensureReady(90)
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
def stopWebServer(self, options):
|
2012-06-15 14:13:04 -07:00
|
|
|
if hasattr(self, 'server'):
|
|
|
|
self.server.stop()
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
def buildURLOptions(self, options, env):
|
2013-07-26 11:40:04 -07:00
|
|
|
self.local_log = options.logFile
|
|
|
|
options.logFile = self.remote_log
|
2013-03-28 13:42:49 -07:00
|
|
|
options.profilePath = self.profile.profile
|
2013-07-26 11:40:04 -07:00
|
|
|
retVal = super(B2GDeviceMochitest, self).buildURLOptions(options, env)
|
2012-04-27 11:44:59 -07:00
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
self.setup_common_options(options)
|
2012-04-27 11:44:59 -07:00
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
options.profilePath = self.remote_profile
|
|
|
|
options.logFile = self.local_log
|
2012-04-27 11:44:59 -07:00
|
|
|
return retVal
|
|
|
|
|
|
|
|
|
2013-08-14 10:57:43 -07:00
|
|
|
class B2GDesktopMochitest(B2GMochitest, Mochitest):
|
2013-01-04 10:41:34 -08:00
|
|
|
|
2013-09-26 06:58:08 -07:00
|
|
|
def __init__(self, marionette, profile_data_dir):
|
2013-08-14 10:57:43 -07:00
|
|
|
B2GMochitest.__init__(self, marionette, out_of_process=False, profile_data_dir=profile_data_dir)
|
2013-09-26 06:58:08 -07:00
|
|
|
Mochitest.__init__(self)
|
2013-01-04 10:41:34 -08:00
|
|
|
|
2013-08-14 10:57:43 -07:00
|
|
|
def runMarionetteScript(self, marionette, test_script, test_script_args):
|
2013-01-04 10:41:34 -08:00
|
|
|
assert(marionette.wait_for_port())
|
|
|
|
marionette.start_session()
|
|
|
|
marionette.set_context(marionette.CONTEXT_CHROME)
|
2013-08-14 10:57:43 -07:00
|
|
|
|
|
|
|
if os.path.isfile(test_script):
|
|
|
|
f = open(test_script, 'r')
|
|
|
|
test_script = f.read()
|
|
|
|
f.close()
|
|
|
|
self.marionette.execute_script(test_script,
|
|
|
|
script_args=test_script_args)
|
2013-01-04 10:41:34 -08:00
|
|
|
|
|
|
|
def startTests(self):
|
|
|
|
# This is run in a separate thread because otherwise, the app's
|
|
|
|
# stdout buffer gets filled (which gets drained only after this
|
|
|
|
# function returns, by waitForFinish), which causes the app to hang.
|
|
|
|
thread = threading.Thread(target=self.runMarionetteScript,
|
2013-07-26 11:40:04 -07:00
|
|
|
args=(self.marionette,
|
2013-08-14 10:57:43 -07:00
|
|
|
self.test_script,
|
|
|
|
self.test_script_args))
|
2013-01-04 10:41:34 -08:00
|
|
|
thread.start()
|
|
|
|
|
|
|
|
def buildURLOptions(self, options, env):
|
2013-07-26 11:40:04 -07:00
|
|
|
retVal = super(B2GDesktopMochitest, self).buildURLOptions(options, env)
|
2013-01-04 10:41:34 -08:00
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
self.setup_common_options(options)
|
2013-01-04 10:41:34 -08:00
|
|
|
|
|
|
|
# Copy the extensions to the B2G bundles dir.
|
|
|
|
extensionDir = os.path.join(options.profilePath, 'extensions', 'staged')
|
|
|
|
bundlesDir = os.path.join(os.path.dirname(options.app),
|
|
|
|
'distribution', 'bundles')
|
|
|
|
|
|
|
|
for filename in os.listdir(extensionDir):
|
|
|
|
shutil.rmtree(os.path.join(bundlesDir, filename), True)
|
|
|
|
shutil.copytree(os.path.join(extensionDir, filename),
|
|
|
|
os.path.join(bundlesDir, filename))
|
|
|
|
|
|
|
|
return retVal
|
|
|
|
|
2013-08-14 10:57:43 -07:00
|
|
|
def buildProfile(self, options):
|
|
|
|
return self.build_profile(options)
|
|
|
|
|
2013-01-04 10:41:34 -08:00
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
def run_remote_mochitests(parser, options):
|
2012-04-27 11:44:59 -07:00
|
|
|
# create our Marionette instance
|
2012-06-05 15:17:26 -07:00
|
|
|
kwargs = {}
|
|
|
|
if options.emulator:
|
|
|
|
kwargs['emulator'] = options.emulator
|
2012-06-08 14:09:14 -07:00
|
|
|
if options.noWindow:
|
|
|
|
kwargs['noWindow'] = True
|
2012-09-28 10:04:03 -07:00
|
|
|
if options.geckoPath:
|
|
|
|
kwargs['gecko_path'] = options.geckoPath
|
2012-11-19 12:52:40 -08:00
|
|
|
if options.logcat_dir:
|
|
|
|
kwargs['logcat_dir'] = options.logcat_dir
|
2013-01-07 07:29:43 -08:00
|
|
|
if options.busybox:
|
|
|
|
kwargs['busybox'] = options.busybox
|
2013-03-26 06:50:00 -07:00
|
|
|
if options.symbolsPath:
|
|
|
|
kwargs['symbols_path'] = options.symbolsPath
|
2012-09-17 18:44:07 -07:00
|
|
|
# needless to say sdcard is only valid if using an emulator
|
|
|
|
if options.sdcard:
|
|
|
|
kwargs['sdcard'] = options.sdcard
|
2012-04-27 11:44:59 -07:00
|
|
|
if options.b2gPath:
|
|
|
|
kwargs['homedir'] = options.b2gPath
|
|
|
|
if options.marionette:
|
2013-01-04 10:41:34 -08:00
|
|
|
host, port = options.marionette.split(':')
|
2012-04-27 11:44:59 -07:00
|
|
|
kwargs['host'] = host
|
|
|
|
kwargs['port'] = int(port)
|
2012-11-19 09:32:57 -08:00
|
|
|
|
|
|
|
marionette = Marionette.getMarionetteOrExit(**kwargs)
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
# create the DeviceManager
|
2012-07-11 11:49:30 -07:00
|
|
|
kwargs = {'adbPath': options.adbPath,
|
2012-12-20 08:11:11 -08:00
|
|
|
'deviceRoot': options.remoteTestRoot}
|
2012-04-27 11:44:59 -07:00
|
|
|
if options.deviceIP:
|
|
|
|
kwargs.update({'host': options.deviceIP,
|
|
|
|
'port': options.devicePort})
|
2013-03-22 13:48:04 -07:00
|
|
|
dm = DeviceManagerADB(**kwargs)
|
2013-07-26 11:40:04 -07:00
|
|
|
options = parser.verifyRemoteOptions(options)
|
2012-04-27 11:44:59 -07:00
|
|
|
if (options == None):
|
|
|
|
print "ERROR: Invalid options specified, use --help for a list of valid options"
|
|
|
|
sys.exit(1)
|
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
mochitest = B2GDeviceMochitest(marionette, dm, options.profile_data_dir, options.xrePath,
|
|
|
|
remote_test_root=options.remoteTestRoot,
|
|
|
|
remote_log_file=options.remoteLogFile)
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
options = parser.verifyOptions(options, mochitest)
|
|
|
|
if (options == None):
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
retVal = 1
|
|
|
|
try:
|
2012-08-08 17:29:26 -07:00
|
|
|
mochitest.cleanup(None, options)
|
2013-07-26 11:40:04 -07:00
|
|
|
retVal = mochitest.run_tests(options)
|
2012-04-27 11:44:59 -07:00
|
|
|
except:
|
2012-11-05 05:03:55 -08:00
|
|
|
print "Automation Error: Exception caught while running tests"
|
2012-06-15 14:13:04 -07:00
|
|
|
traceback.print_exc()
|
2012-04-27 11:44:59 -07:00
|
|
|
mochitest.stopWebServer(options)
|
|
|
|
mochitest.stopWebSocketServer(options)
|
|
|
|
try:
|
|
|
|
mochitest.cleanup(None, options)
|
|
|
|
except:
|
|
|
|
pass
|
2012-11-20 07:24:28 -08:00
|
|
|
retVal = 1
|
2012-04-27 11:44:59 -07:00
|
|
|
|
|
|
|
sys.exit(retVal)
|
|
|
|
|
2013-01-04 10:41:34 -08:00
|
|
|
def run_desktop_mochitests(parser, options):
|
|
|
|
# create our Marionette instance
|
|
|
|
kwargs = {}
|
|
|
|
if options.marionette:
|
|
|
|
host, port = options.marionette.split(':')
|
|
|
|
kwargs['host'] = host
|
|
|
|
kwargs['port'] = int(port)
|
|
|
|
marionette = Marionette.getMarionetteOrExit(**kwargs)
|
2013-09-26 06:58:08 -07:00
|
|
|
mochitest = B2GDesktopMochitest(marionette, options.profile_data_dir)
|
2013-01-04 10:41:34 -08:00
|
|
|
|
2013-10-10 10:25:59 -07:00
|
|
|
# add a -bin suffix if b2g-bin exists, but just b2g was specified
|
|
|
|
if options.app[-4:] != '-bin':
|
|
|
|
if os.path.isfile("%s-bin" % options.app):
|
|
|
|
options.app = "%s-bin" % options.app
|
2013-01-04 10:41:34 -08:00
|
|
|
|
|
|
|
options = MochitestOptions.verifyOptions(parser, options, mochitest)
|
|
|
|
if options == None:
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if options.desktop and not options.profile:
|
|
|
|
raise Exception("must specify --profile when specifying --desktop")
|
|
|
|
|
2013-07-26 11:40:04 -07:00
|
|
|
sys.exit(mochitest.runTests(options, onLaunch=mochitest.startTests))
|
2013-01-04 10:41:34 -08:00
|
|
|
|
|
|
|
def main():
|
2013-07-26 11:40:04 -07:00
|
|
|
parser = B2GOptions()
|
2013-01-04 10:41:34 -08:00
|
|
|
options, args = parser.parse_args()
|
|
|
|
|
|
|
|
if options.desktop:
|
|
|
|
run_desktop_mochitests(parser, options)
|
|
|
|
else:
|
2013-07-26 11:40:04 -07:00
|
|
|
run_remote_mochitests(parser, options)
|
2013-01-04 10:41:34 -08:00
|
|
|
|
2012-04-27 11:44:59 -07:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|