summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/moztest/moztest/results.py
blob: 5193a9db2b71644e0dad866a41735848ec0b5c3e (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# 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 os
import time

import mozinfo
import six


class TestContext(object):
    """Stores context data about the test"""

    attrs = [
        "hostname",
        "arch",
        "env",
        "os",
        "os_version",
        "tree",
        "revision",
        "product",
        "logfile",
        "testgroup",
        "harness",
        "buildtype",
    ]

    def __init__(
        self,
        hostname="localhost",
        tree="",
        revision="",
        product="",
        logfile=None,
        arch="",
        operating_system="",
        testgroup="",
        harness="moztest",
        buildtype="",
    ):
        self.hostname = hostname
        self.arch = arch or mozinfo.processor
        self.env = os.environ.copy()
        self.os = operating_system or mozinfo.os
        self.os_version = mozinfo.version
        self.tree = tree
        self.revision = revision
        self.product = product
        self.logfile = logfile
        self.testgroup = testgroup
        self.harness = harness
        self.buildtype = buildtype

    def __str__(self):
        return "%s (%s, %s)" % (self.hostname, self.os, self.arch)

    def __repr__(self):
        return "<%s>" % self.__str__()

    def __eq__(self, other):
        if not isinstance(other, TestContext):
            return False
        diffs = [a for a in self.attrs if getattr(self, a) != getattr(other, a)]
        return len(diffs) == 0

    def __hash__(self):
        def get(attr):
            value = getattr(self, attr)
            if isinstance(value, dict):
                value = frozenset(six.iteritems(value))
            return value

        return hash(frozenset([get(a) for a in self.attrs]))


class TestResult(object):
    """Stores test result data"""

    FAIL_RESULTS = [
        "UNEXPECTED-PASS",
        "UNEXPECTED-FAIL",
        "ERROR",
    ]
    COMPUTED_RESULTS = FAIL_RESULTS + [
        "PASS",
        "KNOWN-FAIL",
        "SKIPPED",
    ]
    POSSIBLE_RESULTS = [
        "PASS",
        "FAIL",
        "SKIP",
        "ERROR",
    ]

    def __init__(
        self, name, test_class="", time_start=None, context=None, result_expected="PASS"
    ):
        """Create a TestResult instance.
        name = name of the test that is running
        test_class = the class that the test belongs to
        time_start = timestamp (seconds since UNIX epoch) of when the test started
                     running; if not provided, defaults to the current time
                     ! Provide 0 if you only have the duration
        context = TestContext instance; can be None
        result_expected = string representing the expected outcome of the test"""

        msg = "Result '%s' not in possible results: %s" % (
            result_expected,
            ", ".join(self.POSSIBLE_RESULTS),
        )
        assert isinstance(name, six.string_types), "name has to be a string"
        assert result_expected in self.POSSIBLE_RESULTS, msg

        self.name = name
        self.test_class = test_class
        self.context = context
        self.time_start = time_start if time_start is not None else time.time()
        self.time_end = None
        self._result_expected = result_expected
        self._result_actual = None
        self.result = None
        self.filename = None
        self.description = None
        self.output = []
        self.reason = None

    @property
    def test_name(self):
        return "%s.py %s.%s" % (
            self.test_class.split(".")[0],
            self.test_class,
            self.name,
        )

    def __str__(self):
        return "%s | %s (%s) | %s" % (
            self.result or "PENDING",
            self.name,
            self.test_class,
            self.reason,
        )

    def __repr__(self):
        return "<%s>" % self.__str__()

    def calculate_result(self, expected, actual):
        if actual == "ERROR":
            return "ERROR"
        if actual == "SKIP":
            return "SKIPPED"

        if expected == "PASS":
            if actual == "PASS":
                return "PASS"
            if actual == "FAIL":
                return "UNEXPECTED-FAIL"

        if expected == "FAIL":
            if actual == "PASS":
                return "UNEXPECTED-PASS"
            if actual == "FAIL":
                return "KNOWN-FAIL"

        # if actual is skip or error, we return at the beginning, so if we get
        # here it is definitely some kind of error
        return "ERROR"

    def infer_results(self, computed_result):
        assert computed_result in self.COMPUTED_RESULTS
        if computed_result == "UNEXPECTED-PASS":
            expected = "FAIL"
            actual = "PASS"
        elif computed_result == "UNEXPECTED-FAIL":
            expected = "PASS"
            actual = "FAIL"
        elif computed_result == "KNOWN-FAIL":
            expected = actual = "FAIL"
        elif computed_result == "SKIPPED":
            expected = actual = "SKIP"
        else:
            return
        self._result_expected = expected
        self._result_actual = actual

    def finish(self, result, time_end=None, output=None, reason=None):
        """Marks the test as finished, storing its end time and status
        ! Provide the duration as time_end if you only have that."""

        if result in self.POSSIBLE_RESULTS:
            self._result_actual = result
            self.result = self.calculate_result(
                self._result_expected, self._result_actual
            )
        elif result in self.COMPUTED_RESULTS:
            self.infer_results(result)
            self.result = result
        else:
            valid = self.POSSIBLE_RESULTS + self.COMPUTED_RESULTS
            msg = "Result '%s' not valid. Need one of: %s" % (result, ", ".join(valid))
            raise ValueError(msg)

        # use lists instead of multiline strings
        if isinstance(output, six.string_types):
            output = output.splitlines()

        self.time_end = time_end if time_end is not None else time.time()
        self.output = output or self.output
        self.reason = reason

    @property
    def finished(self):
        """Boolean saying if the test is finished or not"""
        return self.result is not None

    @property
    def duration(self):
        """Returns the time it took for the test to finish. If the test is
        not finished, returns the elapsed time so far"""
        if self.result is not None:
            return self.time_end - self.time_start
        else:
            # returns the elapsed time
            return time.time() - self.time_start


class TestResultCollection(list):
    """Container class that stores test results"""

    resultClass = TestResult

    def __init__(self, suite_name, time_taken=0, resultClass=None):
        list.__init__(self)
        self.suite_name = suite_name
        self.time_taken = time_taken
        if resultClass is not None:
            self.resultClass = resultClass

    def __str__(self):
        return "%s (%.2fs)\n%s" % (self.suite_name, self.time_taken, list.__str__(self))

    def subset(self, predicate):
        tests = self.filter(predicate)
        duration = 0
        sub = TestResultCollection(self.suite_name)
        for t in tests:
            sub.append(t)
            duration += t.duration
        sub.time_taken = duration
        return sub

    @property
    def contexts(self):
        """List of unique contexts for the test results contained"""
        cs = [tr.context for tr in self]
        return list(set(cs))

    def filter(self, predicate):
        """Returns a generator of TestResults that satisfy a given predicate"""
        return (tr for tr in self if predicate(tr))

    def tests_with_result(self, result):
        """Returns a generator of TestResults with the given result"""
        msg = "Result '%s' not in possible results: %s" % (
            result,
            ", ".join(self.resultClass.COMPUTED_RESULTS),
        )
        assert result in self.resultClass.COMPUTED_RESULTS, msg
        return self.filter(lambda t: t.result == result)

    @property
    def tests(self):
        """Generator of all tests in the collection"""
        return (t for t in self)

    def add_result(
        self,
        test,
        result_expected="PASS",
        result_actual="PASS",
        output="",
        context=None,
    ):
        def get_class(test):
            return test.__class__.__module__ + "." + test.__class__.__name__

        t = self.resultClass(
            name=str(test).split()[0],
            test_class=get_class(test),
            time_start=0,
            result_expected=result_expected,
            context=context,
        )
        t.finish(result_actual, time_end=0, reason=relevant_line(output), output=output)
        self.append(t)

    @property
    def num_failures(self):
        fails = 0
        for t in self:
            if t.result in self.resultClass.FAIL_RESULTS:
                fails += 1
        return fails

    def add_unittest_result(self, result, context=None):
        """Adds the python unittest result provided to the collection"""
        if hasattr(result, "time_taken"):
            self.time_taken += result.time_taken

        for test, output in result.errors:
            self.add_result(test, result_actual="ERROR", output=output)

        for test, output in result.failures:
            self.add_result(test, result_actual="FAIL", output=output)

        if hasattr(result, "unexpectedSuccesses"):
            for test in result.unexpectedSuccesses:
                self.add_result(test, result_expected="FAIL", result_actual="PASS")

        if hasattr(result, "skipped"):
            for test, output in result.skipped:
                self.add_result(
                    test, result_expected="SKIP", result_actual="SKIP", output=output
                )

        if hasattr(result, "expectedFailures"):
            for test, output in result.expectedFailures:
                self.add_result(
                    test, result_expected="FAIL", result_actual="FAIL", output=output
                )

        # unittest does not store these by default
        if hasattr(result, "tests_passed"):
            for test in result.tests_passed:
                self.add_result(test)

    @classmethod
    def from_unittest_results(cls, context, *results):
        """Creates a TestResultCollection containing the given python
        unittest results"""

        if not results:
            return cls("from unittest")

        # all the TestResult instances share the same context
        context = context or TestContext()

        collection = cls("from %s" % results[0].__class__.__name__)

        for result in results:
            collection.add_unittest_result(result, context)

        return collection


# used to get exceptions/errors from tracebacks
def relevant_line(s):
    KEYWORDS = ("Error:", "Exception:", "error:", "exception:")
    lines = s.splitlines()
    for line in lines:
        for keyword in KEYWORDS:
            if keyword in line:
                return line
    return "N/A"