16
16
# See the License for the specific language governing permissions and
17
17
# limitations under the License.
19
# Colorizer Code is borrowed from Twisted:
20
# Copyright (c) 2001-2010 Twisted Matrix Laboratories.
22
# Permission is hereby granted, free of charge, to any person obtaining
23
# a copy of this software and associated documentation files (the
24
# "Software"), to deal in the Software without restriction, including
25
# without limitation the rights to use, copy, modify, merge, publish,
26
# distribute, sublicense, and/or sell copies of the Software, and to
27
# permit persons to whom the Software is furnished to do so, subject to
28
# the following conditions:
30
# The above copyright notice and this permission notice shall be
31
# included in all copies or substantial portions of the Software.
33
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
34
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
35
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
36
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
37
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
38
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
39
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41
"""Unittest runner for quantum
20
"""Unittest runner for quantum OVS plugin
22
This file should be run from the top dir in the quantum directory
25
python quantum/plugins/cisco/run_tests.py
46
27
To run all unit tests::
47
python run_tests.py unit
28
python quantum/plugins/cisco/run_tests.py quantum.plugins.cisco.tests.unit
49
30
To run all functional tests::
50
python run_tests.py functional
31
python quantum/plugins/openvswitch/run_tests.py functional
52
33
To run a single unit test::
53
python run_tests.py unit.test_stores:TestSwiftBackend.test_get
34
python quantum/plugins/openvswitch/run_tests.py \
35
quantum.plugins.cisco.tests.unit.test_stores:TestSwiftBackend.test_get
55
37
To run a single functional test::
56
python run_tests.py functional.test_service:TestController.test_create
38
python quantum/plugins/openvswitch/run_tests.py \
39
quantum.plugins.cisco.tests.functional.test_service \
40
:TestController.test_create
58
42
To run a single unit test module::
59
python run_tests.py unit.test_stores
43
python quantum/plugins/openvswitch/run_tests.py unit.test_stores
61
45
To run a single functional test module::
62
python run_tests.py functional.test_stores
46
python quantum/plugins/openvswitch/run_tests.py functional.test_stores
71
55
from nose import config
72
from nose import result
76
class _AnsiColorizer(object):
78
A colorizer is an object that loosely wraps around a stream, allowing
79
callers to write text to the stream in a particular color.
81
Colorizer classes must implement C{supported()} and C{write(text, color)}.
83
_colors = dict(black=30, red=31, green=32, yellow=33,
84
blue=34, magenta=35, cyan=36, white=37)
86
def __init__(self, stream):
89
def supported(cls, stream=sys.stdout):
91
A class method that returns True if the current platform supports
92
coloring terminal output using this method. Returns False otherwise.
94
if not stream.isatty():
95
return False # auto color only on TTYs
103
return curses.tigetnum("colors") > 2
106
return curses.tigetnum("colors") > 2
109
# guess false in case of error
111
supported = classmethod(supported)
113
def write(self, text, color):
115
Write the given text to the stream in the given color.
117
@param text: Text to be written to the stream.
119
@param color: A string label for a color. e.g. 'red', 'white'.
121
color = self._colors[color]
122
self.stream.write('\x1b[%s;1m%s\x1b[0m' % (color, text))
125
class _Win32Colorizer(object):
127
See _AnsiColorizer docstring.
129
def __init__(self, stream):
130
from win32console import GetStdHandle, STD_OUT_HANDLE, \
131
FOREGROUND_RED, FOREGROUND_BLUE, FOREGROUND_GREEN, \
133
red, green, blue, bold = (FOREGROUND_RED, FOREGROUND_GREEN,
134
FOREGROUND_BLUE, FOREGROUND_INTENSITY)
136
self.screenBuffer = GetStdHandle(STD_OUT_HANDLE)
138
'normal': red | green | blue,
140
'green': green | bold,
142
'yellow': red | green | bold,
143
'magenta': red | blue | bold,
144
'cyan': green | blue | bold,
145
'white': red | green | blue | bold}
147
def supported(cls, stream=sys.stdout):
150
screenBuffer = win32console.GetStdHandle(
151
win32console.STD_OUT_HANDLE)
156
screenBuffer.SetConsoleTextAttribute(
157
win32console.FOREGROUND_RED |
158
win32console.FOREGROUND_GREEN |
159
win32console.FOREGROUND_BLUE)
160
except pywintypes.error:
164
supported = classmethod(supported)
166
def write(self, text, color):
167
color = self._colors[color]
168
self.screenBuffer.SetConsoleTextAttribute(color)
169
self.stream.write(text)
170
self.screenBuffer.SetConsoleTextAttribute(self._colors['normal'])
173
class _NullColorizer(object):
175
See _AnsiColorizer docstring.
177
def __init__(self, stream):
180
def supported(cls, stream=sys.stdout):
182
supported = classmethod(supported)
184
def write(self, text, color):
185
self.stream.write(text)
188
class QuantumTestResult(result.TextTestResult):
189
def __init__(self, *args, **kw):
190
result.TextTestResult.__init__(self, *args, **kw)
191
self._last_case = None
192
self.colorizer = None
193
# NOTE(vish, tfukushima): reset stdout for the terminal check
194
stdout = sys.__stdout__
195
for colorizer in [_Win32Colorizer, _AnsiColorizer, _NullColorizer]:
196
if colorizer.supported():
197
self.colorizer = colorizer(self.stream)
201
def getDescription(self, test):
204
# NOTE(vish, tfukushima): copied from unittest with edit to add color
205
def addSuccess(self, test):
206
unittest.TestResult.addSuccess(self, test)
208
self.colorizer.write("OK", 'green')
209
self.stream.writeln()
211
self.stream.write('.')
214
# NOTE(vish, tfukushima): copied from unittest with edit to add color
215
def addFailure(self, test, err):
216
unittest.TestResult.addFailure(self, test, err)
218
self.colorizer.write("FAIL", 'red')
219
self.stream.writeln()
221
self.stream.write('F')
224
# NOTE(vish, tfukushima): copied from unittest with edit to add color
225
def addError(self, test, err):
226
"""Overrides normal addError to add support for errorClasses.
227
If the exception is a registered class, the error will be added
228
to the list for that class, not errors.
230
stream = getattr(self, 'stream', None)
233
exc_info = self._exc_info_to_string(err, test)
235
# This is for compatibility with Python 2.3.
236
exc_info = self._exc_info_to_string(err)
237
for cls, (storage, label, isfail) in self.errorClasses.items():
238
if result.isclass(ec) and issubclass(ec, cls):
241
storage.append((test, exc_info))
242
# Might get patched into a streamless result
243
if stream is not None:
246
detail = result._exception_details(err[1])
248
message.append(detail)
249
stream.writeln(": ".join(message))
251
stream.write(label[:1])
253
self.errors.append((test, exc_info))
255
if stream is not None:
257
self.colorizer.write("ERROR", 'red')
258
self.stream.writeln()
262
def startTest(self, test):
263
unittest.TestResult.startTest(self, test)
264
current_case = test.test.__class__.__name__
267
if current_case != self._last_case:
268
self.stream.writeln(current_case)
269
self._last_case = current_case
272
' %s' % str(test.test._testMethodName).ljust(60))
276
class QuantumTestRunner(core.TextTestRunner):
277
def _makeResult(self):
278
return QuantumTestResult(self.stream,
57
sys.path.append(os.getcwd())
59
from quantum.common.test_lib import run_tests, test_config
60
#from quantum.plugins.openvswitch.tests.test_vlan_map import VlanMapTest
284
62
if __name__ == '__main__':
285
# Set up test logger.
286
logger = logging.getLogger()
287
hdlr = logging.StreamHandler()
288
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
289
hdlr.setFormatter(formatter)
290
logger.addHandler(hdlr)
291
logger.setLevel(logging.DEBUG)
65
# if a single test case was specified,
66
# we should only invoked the tests once
67
invoke_once = len(sys.argv) > 1
293
71
working_dir = os.path.abspath("tests")
294
72
c = config.Config(stream=sys.stdout,
297
75
workingDir=working_dir)
298
runner = QuantumTestRunner(stream=c.stream,
299
verbosity=c.verbosity,
301
sys.exit(not core.run(config=c, testRunner=runner))
76
exit_status = run_tests(c)
83
working_dir = os.path.abspath("quantum/plugins/cisco/tests")
84
c = config.Config(stream=sys.stdout,
87
workingDir=working_dir)
88
exit_status = exit_status or run_tests(c)