log_test.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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", "DEBUG", 50, None)
  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", "INFO", 0, "/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", "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. def test_default_severity(self):
  58. logger = isc.log.Logger("child")
  59. self.defaults(logger)
  60. # Try changing the severities little bit
  61. def test_severity(self):
  62. logger = isc.log.Logger("child")
  63. logger.set_severity('DEBUG', 25)
  64. self.assertEqual(logger.get_effective_severity(), "DEBUG")
  65. self.assertEqual(logger.get_debug_level(), 25)
  66. for sev in self.sevs:
  67. logger.set_severity(sev)
  68. self.assertEqual(logger.get_effective_severity(), sev)
  69. self.assertEqual(logger.get_debug_level(), 0)
  70. # Return to default
  71. logger.set_severity(None)
  72. self.defaults(logger)
  73. def test_enabled(self):
  74. logger = isc.log.Logger("child")
  75. self.sevs.insert(0, 'DEBUG')
  76. methods = {
  77. 'DEBUG': logger.is_debug_enabled,
  78. 'INFO': logger.is_info_enabled,
  79. 'WARN': logger.is_warn_enabled,
  80. 'ERROR': logger.is_error_enabled,
  81. 'FATAL': logger.is_fatal_enabled
  82. }
  83. for sev in self.sevs:
  84. logger.set_severity(sev)
  85. enabled = False
  86. for tested in self.sevs:
  87. if tested == sev:
  88. enabled = True
  89. self.assertEqual(methods[tested](), enabled)
  90. logger.set_severity('DEBUG', 50)
  91. self.assertTrue(logger.is_debug_enabled())
  92. self.assertTrue(logger.is_debug_enabled(0))
  93. self.assertTrue(logger.is_debug_enabled(50))
  94. self.assertFalse(logger.is_debug_enabled(99))
  95. def test_invalid_params(self):
  96. """
  97. Tests invalid arguments for logging functions. The output is tested
  98. in check_output.sh.
  99. """
  100. logger = isc.log.Logger("child")
  101. methods = [
  102. logger.info,
  103. logger.warn,
  104. logger.error,
  105. logger.fatal
  106. ]
  107. for meth in methods:
  108. # Not enough arguments
  109. self.assertRaises(TypeError, meth)
  110. # Bad type
  111. self.assertRaises(TypeError, meth, 1)
  112. # Too few arguments
  113. self.assertRaises(TypeError, logger.debug, 42)
  114. self.assertRaises(TypeError, logger.debug)
  115. # Bad type
  116. self.assertRaises(TypeError, logger.debug, "42", "hello")
  117. if __name__ == '__main__':
  118. unittest.main()