11308: Futurize.
[arvados.git] / sdk / python / tests / test_errors.py
1 #!/usr/bin/env python
2
3 from __future__ import absolute_import
4 import traceback
5 import unittest
6
7 import arvados.errors as arv_error
8 from . import arvados_testutil as tutil
9
10 class KeepRequestErrorTestCase(unittest.TestCase):
11     REQUEST_ERRORS = [
12         ('http://keep1.zzzzz.example.org/', IOError("test IOError")),
13         ('http://keep3.zzzzz.example.org/', MemoryError("test MemoryError")),
14         ('http://keep5.zzzzz.example.org/',
15          arv_error.HttpError(500, "Internal Server Error")),
16         ('http://keep7.zzzzz.example.org/', IOError("second test IOError")),
17         ]
18
19     def check_get_message(self, *args):
20         test_exc = arv_error.KeepRequestError("test message", *args)
21         self.assertEqual("test message", test_exc.message)
22
23     def test_get_message_with_request_errors(self):
24         self.check_get_message(self.REQUEST_ERRORS[:])
25
26     def test_get_message_without_request_errors(self):
27         self.check_get_message()
28
29     def check_get_request_errors(self, *args):
30         expected = dict(args[0]) if args else {}
31         test_exc = arv_error.KeepRequestError("test service exceptions", *args)
32         self.assertEqual(expected, test_exc.request_errors())
33
34     def test_get_request_errors(self):
35         self.check_get_request_errors(self.REQUEST_ERRORS[:])
36
37     def test_get_request_errors_none(self):
38         self.check_get_request_errors({})
39
40     def test_empty_exception(self):
41         test_exc = arv_error.KeepRequestError()
42         self.assertFalse(test_exc.message)
43         self.assertEqual({}, test_exc.request_errors())
44
45     def traceback_str(self, exc):
46         return traceback.format_exception_only(type(exc), exc)[-1]
47
48     def test_traceback_str_without_request_errors(self):
49         message = "test plain traceback string"
50         test_exc = arv_error.KeepRequestError(message)
51         exc_report = self.traceback_str(test_exc)
52         self.assertTrue(exc_report.startswith("KeepRequestError: "))
53         self.assertIn(message, exc_report)
54
55     def test_traceback_str_with_request_errors(self):
56         message = "test traceback shows Keep services"
57         test_exc = arv_error.KeepRequestError(message, self.REQUEST_ERRORS[:])
58         exc_report = self.traceback_str(test_exc)
59         self.assertTrue(exc_report.startswith("KeepRequestError: "))
60         for expect_substr in [message, "raised IOError", "raised MemoryError",
61                               "test MemoryError", "second test IOError",
62                               "responded with 500 Internal Server Error"]:
63             self.assertIn(expect_substr, exc_report)
64         # Assert the report maintains order of listed services.
65         last_index = -1
66         for service_key, _ in self.REQUEST_ERRORS:
67             service_index = exc_report.find(service_key)
68             self.assertGreater(service_index, last_index)
69             last_index = service_index