1 # Copyright (c) 2013, 2015 Intel Corporation
3 # Permission is hereby granted, free of charge, to any person obtaining a
4 # copy of this software and associated documentation files (the "Software"),
5 # to deal in the Software without restriction, including without limitation
6 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 # and/or sell copies of the Software, and to permit persons to whom the
8 # Software is furnished to do so, subject to the following conditions:
10 # The above copyright notice and this permission notice (including the next
11 # paragraph) shall be included in all copies or substantial portions of the
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 """ Provides classes that represent test statuses
24 These classes work similar to enums in other languages. They provide a limited
25 set of attributes that are defined per status:
27 A name -- a string representation of the values
28 An integer -- a value that allows classes to be compared using python's rich
30 A tuple representing :
31 [0] -- whether the test is considered a passing status
32 [1] -- whether the test should be added to the total number of tests. This
33 allows statuses like 'skip' to not affect the total percentage.
36 Status ordering from best to worst:
47 SKIP and NOTRUN are not factored into regressions and fixes, they are counted
48 separately. They also derive from a sublcass of Status, which always returns
51 The formula for determining regressions is:
52 old_status < new_status
54 The formula for determining fixes is:
55 old_status > new_status
59 from __future__
import (
60 absolute_import
, division
, print_function
, unicode_literals
65 from framework
import exceptions
, compat
80 def status_lookup(status
):
81 """ Provided a string return a status object instance
83 When provided a string that corresponds to a key in it's status_dict
84 variable, this function returns a status object instance. If the string
85 does not correspond to a key it will raise an exception
88 # Don't go through this if what we've been passed already is a status, it's
90 if isinstance(status
, Status
):
94 return _STATUS_MAP
[status
]
96 # Raise a StatusException rather than a key error
97 raise StatusException(status
)
100 @compat.python_2_unicode_compatible
101 class StatusException(exceptions
.PiglitInternalError
):
102 """ Raise this exception when a string is passed to status_lookup that
105 The primary reason to have a special exception is that otherwise
106 status_lookup returns a KeyError, but there are many cases where it is
107 desirable to except a KeyError and have an exception path. Using a custom
108 Error class here allows for more fine-grained control.
111 def __init__(self
, status
):
112 self
.__status
= status
113 super(StatusException
, self
).__init
__(self
)
116 return u
'Unknown status "{}"'.format(self
.__status
)
119 @compat.python_2_unicode_compatible
120 class Status(object):
121 """ A simple class for representing the output values of tests.
123 This class creatse the necessary magic values to use python's rich
124 comparison methods. This allows two objects to be compared using common
125 operators like <. >, ==, etc. It also alows them to be looked up in
126 containers using ``for x in []`` syntax.
128 This class is meant to be immutable, it (ab)uses two tools to provide this
129 psuedo-immutability: the property decorator, and the __slots__ attribute to
130 1: make the three attributes getters, therefor unwritable, and 2: make
131 adding additional attributes impossible
134 name -- the name of the status
135 value -- an int used to sort statuses from best to worst (0 -> inf)
136 fraction -- a tuple with two ints representing
137 [0]: 1 if the status is 'passing', else 0
138 [1]: 1 if the status counts toward the total number of tests,
142 __slots__
= ['__name', '__value', '__fraction']
144 def __init__(self
, name
, value
, fraction
=(0, 1)):
145 assert isinstance(value
, int), type(value
)
146 # The object is immutable, so calling self.foo = foo will raise a
147 # TypeError. Using setattr from the parrent object works around this.
148 self
.__name
= six
.text_type(name
)
150 self
.__fraction
= fraction
154 """ Return the value of name """
159 """ Return the sorting value """
164 """ Return the totals of the test as a tuple: (<pass>. <total>) """
165 return self
.__fraction
168 return 'Status("{}", {}, {})'.format(
169 self
.name
, self
.value
, self
.fraction
)
173 return str(self
.name
)
175 return bytes(self
.name
, 'utf-8')
180 def __lt__(self
, other
):
181 return not self
.__ge
__(other
)
183 def __le__(self
, other
):
184 return not self
.__gt
__(other
)
186 def __eq__(self
, other
):
187 # This must be int or status, since status comparisons are done using
188 # the __int__ magic method
189 if isinstance(other
, (int, Status
)):
190 return int(self
) == int(other
)
191 elif isinstance(other
, six
.text_type
):
192 return six
.text_type(self
) == other
193 elif isinstance(other
, six
.binary_type
):
194 return six
.binary_type(self
) == other
195 raise TypeError("Cannot compare type: {}".format(type(other
)))
197 def __ne__(self
, other
):
198 return not self
== other
200 def __ge__(self
, other
):
201 return self
.fraction
[1] > other
.fraction
[1] or (
202 self
.fraction
[1] == other
.fraction
[1] and int(self
) >= int(other
))
204 def __gt__(self
, other
):
205 return self
.fraction
[1] > other
.fraction
[1] or int(self
) > int(other
)
211 return hash(self
.name
)
214 class NoChangeStatus(Status
):
215 """ Special sublcass of status that overides rich comparison methods
217 This special class of a Status is for use with NOTRUN and SKIP, it never
218 returns that it is a pass or regression
221 def __init__(self
, name
, value
=0, fraction
=(0, 0)):
222 super(NoChangeStatus
, self
).__init
__(name
, value
, fraction
)
224 def __eq__(self
, other
):
225 if isinstance(other
, (str, six
.text_type
, Status
)):
226 return six
.text_type(self
) == six
.text_type(other
)
227 raise TypeError("Cannot compare type: {}".format(type(other
)))
229 def __ne__(self
, other
):
230 if isinstance(other
, (str, six
.text_type
, Status
)):
231 return six
.text_type(self
) != six
.text_type(other
)
232 raise TypeError("Cannot compare type: {}".format(type(other
)))
235 return hash(self
.name
)
238 NOTRUN
= NoChangeStatus('notrun')
240 SKIP
= NoChangeStatus('skip')
242 PASS
= Status('pass', 0, (1, 1))
244 WARN
= Status('warn', 10)
246 DMESG_WARN
= Status('dmesg-warn', 20)
248 FAIL
= Status('fail', 30)
250 DMESG_FAIL
= Status('dmesg-fail', 40)
252 TIMEOUT
= Status('timeout', 50)
254 CRASH
= Status('crash', 60)
256 INCOMPLETE
= Status('incomplete', 100)
264 'dmesg-warn': DMESG_WARN
,
265 'dmesg-fail': DMESG_FAIL
,
268 'incomplete': INCOMPLETE
,
271 # A tuple (ordered, immutable) of all statuses in this module
272 ALL
= (PASS
, WARN
, DMESG_WARN
, FAIL
, DMESG_FAIL
, TIMEOUT
, CRASH
, INCOMPLETE
,