test_exception.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright 2023 Cloudbase Solutions Srl
  2. # All Rights Reserved.
  3. from unittest import mock
  4. from coriolis import exception
  5. from coriolis.tests import test_base
  6. class ConvertedExceptionTestCase(test_base.CoriolisBaseTestCase):
  7. """Test suite for the Coriolis ConvertedException class."""
  8. def test__init__(self):
  9. result = exception.ConvertedException(
  10. code=403, title='Forbidden', explanation='test')
  11. self.assertEqual(result.code, 403)
  12. self.assertEqual(result.title, 'Forbidden')
  13. self.assertEqual(result.explanation, 'test')
  14. def test__init__without_title(self):
  15. result = exception.ConvertedException(code=403, explanation='test')
  16. self.assertEqual(result.code, 403)
  17. self.assertEqual(result.title, 'Forbidden')
  18. self.assertEqual(result.explanation, 'test')
  19. def test__init__with_code_not_in_status_reasons(self):
  20. result = exception.ConvertedException(code=450, explanation='test')
  21. self.assertEqual(result.code, 450)
  22. self.assertEqual(result.title, 'Unknown Client Error')
  23. self.assertEqual(result.explanation, 'test')
  24. class CoriolisTestException(exception.CoriolisException):
  25. message = "Test message with missing placeholder: %(missing_key)s"
  26. class CoriolisExceptionTestCase(test_base.CoriolisBaseTestCase):
  27. """Test suite for the Coriolis CoriolisException class."""
  28. def test__init__(self):
  29. result = exception.CoriolisException()
  30. self.assertEqual(result.message, 'An unknown exception occurred.')
  31. self.assertEqual(result.code, 500)
  32. self.assertEqual(result.headers, {})
  33. self.assertEqual(result.safe, False)
  34. def test__init__with_exception_in_kwargs_items(self):
  35. kwargs = {'test_key': 'test_value', 'code': 500}
  36. kwargs['exc'] = ValueError('Test exception message')
  37. result = exception.CoriolisException(**kwargs)
  38. kwargs['exc'] = str(kwargs['exc'])
  39. self.assertEqual(result.msg, 'An unknown exception occurred.')
  40. self.assertEqual(result.kwargs, kwargs)
  41. def test__init__custom_message_and_kwargs(self):
  42. custom_message = 'test-message'
  43. kwargs = {'test-key': 'test-value'}
  44. result = exception.CoriolisException(custom_message, **kwargs)
  45. expected_message = custom_message % kwargs
  46. expected_kwargs = {'test-key': 'test-value', 'code': 500}
  47. self.assertEqual(result.msg, expected_message)
  48. self.assertEqual(result.kwargs, expected_kwargs)
  49. @mock.patch.object(exception, 'LOG')
  50. @mock.patch.object(exception, 'CONF')
  51. def test__init__with_exception_in_string_format(self, mock_conf, mock_log):
  52. mock_conf.fatal_exception_format_errors = False
  53. kwargs = {'some_key': 'some_value'}
  54. result = CoriolisTestException(**kwargs)
  55. self.assertEqual(result.msg, CoriolisTestException.message)
  56. mock_log.exception.assert_called_once_with(
  57. 'Exception in string format operation')
  58. expected_calls = [
  59. mock.call("%(name)s: %(value)s",
  60. {'name': 'some_key', 'value': 'some_value'}),
  61. mock.call("%(name)s: %(value)s", {'name': 'code', 'value': 500})
  62. ]
  63. mock_log.error.assert_has_calls(expected_calls)
  64. self.assertEqual(mock_log.error.call_count, 2)
  65. @mock.patch.object(exception, 'CONF')
  66. def test__init__with_fatal_exception_format_errors(self, mock_conf):
  67. mock_conf.fatal_exception_format_errors = True
  68. kwargs = {'some_key': 'some_value'}
  69. self.assertRaises(KeyError, CoriolisTestException, **kwargs)
  70. def test__init__with_exception_message(self):
  71. message = ValueError('Test exception message')
  72. result = exception.CoriolisException(message)
  73. self.assertEqual(str(result), 'Test exception message')
  74. @mock.patch.object(exception, 'CONF')
  75. def test__unicode__(self, mock_conf):
  76. mock_conf.fatal_exception_format_errors = False
  77. kwargs = {'missing_key': 'some_value'}
  78. exception_instance = CoriolisTestException(**kwargs)
  79. result = exception_instance.__unicode__()
  80. self.assertEqual(
  81. result, "Test message with missing placeholder: some_value")
  82. class APIExceptionTestCase(test_base.CoriolisBaseTestCase):
  83. """Test suite for the Coriolis APIException class."""
  84. def test__init__(self):
  85. result = exception.APIException(service='test_service')
  86. self.assertEqual(result.kwargs['service'], 'test_service')
  87. def test__init__no_service(self):
  88. result = exception.APIException()
  89. self.assertEqual(result.kwargs['service'], 'unknown')