2008-02-22 00:08:39 +03:00
|
|
|
#
|
|
|
|
# ***** BEGIN LICENSE BLOCK *****
|
|
|
|
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
#
|
|
|
|
# The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
# 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
# http://www.mozilla.org/MPL/
|
|
|
|
#
|
|
|
|
# Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
# for the specific language governing rights and limitations under the
|
|
|
|
# License.
|
|
|
|
#
|
|
|
|
# The Original Code is mozilla.org code.
|
|
|
|
#
|
|
|
|
# The Initial Developer of the Original Code is
|
|
|
|
# Mozilla Foundation.
|
|
|
|
# Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
# the Initial Developer. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Contributor(s):
|
|
|
|
# Robert Sayre <sayrer@gmail.com>
|
|
|
|
# Jeff Walden <jwalden+bmo@mit.edu>
|
|
|
|
#
|
|
|
|
# Alternatively, the contents of this file may be used under the terms of
|
|
|
|
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
# in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
# of those above. If you wish to allow use of your version of this file only
|
|
|
|
# under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
# use your version of this file under the terms of the MPL, indicate your
|
|
|
|
# decision by deleting the provisions above and replace them with the notice
|
|
|
|
# and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
# the provisions above, a recipient may use your version of this file under
|
|
|
|
# the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
#
|
|
|
|
# ***** END LICENSE BLOCK *****
|
|
|
|
|
2008-06-07 10:43:15 +04:00
|
|
|
import codecs
|
2008-02-22 00:08:39 +03:00
|
|
|
from datetime import datetime
|
|
|
|
import itertools
|
2008-04-08 09:18:45 +04:00
|
|
|
import logging
|
2008-02-22 00:08:39 +03:00
|
|
|
import shutil
|
|
|
|
import os
|
2008-06-07 10:43:15 +04:00
|
|
|
import re
|
2008-04-08 09:18:45 +04:00
|
|
|
import signal
|
2008-02-22 00:08:39 +03:00
|
|
|
import sys
|
2008-04-08 09:18:45 +04:00
|
|
|
import threading
|
2008-02-22 00:08:39 +03:00
|
|
|
|
|
|
|
"""
|
|
|
|
Runs the browser from a script, and provides useful utilities
|
|
|
|
for setting up the browser environment.
|
|
|
|
"""
|
|
|
|
|
2008-04-08 09:18:45 +04:00
|
|
|
__all__ = [
|
|
|
|
"UNIXISH",
|
|
|
|
"IS_WIN32",
|
2008-05-22 23:08:41 +04:00
|
|
|
"IS_MAC",
|
2008-04-08 09:18:45 +04:00
|
|
|
"runApp",
|
|
|
|
"Process",
|
|
|
|
"initializeProfile",
|
|
|
|
"DIST_BIN",
|
|
|
|
"DEFAULT_APP",
|
|
|
|
]
|
|
|
|
|
2008-02-22 00:08:39 +03:00
|
|
|
# These are generated in mozilla/build/Makefile.in
|
|
|
|
#expand DIST_BIN = "./" + __XPC_BIN_PATH__
|
|
|
|
#expand IS_WIN32 = len("__WIN32__") != 0
|
|
|
|
#expand IS_MAC = __IS_MAC__ != 0
|
|
|
|
#ifdef IS_CYGWIN
|
|
|
|
#expand IS_CYGWIN = __IS_CYGWIN__ == 1
|
|
|
|
#else
|
|
|
|
IS_CYGWIN = False
|
|
|
|
#endif
|
2008-05-06 21:52:26 +04:00
|
|
|
#expand IS_CAMINO = __IS_CAMINO__ != 0
|
2008-02-22 00:08:39 +03:00
|
|
|
|
|
|
|
UNIXISH = not IS_WIN32 and not IS_MAC
|
|
|
|
|
|
|
|
#expand DEFAULT_APP = "./" + __BROWSER_PATH__
|
|
|
|
|
2008-04-08 09:18:45 +04:00
|
|
|
###########
|
|
|
|
# LOGGING #
|
|
|
|
###########
|
|
|
|
|
|
|
|
# We use the logging system here primarily because it'll handle multiple
|
|
|
|
# threads, which is needed to process the output of the server and application
|
|
|
|
# processes simultaneously.
|
|
|
|
log = logging.getLogger()
|
|
|
|
handler = logging.StreamHandler(sys.stdout)
|
|
|
|
log.setLevel(logging.INFO)
|
|
|
|
log.addHandler(handler)
|
|
|
|
|
|
|
|
|
2008-02-22 00:08:39 +03:00
|
|
|
#################
|
|
|
|
# SUBPROCESSING #
|
|
|
|
#################
|
|
|
|
|
|
|
|
class Process:
|
|
|
|
"""
|
|
|
|
Represents a subprocess of this process. We don't just directly use the
|
|
|
|
subprocess module here because we want compatibility with Python 2.3 on
|
|
|
|
non-Windows platforms. :-(
|
|
|
|
"""
|
|
|
|
|
2008-06-18 16:30:29 +04:00
|
|
|
def __init__(self, command, args, env):
|
2008-02-22 00:08:39 +03:00
|
|
|
"""
|
2008-04-08 09:18:45 +04:00
|
|
|
Creates a process representing the execution of the given command, which
|
|
|
|
must be an absolute path, with the given arguments in the given environment.
|
|
|
|
The process is then started.
|
2008-02-22 00:08:39 +03:00
|
|
|
"""
|
|
|
|
command = os.path.abspath(command)
|
|
|
|
if IS_WIN32:
|
|
|
|
import subprocess
|
|
|
|
cmd = [command]
|
|
|
|
cmd.extend(args)
|
2008-04-08 12:44:26 +04:00
|
|
|
p = subprocess.Popen(cmd, env = env,
|
|
|
|
stdout = subprocess.PIPE,
|
2008-06-18 16:30:29 +04:00
|
|
|
stderr = subprocess.STDOUT)
|
2008-04-08 09:18:45 +04:00
|
|
|
self._out = p.stdout
|
2008-02-22 00:08:39 +03:00
|
|
|
else:
|
|
|
|
import popen2
|
|
|
|
cmd = []
|
2008-06-18 16:30:29 +04:00
|
|
|
for (k, v) in env.iteritems():
|
|
|
|
cmd.append(k + "='" + v + "' ")
|
2008-02-22 00:08:39 +03:00
|
|
|
cmd.append("'" + command + "'")
|
|
|
|
cmd.extend(map(lambda x: "'" + x + "'", args))
|
|
|
|
cmd = " ".join(cmd)
|
2008-04-08 09:18:45 +04:00
|
|
|
p = popen2.Popen4(cmd)
|
|
|
|
self._out = p.fromchild
|
2008-02-22 00:08:39 +03:00
|
|
|
|
2008-04-08 09:18:45 +04:00
|
|
|
self._process = p
|
|
|
|
self.pid = p.pid
|
|
|
|
|
|
|
|
self._thread = threading.Thread(target = lambda: self._run())
|
|
|
|
self._thread.start()
|
|
|
|
|
|
|
|
def _run(self):
|
|
|
|
"Continues execution of this process on a separate thread."
|
2008-02-22 00:08:39 +03:00
|
|
|
p = self._process
|
2008-04-08 09:18:45 +04:00
|
|
|
out = self._out
|
2008-04-08 16:48:46 +04:00
|
|
|
|
|
|
|
if IS_WIN32:
|
|
|
|
running = lambda: p.poll() is None
|
|
|
|
else:
|
|
|
|
running = lambda: p.poll() == -1
|
|
|
|
|
2008-04-08 09:18:45 +04:00
|
|
|
# read in lines until the process finishes, then read in any last remaining
|
|
|
|
# buffered lines
|
2008-04-08 16:48:46 +04:00
|
|
|
while running():
|
2008-02-22 00:08:39 +03:00
|
|
|
line = out.readline().rstrip()
|
|
|
|
if len(line) > 0:
|
2008-04-08 09:18:45 +04:00
|
|
|
log.info(line)
|
2008-02-22 00:08:39 +03:00
|
|
|
for line in out:
|
|
|
|
line = line.rstrip()
|
|
|
|
if len(line) > 0:
|
2008-04-08 09:18:45 +04:00
|
|
|
log.info(line)
|
|
|
|
self._status = p.poll()
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
"Waits for this process to finish, then returns the process's status."
|
|
|
|
self._thread.join()
|
|
|
|
return self._status
|
2008-02-22 00:08:39 +03:00
|
|
|
|
2008-04-08 09:18:45 +04:00
|
|
|
def kill(self):
|
|
|
|
"Kills this process."
|
|
|
|
try:
|
2008-06-18 16:30:29 +04:00
|
|
|
if not IS_WIN32: # XXX
|
2008-04-08 09:18:45 +04:00
|
|
|
os.kill(self._process.pid, signal.SIGKILL)
|
|
|
|
except:
|
|
|
|
pass
|
2008-02-22 00:08:39 +03:00
|
|
|
|
2008-04-08 09:18:45 +04:00
|
|
|
|
|
|
|
#################
|
|
|
|
# PROFILE SETUP #
|
|
|
|
#################
|
2008-02-22 00:08:39 +03:00
|
|
|
|
2008-06-07 10:43:15 +04:00
|
|
|
class SyntaxError(Exception):
|
|
|
|
"Signifies a syntax error on a particular line in server-locations.txt."
|
|
|
|
|
|
|
|
def __init__(self, lineno, msg = None):
|
|
|
|
self.lineno = lineno
|
|
|
|
self.msg = msg
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
s = "Syntax error on line " + str(self.lineno)
|
|
|
|
if self.msg:
|
|
|
|
s += ": %s." % self.msg
|
|
|
|
else:
|
|
|
|
s += "."
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
class Location:
|
|
|
|
"Represents a location line in server-locations.txt."
|
|
|
|
|
|
|
|
def __init__(self, scheme, host, port, options):
|
|
|
|
self.scheme = scheme
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
self.options = options
|
|
|
|
|
|
|
|
|
2008-06-18 16:30:29 +04:00
|
|
|
def readLocations():
|
2008-06-07 10:43:15 +04:00
|
|
|
"""
|
|
|
|
Reads the locations at which the Mochitest HTTP server is available from
|
|
|
|
server-locations.txt.
|
|
|
|
"""
|
|
|
|
|
2008-06-18 16:30:29 +04:00
|
|
|
locationFile = codecs.open("server-locations.txt", "r", "UTF-8")
|
2008-06-07 10:43:15 +04:00
|
|
|
|
|
|
|
# Perhaps more detail than necessary, but it's the easiest way to make sure
|
|
|
|
# we get exactly the format we want. See server-locations.txt for the exact
|
|
|
|
# format guaranteed here.
|
|
|
|
lineRe = re.compile(r"^(?P<scheme>[a-z][-a-z0-9+.]*)"
|
|
|
|
r"://"
|
|
|
|
r"(?P<host>"
|
|
|
|
r"\d+\.\d+\.\d+\.\d+"
|
|
|
|
r"|"
|
|
|
|
r"(?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)*"
|
|
|
|
r"[a-z](?:[-a-z0-9]*[a-z0-9])?"
|
|
|
|
r")"
|
|
|
|
r":"
|
|
|
|
r"(?P<port>\d+)"
|
|
|
|
r"(?:"
|
|
|
|
r"\s+"
|
2008-06-18 16:30:29 +04:00
|
|
|
r"(?P<options>\w+(?:,\w+)*)"
|
2008-06-07 10:43:15 +04:00
|
|
|
r")?$")
|
|
|
|
locations = []
|
|
|
|
lineno = 0
|
|
|
|
seenPrimary = False
|
|
|
|
for line in locationFile:
|
|
|
|
lineno += 1
|
|
|
|
if line.startswith("#") or line == "\n":
|
|
|
|
continue
|
|
|
|
|
|
|
|
match = lineRe.match(line)
|
|
|
|
if not match:
|
|
|
|
raise SyntaxError(lineno)
|
|
|
|
|
|
|
|
options = match.group("options")
|
|
|
|
if options:
|
|
|
|
options = options.split(",")
|
|
|
|
if "primary" in options:
|
|
|
|
if seenPrimary:
|
|
|
|
raise SyntaxError(lineno, "multiple primary locations")
|
|
|
|
seenPrimary = True
|
|
|
|
else:
|
|
|
|
options = []
|
|
|
|
|
|
|
|
locations.append(Location(match.group("scheme"), match.group("host"),
|
|
|
|
match.group("port"), options))
|
|
|
|
|
|
|
|
if not seenPrimary:
|
|
|
|
raise SyntaxError(lineno + 1, "missing primary location")
|
|
|
|
|
|
|
|
return locations
|
|
|
|
|
|
|
|
|
2008-02-22 00:08:39 +03:00
|
|
|
def initializeProfile(profileDir):
|
|
|
|
"Sets up the standard testing profile."
|
|
|
|
|
|
|
|
# Start with a clean slate.
|
|
|
|
shutil.rmtree(profileDir, True)
|
|
|
|
os.mkdir(profileDir)
|
|
|
|
|
|
|
|
prefs = []
|
|
|
|
|
|
|
|
part = """\
|
|
|
|
user_pref("browser.dom.window.dump.enabled", true);
|
2008-05-14 17:27:47 +04:00
|
|
|
user_pref("dom.allow_scripts_to_close_windows", true);
|
2008-02-22 00:08:39 +03:00
|
|
|
user_pref("dom.disable_open_during_load", false);
|
|
|
|
user_pref("dom.max_script_run_time", 0); // no slow script dialogs
|
|
|
|
user_pref("signed.applets.codebase_principal_support", true);
|
|
|
|
user_pref("security.warn_submit_insecure", false);
|
|
|
|
user_pref("browser.shell.checkDefaultBrowser", false);
|
|
|
|
user_pref("browser.warnOnQuit", false);
|
2008-03-04 10:24:26 +03:00
|
|
|
user_pref("accessibility.typeaheadfind.autostart", false);
|
2008-03-05 01:12:06 +03:00
|
|
|
user_pref("javascript.options.showInConsole", true);
|
2008-04-29 03:56:07 +04:00
|
|
|
user_pref("layout.debug.enable_data_xbl", true);
|
2008-05-06 00:43:44 +04:00
|
|
|
user_pref("browser.EULA.override", true);
|
2008-05-06 21:52:26 +04:00
|
|
|
|
|
|
|
user_pref("camino.warn_when_closing", false); // Camino-only, harmless to others
|
2008-02-22 00:08:39 +03:00
|
|
|
"""
|
|
|
|
prefs.append(part)
|
|
|
|
|
2008-06-07 10:43:15 +04:00
|
|
|
locations = readLocations()
|
|
|
|
|
|
|
|
# Grant God-power to all the privileged servers on which tests run.
|
|
|
|
privileged = filter(lambda loc: "privileged" in loc.options, locations)
|
|
|
|
for (i, l) in itertools.izip(itertools.count(1), privileged):
|
2008-02-22 00:08:39 +03:00
|
|
|
part = """
|
|
|
|
user_pref("capability.principal.codebase.p%(i)d.granted",
|
|
|
|
"UniversalXPConnect UniversalBrowserRead UniversalBrowserWrite \
|
|
|
|
UniversalPreferencesRead UniversalPreferencesWrite \
|
|
|
|
UniversalFileRead");
|
2008-06-07 10:43:15 +04:00
|
|
|
user_pref("capability.principal.codebase.p%(i)d.id", "%(origin)s");
|
2008-02-22 00:08:39 +03:00
|
|
|
user_pref("capability.principal.codebase.p%(i)d.subjectName", "");
|
2008-06-07 10:43:15 +04:00
|
|
|
""" % { "i": i,
|
|
|
|
"origin": (l.scheme + "://" + l.host + ":" + l.port) }
|
2008-02-22 00:08:39 +03:00
|
|
|
prefs.append(part)
|
|
|
|
|
2008-06-07 10:43:15 +04:00
|
|
|
# We need to proxy every server but the primary one.
|
|
|
|
origins = ["'%s://%s:%s'" % (l.scheme, l.host, l.port)
|
|
|
|
for l in filter(lambda l: "primary" not in l.options, locations)]
|
|
|
|
origins = ", ".join(origins)
|
2008-02-22 00:08:39 +03:00
|
|
|
|
|
|
|
pacURL = """data:text/plain,
|
|
|
|
function FindProxyForURL(url, host)
|
|
|
|
{
|
2008-06-07 10:43:15 +04:00
|
|
|
var origins = [%(origins)s];
|
|
|
|
var regex = new RegExp('^([a-z][-a-z0-9+.]*)' +
|
|
|
|
'://' +
|
|
|
|
'(?:[^/@]*@)?' +
|
|
|
|
'(.*?)' +
|
|
|
|
'(?::(\\\\\\\\d+))?/');
|
2008-02-22 00:08:39 +03:00
|
|
|
var matches = regex.exec(url);
|
|
|
|
if (!matches)
|
|
|
|
return 'DIRECT';
|
2008-06-07 10:43:15 +04:00
|
|
|
var isHttp = matches[1] == 'http';
|
|
|
|
if (!matches[3])
|
2008-06-18 16:30:29 +04:00
|
|
|
matches[3] = isHttp ? '80' : '443';
|
2008-06-07 10:43:15 +04:00
|
|
|
var origin = matches[1] + '://' + matches[2] + ':' + matches[3];
|
|
|
|
if (origins.indexOf(origin) < 0)
|
|
|
|
return 'DIRECT';
|
|
|
|
if (isHttp)
|
2008-06-18 16:30:29 +04:00
|
|
|
return 'PROXY localhost:8888';
|
2008-02-22 00:08:39 +03:00
|
|
|
return 'DIRECT';
|
2008-06-07 10:43:15 +04:00
|
|
|
}""" % { "origins": origins }
|
2008-02-22 00:08:39 +03:00
|
|
|
pacURL = "".join(pacURL.splitlines())
|
|
|
|
|
|
|
|
part = """
|
|
|
|
user_pref("network.proxy.type", 2);
|
|
|
|
user_pref("network.proxy.autoconfig_url", "%(pacURL)s");
|
2008-05-06 21:52:26 +04:00
|
|
|
|
|
|
|
user_pref("camino.use_system_proxy_settings", false); // Camino-only, harmless to others
|
2008-02-22 00:08:39 +03:00
|
|
|
""" % {"pacURL": pacURL}
|
|
|
|
prefs.append(part)
|
|
|
|
|
|
|
|
# write the preferences
|
|
|
|
prefsFile = open(profileDir + "/" + "user.js", "a")
|
|
|
|
prefsFile.write("".join(prefs))
|
|
|
|
prefsFile.close()
|
|
|
|
|
|
|
|
|
2008-04-08 09:18:45 +04:00
|
|
|
###############
|
|
|
|
# RUN THE APP #
|
|
|
|
###############
|
2008-02-22 00:08:39 +03:00
|
|
|
|
2008-03-16 01:48:08 +03:00
|
|
|
def runApp(testURL, env, app, profileDir, extraArgs):
|
2008-02-22 00:08:39 +03:00
|
|
|
"Run the app, returning the time at which it was started."
|
|
|
|
# mark the start
|
|
|
|
start = datetime.now()
|
|
|
|
|
|
|
|
# now run with the profile we created
|
|
|
|
cmd = app
|
2008-05-06 21:52:26 +04:00
|
|
|
if IS_MAC and not IS_CAMINO and not cmd.endswith("-bin"):
|
2008-02-22 00:08:39 +03:00
|
|
|
cmd += "-bin"
|
|
|
|
cmd = os.path.abspath(cmd)
|
|
|
|
|
|
|
|
args = []
|
|
|
|
if IS_MAC:
|
|
|
|
args.append("-foreground")
|
|
|
|
|
|
|
|
if IS_CYGWIN:
|
|
|
|
profileDirectory = commands.getoutput("cygpath -w \"" + profileDir + "/\"")
|
|
|
|
else:
|
|
|
|
profileDirectory = profileDir + "/"
|
|
|
|
|
2008-05-06 21:52:26 +04:00
|
|
|
args.extend(("-no-remote", "-profile", profileDirectory))
|
|
|
|
if IS_CAMINO:
|
|
|
|
args.extend(("-url", testURL))
|
|
|
|
else:
|
|
|
|
args.append((testURL))
|
2008-03-16 01:48:08 +03:00
|
|
|
args.extend(extraArgs)
|
2008-02-22 00:08:39 +03:00
|
|
|
proc = Process(cmd, args, env = env)
|
2008-04-08 09:18:45 +04:00
|
|
|
log.info("Application pid: %d", proc.pid)
|
2008-02-22 00:08:39 +03:00
|
|
|
status = proc.wait()
|
|
|
|
if status != 0:
|
2008-04-08 09:18:45 +04:00
|
|
|
log.info("ERROR FAIL Exited with code %d during test run", status)
|
2008-02-22 00:08:39 +03:00
|
|
|
|
2008-02-27 06:48:54 +03:00
|
|
|
return start
|