# 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/.
import json import os import re import tempfile import time import traceback
import mozinfo import mozversion import yaml from mozprofile import Profile from wptserve import server
from .firefoxrunner import TPSFirefoxRunner from .phase import TPSTestPhase
class TempFile(object): """Class for temporary files that delete themselves when garbage-collected."""
def log(self, msg, printToConsole=False): """Appends a string to the logfile"""
f = open(self.logfile, "a")
f.write(msg)
f.close() if printToConsole:
print(msg)
def writeToResultFile(self, postdata, body=None, sendTo=["crossweave@mozilla.com"]): """Writes results to test file"""
results = {"results": []}
if os.access(self.resultfile, os.F_OK):
f = open(self.resultfile, "r")
results = json.loads(f.read())
f.close()
f = open(self.resultfile, "w") if body isnotNone:
postdata["body"] = body if self.numpassed isnotNone:
postdata["numpassed"] = self.numpassed if self.numfailed isnotNone:
postdata["numfailed"] = self.numfailed if self.firefoxRunner and self.firefoxRunner.url:
postdata["firefoxrunnerurl"] = self.firefoxRunner.url
def _zip_add_dir(self, zip, dir, rootDir): try:
zip.write(os.path.join(rootDir, dir), dir) except Exception: # on some OS's, adding directory entries doesn't seem to work pass for root, dirs, files in os.walk(os.path.join(rootDir, dir)): for f in files:
zip.write(os.path.join(root, f), os.path.join(dir, f))
def handle_phase_failure(self, profiles): for profile in profiles:
self.log("\nDumping sync log for profile %s\n" % profiles[profile].profile) for root, dirs, files in os.walk(
os.path.join(profiles[profile].profile, "weave", "logs")
): for f in files:
weavelog = os.path.join(
profiles[profile].profile, "weave", "logs", f
) if os.access(weavelog, os.F_OK): with open(weavelog, "r") as fh: for line in fh:
possible_time = line[0:13] if len(possible_time) == 13 and possible_time.isdigit():
time_ms = int(possible_time) # pylint --py3k W1619
formatted = time.strftime( "%Y-%m-%d %H:%M:%S",
time.localtime(time_ms / 1000),
)
self.log( "%s.%03d %s"
% (formatted, time_ms % 1000, line[14:])
) else:
self.log(line)
# Read and parse the test file, merge it with the contents of the config # file, and write the combined output to a temporary file.
f = open(testpath, "r")
testcontent = f.read()
f.close() # We use yaml to parse the tests because it is a superset of json # but tolerates things like property names not being quoted, trailing # commas, etc. try:
test = yaml.safe_load(testcontent) except Exception:
test = yaml.safe_load(
testcontent[testcontent.find("{") : testcontent.find("}") + 1]
)
# generate the profiles defined in the test, and a list of test phases
profiles = {}
phaselist = [] for phase in test:
profilename = test[phase]
# create the profile if necessary if profilename notin profiles:
profiles[profilename] = Profile(
preferences=self.preferences.copy(), addons=self.extensions
)
# create the test phase
phaselist.append(
TPSTestPhase(
phase,
profiles[profilename],
testname,
testpath,
self.logfile,
self.env,
self.firefoxRunner,
self.log,
ignore_unused_engines=self.ignore_unused_engines,
)
)
# sort the phase list by name
phaselist = sorted(phaselist, key=lambda phase: phase.phase)
# run each phase in sequence, aborting at the first failure
failed = False for phase in phaselist:
phase.run() if phase.status != "PASS":
failed = True break
cleanup_phase.run() if cleanup_phase.status != "PASS":
failed = True # Keep going to run the remaining cleanup phases.
if failed:
self.handle_phase_failure(profiles)
# grep the log for FF and sync versions
f = open(self.logfile)
logdata = f.read()
match = self.syncVerRe.search(logdata)
sync_version = match.group("syncversion") if match else"unknown"
match = self.ffVerRe.search(logdata)
firefox_version = match.group("ffver") if match else"unknown"
match = self.ffBuildIDRe.search(logdata)
firefox_buildid = match.group("ffbuildid") if match else"unknown"
f.close() if phase.status == "PASS":
logdata = "" else: # we only care about the log data for this specific test
logdata = logdata[logdata.find("Running test %s" % (str(testname))) :]
result = { "PASS": lambda x: ("TEST-PASS", ""), "FAIL": lambda x: ("TEST-UNEXPECTED-FAIL", x.rstrip()), "unknown": lambda x: ("TEST-UNEXPECTED-FAIL", "test did not complete"),
}[phase.status](phase.errline)
logstr = "\n%s | %s%s\n" % (
result[0],
testname,
(" | %s" % result[1] if result[1] else""),
)
# save logdata to a temporary file for posting to the db
tmplogfile = None if logdata:
tmplogfile = TempFile(prefix="tps_log_")
tmplogfile.write(logdata.encode("utf-8"))
tmplogfile.close()
self.errorlogs[testname] = tmplogfile
if self.mobile:
self.preferences.update({"services.sync.client.type": "mobile"})
# If we are using legacy Sync, then set a dummy username to force the # correct authentication type. Without this pref set to a value # without an '@' character, Sync will initialize for FxA. if self.config.get("auth_type", "fx_account") != "fx_account":
self.preferences.update({"services.sync.username": "dummy"})
if self.debug:
self.preferences.update(self.debug_preferences)
def run_tests(self): # delete the logfile if it already exists if os.access(self.logfile, os.F_OK):
os.remove(self.logfile)
# Copy the system env variables, and update them for custom settings
self.env = os.environ.copy()
self.env.update(self.extra_env)
# Update preferences for custom settings
self.update_preferences()
# Acquire a lock to make sure no other threads are running tests # at the same time. if self.rlock:
self.rlock.acquire()
try: # Create the Firefox runner, which will download and install the # build, as needed. ifnot self.firefoxRunner:
self.firefoxRunner = TPSFirefoxRunner(self.binary)
# release our lock if self.rlock:
self.rlock.release()
# dump out a summary of test results
print("Test Summary\n") for test in self.postdata.get("tests", {}):
print("{} | {} | {}".format(test["state"], test["name"], test["message"]))
def run_test_group(self):
self.results = []
# reset number of passed/failed tests
self.numpassed = 0
self.numfailed = 0
# build the test list try:
f = open(self.testfile)
jsondata = f.read()
f.close()
testfiles = json.loads(jsondata)
testlist = [] for filename, meta in testfiles["tests"].items():
skip_reason = meta.get("disabled") if skip_reason:
print("Skipping test {} - {}".format(filename, skip_reason)) else:
testlist.append(filename) except ValueError:
testlist = [os.path.basename(self.testfile)]
testdir = os.path.dirname(self.testfile)
self.results.append(
{ "state": result["state"], "name": result["name"], "message": result["message"], "logdata": result["logdata"],
}
) if result["state"] == "TEST-PASS":
self.numpassed += 1 else:
self.numfailed += 1 if self.stop_on_error:
print( "\nTest failed with --stop-on-error specified; " "not running any more tests.\n"
) break
self.server.stop()
# generate the postdata we'll use to post the results to the db
self.postdata = { "tests": self.results, "os": "%s %sbit" % (mozinfo.version, mozinfo.bits), "testtype": "crossweave", "productversion": self.productversion, "addonversion": self.addonversion, "synctype": self.synctype,
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.