log_test.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. import bind10_config
  19. from isc.config.ccsession import path_search
  20. class LogDict(unittest.TestCase):
  21. def setUp(self):
  22. # We work on a test dictionary now.
  23. isc.log.set_test_dictionary(True)
  24. def tearDown(self):
  25. # Return to the global dictionary
  26. isc.log.set_test_dictionary(False)
  27. def test_load_msgs(self):
  28. # Try loading a message and see it's there, but nothing more
  29. self.assertEqual(isc.log.create_message("ID", "Text"), "ID")
  30. self.assertEqual(isc.log.get_message("ID"), "Text")
  31. self.assertEqual(isc.log.get_message("no-ID"), None)
  32. class Manager(unittest.TestCase):
  33. def tearDown(self):
  34. isc.log.reset()
  35. def test_init_debug(self):
  36. # We try calling it now only, as we don't have any other functions
  37. # to check the outcome by it. Once we add the logger class, we may
  38. # check more.
  39. isc.log.init("root", "DEBUG", 50, None)
  40. def test_init_defaults(self):
  41. # We try calling it now only, as we don't have any other functions
  42. # to check the outcome by it. Once we add the logger class, we may
  43. # check more.
  44. isc.log.init("root")
  45. def test_init_notfound(self):
  46. # This should not throw, because the C++ one doesn't. Should we really
  47. # ignore errors like missing file?
  48. isc.log.init("root", "INFO", 0, "/no/such/file");
  49. def test_log_config_update(self):
  50. log_spec = isc.config.module_spec_from_file(path_search('logging.spec', bind10_config.PLUGIN_PATHS)).get_full_spec()
  51. self.assertRaises(TypeError, isc.log.log_config_update)
  52. self.assertRaises(TypeError, isc.log.log_config_update, 1)
  53. self.assertRaises(TypeError, isc.log.log_config_update, 1, 1)
  54. self.assertRaises(TypeError, isc.log.log_config_update, 1, 1, 1)
  55. self.assertRaises(TypeError, isc.log.log_config_update, 1, log_spec)
  56. self.assertRaises(TypeError, isc.log.log_config_update, [], log_spec)
  57. self.assertRaises(TypeError, isc.log.log_config_update, "foo", log_spec)
  58. # empty should pass
  59. isc.log.log_config_update({}, log_spec)
  60. # bad spec
  61. self.assertRaises(TypeError, isc.log.log_config_update, {}, {"foo": "bar"})
  62. # Try a correct one
  63. log_conf = {"loggers":
  64. [{"name": "b10-xfrout", "output_options":
  65. [{"output": "/tmp/bind10.log",
  66. "destination": "file",
  67. "flush": True}]}]}
  68. isc.log.log_config_update(log_conf, log_spec)
  69. class Logger(unittest.TestCase):
  70. def tearDown(self):
  71. isc.log.reset()
  72. def setUp(self):
  73. isc.log.init("root", "DEBUG", 50)
  74. self.sevs = ['INFO', 'WARN', 'ERROR', 'FATAL']
  75. # Checks defaults of the logger
  76. def defaults(self, logger):
  77. self.assertEqual(logger.get_effective_severity(), "DEBUG")
  78. self.assertEqual(logger.get_effective_debug_level(), 50)
  79. def test_default_severity(self):
  80. logger = isc.log.Logger("child")
  81. self.defaults(logger)
  82. # Try changing the severities little bit
  83. def test_severity(self):
  84. logger = isc.log.Logger("child")
  85. logger.set_severity('DEBUG', 25)
  86. self.assertEqual(logger.get_effective_severity(), "DEBUG")
  87. self.assertEqual(logger.get_effective_debug_level(), 25)
  88. for sev in self.sevs:
  89. logger.set_severity(sev)
  90. self.assertEqual(logger.get_effective_severity(), sev)
  91. self.assertEqual(logger.get_effective_debug_level(), 0)
  92. # Return to default
  93. logger.set_severity(None)
  94. self.defaults(logger)
  95. def test_enabled(self):
  96. logger = isc.log.Logger("child")
  97. self.sevs.insert(0, 'DEBUG')
  98. methods = {
  99. 'DEBUG': logger.is_debug_enabled,
  100. 'INFO': logger.is_info_enabled,
  101. 'WARN': logger.is_warn_enabled,
  102. 'ERROR': logger.is_error_enabled,
  103. 'FATAL': logger.is_fatal_enabled
  104. }
  105. for sev in self.sevs:
  106. logger.set_severity(sev)
  107. enabled = False
  108. for tested in self.sevs:
  109. if tested == sev:
  110. enabled = True
  111. self.assertEqual(methods[tested](), enabled)
  112. logger.set_severity('DEBUG', 50)
  113. self.assertTrue(logger.is_debug_enabled())
  114. self.assertTrue(logger.is_debug_enabled(0))
  115. self.assertTrue(logger.is_debug_enabled(50))
  116. self.assertFalse(logger.is_debug_enabled(99))
  117. def test_invalid_params(self):
  118. """
  119. Tests invalid arguments for logging functions. The output is tested
  120. in check_output.sh.
  121. """
  122. logger = isc.log.Logger("child")
  123. methods = [
  124. logger.info,
  125. logger.warn,
  126. logger.error,
  127. logger.fatal
  128. ]
  129. for meth in methods:
  130. # Not enough arguments
  131. self.assertRaises(TypeError, meth)
  132. # Bad type
  133. self.assertRaises(TypeError, meth, 1)
  134. # Too few arguments
  135. self.assertRaises(TypeError, logger.debug, 42)
  136. self.assertRaises(TypeError, logger.debug)
  137. # Bad type
  138. self.assertRaises(TypeError, logger.debug, "42", "hello")
  139. if __name__ == '__main__':
  140. unittest.main()