• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2010 Google Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#     * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13#     * Neither the name of Google Inc. nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29import cPickle
30
31from webkitpy.layout_tests.models import test_expectations
32
33
34def is_reftest_failure(failure_list):
35    failure_types = [type(f) for f in failure_list]
36    return set((FailureReftestMismatch, FailureReftestMismatchDidNotOccur, FailureReftestNoImagesGenerated)).intersection(failure_types)
37
38# FIXME: This is backwards.  Each TestFailure subclass should know what
39# test_expectation type it corresponds too.  Then this method just
40# collects them all from the failure list and returns the worst one.
41def determine_result_type(failure_list):
42    """Takes a set of test_failures and returns which result type best fits
43    the list of failures. "Best fits" means we use the worst type of failure.
44
45    Returns:
46      one of the test_expectations result types - PASS, FAIL, CRASH, etc."""
47
48    if not failure_list or len(failure_list) == 0:
49        return test_expectations.PASS
50
51    failure_types = [type(f) for f in failure_list]
52    if FailureCrash in failure_types:
53        return test_expectations.CRASH
54    elif FailureLeak in failure_types:
55        return test_expectations.LEAK
56    elif FailureTimeout in failure_types:
57        return test_expectations.TIMEOUT
58    elif FailureEarlyExit in failure_types:
59        return test_expectations.SKIP
60    elif (FailureMissingResult in failure_types or
61          FailureMissingImage in failure_types or
62          FailureMissingImageHash in failure_types or
63          FailureMissingAudio in failure_types):
64        return test_expectations.MISSING
65    else:
66        is_text_failure = (FailureTextMismatch in failure_types or
67                           FailureTestHarnessAssertion in failure_types)
68        is_image_failure = (FailureImageHashIncorrect in failure_types or
69                            FailureImageHashMismatch in failure_types)
70        is_audio_failure = (FailureAudioMismatch in failure_types)
71        if is_text_failure and is_image_failure:
72            return test_expectations.IMAGE_PLUS_TEXT
73        elif is_text_failure:
74            return test_expectations.TEXT
75        elif is_image_failure or is_reftest_failure(failure_list):
76            return test_expectations.IMAGE
77        elif is_audio_failure:
78            return test_expectations.AUDIO
79        else:
80            raise ValueError("unclassifiable set of failures: "
81                             + str(failure_types))
82
83
84class TestFailure(object):
85    """Abstract base class that defines the failure interface."""
86
87    @staticmethod
88    def loads(s):
89        """Creates a TestFailure object from the specified string."""
90        return cPickle.loads(s)
91
92    def message(self):
93        """Returns a string describing the failure in more detail."""
94        raise NotImplementedError
95
96    def __eq__(self, other):
97        return self.__class__.__name__ == other.__class__.__name__
98
99    def __ne__(self, other):
100        return self.__class__.__name__ != other.__class__.__name__
101
102    def __hash__(self):
103        return hash(self.__class__.__name__)
104
105    def dumps(self):
106        """Returns the string/JSON representation of a TestFailure."""
107        return cPickle.dumps(self)
108
109    def driver_needs_restart(self):
110        """Returns True if we should kill the driver before the next test."""
111        return False
112
113
114class FailureTimeout(TestFailure):
115    def __init__(self, is_reftest=False):
116        super(FailureTimeout, self).__init__()
117        self.is_reftest = is_reftest
118
119    def message(self):
120        return "test timed out"
121
122    def driver_needs_restart(self):
123        return True
124
125
126class FailureCrash(TestFailure):
127    def __init__(self, is_reftest=False, process_name='content_shell', pid=None):
128        super(FailureCrash, self).__init__()
129        self.process_name = process_name
130        self.pid = pid
131        self.is_reftest = is_reftest
132
133    def message(self):
134        if self.pid:
135            return "%s crashed [pid=%d]" % (self.process_name, self.pid)
136        return self.process_name + " crashed"
137
138    def driver_needs_restart(self):
139        return True
140
141
142class FailureLeak(TestFailure):
143    def __init__(self, is_reftest=False, log=''):
144        super(FailureLeak, self).__init__()
145        self.is_reftest = is_reftest
146        self.log = log
147
148    def message(self):
149        return "leak detected: %s" % (self.log)
150
151
152class FailureMissingResult(TestFailure):
153    def message(self):
154        return "-expected.txt was missing"
155
156
157class FailureTestHarnessAssertion(TestFailure):
158    def message(self):
159        return "asserts failed"
160
161
162class FailureTextMismatch(TestFailure):
163    def message(self):
164        return "text diff"
165
166
167class FailureMissingImageHash(TestFailure):
168    def message(self):
169        return "-expected.png was missing an embedded checksum"
170
171
172class FailureMissingImage(TestFailure):
173    def message(self):
174        return "-expected.png was missing"
175
176
177class FailureImageHashMismatch(TestFailure):
178    def message(self):
179        return "image diff"
180
181
182class FailureImageHashIncorrect(TestFailure):
183    def message(self):
184        return "-expected.png embedded checksum is incorrect"
185
186
187class FailureReftestMismatch(TestFailure):
188    def __init__(self, reference_filename=None):
189        super(FailureReftestMismatch, self).__init__()
190        self.reference_filename = reference_filename
191
192    def message(self):
193        return "reference mismatch"
194
195
196class FailureReftestMismatchDidNotOccur(TestFailure):
197    def __init__(self, reference_filename=None):
198        super(FailureReftestMismatchDidNotOccur, self).__init__()
199        self.reference_filename = reference_filename
200
201    def message(self):
202        return "reference mismatch didn't happen"
203
204
205class FailureReftestNoImagesGenerated(TestFailure):
206    def __init__(self, reference_filename=None):
207        super(FailureReftestNoImagesGenerated, self).__init__()
208        self.reference_filename = reference_filename
209
210    def message(self):
211        return "reference didn't generate pixel results."
212
213
214class FailureMissingAudio(TestFailure):
215    def message(self):
216        return "expected audio result was missing"
217
218
219class FailureAudioMismatch(TestFailure):
220    def message(self):
221        return "audio mismatch"
222
223
224class FailureEarlyExit(TestFailure):
225    def message(self):
226        return "skipped due to early exit"
227
228
229# Convenient collection of all failure classes for anything that might
230# need to enumerate over them all.
231ALL_FAILURE_CLASSES = (FailureTimeout, FailureCrash, FailureMissingResult,
232                       FailureTestHarnessAssertion,
233                       FailureTextMismatch, FailureMissingImageHash,
234                       FailureMissingImage, FailureImageHashMismatch,
235                       FailureImageHashIncorrect, FailureReftestMismatch,
236                       FailureReftestMismatchDidNotOccur, FailureReftestNoImagesGenerated,
237                       FailureMissingAudio, FailureAudioMismatch,
238                       FailureEarlyExit)
239