1
0

test_log.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. tests.unit.log_test
  5. ~~~~~~~~~~~~~~~~~~~
  6. Test salt's "hacked" logging
  7. """
  8. # Import python libs
  9. from __future__ import absolute_import
  10. import logging
  11. from salt._logging.handlers import StreamHandler
  12. # Import Salt libs
  13. from salt._logging.impl import SaltLoggingClass
  14. from salt.ext.six.moves import StringIO
  15. # Import Salt Testing libs
  16. from tests.support.case import TestCase
  17. from tests.support.helpers import TstSuiteLoggingHandler
  18. class TestLog(TestCase):
  19. """
  20. Test several logging settings
  21. """
  22. def test_issue_2853_regex_TypeError(self):
  23. # Now, python's logging logger class is ours.
  24. # Let's make sure we have at least one instance
  25. log = SaltLoggingClass(__name__)
  26. # Test for a format which includes digits in name formatting.
  27. log_format = "[%(name)-15s] %(message)s"
  28. handler = TstSuiteLoggingHandler(format=log_format)
  29. log.addHandler(handler)
  30. # Trigger TstSuiteLoggingHandler.__enter__
  31. with handler:
  32. # Let's create another log instance to trigger salt's logging class
  33. # calculations.
  34. try:
  35. SaltLoggingClass("{0}.with_digits".format(__name__))
  36. except Exception as err: # pylint: disable=broad-except
  37. raise AssertionError(
  38. "No exception should have been raised: {0}".format(err)
  39. )
  40. # Remove the testing handler
  41. log.removeHandler(handler)
  42. # Test for a format which does not include digits in name formatting.
  43. log_format = "[%(name)s] %(message)s"
  44. handler = TstSuiteLoggingHandler(format=log_format)
  45. log.addHandler(handler)
  46. # Trigger TstSuiteLoggingHandler.__enter__
  47. with handler:
  48. # Let's create another log instance to trigger salt's logging class
  49. # calculations.
  50. try:
  51. SaltLoggingClass("{0}.without_digits".format(__name__))
  52. except Exception as err: # pylint: disable=broad-except
  53. raise AssertionError(
  54. "No exception should have been raised: {0}".format(err)
  55. )
  56. # Remove the testing handler
  57. log.removeHandler(handler)
  58. def test_exc_info_on_loglevel(self):
  59. def raise_exception_on_purpose():
  60. 1 / 0 # pylint: disable=pointless-statement
  61. log = SaltLoggingClass(__name__)
  62. # Only stream2 should contain the traceback
  63. stream1 = StringIO()
  64. stream2 = StringIO()
  65. handler1 = StreamHandler(stream1)
  66. handler2 = StreamHandler(stream2)
  67. handler1.setLevel(logging.INFO)
  68. handler2.setLevel(logging.DEBUG)
  69. log.addHandler(handler1)
  70. log.addHandler(handler2)
  71. try:
  72. raise_exception_on_purpose()
  73. except ZeroDivisionError as exc:
  74. log.error(
  75. "Exception raised on purpose caught: ZeroDivisionError",
  76. exc_info_on_loglevel=logging.DEBUG,
  77. )
  78. try:
  79. self.assertIn(
  80. "Exception raised on purpose caught: ZeroDivisionError",
  81. stream1.getvalue(),
  82. )
  83. self.assertNotIn("Traceback (most recent call last)", stream1.getvalue())
  84. self.assertIn(
  85. "Exception raised on purpose caught: ZeroDivisionError",
  86. stream2.getvalue(),
  87. )
  88. self.assertIn("Traceback (most recent call last)", stream2.getvalue())
  89. finally:
  90. log.removeHandler(handler1)
  91. log.removeHandler(handler2)
  92. # Both streams should contain the traceback
  93. stream1 = StringIO()
  94. stream2 = StringIO()
  95. handler1 = StreamHandler(stream1)
  96. handler2 = StreamHandler(stream2)
  97. handler1.setLevel(logging.INFO)
  98. handler2.setLevel(logging.DEBUG)
  99. log.addHandler(handler1)
  100. log.addHandler(handler2)
  101. try:
  102. raise_exception_on_purpose()
  103. except ZeroDivisionError as exc:
  104. log.error(
  105. "Exception raised on purpose caught: ZeroDivisionError",
  106. exc_info_on_loglevel=logging.INFO,
  107. )
  108. try:
  109. self.assertIn(
  110. "Exception raised on purpose caught: ZeroDivisionError",
  111. stream1.getvalue(),
  112. )
  113. self.assertIn("Traceback (most recent call last)", stream1.getvalue())
  114. self.assertIn(
  115. "Exception raised on purpose caught: ZeroDivisionError",
  116. stream2.getvalue(),
  117. )
  118. self.assertIn("Traceback (most recent call last)", stream2.getvalue())
  119. finally:
  120. log.removeHandler(handler1)
  121. log.removeHandler(handler2)
  122. # No streams should contain the traceback
  123. stream1 = StringIO()
  124. stream2 = StringIO()
  125. handler1 = StreamHandler(stream1)
  126. handler2 = StreamHandler(stream2)
  127. handler1.setLevel(logging.ERROR)
  128. handler2.setLevel(logging.INFO)
  129. log.addHandler(handler1)
  130. log.addHandler(handler2)
  131. try:
  132. raise_exception_on_purpose()
  133. except ZeroDivisionError as exc:
  134. log.error(
  135. "Exception raised on purpose caught: ZeroDivisionError",
  136. exc_info_on_loglevel=logging.DEBUG,
  137. )
  138. try:
  139. self.assertIn(
  140. "Exception raised on purpose caught: ZeroDivisionError",
  141. stream1.getvalue(),
  142. )
  143. self.assertNotIn("Traceback (most recent call last)", stream1.getvalue())
  144. self.assertIn(
  145. "Exception raised on purpose caught: ZeroDivisionError",
  146. stream2.getvalue(),
  147. )
  148. self.assertNotIn("Traceback (most recent call last)", stream2.getvalue())
  149. finally:
  150. log.removeHandler(handler1)
  151. log.removeHandler(handler2)