summaryrefslogtreecommitdiffstats
path: root/testing/talos/mach_commands.py
blob: 0b15af7a2cb33dea22584c1ffa1cbd32e28f8052 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 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/.

# Integrates Talos mozharness with mach

import json
import logging
import os
import socket
import sys

import six
from mach.decorators import Command
from mach.util import get_state_dir
from mozbuild.base import BinaryNotFoundException, MozbuildObject

HERE = os.path.dirname(os.path.realpath(__file__))


class TalosRunner(MozbuildObject):
    def run_test(self, talos_args):
        """
        We want to do couple of things before running Talos
        1. Clone mozharness
        2. Make config for Talos Mozharness
        3. Run mozharness
        """

        try:
            self.init_variables(talos_args)
        except BinaryNotFoundException as e:
            self.log(logging.ERROR, "talos", {"error": str(e)}, "ERROR: {error}")
            self.log(logging.INFO, "raptor", {"help": e.help()}, "{help}")
            return 1

        self.make_config()
        self.write_config()
        self.make_args()
        return self.run_mozharness()

    def init_variables(self, talos_args):
        self.talos_dir = os.path.join(self.topsrcdir, "testing", "talos")
        self.mozharness_dir = os.path.join(self.topsrcdir, "testing", "mozharness")
        self.talos_json = os.path.join(self.talos_dir, "talos.json")
        self.config_file_path = os.path.join(
            self._topobjdir, "testing", "talos-in_tree_conf.json"
        )
        self.binary_path = self.get_binary_path()
        self.virtualenv_script = os.path.join(
            self.topsrcdir, "third_party", "python", "virtualenv", "virtualenv.py"
        )
        self.virtualenv_path = os.path.join(self._topobjdir, "testing", "talos-venv")
        self.python_interp = sys.executable
        self.talos_args = talos_args

    def make_config(self):
        default_actions = ["populate-webroot"]
        default_actions.extend(
            [
                "create-virtualenv",
                "run-tests",
            ]
        )
        self.config = {
            "run_local": True,
            "talos_json": self.talos_json,
            "binary_path": self.binary_path,
            "repo_path": self.topsrcdir,
            "obj_path": self.topobjdir,
            "log_name": "talos",
            "virtualenv_path": self.virtualenv_path,
            "pypi_url": "http://pypi.python.org/simple",
            "base_work_dir": self.mozharness_dir,
            "exes": {
                "python": self.python_interp,
                "virtualenv": [self.python_interp, self.virtualenv_script],
            },
            "title": socket.gethostname(),
            "default_actions": default_actions,
            "talos_extra_options": ["--develop"] + self.talos_args,
            "python3_manifest": {
                "win32": "python3.manifest",
                "win64": "python3_x64.manifest",
            },
            "mozbuild_path": get_state_dir(),
        }

    def make_args(self):
        self.args = {
            "config": {},
            "initial_config_file": self.config_file_path,
        }

    def write_config(self):
        try:
            config_file = open(self.config_file_path, "wb")
            config_file.write(six.ensure_binary(json.dumps(self.config)))
        except IOError as e:
            err_str = "Error writing to Talos Mozharness config file {0}:{1}"
            print(err_str.format(self.config_file_path, str(e)))
            raise e

    def run_mozharness(self):
        sys.path.insert(0, self.mozharness_dir)
        from mozharness.mozilla.testing.talos import Talos

        talos_mh = Talos(
            config=self.args["config"],
            initial_config_file=self.args["initial_config_file"],
        )
        return talos_mh.run()


def create_parser():
    sys.path.insert(0, HERE)  # allow to import the talos package
    from talos.cmdline import create_parser

    return create_parser(mach_interface=True)


def setup_toolchain_artifacts(args, command_context):
    if not any(arg.lower() == "pdfpaint" for arg in args):
        return

    from mozbuild.bootstrap import bootstrap_toolchain

    print("Setting up pdfpaint PDFs...")
    bootstrap_toolchain("talos-pdfs")


@Command(
    "talos-test",
    category="testing",
    description="Run talos tests (performance testing).",
    parser=create_parser,
)
def run_talos_test(command_context, **kwargs):
    talos = command_context._spawn(TalosRunner)

    try:
        args = sys.argv[2:]
        setup_toolchain_artifacts(args, command_context)
        return talos.run_test(args)
    except Exception as e:
        print(str(e))
        return 1