summaryrefslogtreecommitdiffstats
path: root/taskcluster/gecko_taskgraph/actions/confirm_failure.py
blob: 84dbda29971656af8a7cf6ad47c070025479c55b (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# 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 logging
from functools import partial

from taskgraph.util.taskcluster import get_artifact, get_task_definition, list_artifacts

from .registry import register_callback_action
from .retrigger import retrigger_action
from .util import add_args_to_command, create_tasks, fetch_graph_and_labels

logger = logging.getLogger(__name__)


def get_failures(task_id, task_definition):
    """Returns a dict containing properties containing a list of
    directories containing test failures and a separate list of
    individual test failures from the errorsummary.log artifact for
    the task.

    Find test path to pass to the task in
    MOZHARNESS_TEST_PATHS.  If no appropriate test path can be
    determined, nothing is returned.
    """

    def fix_wpt_name(test):
        # TODO: find other cases to handle
        if ".any." in test:
            test = "%s.any.js" % test.split(".any.")[0]
        if ".window.html" in test:
            test = test.replace(".window.html", ".window.js")

        if test.startswith("/_mozilla"):
            test = "testing/web-platform/mozilla/tests" + test[len("_mozilla") :]
        else:
            test = "testing/web-platform/tests/" + test.strip("/")
        # some wpt tests have params, those are not supported
        test = test.split("?")[0]

        return test

    # collect dirs that don't have a specific manifest
    dirs = []
    tests = []

    artifacts = list_artifacts(task_id)
    for artifact in artifacts:
        if "name" not in artifact or not artifact["name"].endswith("errorsummary.log"):
            continue

        stream = get_artifact(task_id, artifact["name"])
        if not stream:
            continue

        # We handle the stream as raw bytes because it may contain invalid
        # UTF-8 characters in portions other than those containing the error
        # messages we're looking for.
        for line in stream.read().split(b"\n"):
            if not line.strip():
                continue

            l = json.loads(line)
            if "group_results" in l.keys() and l["status"] != "OK":
                dirs.append(l["group_results"].group())

            elif "test" in l.keys():
                if not l["test"]:
                    print("Warning: no testname in errorsummary line: %s" % l)
                    continue

                test_path = l["test"].split(" ")[0]
                found_path = False

                # tests with url params (wpt), will get confused here
                if "?" not in test_path:
                    test_path = test_path.split(":")[-1]

                # edge case where a crash on shutdown has a "test" name == group name
                if (
                    test_path.endswith(".toml")
                    or test_path.endswith(".ini")
                    or test_path.endswith(".list")
                ):
                    # TODO: consider running just the manifest
                    continue

                # edge cases with missing test names
                if (
                    test_path is None
                    or test_path == "None"
                    or "SimpleTest" in test_path
                ):
                    continue

                if "signature" in l.keys():
                    # dealing with a crash
                    found_path = True
                    if "web-platform" in task_definition["extra"]["suite"]:
                        test_path = fix_wpt_name(test_path)
                else:
                    if "status" not in l and "expected" not in l:
                        continue

                    if l["status"] != l["expected"]:
                        if l["status"] not in l.get("known_intermittent", []):
                            found_path = True
                            if "web-platform" in task_definition["extra"]["suite"]:
                                test_path = fix_wpt_name(test_path)

                if found_path and test_path:
                    fpath = test_path.replace("\\", "/")
                    tval = {"path": fpath, "group": l["group"]}
                    # only store one failure per test
                    if not [t for t in tests if t["path"] == fpath]:
                        tests.append(tval)

                # only run the failing test not both test + dir
                if l["group"] in dirs:
                    dirs.remove(l["group"])

            # TODO: 10 is too much; how to get only NEW failures?
            if len(tests) > 10:
                break

    dirs = [{"path": "", "group": d} for d in list(set(dirs))]
    return {"dirs": dirs, "tests": tests}


def get_repeat_args(task_definition, failure_group):
    task_name = task_definition["metadata"]["name"]
    repeatable_task = False
    if (
        "crashtest" in task_name
        or "mochitest" in task_name
        or "reftest" in task_name
        or "xpcshell" in task_name
        or "web-platform" in task_name
        and "jsreftest" not in task_name
    ):
        repeatable_task = True

    repeat_args = ""
    if not repeatable_task:
        return repeat_args

    if failure_group == "dirs":
        # execute 3 total loops
        repeat_args = ["--repeat=2"] if repeatable_task else []
    elif failure_group == "tests":
        # execute 5 total loops
        repeat_args = ["--repeat=4"] if repeatable_task else []

    return repeat_args


def confirm_modifier(task, input):
    if task.label != input["label"]:
        return task

    logger.debug(f"Modifying paths for {task.label}")

    # If the original task has defined test paths
    suite = input.get("suite")
    test_path = input.get("test_path")
    test_group = input.get("test_group")
    if test_path or test_group:
        repeat_args = input.get("repeat_args")

        if repeat_args:
            task.task["payload"]["command"] = add_args_to_command(
                task.task["payload"]["command"], extra_args=repeat_args
            )

        # TODO: do we need this attribute?
        task.attributes["test_path"] = test_path

        task.task["payload"]["env"]["MOZHARNESS_TEST_PATHS"] = json.dumps(
            {suite: [test_group]}, sort_keys=True
        )
        task.task["payload"]["env"]["MOZHARNESS_CONFIRM_PATHS"] = json.dumps(
            {suite: [test_path]}, sort_keys=True
        )
        task.task["payload"]["env"]["MOZLOG_DUMP_ALL_TESTS"] = "1"

        task.task["metadata"]["name"] = task.label
        task.task["tags"]["action"] = "confirm-failure"
    return task


@register_callback_action(
    name="confirm-failures",
    title="Confirm failures in job",
    symbol="cf",
    description="Re-run Tests for original manifest, directories or tests for failing tests.",
    order=150,
    context=[{"kind": "test"}],
    schema={
        "type": "object",
        "properties": {
            "label": {"type": "string", "description": "A task label"},
            "suite": {"type": "string", "description": "Test suite"},
            "test_path": {"type": "string", "description": "A full path to test"},
            "test_group": {
                "type": "string",
                "description": "A full path to group name",
            },
            "repeat_args": {
                "type": "string",
                "description": "args to pass to test harness",
            },
        },
        "additionalProperties": False,
    },
)
def confirm_failures(parameters, graph_config, input, task_group_id, task_id):
    task_definition = get_task_definition(task_id)
    decision_task_id, full_task_graph, label_to_taskid, _ = fetch_graph_and_labels(
        parameters, graph_config
    )

    # create -cf label; ideally make this a common function
    task_definition["metadata"]["name"].split("-")
    cfname = "%s-cf" % task_definition["metadata"]["name"]

    if cfname not in full_task_graph.tasks:
        raise Exception(f"{cfname} was not found in the task-graph")

    to_run = [cfname]

    suite = task_definition["extra"]["suite"]
    if "-coverage" in suite:
        suite = suite[: suite.index("-coverage")]
    if "-qr" in suite:
        suite = suite[: suite.index("-qr")]
    failures = get_failures(task_id, task_definition)

    if failures["dirs"] == [] and failures["tests"] == []:
        logger.info("need to retrigger task as no specific test failures found")
        retrigger_action(parameters, graph_config, input, decision_task_id, task_id)
        return

    # for each unique failure, create a new confirm failure job
    for failure_group in failures:
        for failure_path in failures[failure_group]:
            repeat_args = get_repeat_args(task_definition, failure_group)

            input = {
                "label": cfname,
                "suite": suite,
                "test_path": failure_path["path"],
                "test_group": failure_path["group"],
                "repeat_args": repeat_args,
            }

            logger.info("confirm_failures: %s" % failures)
            create_tasks(
                graph_config,
                to_run,
                full_task_graph,
                label_to_taskid,
                parameters,
                decision_task_id,
                modifier=partial(confirm_modifier, input=input),
            )