/usr/lib/python2.7/dist-packages/taskflow/tests/unit/test_failure.py is in python-taskflow 2.3.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | # -*- coding: utf-8 -*-
# Copyright (C) 2013 Yahoo! Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import sys
from oslo_utils import encodeutils
import six
from six.moves import cPickle as pickle
import testtools
from taskflow import exceptions
from taskflow import test
from taskflow.tests import utils as test_utils
from taskflow.types import failure
def _captured_failure(msg):
try:
raise RuntimeError(msg)
except Exception:
return failure.Failure()
def _make_exc_info(msg):
try:
raise RuntimeError(msg)
except Exception:
return sys.exc_info()
class GeneralFailureObjTestsMixin(object):
def test_captures_message(self):
self.assertEqual('Woot!', self.fail_obj.exception_str)
def test_str(self):
self.assertEqual('Failure: RuntimeError: Woot!',
str(self.fail_obj))
def test_exception_types(self):
self.assertEqual(test_utils.RUNTIME_ERROR_CLASSES[:-2],
list(self.fail_obj))
def test_pformat_no_traceback(self):
text = self.fail_obj.pformat()
self.assertNotIn("Traceback", text)
def test_check_str(self):
val = 'Exception'
self.assertEqual(val, self.fail_obj.check(val))
def test_check_str_not_there(self):
val = 'ValueError'
self.assertEqual(None, self.fail_obj.check(val))
def test_check_type(self):
self.assertIs(self.fail_obj.check(RuntimeError), RuntimeError)
def test_check_type_not_there(self):
self.assertIs(self.fail_obj.check(ValueError), None)
class CaptureFailureTestCase(test.TestCase, GeneralFailureObjTestsMixin):
def setUp(self):
super(CaptureFailureTestCase, self).setUp()
self.fail_obj = _captured_failure('Woot!')
def test_captures_value(self):
self.assertIsInstance(self.fail_obj.exception, RuntimeError)
def test_captures_exc_info(self):
exc_info = self.fail_obj.exc_info
self.assertEqual(3, len(exc_info))
self.assertEqual(RuntimeError, exc_info[0])
self.assertIs(exc_info[1], self.fail_obj.exception)
def test_reraises(self):
self.assertRaisesRegexp(RuntimeError, '^Woot!$', self.fail_obj.reraise)
class ReCreatedFailureTestCase(test.TestCase, GeneralFailureObjTestsMixin):
def setUp(self):
super(ReCreatedFailureTestCase, self).setUp()
fail_obj = _captured_failure('Woot!')
self.fail_obj = failure.Failure(exception_str=fail_obj.exception_str,
traceback_str=fail_obj.traceback_str,
exc_type_names=list(fail_obj))
def test_value_lost(self):
self.assertIs(self.fail_obj.exception, None)
def test_no_exc_info(self):
self.assertIs(self.fail_obj.exc_info, None)
def test_pformat_traceback(self):
text = self.fail_obj.pformat(traceback=True)
self.assertIn("Traceback (most recent call last):", text)
def test_reraises(self):
exc = self.assertRaises(exceptions.WrappedFailure,
self.fail_obj.reraise)
self.assertIs(exc.check(RuntimeError), RuntimeError)
def test_no_type_names(self):
fail_obj = _captured_failure('Woot!')
fail_obj = failure.Failure(exception_str=fail_obj.exception_str,
traceback_str=fail_obj.traceback_str,
exc_type_names=[])
self.assertEqual([], list(fail_obj))
self.assertEqual("Failure: Woot!", fail_obj.pformat())
class FromExceptionTestCase(test.TestCase, GeneralFailureObjTestsMixin):
def setUp(self):
super(FromExceptionTestCase, self).setUp()
self.fail_obj = failure.Failure.from_exception(RuntimeError('Woot!'))
def test_pformat_no_traceback(self):
text = self.fail_obj.pformat(traceback=True)
self.assertIn("Traceback not available", text)
class FailureObjectTestCase(test.TestCase):
def test_invalids(self):
f = {
'exception_str': 'blah',
'traceback_str': 'blah',
'exc_type_names': [],
}
self.assertRaises(exceptions.InvalidFormat,
failure.Failure.validate, f)
f = {
'exception_str': 'blah',
'exc_type_names': ['Exception'],
}
self.assertRaises(exceptions.InvalidFormat,
failure.Failure.validate, f)
f = {
'exception_str': 'blah',
'traceback_str': 'blah',
'exc_type_names': ['Exception'],
'version': -1,
}
self.assertRaises(exceptions.InvalidFormat,
failure.Failure.validate, f)
def test_valid_from_dict_to_dict(self):
f = _captured_failure('Woot!')
d_f = f.to_dict()
failure.Failure.validate(d_f)
f2 = failure.Failure.from_dict(d_f)
self.assertTrue(f.matches(f2))
def test_bad_root_exception(self):
f = _captured_failure('Woot!')
d_f = f.to_dict()
d_f['exc_type_names'] = ['Junk']
self.assertRaises(exceptions.InvalidFormat,
failure.Failure.validate, d_f)
def test_valid_from_dict_to_dict_2(self):
f = _captured_failure('Woot!')
d_f = f.to_dict()
d_f['exc_type_names'] = ['RuntimeError', 'Exception', 'BaseException']
failure.Failure.validate(d_f)
def test_cause_exception_args(self):
f = _captured_failure('Woot!')
d_f = f.to_dict()
self.assertEqual(1, len(d_f['exc_args']))
self.assertEqual(("Woot!",), d_f['exc_args'])
f2 = failure.Failure.from_dict(d_f)
self.assertEqual(f.exception_args, f2.exception_args)
def test_dont_catch_base_exception(self):
try:
raise SystemExit()
except BaseException:
self.assertRaises(TypeError, failure.Failure)
def test_unknown_argument(self):
exc = self.assertRaises(TypeError, failure.Failure,
exception_str='Woot!',
traceback_str=None,
exc_type_names=['Exception'],
hi='hi there')
expected = "Failure.__init__ got unexpected keyword argument(s): hi"
self.assertEqual(expected, str(exc))
def test_empty_does_not_reraise(self):
self.assertIs(failure.Failure.reraise_if_any([]), None)
def test_reraises_one(self):
fls = [_captured_failure('Woot!')]
self.assertRaisesRegexp(RuntimeError, '^Woot!$',
failure.Failure.reraise_if_any, fls)
def test_reraises_several(self):
fls = [
_captured_failure('Woot!'),
_captured_failure('Oh, not again!')
]
exc = self.assertRaises(exceptions.WrappedFailure,
failure.Failure.reraise_if_any, fls)
self.assertEqual(fls, list(exc))
def test_failure_copy(self):
fail_obj = _captured_failure('Woot!')
copied = fail_obj.copy()
self.assertIsNot(fail_obj, copied)
self.assertEqual(fail_obj, copied)
self.assertTrue(fail_obj.matches(copied))
def test_failure_copy_recaptured(self):
captured = _captured_failure('Woot!')
fail_obj = failure.Failure(exception_str=captured.exception_str,
traceback_str=captured.traceback_str,
exc_type_names=list(captured))
copied = fail_obj.copy()
self.assertIsNot(fail_obj, copied)
self.assertEqual(fail_obj, copied)
self.assertFalse(fail_obj != copied)
self.assertTrue(fail_obj.matches(copied))
def test_recaptured_not_eq(self):
captured = _captured_failure('Woot!')
fail_obj = failure.Failure(exception_str=captured.exception_str,
traceback_str=captured.traceback_str,
exc_type_names=list(captured),
exc_args=list(captured.exception_args))
self.assertFalse(fail_obj == captured)
self.assertTrue(fail_obj != captured)
self.assertTrue(fail_obj.matches(captured))
def test_two_captured_eq(self):
captured = _captured_failure('Woot!')
captured2 = _captured_failure('Woot!')
self.assertEqual(captured, captured2)
def test_two_recaptured_neq(self):
captured = _captured_failure('Woot!')
fail_obj = failure.Failure(exception_str=captured.exception_str,
traceback_str=captured.traceback_str,
exc_type_names=list(captured))
new_exc_str = captured.exception_str.replace('Woot', 'w00t')
fail_obj2 = failure.Failure(exception_str=new_exc_str,
traceback_str=captured.traceback_str,
exc_type_names=list(captured))
self.assertNotEqual(fail_obj, fail_obj2)
self.assertFalse(fail_obj2.matches(fail_obj))
def test_compares_to_none(self):
captured = _captured_failure('Woot!')
self.assertNotEqual(captured, None)
self.assertFalse(captured.matches(None))
def test_pformat_traceback(self):
captured = _captured_failure('Woot!')
text = captured.pformat(traceback=True)
self.assertIn("Traceback (most recent call last):", text)
def test_pformat_traceback_captured_no_exc_info(self):
captured = _captured_failure('Woot!')
captured = failure.Failure.from_dict(captured.to_dict())
text = captured.pformat(traceback=True)
self.assertIn("Traceback (most recent call last):", text)
def test_no_capture_exc_args(self):
captured = _captured_failure(Exception("I am not valid JSON"))
fail_obj = failure.Failure(exception_str=captured.exception_str,
traceback_str=captured.traceback_str,
exc_type_names=list(captured),
exc_args=list(captured.exception_args))
fail_json = fail_obj.to_dict(include_args=False)
self.assertNotEqual(fail_obj.exception_args, fail_json['exc_args'])
self.assertEqual(fail_json['exc_args'], tuple())
class WrappedFailureTestCase(test.TestCase):
def test_simple_iter(self):
fail_obj = _captured_failure('Woot!')
wf = exceptions.WrappedFailure([fail_obj])
self.assertEqual(1, len(wf))
self.assertEqual([fail_obj], list(wf))
def test_simple_check(self):
fail_obj = _captured_failure('Woot!')
wf = exceptions.WrappedFailure([fail_obj])
self.assertEqual(RuntimeError, wf.check(RuntimeError))
self.assertEqual(None, wf.check(ValueError))
def test_two_failures(self):
fls = [
_captured_failure('Woot!'),
_captured_failure('Oh, not again!')
]
wf = exceptions.WrappedFailure(fls)
self.assertEqual(2, len(wf))
self.assertEqual(fls, list(wf))
def test_flattening(self):
f1 = _captured_failure('Wrap me')
f2 = _captured_failure('Wrap me, too')
f3 = _captured_failure('Woot!')
try:
raise exceptions.WrappedFailure([f1, f2])
except Exception:
fail_obj = failure.Failure()
wf = exceptions.WrappedFailure([fail_obj, f3])
self.assertEqual([f1, f2, f3], list(wf))
class NonAsciiExceptionsTestCase(test.TestCase):
def test_exception_with_non_ascii_str(self):
bad_string = chr(200)
excp = ValueError(bad_string)
fail = failure.Failure.from_exception(excp)
self.assertEqual(encodeutils.exception_to_unicode(excp),
fail.exception_str)
# This is slightly different on py2 vs py3... due to how
# __str__ or __unicode__ is called and what is expected from
# both...
if six.PY2:
msg = encodeutils.exception_to_unicode(excp)
expected = 'Failure: ValueError: %s' % msg.encode('utf-8')
else:
expected = u'Failure: ValueError: \xc8'
self.assertEqual(expected, str(fail))
def test_exception_non_ascii_unicode(self):
hi_ru = u'привет'
fail = failure.Failure.from_exception(ValueError(hi_ru))
self.assertEqual(hi_ru, fail.exception_str)
self.assertIsInstance(fail.exception_str, six.text_type)
self.assertEqual(u'Failure: ValueError: %s' % hi_ru,
six.text_type(fail))
def test_wrapped_failure_non_ascii_unicode(self):
hi_cn = u'嗨'
fail = ValueError(hi_cn)
self.assertEqual(hi_cn, encodeutils.exception_to_unicode(fail))
fail = failure.Failure.from_exception(fail)
wrapped_fail = exceptions.WrappedFailure([fail])
expected_result = (u"WrappedFailure: "
"[Failure: ValueError: %s]" % (hi_cn))
self.assertEqual(expected_result, six.text_type(wrapped_fail))
def test_failure_equality_with_non_ascii_str(self):
bad_string = chr(200)
fail = failure.Failure.from_exception(ValueError(bad_string))
copied = fail.copy()
self.assertEqual(fail, copied)
def test_failure_equality_non_ascii_unicode(self):
hi_ru = u'привет'
fail = failure.Failure.from_exception(ValueError(hi_ru))
copied = fail.copy()
self.assertEqual(fail, copied)
@testtools.skipIf(not six.PY3, 'this test only works on python 3.x')
class FailureCausesTest(test.TestCase):
@classmethod
def _raise_many(cls, messages):
if not messages:
return
msg = messages.pop(0)
e = RuntimeError(msg)
try:
cls._raise_many(messages)
raise e
except RuntimeError as e1:
six.raise_from(e, e1)
def test_causes(self):
f = None
try:
self._raise_many(["Still still not working",
"Still not working", "Not working"])
except RuntimeError:
f = failure.Failure()
self.assertIsNotNone(f)
self.assertEqual(2, len(f.causes))
self.assertEqual("Still not working", f.causes[0].exception_str)
self.assertEqual("Not working", f.causes[1].exception_str)
f = f.causes[0]
self.assertEqual(1, len(f.causes))
self.assertEqual("Not working", f.causes[0].exception_str)
f = f.causes[0]
self.assertEqual(0, len(f.causes))
def test_causes_to_from_dict(self):
f = None
try:
self._raise_many(["Still still not working",
"Still not working", "Not working"])
except RuntimeError:
f = failure.Failure()
self.assertIsNotNone(f)
d_f = f.to_dict()
failure.Failure.validate(d_f)
f = failure.Failure.from_dict(d_f)
self.assertEqual(2, len(f.causes))
self.assertEqual("Still not working", f.causes[0].exception_str)
self.assertEqual("Not working", f.causes[1].exception_str)
f = f.causes[0]
self.assertEqual(1, len(f.causes))
self.assertEqual("Not working", f.causes[0].exception_str)
f = f.causes[0]
self.assertEqual(0, len(f.causes))
def test_causes_pickle(self):
f = None
try:
self._raise_many(["Still still not working",
"Still not working", "Not working"])
except RuntimeError:
f = failure.Failure()
self.assertIsNotNone(f)
p_f = pickle.dumps(f)
f = pickle.loads(p_f)
self.assertEqual(2, len(f.causes))
self.assertEqual("Still not working", f.causes[0].exception_str)
self.assertEqual("Not working", f.causes[1].exception_str)
f = f.causes[0]
self.assertEqual(1, len(f.causes))
self.assertEqual("Not working", f.causes[0].exception_str)
f = f.causes[0]
self.assertEqual(0, len(f.causes))
def test_causes_suppress_context(self):
f = None
try:
try:
self._raise_many(["Still still not working",
"Still not working", "Not working"])
except RuntimeError as e:
six.raise_from(e, None)
except RuntimeError:
f = failure.Failure()
self.assertIsNotNone(f)
self.assertEqual([], list(f.causes))
class ExcInfoUtilsTest(test.TestCase):
def test_copy_none(self):
result = failure._copy_exc_info(None)
self.assertIsNone(result)
def test_copy_exc_info(self):
exc_info = _make_exc_info("Woot!")
result = failure._copy_exc_info(exc_info)
self.assertIsNot(result, exc_info)
self.assertIs(result[0], RuntimeError)
self.assertIsNot(result[1], exc_info[1])
self.assertIs(result[2], exc_info[2])
def test_none_equals(self):
self.assertTrue(failure._are_equal_exc_info_tuples(None, None))
def test_none_ne_tuple(self):
exc_info = _make_exc_info("Woot!")
self.assertFalse(failure._are_equal_exc_info_tuples(None, exc_info))
def test_tuple_nen_none(self):
exc_info = _make_exc_info("Woot!")
self.assertFalse(failure._are_equal_exc_info_tuples(exc_info, None))
def test_tuple_equals_itself(self):
exc_info = _make_exc_info("Woot!")
self.assertTrue(failure._are_equal_exc_info_tuples(exc_info, exc_info))
def test_typle_equals_copy(self):
exc_info = _make_exc_info("Woot!")
copied = failure._copy_exc_info(exc_info)
self.assertTrue(failure._are_equal_exc_info_tuples(exc_info, copied))
|