traceback_handler_test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2013 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. import unittest
  16. import os
  17. import isc.log
  18. import isc.util.traceback_handler
  19. class TracebackHandlerTest(unittest.TestCase):
  20. def setUp(self):
  21. """
  22. Save some things to be restored later, if we overwrite them
  23. for tests.
  24. """
  25. self.exit = isc.util.traceback_handler.sys.exit
  26. self.logger = isc.util.traceback_handler.logger
  27. # Sanity check - the functions exist.
  28. self.assertTrue(self.exit)
  29. self.assertTrue(self.logger)
  30. def tearDown(self):
  31. """
  32. Restore mocked things.
  33. """
  34. isc.util.traceback_handler.sys.exit = self.exit
  35. isc.util.traceback_handler.logger = self.logger
  36. def test_success(self):
  37. """
  38. Test the handler doesn't influence the result of successful
  39. function.
  40. """
  41. self.called = False
  42. def succ():
  43. self.called = True
  44. return 42
  45. self.assertEqual(42,
  46. isc.util.traceback_handler.traceback_handler(succ))
  47. self.assertTrue(self.called)
  48. def test_success_no_returned_value(self):
  49. """
  50. Test the handler handles the case where main() returns nothing.
  51. """
  52. self.called = False
  53. def succ():
  54. self.called = True
  55. return
  56. self.assertEqual(None,
  57. isc.util.traceback_handler.traceback_handler(succ))
  58. self.assertTrue(self.called)
  59. def test_exception(self):
  60. """
  61. Test the exception is caught and logged, but not propagated.
  62. """
  63. # Mock up bunch of things
  64. self.exited = False
  65. def exit(status):
  66. self.assertEqual(1, status)
  67. self.exited = True
  68. isc.util.traceback_handler.sys.exit = exit
  69. self.logged = False
  70. obj = self
  71. class Logger:
  72. def fatal(self, message, ename, exception, filename):
  73. obj.assertTrue(isinstance(exception, Exception))
  74. obj.assertEqual('Exception', ename)
  75. obj.assertTrue(os.path.isfile(filename))
  76. with open(filename) as f:
  77. text = f.read()
  78. obj.assertTrue(text.startswith('Traceback'))
  79. os.remove(filename)
  80. obj.logged = True
  81. isc.util.traceback_handler.logger = Logger()
  82. # The failing function
  83. def fail():
  84. raise Exception('Anybody there?')
  85. # Does not raise, but returns nothing
  86. self.assertIsNone(isc.util.traceback_handler.traceback_handler(fail))
  87. # It logged and exited (sane values for those are checked in the mocks)
  88. self.assertTrue(self.exited)
  89. self.assertTrue(self.logged)
  90. if __name__ == "__main__":
  91. isc.log.init("bind10")
  92. isc.log.resetUnitTestRootLogger()
  93. unittest.main()