1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
8 import arvados.errors as arv_error
9 from . import arvados_testutil as tutil
11 class KeepRequestErrorTestCase(unittest.TestCase):
13 ('http://keep1.zzzzz.example.org/', IOError("test IOError")),
14 ('http://keep3.zzzzz.example.org/', MemoryError("test MemoryError")),
15 ('http://keep5.zzzzz.example.org/',
16 arv_error.HttpError(500, "Internal Server Error")),
17 ('http://keep7.zzzzz.example.org/', IOError("second test IOError")),
20 def check_get_message(self, *args):
21 test_exc = arv_error.KeepRequestError("test message", *args)
22 self.assertEqual("test message", test_exc.message)
24 def test_get_message_with_request_errors(self):
25 self.check_get_message(self.REQUEST_ERRORS[:])
27 def test_get_message_without_request_errors(self):
28 self.check_get_message()
30 def check_get_request_errors(self, *args):
31 expected = dict(args[0]) if args else {}
32 test_exc = arv_error.KeepRequestError("test service exceptions", *args)
33 self.assertEqual(expected, test_exc.request_errors())
35 def test_get_request_errors(self):
36 self.check_get_request_errors(self.REQUEST_ERRORS[:])
38 def test_get_request_errors_none(self):
39 self.check_get_request_errors({})
41 def test_empty_exception(self):
42 test_exc = arv_error.KeepRequestError()
43 self.assertFalse(test_exc.message)
44 self.assertEqual({}, test_exc.request_errors())
46 def traceback_str(self, exc):
47 return traceback.format_exception_only(type(exc), exc)[-1]
49 def test_traceback_str_without_request_errors(self):
50 message = "test plain traceback string"
51 test_exc = arv_error.KeepRequestError(message)
52 exc_report = self.traceback_str(test_exc)
53 self.assertRegex(exc_report, r"^(arvados\.errors\.)?KeepRequestError: ")
54 self.assertIn(message, exc_report)
56 def test_traceback_str_with_request_errors(self):
57 message = "test traceback shows Keep services"
58 test_exc = arv_error.KeepRequestError(message, self.REQUEST_ERRORS[:])
59 exc_report = self.traceback_str(test_exc)
60 self.assertRegex(exc_report, r"^(arvados\.errors\.)?KeepRequestError: ")
61 self.assertIn(message, exc_report)
63 r"raised (IOError|OSError)", # IOError in Python2, OSError in Python3
64 r"raised MemoryError",
66 r"second test IOError",
67 r"responded with 500 Internal Server Error"]:
68 self.assertRegex(exc_report, expect_re)
69 # Assert the report maintains order of listed services.
71 for service_key, _ in self.REQUEST_ERRORS:
72 service_index = exc_report.find(service_key)
73 self.assertGreater(service_index, last_index)
74 last_index = service_index