Merge branch '21855-rpm-url-update'
[arvados.git] / sdk / python / tests / test_errors.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 import traceback
6 import unittest
7
8 import arvados.errors as arv_error
9 from . import arvados_testutil as tutil
10
11 class KeepRequestErrorTestCase(unittest.TestCase):
12     REQUEST_ERRORS = [
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")),
18         ]
19
20     def check_get_message(self, *args):
21         test_exc = arv_error.KeepRequestError("test message", *args)
22         self.assertEqual("test message", test_exc.message)
23
24     def test_get_message_with_request_errors(self):
25         self.check_get_message(self.REQUEST_ERRORS[:])
26
27     def test_get_message_without_request_errors(self):
28         self.check_get_message()
29
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())
34
35     def test_get_request_errors(self):
36         self.check_get_request_errors(self.REQUEST_ERRORS[:])
37
38     def test_get_request_errors_none(self):
39         self.check_get_request_errors({})
40
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())
45
46     def traceback_str(self, exc):
47         return traceback.format_exception_only(type(exc), exc)[-1]
48
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)
55
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)
62         for expect_re in [
63                 r"raised (IOError|OSError)", # IOError in Python2, OSError in Python3
64                 r"raised MemoryError",
65                 r"test 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.
70         last_index = -1
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