2 # Copyright (c) 2013, 2015 Intel Corporation
4 # Permission is hereby granted, free of charge, to any person obtaining a
5 # copy of this software and associated documentation files (the "Software"),
6 # to deal in the Software without restriction, including without limitation
7 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Software, and to permit persons to whom the
9 # Software is furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice (including the next
12 # paragraph) shall be included in all copies or substantial portions of the
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 """ Provides classes that represent test statuses
25 These classes work similar to enums in other languages. They provide a limited
26 set of attributes that are defined per status:
28 A name -- a string representation of the values
29 An integer -- a value that allows classes to be compared using python's rich
31 A tuple representing :
32 [0] -- whether the test is considered a passing status
33 [1] -- whether the test should be added to the total number of tests. This
34 allows statuses like 'skip' to not affect the total percentage.
37 Status ordering from best to worst:
48 SKIP and NOTRUN are not factored into regressions and fixes, they are counted
49 separately. They also derive from a subclass of Status, which always returns
52 The formula for determining regressions is:
53 old_status < new_status
55 The formula for determining fixes is:
56 old_status > new_status
60 from framework
import exceptions
75 def status_lookup(status
):
76 """ Provided a string return a status object instance
78 When provided a string that corresponds to a key in it's status_dict
79 variable, this function returns a status object instance. If the string
80 does not correspond to a key it will raise an exception
83 # Don't go through this if what we've been passed already is a status, it's
85 if isinstance(status
, Status
):
89 return _STATUS_MAP
[status
]
91 # Raise a StatusException rather than a key error
92 raise StatusException(status
)
95 class StatusException(exceptions
.PiglitInternalError
):
96 """ Raise this exception when a string is passed to status_lookup that
99 The primary reason to have a special exception is that otherwise
100 status_lookup returns a KeyError, but there are many cases where it is
101 desirable to except a KeyError and have an exception path. Using a custom
102 Error class here allows for more fine-grained control.
105 def __init__(self
, status
):
106 self
.__status
= status
107 super(StatusException
, self
).__init
__(self
)
110 return u
'Unknown status "{}"'.format(self
.__status
)
113 class Status(object):
114 """ A simple class for representing the output values of tests.
116 This class creatse the necessary magic values to use python's rich
117 comparison methods. This allows two objects to be compared using common
118 operators like <. >, ==, etc. It also allows them to be looked up in
119 containers using ``for x in []`` syntax.
121 This class is meant to be immutable, it (ab)uses two tools to provide this
122 psuedo-immutability: the property decorator, and the __slots__ attribute to
123 1: make the three attributes getters, therefore unwritable, and 2: make
124 adding additional attributes impossible
127 name -- the name of the status
128 value -- an int used to sort statuses from best to worst (0 -> inf)
129 fraction -- a tuple with two ints representing
130 [0]: 1 if the status is 'passing', else 0
131 [1]: 1 if the status counts toward the total number of tests,
135 __slots__
= ['__name', '__value', '__fraction']
137 def __init__(self
, name
, value
, fraction
=(0, 1)):
138 assert isinstance(value
, int), type(value
)
139 # The object is immutable, so calling self.foo = foo will raise a
140 # TypeError. Using setattr from the parent object works around this.
141 self
.__name
= str(name
)
143 self
.__fraction
= fraction
147 """ Return the value of name """
152 """ Return the sorting value """
157 """ Return the totals of the test as a tuple: (<pass>. <total>) """
158 return self
.__fraction
161 return 'Status("{}", {}, {})'.format(
162 self
.name
, self
.value
, self
.fraction
)
165 return bytes(self
.name
, 'utf-8')
170 def __lt__(self
, other
):
171 return not self
.__ge
__(other
)
173 def __le__(self
, other
):
174 return not self
.__gt
__(other
)
176 def __eq__(self
, other
):
177 # This must be int or status, since status comparisons are done using
178 # the __int__ magic method
179 if isinstance(other
, (int, Status
)):
180 return int(self
) == int(other
)
181 elif isinstance(other
, str):
182 return str(self
) == other
183 elif isinstance(other
, bytes
):
184 return bytes(self
) == other
185 raise TypeError("Cannot compare type: {}".format(type(other
)))
187 def __ne__(self
, other
):
188 return not self
== other
190 def __ge__(self
, other
):
191 return self
.fraction
[1] > other
.fraction
[1] or (
192 self
.fraction
[1] == other
.fraction
[1] and int(self
) >= int(other
))
194 def __gt__(self
, other
):
195 return self
.fraction
[1] > other
.fraction
[1] or int(self
) > int(other
)
201 return hash(self
.name
)
204 class NoChangeStatus(Status
):
205 """ Special subclass of status that overrides rich comparison methods
207 This special class of a Status is for use with NOTRUN and SKIP, it never
208 returns that it is a pass or regression
211 def __init__(self
, name
, value
=0, fraction
=(0, 0)):
212 super(NoChangeStatus
, self
).__init
__(name
, value
, fraction
)
214 def __eq__(self
, other
):
215 if isinstance(other
, (str, Status
)):
216 return str(self
) == str(other
)
217 raise TypeError("Cannot compare type: {}".format(type(other
)))
219 def __ne__(self
, other
):
220 if isinstance(other
, (str, Status
)):
221 return str(self
) != str(other
)
222 raise TypeError("Cannot compare type: {}".format(type(other
)))
225 return hash(self
.name
)
228 NOTRUN
= NoChangeStatus('notrun')
230 SKIP
= NoChangeStatus('skip')
232 PASS
= Status('pass', 0, (1, 1))
234 WARN
= Status('warn', 10)
236 DMESG_WARN
= Status('dmesg-warn', 20)
238 FAIL
= Status('fail', 30)
240 DMESG_FAIL
= Status('dmesg-fail', 40)
242 TIMEOUT
= Status('timeout', 50)
244 CRASH
= Status('crash', 60)
246 INCOMPLETE
= Status('incomplete', 100)
254 'dmesg-warn': DMESG_WARN
,
255 'dmesg-fail': DMESG_FAIL
,
258 'incomplete': INCOMPLETE
,
261 # A tuple (ordered, immutable) of all statuses in this module
262 ALL
= (PASS
, WARN
, DMESG_WARN
, FAIL
, DMESG_FAIL
, TIMEOUT
, CRASH
, INCOMPLETE
,