log_test.py 6.3 KB

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