log_test.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright (C) 2011 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. # This tests it can be loaded, nothing more yet
  16. import isc.log
  17. import unittest
  18. class LogDict(unittest.TestCase):
  19. def setUp(self):
  20. # We work on a test dictionary now.
  21. isc.log.set_test_dictionary(True)
  22. def tearDown(self):
  23. # Return to the global dictionary
  24. isc.log.set_test_dictionary(False)
  25. def test_load_msgs(self):
  26. # Try loading a message and see it's there, but nothing more
  27. self.assertEqual(isc.log.create_message("ID", "Text"), "ID")
  28. self.assertEqual(isc.log.get_message("ID"), "Text")
  29. self.assertEqual(isc.log.get_message("no-ID"), None)
  30. class Manager(unittest.TestCase):
  31. def tearDown(self):
  32. isc.log.reset()
  33. def test_init_debug(self):
  34. # We try calling it now only, as we don't have any other functions
  35. # to check the outcome by it. Once we add the logger class, we may
  36. # check more.
  37. isc.log.init("root", None, "DEBUG", 50)
  38. def test_init_defaults(self):
  39. # We try calling it now only, as we don't have any other functions
  40. # to check the outcome by it. Once we add the logger class, we may
  41. # check more.
  42. isc.log.init("root")
  43. def test_init_notfound(self):
  44. # This should not throw, because the C++ one doesn't. Should we really
  45. # ignore errors like missing file?
  46. isc.log.init("root", "/no/such/file");
  47. class Logger(unittest.TestCase):
  48. def tearDown(self):
  49. isc.log.reset()
  50. def setUp(self):
  51. isc.log.init("root", None, "DEBUG", 50)
  52. self.sevs = ['INFO', 'WARN', 'ERROR', 'FATAL']
  53. # Checks defaults of the logger
  54. def defaults(self, logger):
  55. self.assertEqual(logger.get_effective_severity(), "DEBUG")
  56. self.assertEqual(logger.get_debug_level(), 0)
  57. # Because there's a bug in the C++ backend currently. When it's fixed,
  58. # it should no longer fail
  59. @unittest.expectedFailure
  60. def test_default_severity(self):
  61. logger = isc.log.Logger("child")
  62. self.defaults(logger)
  63. # Try changing the severities little bit
  64. def test_severity(self):
  65. logger = isc.log.Logger("child")
  66. logger.set_severity('DEBUG', 25)
  67. self.assertEqual(logger.get_effective_severity(), "DEBUG")
  68. self.assertEqual(logger.get_debug_level(), 25)
  69. for sev in self.sevs:
  70. logger.set_severity(sev)
  71. self.assertEqual(logger.get_effective_severity(), sev)
  72. self.assertEqual(logger.get_debug_level(), 0)
  73. # Return to default
  74. logger.set_severity(None)
  75. # The same bug here
  76. #self.defaults(logger)
  77. def test_enabled(self):
  78. logger = isc.log.Logger("child")
  79. self.sevs.insert(0, 'DEBUG')
  80. methods = {
  81. 'DEBUG': logger.is_debug_enabled,
  82. 'INFO': logger.is_info_enabled,
  83. 'WARN': logger.is_warn_enabled,
  84. 'ERROR': logger.is_error_enabled,
  85. 'FATAL': logger.is_fatal_enabled
  86. }
  87. for sev in self.sevs:
  88. logger.set_severity(sev)
  89. enabled = False
  90. for tested in self.sevs:
  91. if tested == sev:
  92. enabled = True
  93. self.assertEqual(methods[tested](), enabled)
  94. logger.set_severity('DEBUG', 50)
  95. self.assertTrue(logger.is_debug_enabled())
  96. self.assertTrue(logger.is_debug_enabled(0))
  97. self.assertTrue(logger.is_debug_enabled(50))
  98. self.assertFalse(logger.is_debug_enabled(99))
  99. def test_invalid_params(self):
  100. """
  101. Tests invalid arguments for logging functions. The output is tested
  102. in check_output.sh.
  103. """
  104. logger = isc.log.Logger("child")
  105. methods = [
  106. logger.info,
  107. logger.warn,
  108. logger.error,
  109. logger.fatal
  110. ]
  111. for meth in methods:
  112. # Not enough arguments
  113. self.assertRaises(TypeError, meth)
  114. # Bad type
  115. self.assertRaises(TypeError, meth, 1)
  116. # Too few arguments
  117. self.assertRaises(TypeError, logger.debug, 42)
  118. self.assertRaises(TypeError, logger.debug)
  119. # Bad type
  120. self.assertRaises(TypeError, logger.debug, "42", "hello")
  121. if __name__ == '__main__':
  122. unittest.main()