summaryrefslogtreecommitdiffstats
path: root/testing/mozharness/mach_commands.py
blob: f453397db79d22efa63fb83151e028c5294adcd0 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# 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

import argparse
import os
import re
import subprocess
import sys
import urllib
import urlparse

import mozinfo

from mach.decorators import (
    CommandArgument,
    CommandProvider,
    Command,
)

from mozbuild.base import MachCommandBase, MozbuildObject
from mozbuild.base import MachCommandConditions as conditions
from argparse import ArgumentParser

def get_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument("suite_name", nargs=1, type=str, action="store",
                        help="Suite to run in mozharness")
    parser.add_argument("mozharness_args", nargs=argparse.REMAINDER,
                        help="Extra arguments to pass to mozharness")
    return parser

class MozharnessRunner(MozbuildObject):
    def __init__(self, *args, **kwargs):
        MozbuildObject.__init__(self, *args, **kwargs)


        self.test_packages_url = self._test_packages_url()
        self.installer_url = self._installer_url()

        desktop_unittest_config = [
            "--config-file", lambda: self.config_path("unittests",
                                                      "%s_unittest.py" % mozinfo.info['os']),
            "--config-file", lambda: self.config_path("developer_config.py")]

        self.config = {
            "__defaults__": {
                "config": ["--no-read-buildbot-config",
                           "--download-symbols", "ondemand",
                           "--installer-url", self.installer_url,
                           "--test-packages-url", self.test_packages_url]
            },

            "mochitest-valgrind": {
                "script": "desktop_unittest.py",
                "config": desktop_unittest_config + [
                    "--mochitest-suite", "valgrind-plain"]
            },
            "mochitest": {
                "script": "desktop_unittest.py",
                "config": desktop_unittest_config + [
                    "--mochitest-suite", "plain"]
            },
            "mochitest-chrome": {
                "script": "desktop_unittest.py",
                "config": desktop_unittest_config + [
                    "--mochitest-suite", "chrome"]
            },
            "mochitest-browser-chrome": {
                "script": "desktop_unittest.py",
                "config": desktop_unittest_config + [
                    "--mochitest-suite", "browser-chrome"]
            },
            "mochitest-devtools-chrome": {
                "script": "desktop_unittest.py",
                "config": desktop_unittest_config + [
                    "--mochitest-suite", "mochitest-devtools-chrome"]
            },
            "crashtest": {
                "script": "desktop_unittest.py",
                "config": desktop_unittest_config + [
                    "--reftest-suite", "crashtest"]
            },
            "jsreftest": {
                "script": "desktop_unittest.py",
                "config": desktop_unittest_config + [
                    "--reftest-suite", "jsreftest"]
            },
            "reftest": {
                "script": "desktop_unittest.py",
                "config": desktop_unittest_config + [
                    "--reftest-suite", "reftest"]
            },
            "reftest-no-accel": {
                "script": "desktop_unittest.py",
                "config": desktop_unittest_config + [
                    "--reftest-suite", "reftest-no-accel"]
            },
            "cppunittest": {
                "script": "desktop_unittest.py",
                "config": desktop_unittest_config + [
                    "--cppunittest-suite", "cppunittest"]
            },
            "xpcshell": {
                "script": "desktop_unittest.py",
                "config": desktop_unittest_config + [
                    "--xpcshell-suite", "xpcshell"]
            },
            "xpcshell-addons": {
                "script": "desktop_unittest.py",
                "config": desktop_unittest_config + [
                    "--xpcshell-suite", "xpcshell-addons"]
            },
            "jittest": {
                "script": "desktop_unittest.py",
                "config": desktop_unittest_config + [
                    "--jittest-suite", "jittest"]
            },
            "mozbase": {
                "script": "desktop_unittest.py",
                "config": desktop_unittest_config + [
                    "--mozbase-suite", "mozbase"]
            },
            "marionette": {
                "script": "marionette.py",
                "config": ["--config-file", self.config_path("marionette",
                                                             "test_config.py")]
            },
            "web-platform-tests": {
                "script": "web_platform_tests.py",
                "config": ["--config-file", self.config_path("web_platform_tests",
                                                             self.wpt_config)]
            },
        }


    def path_to_url(self, path):
        return urlparse.urljoin('file:', urllib.pathname2url(path))

    def _installer_url(self):
        package_re = {
            "linux": re.compile("^firefox-\d+\..+\.tar\.bz2$"),
            "win": re.compile("^firefox-\d+\..+\.installer\.exe$"),
            "mac": re.compile("^firefox-\d+\..+\.mac(?:64)?\.dmg$"),
        }[mozinfo.info['os']]
        dist_path = os.path.join(self.topobjdir, "dist")
        filenames = [item for item in os.listdir(dist_path) if
                     package_re.match(item)]
        assert len(filenames) == 1
        return self.path_to_url(os.path.join(dist_path, filenames[0]))

    def _test_packages_url(self):
        dist_path = os.path.join(self.topobjdir, "dist")
        filenames = [item for item in os.listdir(dist_path) if
                     item.endswith('test_packages.json')]
        assert len(filenames) == 1
        return self.path_to_url(os.path.join(dist_path, filenames[0]))

    def config_path(self, *parts):
        return self.path_to_url(os.path.join(self.topsrcdir, "testing", "mozharness",
                                             "configs", *parts))

    @property
    def wpt_config(self):
        return "test_config.py" if mozinfo.info['os'] != "win" else "test_config_windows.py"

    def run_suite(self, suite, **kwargs):
        default_config = self.config.get("__defaults__")
        suite_config = self.config.get(suite)

        if suite_config is None:
            print("Unknown suite %s" % suite)
            return 1

        script = os.path.join(self.topsrcdir, "testing", "mozharness",
                              "scripts", suite_config["script"])
        options = [item() if callable(item) else item
                   for item in default_config["config"] + suite_config["config"]]

        cmd = [script] + options

        rv = subprocess.call(cmd, cwd=os.path.dirname(script))
        return rv


@CommandProvider
class MozharnessCommands(MachCommandBase):
    @Command('mozharness', category='testing',
             description='Run tests using mozharness.',
             conditions=[conditions.is_firefox],
             parser=get_parser)
    def mozharness(self, **kwargs):
        runner = self._spawn(MozharnessRunner)
        return runner.run_suite(kwargs.pop("suite_name")[0], **kwargs)