2019-08-16 15:58:06 +03: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/.
|
|
|
|
|
|
|
|
from __future__ import (
|
|
|
|
absolute_import,
|
|
|
|
print_function,
|
|
|
|
unicode_literals,
|
|
|
|
)
|
|
|
|
|
2020-03-10 18:19:16 +03:00
|
|
|
import argparse
|
2019-11-08 14:58:08 +03:00
|
|
|
import json
|
2019-08-16 15:58:06 +03:00
|
|
|
import os
|
2020-07-09 17:01:50 +03:00
|
|
|
import re
|
2019-08-16 15:58:06 +03:00
|
|
|
import shutil
|
|
|
|
import subprocess
|
2020-07-09 17:01:50 +03:00
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
|
2020-03-23 18:25:33 +03:00
|
|
|
from collections import OrderedDict
|
2019-08-16 15:58:06 +03:00
|
|
|
|
2020-03-10 18:19:16 +03:00
|
|
|
from six import iteritems
|
|
|
|
|
2019-08-16 15:58:06 +03:00
|
|
|
from mach.decorators import (
|
|
|
|
Command,
|
|
|
|
CommandArgument,
|
|
|
|
CommandProvider,
|
|
|
|
SubCommand,
|
|
|
|
)
|
|
|
|
|
2019-08-16 15:58:15 +03:00
|
|
|
from mozbuild.base import (
|
|
|
|
MachCommandBase,
|
|
|
|
MozbuildObject,
|
2020-04-21 14:58:04 +03:00
|
|
|
BinaryNotFoundException,
|
2019-08-16 15:58:15 +03:00
|
|
|
)
|
|
|
|
from mozbuild import nodeutil
|
2020-03-10 18:19:16 +03:00
|
|
|
import mozlog
|
2019-08-16 15:58:15 +03:00
|
|
|
import mozprofile
|
2019-08-16 15:58:06 +03:00
|
|
|
|
|
|
|
|
|
|
|
EX_CONFIG = 78
|
|
|
|
EX_SOFTWARE = 70
|
|
|
|
EX_USAGE = 64
|
|
|
|
|
|
|
|
|
2019-08-16 15:58:15 +03:00
|
|
|
def setup():
|
|
|
|
# add node and npm from mozbuild to front of system path
|
|
|
|
npm, _ = nodeutil.find_npm_executable()
|
|
|
|
if not npm:
|
|
|
|
exit(EX_CONFIG, "could not find npm executable")
|
|
|
|
path = os.path.abspath(os.path.join(npm, os.pardir))
|
|
|
|
os.environ["PATH"] = "{}:{}".format(path, os.environ["PATH"])
|
|
|
|
|
|
|
|
|
2019-08-16 15:58:06 +03:00
|
|
|
@CommandProvider
|
|
|
|
class RemoteCommands(MachCommandBase):
|
Bug 1657650 - Require that Mach command providers subclass MachCommandBase. r=remote-protocol-reviewers,marionette-reviewers,maja_zf,mhentges,froydnj
Today we don't require that `mach` `CommandProvider`s subclass from any particular parent class and we're very lax about the requirements they must meet. While that's convenient in certain circumstances, it has some unfortunate implications for feature development.
Today the only requirements that we have for `CommandProvider`s are that they have an `__init__()` method that takes either 1 or 2 arguments, the second of which must be called `context` and is populated with the `mach` `CommandContext`. Again, while this flexibility is occasionally convenient, it is limiting. As we add features to `mach`, having a better idea what the shape of our `CommandProvider`s are and how we can instantiate them and use them is increasingly important, and this gives us additional control when having `mach` configure `CommandProvider`s based on data that is only available at the `mach` level. In particular, we plan to leverage this in bugs 985141 and 1654074.
Here we add validation to the `CommandProvider` decorator to ensure all classes inherit from `MachCommandBase`, update all `CommandProvider`s in-tree to inherit from `MachCommandBase`, and update source and test code accordingly.
Follow-up work: we now require (de facto) that the `context` be populated with a `topdir` attribute by the `populate_context_handler` function, since instantiating the `MachCommandBase` requires a `topdir` be provided. This is fine for now in the interest of keeping this patch reasonably sized, but some additional refactoring could make this cleaner.
Differential Revision: https://phabricator.services.mozilla.com/D86255
2020-08-07 21:24:59 +03:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(RemoteCommands, self).__init__(*args, **kwargs)
|
2019-08-16 15:58:06 +03:00
|
|
|
self.remotedir = os.path.join(self.topsrcdir, "remote")
|
|
|
|
|
|
|
|
@Command(
|
|
|
|
"remote", category="misc", description="Remote protocol related operations."
|
|
|
|
)
|
|
|
|
def remote(self):
|
2020-05-20 07:35:21 +03:00
|
|
|
"""The remote subcommands all relate to the remote protocol."""
|
2020-05-29 19:16:31 +03:00
|
|
|
self._sub_mach(["help", "remote"])
|
|
|
|
return 1
|
2019-08-16 15:58:06 +03:00
|
|
|
|
|
|
|
@SubCommand(
|
|
|
|
"remote", "vendor-puppeteer", "Pull in latest changes of the Puppeteer client."
|
|
|
|
)
|
|
|
|
@CommandArgument(
|
|
|
|
"--repository",
|
|
|
|
metavar="REPO",
|
2020-06-02 23:49:00 +03:00
|
|
|
required=True,
|
|
|
|
help="The (possibly remote) repository to clone from.",
|
|
|
|
)
|
2019-08-16 15:58:06 +03:00
|
|
|
@CommandArgument(
|
|
|
|
"--commitish",
|
|
|
|
metavar="COMMITISH",
|
2020-06-02 23:49:00 +03:00
|
|
|
required=True,
|
|
|
|
help="The commit or tag object name to check out.",
|
|
|
|
)
|
2020-12-04 18:08:17 +03:00
|
|
|
@CommandArgument(
|
|
|
|
"--no-install",
|
|
|
|
dest="install",
|
|
|
|
action="store_false",
|
|
|
|
default=True,
|
|
|
|
help="Do not install the just-pulled Puppeteer package,",
|
|
|
|
)
|
|
|
|
def vendor_puppeteer(self, repository, commitish, install):
|
2020-07-02 21:46:42 +03:00
|
|
|
puppeteer_dir = os.path.join(self.remotedir, "test", "puppeteer")
|
2019-08-16 15:58:06 +03:00
|
|
|
|
2020-07-02 21:46:42 +03:00
|
|
|
# Preserve our custom mocha reporter
|
|
|
|
shutil.move(
|
|
|
|
os.path.join(puppeteer_dir, "json-mocha-reporter.js"), self.remotedir
|
|
|
|
)
|
|
|
|
shutil.rmtree(puppeteer_dir, ignore_errors=True)
|
|
|
|
os.makedirs(puppeteer_dir)
|
2019-08-16 15:58:06 +03:00
|
|
|
with TemporaryDirectory() as tmpdir:
|
|
|
|
git("clone", "-q", repository, tmpdir)
|
|
|
|
git("checkout", commitish, worktree=tmpdir)
|
|
|
|
git(
|
|
|
|
"checkout-index",
|
|
|
|
"-a",
|
|
|
|
"-f",
|
2020-07-02 21:46:42 +03:00
|
|
|
"--prefix",
|
|
|
|
"{}/".format(puppeteer_dir),
|
2019-08-16 15:58:06 +03:00
|
|
|
worktree=tmpdir,
|
|
|
|
)
|
|
|
|
|
|
|
|
# remove files which may interfere with git checkout of central
|
|
|
|
try:
|
2020-07-02 21:46:42 +03:00
|
|
|
os.remove(os.path.join(puppeteer_dir, ".gitattributes"))
|
|
|
|
os.remove(os.path.join(puppeteer_dir, ".gitignore"))
|
2019-08-16 15:58:06 +03:00
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2020-12-04 12:11:46 +03:00
|
|
|
unwanted_dirs = ["experimental", "docs"]
|
|
|
|
|
|
|
|
for dir in unwanted_dirs:
|
|
|
|
dir_path = os.path.join(puppeteer_dir, dir)
|
|
|
|
if os.path.isdir(dir_path):
|
|
|
|
shutil.rmtree(dir_path)
|
2020-06-02 23:49:00 +03:00
|
|
|
|
2020-07-02 21:46:42 +03:00
|
|
|
shutil.move(
|
|
|
|
os.path.join(self.remotedir, "json-mocha-reporter.js"), puppeteer_dir
|
|
|
|
)
|
|
|
|
|
2019-08-16 15:58:06 +03:00
|
|
|
import yaml
|
2020-10-26 21:34:53 +03:00
|
|
|
|
2019-08-16 15:58:06 +03:00
|
|
|
annotation = {
|
|
|
|
"schema": 1,
|
|
|
|
"bugzilla": {
|
|
|
|
"product": "Remote Protocol",
|
|
|
|
"component": "Agent",
|
|
|
|
},
|
|
|
|
"origin": {
|
|
|
|
"name": "puppeteer",
|
|
|
|
"description": "Headless Chrome Node API",
|
|
|
|
"url": repository,
|
|
|
|
"license": "Apache-2.0",
|
|
|
|
"release": commitish,
|
|
|
|
},
|
|
|
|
}
|
2020-07-02 21:46:42 +03:00
|
|
|
with open(os.path.join(puppeteer_dir, "moz.yaml"), "w") as fh:
|
2019-08-16 15:58:06 +03:00
|
|
|
yaml.safe_dump(
|
|
|
|
annotation,
|
|
|
|
fh,
|
|
|
|
default_flow_style=False,
|
|
|
|
encoding="utf-8",
|
|
|
|
allow_unicode=True,
|
|
|
|
)
|
|
|
|
|
2020-12-04 18:08:17 +03:00
|
|
|
if install:
|
|
|
|
env = {"PUPPETEER_SKIP_DOWNLOAD": "1"}
|
|
|
|
npm("install", cwd=os.path.join(self.topsrcdir, puppeteer_dir), env=env)
|
|
|
|
|
2019-08-16 15:58:06 +03:00
|
|
|
|
|
|
|
def git(*args, **kwargs):
|
|
|
|
cmd = ("git",)
|
|
|
|
if kwargs.get("worktree"):
|
|
|
|
cmd += ("-C", kwargs["worktree"])
|
|
|
|
cmd += args
|
|
|
|
|
|
|
|
pipe = kwargs.get("pipe")
|
|
|
|
git_p = subprocess.Popen(
|
|
|
|
cmd,
|
|
|
|
env={"GIT_CONFIG_NOSYSTEM": "1"},
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
)
|
|
|
|
pipe_p = None
|
|
|
|
if pipe:
|
|
|
|
pipe_p = subprocess.Popen(pipe, stdin=git_p.stdout, stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
if pipe:
|
|
|
|
_, pipe_err = pipe_p.communicate()
|
|
|
|
out, git_err = git_p.communicate()
|
|
|
|
|
|
|
|
# use error from first program that failed
|
|
|
|
if git_p.returncode > 0:
|
|
|
|
exit(EX_SOFTWARE, git_err)
|
|
|
|
if pipe and pipe_p.returncode > 0:
|
|
|
|
exit(EX_SOFTWARE, pipe_err)
|
|
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
2019-08-16 15:58:15 +03:00
|
|
|
def npm(*args, **kwargs):
|
2020-03-10 18:19:16 +03:00
|
|
|
from mozprocess import processhandler
|
2020-10-26 21:34:53 +03:00
|
|
|
|
2019-08-16 15:58:15 +03:00
|
|
|
env = None
|
|
|
|
if kwargs.get("env"):
|
|
|
|
env = os.environ.copy()
|
|
|
|
env.update(kwargs["env"])
|
|
|
|
|
2020-03-10 18:19:16 +03:00
|
|
|
proc_kwargs = {}
|
|
|
|
if "processOutputLine" in kwargs:
|
|
|
|
proc_kwargs["processOutputLine"] = kwargs["processOutputLine"]
|
|
|
|
|
|
|
|
p = processhandler.ProcessHandler(
|
|
|
|
cmd="npm",
|
|
|
|
args=list(args),
|
|
|
|
cwd=kwargs.get("cwd"),
|
|
|
|
env=env,
|
2020-05-29 15:37:45 +03:00
|
|
|
universal_newlines=True,
|
2020-03-10 18:19:16 +03:00
|
|
|
**proc_kwargs
|
|
|
|
)
|
|
|
|
if not kwargs.get("wait", True):
|
|
|
|
return p
|
|
|
|
|
|
|
|
wait_proc(p, cmd="npm", exit_on_fail=kwargs.get("exit_on_fail", True))
|
|
|
|
|
|
|
|
return p.returncode
|
|
|
|
|
|
|
|
|
2020-06-05 21:53:38 +03:00
|
|
|
def wait_proc(p, cmd=None, exit_on_fail=True, output_timeout=None):
|
2020-03-10 18:19:16 +03:00
|
|
|
try:
|
2020-06-05 21:53:38 +03:00
|
|
|
p.run(outputTimeout=output_timeout)
|
2020-03-10 18:19:16 +03:00
|
|
|
p.wait()
|
2020-06-05 21:53:38 +03:00
|
|
|
if p.timedOut:
|
|
|
|
# In some cases, we wait longer for a mocha timeout
|
|
|
|
print("Timed out after {} seconds of no output".format(output_timeout))
|
2020-03-10 18:19:16 +03:00
|
|
|
finally:
|
|
|
|
p.kill()
|
|
|
|
if exit_on_fail and p.returncode > 0:
|
|
|
|
msg = (
|
|
|
|
"%s: exit code %s" % (cmd, p.returncode)
|
|
|
|
if cmd
|
|
|
|
else "exit code %s" % p.returncode
|
|
|
|
)
|
|
|
|
exit(p.returncode, msg)
|
|
|
|
|
|
|
|
|
|
|
|
class MochaOutputHandler(object):
|
|
|
|
def __init__(self, logger, expected):
|
2020-07-09 17:01:50 +03:00
|
|
|
self.hook_re = re.compile('"before\b?.*" hook|"after\b?.*" hook')
|
|
|
|
|
2020-03-10 18:19:16 +03:00
|
|
|
self.logger = logger
|
|
|
|
self.proc = None
|
2020-03-23 18:25:33 +03:00
|
|
|
self.test_results = OrderedDict()
|
2020-03-10 18:19:16 +03:00
|
|
|
self.expected = expected
|
2020-06-25 08:39:22 +03:00
|
|
|
self.unexpected_skips = set()
|
2020-03-10 18:19:16 +03:00
|
|
|
|
|
|
|
self.has_unexpected = False
|
|
|
|
self.logger.suite_start([], name="puppeteer-tests")
|
2020-04-07 18:25:52 +03:00
|
|
|
self.status_map = {
|
|
|
|
"CRASHED": "CRASH",
|
|
|
|
"OK": "PASS",
|
|
|
|
"TERMINATED": "CRASH",
|
2020-06-05 21:53:38 +03:00
|
|
|
"pass": "PASS",
|
|
|
|
"fail": "FAIL",
|
|
|
|
"pending": "SKIP",
|
2020-04-07 18:25:52 +03:00
|
|
|
}
|
2020-03-10 18:19:16 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def pid(self):
|
|
|
|
return self.proc and self.proc.pid
|
|
|
|
|
|
|
|
def __call__(self, line):
|
2020-06-05 21:53:38 +03:00
|
|
|
event = None
|
|
|
|
try:
|
|
|
|
if line.startswith("[") and line.endswith("]"):
|
|
|
|
event = json.loads(line)
|
|
|
|
self.process_event(event)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
self.logger.process_output(self.pid, line, command="npm")
|
|
|
|
|
|
|
|
def process_event(self, event):
|
|
|
|
if isinstance(event, list) and len(event) > 1:
|
|
|
|
status = self.status_map.get(event[0])
|
|
|
|
test_start = event[0] == "test-start"
|
|
|
|
if not status and not test_start:
|
|
|
|
return
|
|
|
|
test_info = event[1]
|
|
|
|
test_name = test_info.get("fullTitle", "")
|
|
|
|
test_path = test_info.get("file", "")
|
|
|
|
test_err = test_info.get("err")
|
|
|
|
if status == "FAIL" and test_err:
|
|
|
|
if "timeout" in test_err.lower():
|
|
|
|
status = "TIMEOUT"
|
|
|
|
if test_name and test_path:
|
|
|
|
test_name = "{} ({})".format(test_name, os.path.basename(test_path))
|
2020-07-09 17:01:50 +03:00
|
|
|
# mocha hook failures are not tracked in metadata
|
|
|
|
if status != "PASS" and self.hook_re.search(test_name):
|
|
|
|
self.logger.error("TEST-UNEXPECTED-ERROR %s" % (test_name,))
|
|
|
|
return
|
2020-06-05 21:53:38 +03:00
|
|
|
if test_start:
|
|
|
|
self.logger.test_start(test_name)
|
|
|
|
return
|
2020-06-25 08:39:22 +03:00
|
|
|
expected = self.expected.get(test_name, ["PASS"])
|
2020-03-19 14:06:42 +03:00
|
|
|
# mozlog doesn't really allow unexpected skip,
|
2020-06-25 08:39:22 +03:00
|
|
|
# so if a test is disabled just expect that and note the unexpected skip
|
2020-06-05 21:53:38 +03:00
|
|
|
# Also, mocha doesn't log test-start for skipped tests
|
2020-03-19 14:06:42 +03:00
|
|
|
if status == "SKIP":
|
2020-06-05 21:53:38 +03:00
|
|
|
self.logger.test_start(test_name)
|
2020-07-02 21:46:57 +03:00
|
|
|
if self.expected and status not in expected:
|
2020-06-25 08:39:22 +03:00
|
|
|
self.unexpected_skips.add(test_name)
|
2020-03-19 14:06:42 +03:00
|
|
|
expected = ["SKIP"]
|
2020-03-10 18:19:16 +03:00
|
|
|
known_intermittent = expected[1:]
|
|
|
|
expected_status = expected[0]
|
|
|
|
|
2020-10-22 17:11:21 +03:00
|
|
|
# check if we've seen a result for this test before this log line
|
|
|
|
result_recorded = self.test_results.get(test_name)
|
|
|
|
if result_recorded:
|
|
|
|
self.logger.warning(
|
|
|
|
"Received a second status for {}: "
|
|
|
|
"first {}, now {}".format(test_name, result_recorded, status)
|
|
|
|
)
|
|
|
|
# mocha intermittently logs an additional test result after the
|
|
|
|
# test has already timed out. Avoid recording this second status.
|
|
|
|
if result_recorded != "TIMEOUT":
|
|
|
|
self.test_results[test_name] = status
|
|
|
|
if status not in expected:
|
|
|
|
self.has_unexpected = True
|
2020-03-10 18:19:16 +03:00
|
|
|
self.logger.test_end(
|
|
|
|
test_name,
|
|
|
|
status=status,
|
|
|
|
expected=expected_status,
|
|
|
|
known_intermittent=known_intermittent,
|
|
|
|
)
|
|
|
|
|
|
|
|
def new_expected(self):
|
2020-03-23 18:25:33 +03:00
|
|
|
new_expected = OrderedDict()
|
2020-03-10 18:19:16 +03:00
|
|
|
for test_name, status in iteritems(self.test_results):
|
|
|
|
if test_name not in self.expected:
|
2020-03-23 18:25:33 +03:00
|
|
|
new_status = [status]
|
2020-03-10 18:19:16 +03:00
|
|
|
else:
|
|
|
|
if status in self.expected[test_name]:
|
2020-03-23 18:25:33 +03:00
|
|
|
new_status = self.expected[test_name]
|
2020-03-10 18:19:16 +03:00
|
|
|
else:
|
2020-03-23 18:25:33 +03:00
|
|
|
new_status = [status]
|
|
|
|
new_expected[test_name] = new_status
|
2020-03-10 18:19:16 +03:00
|
|
|
return new_expected
|
|
|
|
|
2020-03-18 15:27:39 +03:00
|
|
|
def after_end(self, subset=False):
|
|
|
|
if not subset:
|
|
|
|
missing = set(self.expected) - set(self.test_results)
|
2020-06-25 08:39:22 +03:00
|
|
|
extra = set(self.test_results) - set(self.expected)
|
2020-03-18 15:27:39 +03:00
|
|
|
if missing:
|
|
|
|
self.has_unexpected = True
|
|
|
|
for test_name in missing:
|
|
|
|
self.logger.error("TEST-UNEXPECTED-MISSING %s" % (test_name,))
|
2020-06-25 08:39:22 +03:00
|
|
|
if self.expected and extra:
|
|
|
|
self.has_unexpected = True
|
|
|
|
for test_name in extra:
|
|
|
|
self.logger.error(
|
|
|
|
"TEST-UNEXPECTED-MISSING Unknown new test %s" % (test_name,)
|
2020-10-26 21:34:53 +03:00
|
|
|
)
|
2020-06-25 08:39:22 +03:00
|
|
|
|
|
|
|
if self.unexpected_skips:
|
|
|
|
self.has_unexpected = True
|
|
|
|
for test_name in self.unexpected_skips:
|
|
|
|
self.logger.error(
|
|
|
|
"TEST-UNEXPECTED-MISSING Unexpected skipped %s" % (test_name,)
|
2020-10-26 21:34:53 +03:00
|
|
|
)
|
2020-03-10 18:19:16 +03:00
|
|
|
self.logger.suite_end()
|
2019-08-16 15:58:15 +03:00
|
|
|
|
|
|
|
|
2019-08-16 15:58:06 +03:00
|
|
|
# tempfile.TemporaryDirectory missing from Python 2.7
|
|
|
|
class TemporaryDirectory(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.path = tempfile.mkdtemp()
|
|
|
|
self._closed = False
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<{} {!r}>".format(self.__class__.__name__, self.path)
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self.path
|
|
|
|
|
|
|
|
def __exit__(self, exc, value, tb):
|
|
|
|
self.clean()
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.clean()
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
if self.path and not self._closed:
|
|
|
|
shutil.rmtree(self.path)
|
|
|
|
self._closed = True
|
|
|
|
|
|
|
|
|
2019-08-16 15:58:15 +03:00
|
|
|
class PuppeteerRunner(MozbuildObject):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(PuppeteerRunner, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
self.remotedir = os.path.join(self.topsrcdir, "remote")
|
2020-07-02 21:46:42 +03:00
|
|
|
self.puppeteer_dir = os.path.join(self.remotedir, "test", "puppeteer")
|
2019-08-16 15:58:15 +03:00
|
|
|
|
2020-03-10 18:19:16 +03:00
|
|
|
def run_test(self, logger, *tests, **params):
|
2019-08-16 15:58:15 +03:00
|
|
|
"""
|
|
|
|
Runs Puppeteer unit tests with npm.
|
|
|
|
|
|
|
|
Possible optional test parameters:
|
|
|
|
|
|
|
|
`binary`:
|
|
|
|
Path for the browser binary to use. Defaults to the local
|
|
|
|
build.
|
|
|
|
`headless`:
|
|
|
|
Boolean to indicate whether to activate Firefox' headless mode.
|
|
|
|
`extra_prefs`:
|
|
|
|
Dictionary of extra preferences to write to the profile,
|
|
|
|
before invoking npm. Overrides default preferences.
|
2020-03-10 18:19:16 +03:00
|
|
|
`write_results`:
|
|
|
|
Path to write the results json file
|
2020-03-18 15:27:39 +03:00
|
|
|
`subset`
|
|
|
|
Indicates only a subset of tests are being run, so we should
|
|
|
|
skip the check for missing results
|
2019-08-16 15:58:15 +03:00
|
|
|
"""
|
|
|
|
setup()
|
|
|
|
|
|
|
|
binary = params.get("binary") or self.get_binary_path()
|
2019-11-08 14:58:08 +03:00
|
|
|
product = params.get("product", "firefox")
|
|
|
|
|
2020-06-05 21:53:38 +03:00
|
|
|
env = {
|
|
|
|
# Print browser process ouptut
|
|
|
|
"DUMPIO": "1",
|
|
|
|
# Checked by Puppeteer's custom mocha config
|
|
|
|
"CI": "1",
|
|
|
|
# Causes some tests to be skipped due to assumptions about install
|
|
|
|
"PUPPETEER_ALT_INSTALL": "1",
|
|
|
|
}
|
2019-11-08 14:58:08 +03:00
|
|
|
extra_options = {}
|
|
|
|
for k, v in params.get("extra_launcher_options", {}).items():
|
|
|
|
extra_options[k] = json.loads(v)
|
2019-08-16 15:58:15 +03:00
|
|
|
|
2020-06-05 21:53:38 +03:00
|
|
|
# Override upstream defaults: no retries, shorter timeout
|
|
|
|
mocha_options = [
|
|
|
|
"--reporter",
|
|
|
|
"./json-mocha-reporter.js",
|
|
|
|
"--retries",
|
|
|
|
"0",
|
|
|
|
"--fullTrace",
|
2020-10-22 17:11:16 +03:00
|
|
|
"--timeout",
|
|
|
|
"20000",
|
2020-07-02 21:52:52 +03:00
|
|
|
"--no-parallel",
|
2020-06-05 21:53:38 +03:00
|
|
|
]
|
2019-11-08 14:58:08 +03:00
|
|
|
if product == "firefox":
|
|
|
|
env["BINARY"] = binary
|
2020-06-05 21:53:38 +03:00
|
|
|
env["PUPPETEER_PRODUCT"] = "firefox"
|
|
|
|
command = ["run", "unit", "--"] + mocha_options
|
2019-08-16 15:58:15 +03:00
|
|
|
|
2019-10-10 18:10:48 +03:00
|
|
|
env["HEADLESS"] = str(params.get("headless", False))
|
2019-08-16 15:58:15 +03:00
|
|
|
|
2019-11-08 14:58:08 +03:00
|
|
|
prefs = {}
|
2019-08-16 15:58:15 +03:00
|
|
|
for k, v in params.get("extra_prefs", {}).items():
|
|
|
|
prefs[k] = mozprofile.Preferences.cast(v)
|
|
|
|
|
2019-11-08 14:58:08 +03:00
|
|
|
if prefs:
|
2020-01-10 17:56:21 +03:00
|
|
|
extra_options["extraPrefsFirefox"] = prefs
|
2019-08-16 15:58:15 +03:00
|
|
|
|
2019-11-08 14:58:08 +03:00
|
|
|
if extra_options:
|
|
|
|
env["EXTRA_LAUNCH_OPTIONS"] = json.dumps(extra_options)
|
2019-08-16 15:58:15 +03:00
|
|
|
|
2020-03-10 18:19:16 +03:00
|
|
|
expected_path = os.path.join(
|
|
|
|
os.path.dirname(__file__), "puppeteer-expected.json"
|
|
|
|
)
|
|
|
|
if product == "firefox" and os.path.exists(expected_path):
|
|
|
|
with open(expected_path) as f:
|
|
|
|
expected_data = json.load(f)
|
|
|
|
else:
|
|
|
|
expected_data = {}
|
|
|
|
|
|
|
|
output_handler = MochaOutputHandler(logger, expected_data)
|
2020-07-02 21:46:42 +03:00
|
|
|
proc = npm(
|
|
|
|
*command,
|
|
|
|
cwd=self.puppeteer_dir,
|
|
|
|
env=env,
|
2020-03-10 18:19:16 +03:00
|
|
|
processOutputLine=output_handler,
|
|
|
|
wait=False
|
|
|
|
)
|
|
|
|
output_handler.proc = proc
|
|
|
|
|
2020-06-05 21:53:38 +03:00
|
|
|
# Puppeteer unit tests don't always clean-up child processes in case of
|
|
|
|
# failure, so use an output_timeout as a fallback
|
|
|
|
wait_proc(proc, "npm", output_timeout=60, exit_on_fail=False)
|
2020-03-10 18:19:16 +03:00
|
|
|
|
2020-03-18 15:27:39 +03:00
|
|
|
output_handler.after_end(params.get("subset", False))
|
2020-03-10 18:19:16 +03:00
|
|
|
|
|
|
|
# Non-zero return codes are non-fatal for now since we have some
|
|
|
|
# issues with unresolved promises that shouldn't otherwise block
|
|
|
|
# running the tests
|
|
|
|
if proc.returncode != 0:
|
|
|
|
logger.warning("npm exited with code %s" % proc.returncode)
|
|
|
|
|
2020-03-23 18:25:10 +03:00
|
|
|
if params["write_results"]:
|
2020-03-10 18:19:16 +03:00
|
|
|
with open(params["write_results"], "w") as f:
|
2020-03-23 18:25:33 +03:00
|
|
|
json.dump(
|
|
|
|
output_handler.new_expected(), f, indent=2, separators=(",", ": ")
|
|
|
|
)
|
2020-03-10 18:19:16 +03:00
|
|
|
|
|
|
|
if output_handler.has_unexpected:
|
|
|
|
exit(1, "Got unexpected results")
|
|
|
|
|
|
|
|
|
|
|
|
def create_parser_puppeteer():
|
|
|
|
p = argparse.ArgumentParser()
|
|
|
|
p.add_argument(
|
|
|
|
"--product", type=str, default="firefox", choices=["chrome", "firefox"]
|
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
"--binary",
|
|
|
|
type=str,
|
|
|
|
help="Path to browser binary. Defaults to local Firefox build.",
|
|
|
|
)
|
2021-02-01 13:59:38 +03:00
|
|
|
p.add_argument(
|
|
|
|
"--ci",
|
|
|
|
action="store_true",
|
|
|
|
help="Flag that indicates that tests run in a CI environment.",
|
|
|
|
)
|
2020-03-10 18:19:16 +03:00
|
|
|
p.add_argument(
|
|
|
|
"--enable-fission",
|
|
|
|
action="store_true",
|
|
|
|
help="Enable Fission (site isolation) in Gecko.",
|
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
"-z", "--headless", action="store_true", help="Run browser in headless mode."
|
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
"--setpref",
|
|
|
|
action="append",
|
|
|
|
dest="extra_prefs",
|
|
|
|
metavar="<pref>=<value>",
|
|
|
|
help="Defines additional user preferences.",
|
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
"--setopt",
|
|
|
|
action="append",
|
|
|
|
dest="extra_options",
|
|
|
|
metavar="<option>=<value>",
|
|
|
|
help="Defines additional options for `puppeteer.launch`.",
|
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
"-v",
|
|
|
|
dest="verbosity",
|
|
|
|
action="count",
|
|
|
|
default=0,
|
|
|
|
help="Increase remote agent logging verbosity to include "
|
|
|
|
"debug level messages with -v, trace messages with -vv,"
|
|
|
|
"and to not truncate long trace messages with -vvv",
|
|
|
|
)
|
|
|
|
p.add_argument(
|
|
|
|
"--write-results",
|
|
|
|
action="store",
|
|
|
|
nargs="?",
|
|
|
|
default=None,
|
|
|
|
const=os.path.join(os.path.dirname(__file__), "puppeteer-expected.json"),
|
|
|
|
help="Path to write updated results to (defaults to the "
|
|
|
|
"expectations file if the argument is provided but "
|
|
|
|
"no path is passed)",
|
|
|
|
)
|
2020-03-18 15:27:39 +03:00
|
|
|
p.add_argument(
|
|
|
|
"--subset",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Indicate that only a subset of the tests are running, "
|
|
|
|
"so checks for missing tests should be skipped",
|
|
|
|
)
|
2020-03-10 18:19:16 +03:00
|
|
|
p.add_argument("tests", nargs="*")
|
|
|
|
mozlog.commandline.add_logging_group(p)
|
|
|
|
return p
|
2019-08-16 15:58:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
@CommandProvider
|
|
|
|
class PuppeteerTest(MachCommandBase):
|
|
|
|
@Command(
|
|
|
|
"puppeteer-test",
|
|
|
|
category="testing",
|
2020-03-10 18:19:16 +03:00
|
|
|
description="Run Puppeteer unit tests.",
|
|
|
|
parser=create_parser_puppeteer,
|
|
|
|
)
|
2020-01-13 14:39:33 +03:00
|
|
|
def puppeteer_test(
|
|
|
|
self,
|
|
|
|
binary=None,
|
2021-02-01 13:59:38 +03:00
|
|
|
ci=False,
|
2020-01-13 14:39:33 +03:00
|
|
|
enable_fission=False,
|
|
|
|
headless=False,
|
2020-07-02 21:46:49 +03:00
|
|
|
extra_prefs=None,
|
|
|
|
extra_options=None,
|
|
|
|
verbosity=0,
|
2020-03-18 15:27:39 +03:00
|
|
|
tests=None,
|
|
|
|
product="firefox",
|
|
|
|
write_results=None,
|
|
|
|
subset=False,
|
|
|
|
**kwargs
|
|
|
|
):
|
2020-10-26 21:34:53 +03:00
|
|
|
|
2021-02-01 13:59:38 +03:00
|
|
|
self.ci = ci
|
|
|
|
|
2020-03-10 18:19:16 +03:00
|
|
|
logger = mozlog.commandline.setup_logging(
|
|
|
|
"puppeteer-test", kwargs, {"mach": sys.stdout}
|
|
|
|
)
|
|
|
|
|
2019-08-16 15:58:15 +03:00
|
|
|
# moztest calls this programmatically with test objects or manifests
|
|
|
|
if "test_objects" in kwargs and tests is not None:
|
2020-03-10 18:19:16 +03:00
|
|
|
logger.error("Expected either 'test_objects' or 'tests'")
|
|
|
|
exit(1)
|
2019-08-16 15:58:15 +03:00
|
|
|
|
2019-11-08 14:58:08 +03:00
|
|
|
if product != "firefox" and extra_prefs is not None:
|
2020-03-10 18:19:16 +03:00
|
|
|
logger.error("User preferences are not recognized by %s" % product)
|
|
|
|
exit(1)
|
2019-11-08 14:58:08 +03:00
|
|
|
|
2019-08-16 15:58:15 +03:00
|
|
|
if "test_objects" in kwargs:
|
|
|
|
tests = []
|
|
|
|
for test in kwargs["test_objects"]:
|
|
|
|
tests.append(test["path"])
|
|
|
|
|
|
|
|
prefs = {}
|
|
|
|
for s in extra_prefs or []:
|
|
|
|
kv = s.split("=")
|
|
|
|
if len(kv) != 2:
|
2020-03-10 18:19:16 +03:00
|
|
|
logger.error("syntax error in --setpref={}".format(s))
|
|
|
|
exit(EX_USAGE)
|
2019-08-16 15:58:15 +03:00
|
|
|
prefs[kv[0]] = kv[1].strip()
|
|
|
|
|
2019-11-08 14:58:08 +03:00
|
|
|
options = {}
|
|
|
|
for s in extra_options or []:
|
|
|
|
kv = s.split("=")
|
|
|
|
if len(kv) != 2:
|
2020-03-10 18:19:16 +03:00
|
|
|
logger.error("syntax error in --setopt={}".format(s))
|
|
|
|
exit(EX_USAGE)
|
2019-11-08 14:58:08 +03:00
|
|
|
options[kv[0]] = kv[1].strip()
|
|
|
|
|
2020-01-06 12:55:01 +03:00
|
|
|
if enable_fission:
|
2020-07-31 16:06:08 +03:00
|
|
|
prefs.update(
|
|
|
|
{"fission.autostart": True, "dom.serviceWorkers.parent_intercept": True}
|
|
|
|
)
|
2020-01-06 12:55:01 +03:00
|
|
|
|
2020-01-13 14:39:33 +03:00
|
|
|
if verbosity == 1:
|
|
|
|
prefs["remote.log.level"] = "Debug"
|
|
|
|
elif verbosity > 1:
|
|
|
|
prefs["remote.log.level"] = "Trace"
|
2020-02-15 11:22:56 +03:00
|
|
|
if verbosity > 2:
|
|
|
|
prefs["remote.log.truncate"] = False
|
2020-01-13 14:39:33 +03:00
|
|
|
|
2019-11-08 14:58:08 +03:00
|
|
|
self.install_puppeteer(product)
|
2019-08-16 15:58:15 +03:00
|
|
|
|
|
|
|
params = {
|
|
|
|
"binary": binary,
|
|
|
|
"headless": headless,
|
|
|
|
"extra_prefs": prefs,
|
2019-11-08 14:58:08 +03:00
|
|
|
"product": product,
|
2020-03-10 18:19:16 +03:00
|
|
|
"extra_launcher_options": options,
|
2020-03-18 15:27:39 +03:00
|
|
|
"write_results": write_results,
|
|
|
|
"subset": subset,
|
|
|
|
}
|
2019-08-16 15:58:15 +03:00
|
|
|
puppeteer = self._spawn(PuppeteerRunner)
|
|
|
|
try:
|
2020-03-10 18:19:16 +03:00
|
|
|
return puppeteer.run_test(logger, *tests, **params)
|
2020-04-21 14:58:04 +03:00
|
|
|
except BinaryNotFoundException as e:
|
|
|
|
logger.error(e)
|
|
|
|
logger.info(e.help())
|
|
|
|
exit(1)
|
2019-08-16 15:58:15 +03:00
|
|
|
except Exception as e:
|
|
|
|
exit(EX_SOFTWARE, e)
|
|
|
|
|
2019-11-08 14:58:08 +03:00
|
|
|
def install_puppeteer(self, product):
|
2019-08-16 15:58:15 +03:00
|
|
|
setup()
|
2019-11-08 14:58:08 +03:00
|
|
|
env = {}
|
2020-06-05 21:53:38 +03:00
|
|
|
from mozversioncontrol import get_repository_object
|
2020-10-26 21:34:53 +03:00
|
|
|
|
2020-06-05 21:53:38 +03:00
|
|
|
repo = get_repository_object(self.topsrcdir)
|
|
|
|
puppeteer_dir = os.path.join("remote", "test", "puppeteer")
|
|
|
|
changed_files = False
|
|
|
|
for f in repo.get_changed_files():
|
2020-07-02 21:52:55 +03:00
|
|
|
if f.startswith(puppeteer_dir) and f.endswith(".ts"):
|
2020-06-05 21:53:38 +03:00
|
|
|
changed_files = True
|
|
|
|
break
|
|
|
|
|
2019-11-08 14:58:08 +03:00
|
|
|
if product != "chrome":
|
2020-06-05 21:53:38 +03:00
|
|
|
env["PUPPETEER_SKIP_DOWNLOAD"] = "1"
|
|
|
|
lib_dir = os.path.join(self.topsrcdir, puppeteer_dir, "lib")
|
|
|
|
if changed_files and os.path.isdir(lib_dir):
|
|
|
|
# clobber lib to force `tsc compile` step
|
|
|
|
shutil.rmtree(lib_dir)
|
2021-02-01 13:59:38 +03:00
|
|
|
|
|
|
|
command = "ci" if self.ci else "install"
|
|
|
|
npm(command, cwd=os.path.join(self.topsrcdir, puppeteer_dir), env=env)
|
2019-08-16 15:58:15 +03:00
|
|
|
|
|
|
|
|
2019-08-16 15:58:06 +03:00
|
|
|
def exit(code, error=None):
|
|
|
|
if error is not None:
|
|
|
|
if isinstance(error, Exception):
|
|
|
|
import traceback
|
2020-10-26 21:34:53 +03:00
|
|
|
|
2019-08-16 15:58:06 +03:00
|
|
|
traceback.print_exc()
|
|
|
|
else:
|
|
|
|
message = str(error).split("\n")[0].strip()
|
|
|
|
print("{}: {}".format(sys.argv[0], message), file=sys.stderr)
|
|
|
|
sys.exit(code)
|