def get_running_env(**kwargs): from mozbuild.base import MozbuildObject config = MozbuildObject.from_environment() mach_cmd = MagicMock() def get_binary_path(*args): return "" mach_cmd.get_binary_path = get_binary_path mach_cmd.topsrcdir = config.topsrcdir mach_cmd.topobjdir = config.topobjdir mach_cmd.distdir = config.distdir mach_cmd._mach_context = MagicMock() mach_cmd._mach_context.state_dir = tempfile.mkdtemp() mach_cmd.run_process.return_value = 0 mach_args = { "flavor": "desktop-browser", "test_objects": None, "resolve_tests": True, "browsertime-clobber": False, "browsertime-install-url": None, } mach_args.update(kwargs) hooks = Hooks(mach_cmd, mach_args.pop("hooks", None)) env = MachEnvironment(mach_cmd, hooks=hooks, **mach_args) metadata = Metadata(mach_cmd, env, "desktop-browser") return mach_cmd, metadata, env
def get_running_env(**kwargs): from mozbuild.base import MozbuildObject config = MozbuildObject.from_environment() mach_cmd = MagicMock() def get_binary_path(*args): return "" def run_pip(args): pip = Path(sys.executable).parent / "pip" subprocess.check_call( [str(pip)] + args, stderr=subprocess.STDOUT, cwd=config.topsrcdir, universal_newlines=True, ) mach_cmd.get_binary_path = get_binary_path mach_cmd.topsrcdir = config.topsrcdir mach_cmd.topobjdir = config.topobjdir mach_cmd.distdir = config.distdir mach_cmd._mach_context = MagicMock() mach_cmd._mach_context.state_dir = tempfile.mkdtemp() mach_cmd.run_process.return_value = 0 mach_cmd.virtualenv_manager = MagicMock() mach_cmd.virtualenv_manager.python_path = sys.executable mach_cmd.virtualenv_manager.bin_path = Path(sys.executable).parent mach_cmd.virtualenv_manager._run_pip = run_pip mach_args = { "flavor": "desktop-browser", "test_objects": None, "resolve_tests": True, "browsertime-clobber": False, "browsertime-install-url": None, } mach_args.update(kwargs) hooks = Hooks(mach_cmd, mach_args.pop("hooks", None)) tests = mach_args.get("tests", []) if len(tests) > 0: script = ScriptInfo(tests[0]) else: script = None env = MachEnvironment(mach_cmd, hooks=hooks, **mach_args) metadata = Metadata(mach_cmd, env, "desktop-browser", script) return mach_cmd, metadata, env
def __init__(self, mach_cmd, flavor="desktop-browser", hooks=None, **kwargs): MachLogger.__init__(self, mach_cmd) self._mach_cmd = mach_cmd self._mach_args = dict([(self._normalize(key), value) for key, value in kwargs.items()]) self.layers = [] if flavor not in FLAVORS: raise NotImplementedError(flavor) for layer in (pick_system, pick_test, pick_metrics): self.add_layer(layer(self, flavor, mach_cmd)) if hooks is None: # we just load an empty Hooks instance hooks = Hooks(mach_cmd) self.hooks = hooks
def _get_env(hooks_path): return MachEnvironment(mock.MagicMock(), hooks=Hooks(mock.MagicMock(), hooks_path))
def run_tests(mach_cmd, **kwargs): """This tests runner can be used directly via main or via Mach. When the --on-try option is used, the test runner looks at the `PERFTEST_OPTIONS` environment variable that contains all options passed by the user via a ./mach perftest --push-to-try call. """ _setup_path() on_try = kwargs.pop("on_try", False) # trying to get the arguments from the task params if on_try: try_options = json.loads(os.environ["PERFTEST_OPTIONS"]) kwargs.update(try_options) from mozperftest.utils import build_test_list from mozperftest import MachEnvironment, Metadata from mozperftest.hooks import Hooks hooks = Hooks(mach_cmd, kwargs.pop("hooks", None)) verbose = kwargs.get("verbose", False) log_level = logging.DEBUG if verbose else logging.INFO # If we run through mach, we just want to set the level # of the existing termminal handler. # Otherwise, we're adding it. if mach_cmd.log_manager.terminal_handler is not None: mach_cmd.log_manager.terminal_handler.level = log_level else: mach_cmd.log_manager.add_terminal_logging(level=log_level) mach_cmd.log_manager.enable_all_structured_loggers() mach_cmd.log_manager.enable_unstructured() try: hooks.run("before_iterations", kwargs) for iteration in range(kwargs.get("test_iterations", 1)): flavor = kwargs["flavor"] kwargs["tests"], tmp_dir = build_test_list( kwargs["tests"], randomized=flavor != "doc") try: # XXX this doc is specific to browsertime scripts # maybe we want to move it if flavor == "doc": from mozperftest.test.browsertime.script import ScriptInfo for test in kwargs["tests"]: print(ScriptInfo(test)) return env = MachEnvironment(mach_cmd, hooks=hooks, **kwargs) metadata = Metadata(mach_cmd, env, flavor) hooks.run("before_runs", env) try: with env.frozen() as e: e.run(metadata) finally: hooks.run("after_runs", env) finally: if tmp_dir is not None: shutil.rmtree(tmp_dir) finally: hooks.cleanup()
def run_tests(mach_cmd, kwargs, client_args): """This tests runner can be used directly via main or via Mach. When the --on-try option is used, the test runner looks at the `PERFTEST_OPTIONS` environment variable that contains all options passed by the user via a ./mach perftest --push-to-try call. """ _setup_path() on_try = kwargs.pop("on_try", False) # trying to get the arguments from the task params if on_try: try_options = json.loads(os.environ["PERFTEST_OPTIONS"]) print("Loading options from $PERFTEST_OPTIONS") print(json.dumps(try_options, indent=4, sort_keys=True)) kwargs.update(try_options) from mozperftest.utils import build_test_list from mozperftest import MachEnvironment, Metadata from mozperftest.hooks import Hooks from mozperftest.script import ScriptInfo hooks_file = kwargs.pop("hooks", None) hooks = Hooks(mach_cmd, hooks_file) verbose = kwargs.get("verbose", False) log_level = logging.DEBUG if verbose else logging.INFO # If we run through mach, we just want to set the level # of the existing termminal handler. # Otherwise, we're adding it. if mach_cmd.log_manager.terminal_handler is not None: mach_cmd.log_manager.terminal_handler.level = log_level else: mach_cmd.log_manager.add_terminal_logging(level=log_level) mach_cmd.log_manager.enable_all_structured_loggers() mach_cmd.log_manager.enable_unstructured() try: # Only pass the virtualenv to the before_iterations hook # so that users can install test-specific packages if needed. mach_cmd.activate_virtualenv() kwargs["virtualenv"] = mach_cmd.virtualenv_manager hooks.run("before_iterations", kwargs) del kwargs["virtualenv"] tests, tmp_dir = build_test_list(kwargs["tests"]) for test in tests: script = ScriptInfo(test) # update the arguments with options found in the script, if any args = script.update_args(**client_args) # XXX this should be the default pool for update_args for key, value in kwargs.items(): if key not in args: args[key] = value # update the hooks, or use a copy of the general one script_hooks = Hooks(mach_cmd, args.pop("hooks", hooks_file)) flavor = args["flavor"] if flavor == "doc": print(script) continue for iteration in range(args.get("test_iterations", 1)): try: env = MachEnvironment(mach_cmd, hooks=script_hooks, **args) metadata = Metadata(mach_cmd, env, flavor, script) script_hooks.run("before_runs", env) try: with env.frozen() as e: e.run(metadata) finally: script_hooks.run("after_runs", env) finally: if tmp_dir is not None: shutil.rmtree(tmp_dir) finally: hooks.cleanup()