summaryrefslogtreecommitdiffstats
path: root/layout/tools/reftest/selftest/test_reftest_output.py
blob: 3f3d8966e05ef3620759bc08d18d719c5b59eb68 (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
# 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

import os

try:
    # Python2
    from cStringIO import StringIO
except ImportError:
    # Python3
    from io import StringIO
from functools import partial

import mozunit
import pytest
from moztest.selftest.output import get_mozharness_status, filter_action

from mozharness.base.log import INFO, WARNING, ERROR
from mozharness.mozilla.automation import TBPL_SUCCESS, TBPL_WARNING, TBPL_FAILURE

here = os.path.abspath(os.path.dirname(__file__))
get_mozharness_status = partial(get_mozharness_status, "reftest")


def test_output_pass(runtests):
    status, lines = runtests("reftest-pass.list")
    assert status == 0

    tbpl_status, log_level, summary = get_mozharness_status(lines, status)
    assert tbpl_status == TBPL_SUCCESS
    assert log_level in (INFO, WARNING)

    test_status = filter_action("test_status", lines)
    assert len(test_status) == 3
    assert all(t["status"] == "PASS" for t in test_status)

    test_end = filter_action("test_end", lines)
    assert len(test_end) == 3
    assert all(t["status"] == "OK" for t in test_end)


def test_output_fail(runtests):
    formatter = pytest.importorskip("output").ReftestFormatter()

    status, lines = runtests("reftest-fail.list")
    assert status == 0

    buf = StringIO()
    tbpl_status, log_level, summary = get_mozharness_status(
        lines, status, formatter=formatter, buf=buf
    )

    assert tbpl_status == TBPL_WARNING
    assert log_level == WARNING

    test_status = filter_action("test_status", lines)
    assert len(test_status) == 3
    assert all(t["status"] == "FAIL" for t in test_status)
    assert all("reftest_screenshots" in t["extra"] for t in test_status)

    test_end = filter_action("test_end", lines)
    assert len(test_end) == 3
    assert all(t["status"] == "OK" for t in test_end)

    # ensure screenshots were printed
    formatted = buf.getvalue()
    assert "REFTEST   IMAGE 1" in formatted
    assert "REFTEST   IMAGE 2" in formatted


@pytest.mark.skip_mozinfo("!crashreporter")
def test_output_crash(runtests):
    status, lines = runtests(
        "reftest-crash.list", environment=["MOZ_CRASHREPORTER_SHUTDOWN=1"]
    )
    assert status == 1

    tbpl_status, log_level, summary = get_mozharness_status(lines, status)
    assert tbpl_status == TBPL_FAILURE
    assert log_level == ERROR

    crash = filter_action("crash", lines)
    assert len(crash) == 1
    assert crash[0]["action"] == "crash"
    assert crash[0]["signature"]
    assert crash[0]["minidump_path"]

    lines = filter_action("test_end", lines)
    assert len(lines) == 0


@pytest.mark.skip_mozinfo("!asan")
def test_output_asan(runtests):
    status, lines = runtests(
        "reftest-crash.list", environment=["MOZ_CRASHREPORTER_SHUTDOWN=1"]
    )
    assert status == 0

    tbpl_status, log_level, summary = get_mozharness_status(lines, status)
    assert tbpl_status == TBPL_FAILURE
    assert log_level == ERROR

    crash = filter_action("crash", lines)
    assert len(crash) == 0

    process_output = filter_action("process_output", lines)
    assert any("ERROR: AddressSanitizer" in l["data"] for l in process_output)


@pytest.mark.skip_mozinfo("!debug")
def test_output_assertion(runtests):
    status, lines = runtests("reftest-assert.list")
    assert status == 0

    tbpl_status, log_level, summary = get_mozharness_status(lines, status)
    assert tbpl_status == TBPL_WARNING
    assert log_level == WARNING

    test_status = filter_action("test_status", lines)
    assert len(test_status) == 1
    assert test_status[0]["status"] == "PASS"

    test_end = filter_action("test_end", lines)
    assert len(test_end) == 1
    assert test_end[0]["status"] == "OK"

    assertions = filter_action("assertion_count", lines)
    assert len(assertions) == 1
    assert assertions[0]["count"] == 1


@pytest.mark.skip_mozinfo("!debug")
def test_output_leak(monkeypatch, runtests):
    # Monkeypatch mozleak so we always process a failing leak log
    # instead of the actual one.
    import mozleak

    old_process_leak_log = mozleak.process_leak_log

    def process_leak_log(*args, **kwargs):
        return old_process_leak_log(
            os.path.join(here, "files", "leaks.log"), *args[1:], **kwargs
        )

    monkeypatch.setattr("mozleak.process_leak_log", process_leak_log)

    status, lines = runtests("reftest-pass.list")
    assert status == 0

    tbpl_status, log_level, summary = get_mozharness_status(lines, status)
    assert tbpl_status == TBPL_WARNING
    assert log_level == WARNING

    leaks = filter_action("mozleak_total", lines)
    assert len(leaks) == 1
    assert leaks[0]["process"] == "default"
    assert leaks[0]["bytes"] == 19915


if __name__ == "__main__":
    mozunit.main()