log_test.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 json
  19. import sys
  20. import bind10_config
  21. from isc.config.ccsession import path_search
  22. class LogDict(unittest.TestCase):
  23. def setUp(self):
  24. # We work on a test dictionary now.
  25. isc.log.set_test_dictionary(True)
  26. def tearDown(self):
  27. # Return to the global dictionary
  28. isc.log.set_test_dictionary(False)
  29. def test_load_msgs(self):
  30. # Try loading a message and see it's there, but nothing more
  31. self.assertEqual(isc.log.create_message("ID", "Text"), "ID")
  32. self.assertEqual(isc.log.get_message("ID"), "Text")
  33. self.assertEqual(isc.log.get_message("no-ID"), None)
  34. class Manager(unittest.TestCase):
  35. def tearDown(self):
  36. isc.log.reset()
  37. def test_init_debug(self):
  38. # We try calling it now only, as we don't have any other functions
  39. # to check the outcome by it. Once we add the logger class, we may
  40. # check more.
  41. isc.log.init("root", "DEBUG", 50, None)
  42. def test_init_defaults(self):
  43. # We try calling it now only, as we don't have any other functions
  44. # to check the outcome by it. Once we add the logger class, we may
  45. # check more.
  46. isc.log.init("root")
  47. def test_init_notfound(self):
  48. # This should not throw, because the C++ one doesn't. Should we really
  49. # ignore errors like missing file?
  50. isc.log.init("root", "INFO", 0, "/no/such/file");
  51. def test_log_config_update(self):
  52. log_spec = json.dumps(isc.config.module_spec_from_file(path_search('logging.spec', bind10_config.PLUGIN_PATHS)).get_full_spec())
  53. self.assertRaises(TypeError, isc.log.log_config_update)
  54. self.assertRaises(TypeError, isc.log.log_config_update, 1)
  55. self.assertRaises(TypeError, isc.log.log_config_update, 1, 1)
  56. self.assertRaises(TypeError, isc.log.log_config_update, 1, 1, 1)
  57. self.assertRaises(TypeError, isc.log.log_config_update, 1, log_spec)
  58. self.assertRaises(TypeError, isc.log.log_config_update, [], log_spec)
  59. self.assertRaises(TypeError, isc.log.log_config_update, "foo", log_spec)
  60. self.assertRaises(TypeError, isc.log.log_config_update, "{ '", log_spec)
  61. # empty should pass
  62. isc.log.log_config_update("{}", log_spec)
  63. # bad spec
  64. self.assertRaises(TypeError, isc.log.log_config_update, "{}", json.dumps({"foo": "bar"}))
  65. # Try a correct one
  66. log_conf = json.dumps({"loggers":
  67. [{"name": "b10-xfrout", "output_options":
  68. [{"output": "/tmp/bind10.log",
  69. "destination": "file",
  70. "flush": True}]}]})
  71. isc.log.log_config_update(log_conf, log_spec)
  72. class Logger(unittest.TestCase):
  73. def tearDown(self):
  74. isc.log.reset()
  75. def setUp(self):
  76. isc.log.init("root", "DEBUG", 50)
  77. self.sevs = ['INFO', 'WARN', 'ERROR', 'FATAL']
  78. self.TEST_MSG = isc.log.create_message('TEST_MESSAGE', '%1')
  79. # Checks defaults of the logger
  80. def defaults(self, logger):
  81. self.assertEqual(logger.get_effective_severity(), "DEBUG")
  82. self.assertEqual(logger.get_effective_debug_level(), 50)
  83. def test_default_severity(self):
  84. logger = isc.log.Logger("child")
  85. self.defaults(logger)
  86. # Try changing the severities little bit
  87. def test_severity(self):
  88. logger = isc.log.Logger("child")
  89. logger.set_severity('DEBUG', 25)
  90. self.assertEqual(logger.get_effective_severity(), "DEBUG")
  91. self.assertEqual(logger.get_effective_debug_level(), 25)
  92. for sev in self.sevs:
  93. logger.set_severity(sev)
  94. self.assertEqual(logger.get_effective_severity(), sev)
  95. self.assertEqual(logger.get_effective_debug_level(), 0)
  96. # Return to default
  97. logger.set_severity(None)
  98. self.defaults(logger)
  99. def test_enabled(self):
  100. logger = isc.log.Logger("child")
  101. self.sevs.insert(0, 'DEBUG')
  102. methods = {
  103. 'DEBUG': logger.is_debug_enabled,
  104. 'INFO': logger.is_info_enabled,
  105. 'WARN': logger.is_warn_enabled,
  106. 'ERROR': logger.is_error_enabled,
  107. 'FATAL': logger.is_fatal_enabled
  108. }
  109. for sev in self.sevs:
  110. logger.set_severity(sev)
  111. enabled = False
  112. for tested in self.sevs:
  113. if tested == sev:
  114. enabled = True
  115. self.assertEqual(methods[tested](), enabled)
  116. logger.set_severity('DEBUG', 50)
  117. self.assertTrue(logger.is_debug_enabled())
  118. self.assertTrue(logger.is_debug_enabled(0))
  119. self.assertTrue(logger.is_debug_enabled(50))
  120. self.assertFalse(logger.is_debug_enabled(99))
  121. def test_invalid_params(self):
  122. """
  123. Tests invalid arguments for logging functions. The output is tested
  124. in check_output.sh.
  125. """
  126. logger = isc.log.Logger("child")
  127. methods = [
  128. logger.info,
  129. logger.warn,
  130. logger.error,
  131. logger.fatal
  132. ]
  133. for meth in methods:
  134. # Not enough arguments
  135. self.assertRaises(TypeError, meth)
  136. # Bad type
  137. self.assertRaises(TypeError, meth, 1)
  138. # Too few arguments
  139. self.assertRaises(TypeError, logger.debug, 42)
  140. self.assertRaises(TypeError, logger.debug)
  141. # Bad type
  142. self.assertRaises(TypeError, logger.debug, "42", "hello")
  143. def test_dbglevel_constants(self):
  144. """
  145. Just check a constant to make sure it is defined and is the
  146. correct value. (The constant chosen has a non-zero value to
  147. ensure that the code has both define the constant and set its
  148. value correctly.)
  149. """
  150. logger = isc.log.Logger("child")
  151. self.assertEqual(logger.DBGLVL_COMMAND, 10)
  152. def test_param_reference(self):
  153. """
  154. Check whether passing a parameter to a logger causes a reference leak.
  155. """
  156. class LogParam:
  157. def __str__(self):
  158. return 'LogParam'
  159. logger = isc.log.Logger("child")
  160. param = LogParam()
  161. orig_msgrefcnt = sys.getrefcount(param)
  162. orig_idrefcnt = sys.getrefcount(self.TEST_MSG)
  163. logger.info(self.TEST_MSG, param);
  164. self.assertEqual(sys.getrefcount(self.TEST_MSG), orig_idrefcnt)
  165. self.assertEqual(sys.getrefcount(param), orig_msgrefcnt)
  166. # intentionally pass an invalid type for debug level. It will
  167. # result in TypeError. The passed object still shouldn't leak a
  168. # reference.
  169. self.assertRaises(TypeError, logger.debug, param, self.TEST_MSG, param)
  170. self.assertEqual(sys.getrefcount(param), orig_msgrefcnt)
  171. def test_bad_parameter(self):
  172. # a log parameter cannot be converted to a string object.
  173. class LogParam:
  174. def __str__(self):
  175. raise ValueError("LogParam can't be converted to string")
  176. logger = isc.log.Logger("child")
  177. self.assertRaises(ValueError, logger.info, self.TEST_MSG, LogParam())
  178. if __name__ == '__main__':
  179. unittest.main()