Allow a port to run tests with a custom device setup
[WebKit-https.git] / Tools / Scripts / webkitpy / layout_tests / models / test_run_results.py
1 # Copyright (C) 2010 Google Inc. All rights reserved.
2 # Copyright (C) 2010 Gabor Rapcsanyi (rgabor@inf.u-szeged.hu), University of Szeged
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 #     * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #     * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #     * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 import datetime
31 import logging
32 import signal
33
34 from webkitpy.layout_tests.models import test_expectations
35 from webkitpy.layout_tests.models import test_failures
36
37
38 _log = logging.getLogger(__name__)
39
40 INTERRUPTED_EXIT_STATUS = signal.SIGINT + 128
41
42
43 class TestRunResults(object):
44     def __init__(self, expectations, num_tests):
45         self.total = num_tests
46         self.remaining = self.total
47         self.expectations = expectations
48         self.expected = 0
49         self.unexpected = 0
50         self.unexpected_failures = 0
51         self.unexpected_crashes = 0
52         self.unexpected_timeouts = 0
53         self.tests_by_expectation = {}
54         self.tests_by_timeline = {}
55         self.results_by_name = {}  # Map of test name to the last result for the test.
56         self.all_results = []  # All results from a run, including every iteration of every test.
57         self.unexpected_results_by_name = {}
58         self.failures_by_name = {}
59         self.total_failures = 0
60         self.expected_skips = 0
61         for expectation in test_expectations.TestExpectations.EXPECTATIONS.values():
62             self.tests_by_expectation[expectation] = set()
63         for timeline in test_expectations.TestExpectations.TIMELINES.values():
64             self.tests_by_timeline[timeline] = expectations.model().get_tests_with_timeline(timeline)
65         self.slow_tests = set()
66         self.interrupted = False
67         self.keyboard_interrupted = False
68
69     def add(self, test_result, expected, test_is_slow):
70         self.tests_by_expectation[test_result.type].add(test_result.test_name)
71         self.results_by_name[test_result.test_name] = test_result
72         if test_result.is_other_crash:
73             return
74         if test_result.type != test_expectations.SKIP:
75             self.all_results.append(test_result)
76         self.remaining -= 1
77         if len(test_result.failures):
78             self.total_failures += 1
79             self.failures_by_name[test_result.test_name] = test_result.failures
80         if expected:
81             self.expected += 1
82             if test_result.type == test_expectations.SKIP:
83                 self.expected_skips += 1
84         else:
85             self.unexpected_results_by_name[test_result.test_name] = test_result
86             self.unexpected += 1
87             if len(test_result.failures):
88                 self.unexpected_failures += 1
89             if test_result.type == test_expectations.CRASH:
90                 self.unexpected_crashes += 1
91             elif test_result.type == test_expectations.TIMEOUT:
92                 self.unexpected_timeouts += 1
93         if test_is_slow:
94             self.slow_tests.add(test_result.test_name)
95
96     def merge(self, test_run_results):
97         # self.expectations should be the same for both
98         self.total += test_run_results.total
99         self.remaining += test_run_results.remaining
100         self.expected += test_run_results.expected
101         self.unexpected += test_run_results.unexpected
102         self.unexpected_failures += test_run_results.unexpected_failures
103         self.unexpected_crashes += test_run_results.unexpected_crashes
104         self.unexpected_timeouts += test_run_results.unexpected_timeouts
105         self.tests_by_expectation.update(test_run_results.tests_by_expectation)
106         self.tests_by_timeline.update(test_run_results.tests_by_timeline)
107         self.results_by_name.update(test_run_results.results_by_name)
108         self.all_results += test_run_results.all_results
109         self.unexpected_results_by_name.update(test_run_results.unexpected_results_by_name)
110         self.failures_by_name.update(test_run_results.failures_by_name)
111         self.total_failures += test_run_results.total_failures
112         self.expected_skips += test_run_results.expected_skips
113         self.tests_by_expectation.update(test_run_results.tests_by_expectation)
114         self.tests_by_timeline.update(test_run_results.tests_by_timeline)
115         self.slow_tests.update(test_run_results.slow_tests)
116
117         self.interrupted |= test_run_results.interrupted
118         self.keyboard_interrupted |= test_run_results.keyboard_interrupted
119         return self
120
121 class RunDetails(object):
122     def __init__(self, exit_code, summarized_results=None, initial_results=None, retry_results=None, enabled_pixel_tests_in_retry=False):
123         self.exit_code = exit_code
124         self.summarized_results = summarized_results
125         self.initial_results = initial_results
126         self.retry_results = retry_results
127         self.enabled_pixel_tests_in_retry = enabled_pixel_tests_in_retry
128
129
130 def _interpret_test_failures(failures):
131     test_dict = {}
132     failure_types = [type(failure) for failure in failures]
133     # FIXME: get rid of all this is_* values once there is a 1:1 map between
134     # TestFailure type and test_expectations.EXPECTATION.
135     if test_failures.FailureMissingAudio in failure_types:
136         test_dict['is_missing_audio'] = True
137
138     if test_failures.FailureMissingResult in failure_types:
139         test_dict['is_missing_text'] = True
140
141     if test_failures.FailureMissingImage in failure_types or test_failures.FailureMissingImageHash in failure_types:
142         test_dict['is_missing_image'] = True
143
144     if 'image_diff_percent' not in test_dict:
145         for failure in failures:
146             if isinstance(failure, test_failures.FailureImageHashMismatch) or isinstance(failure, test_failures.FailureReftestMismatch):
147                 test_dict['image_diff_percent'] = failure.diff_percent
148
149     return test_dict
150
151
152 # These results must match ones in print_unexpected_results() in views/buildbot_results.py.
153 def summarize_results(port_obj, expectations, initial_results, retry_results, enabled_pixel_tests_in_retry, include_passes=False, include_time_and_modifiers=False):
154     """Returns a dictionary containing a summary of the test runs, with the following fields:
155         'version': a version indicator
156         'fixable': The number of fixable tests (NOW - PASS)
157         'skipped': The number of skipped tests (NOW & SKIPPED)
158         'num_regressions': The number of non-flaky failures
159         'num_flaky': The number of flaky failures
160         'num_missing': The number of tests with missing results
161         'num_passes': The number of unexpected passes
162         'tests': a dict of tests -> {'expected': '...', 'actual': '...'}
163         'date': the current date and time
164     """
165     results = {}
166     results['version'] = 4
167
168     tbe = initial_results.tests_by_expectation
169     tbt = initial_results.tests_by_timeline
170     results['fixable'] = len(tbt[test_expectations.NOW] - tbe[test_expectations.PASS])
171     results['skipped'] = len(tbt[test_expectations.NOW] & tbe[test_expectations.SKIP])
172
173     num_passes = 0
174     num_flaky = 0
175     num_missing = 0
176     num_regressions = 0
177     keywords = {}
178     for expecation_string, expectation_enum in test_expectations.TestExpectations.EXPECTATIONS.iteritems():
179         keywords[expectation_enum] = expecation_string.upper()
180
181     for modifier_string, modifier_enum in test_expectations.TestExpectations.MODIFIERS.iteritems():
182         keywords[modifier_enum] = modifier_string.upper()
183
184     tests = {}
185     other_crashes_dict = {}
186
187     for test_name, result in initial_results.results_by_name.iteritems():
188         # Note that if a test crashed in the original run, we ignore
189         # whether or not it crashed when we retried it (if we retried it),
190         # and always consider the result not flaky.
191         expected = expectations.model().get_expectations_string(test_name)
192         result_type = result.type
193         actual = [keywords[result_type]]
194
195         if result_type == test_expectations.SKIP:
196             continue
197
198         if result.is_other_crash:
199             other_crashes_dict[test_name] = {}
200             continue
201
202         test_dict = {}
203         if result.has_stderr:
204             test_dict['has_stderr'] = True
205
206         if result.reftest_type:
207             test_dict.update(reftest_type=list(result.reftest_type))
208
209         if expectations.model().has_modifier(test_name, test_expectations.WONTFIX):
210             test_dict['wontfix'] = True
211
212         if result_type == test_expectations.PASS:
213             num_passes += 1
214             # FIXME: include passing tests that have stderr output.
215             if expected == 'PASS' and not include_passes:
216                 continue
217         elif result_type == test_expectations.CRASH:
218             if test_name in initial_results.unexpected_results_by_name:
219                 num_regressions += 1
220                 test_dict['report'] = 'REGRESSION'
221         elif result_type == test_expectations.MISSING:
222             if test_name in initial_results.unexpected_results_by_name:
223                 num_missing += 1
224                 test_dict['report'] = 'MISSING'
225         elif test_name in initial_results.unexpected_results_by_name:
226             if retry_results and test_name not in retry_results.unexpected_results_by_name:
227                 actual.extend(expectations.model().get_expectations_string(test_name).split(" "))
228                 num_flaky += 1
229                 test_dict['report'] = 'FLAKY'
230             elif retry_results:
231                 retry_result_type = retry_results.unexpected_results_by_name[test_name].type
232                 if result_type != retry_result_type:
233                     if enabled_pixel_tests_in_retry and result_type == test_expectations.TEXT and (retry_result_type == test_expectations.IMAGE_PLUS_TEXT or retry_result_type == test_expectations.MISSING):
234                         if retry_result_type == test_expectations.MISSING:
235                             num_missing += 1
236                         num_regressions += 1
237                         test_dict['report'] = 'REGRESSION'
238                     else:
239                         num_flaky += 1
240                         test_dict['report'] = 'FLAKY'
241                     actual.append(keywords[retry_result_type])
242                 else:
243                     num_regressions += 1
244                     test_dict['report'] = 'REGRESSION'
245             else:
246                 num_regressions += 1
247                 test_dict['report'] = 'REGRESSION'
248
249         test_dict['expected'] = expected
250         test_dict['actual'] = " ".join(actual)
251         if include_time_and_modifiers:
252             test_dict['time'] = round(1000 * result.test_run_time)
253             # FIXME: Fix get_modifiers to return modifiers in new format.
254             test_dict['modifiers'] = ' '.join(expectations.model().get_modifiers(test_name)).replace('BUGWK', 'webkit.org/b/')
255
256         test_dict.update(_interpret_test_failures(result.failures))
257
258         if retry_results:
259             retry_result = retry_results.unexpected_results_by_name.get(test_name)
260             if retry_result:
261                 test_dict.update(_interpret_test_failures(retry_result.failures))
262
263         # Store test hierarchically by directory. e.g.
264         # foo/bar/baz.html: test_dict
265         # foo/bar/baz1.html: test_dict
266         #
267         # becomes
268         # foo: {
269         #     bar: {
270         #         baz.html: test_dict,
271         #         baz1.html: test_dict
272         #     }
273         # }
274         parts = test_name.split('/')
275         current_map = tests
276         for i, part in enumerate(parts):
277             if i == (len(parts) - 1):
278                 current_map[part] = test_dict
279                 break
280             if part not in current_map:
281                 current_map[part] = {}
282             current_map = current_map[part]
283
284     results['tests'] = tests
285     results['num_passes'] = num_passes
286     results['num_flaky'] = num_flaky
287     results['num_missing'] = num_missing
288     results['num_regressions'] = num_regressions
289     results['uses_expectations_file'] = port_obj.uses_test_expectations_file()
290     results['interrupted'] = initial_results.interrupted  # Does results.html have enough information to compute this itself? (by checking total number of results vs. total number of tests?)
291     results['layout_tests_dir'] = port_obj.layout_tests_dir()
292     results['has_pretty_patch'] = port_obj.pretty_patch.pretty_patch_available()
293     results['pixel_tests_enabled'] = port_obj.get_option('pixel_tests')
294     results['other_crashes'] = other_crashes_dict
295     results['date'] = datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")
296
297     try:
298         # We only use the svn revision for using trac links in the results.html file,
299         # Don't do this by default since it takes >100ms.
300         # FIXME: Do we really need to populate this both here and in the json_results_generator?
301         if port_obj.get_option("builder_name"):
302             port_obj.host.initialize_scm()
303             results['revision'] = port_obj.host.scm().head_svn_revision()
304     except Exception, e:
305         _log.warn("Failed to determine svn revision for checkout (cwd: %s, webkit_base: %s), leaving 'revision' key blank in full_results.json.\n%s" % (port_obj._filesystem.getcwd(), port_obj.path_from_webkit_base(), e))
306         # Handle cases where we're running outside of version control.
307         import traceback
308         _log.debug('Failed to learn head svn revision:')
309         _log.debug(traceback.format_exc())
310         results['revision'] = ""
311
312     return results