test_log.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.ext.six.moves import StringIO
  12. # Import Salt Testing libs
  13. from tests.support.case import TestCase
  14. from tests.support.helpers import TstSuiteLoggingHandler
  15. # Import Salt libs
  16. from salt._logging.impl import SaltLoggingClass
  17. from salt._logging.handlers import StreamHandler
  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('Exception raised on purpose caught: ZeroDivisionError',
  75. exc_info_on_loglevel=logging.DEBUG)
  76. try:
  77. self.assertIn(
  78. 'Exception raised on purpose caught: ZeroDivisionError',
  79. stream1.getvalue()
  80. )
  81. self.assertNotIn('Traceback (most recent call last)', stream1.getvalue())
  82. self.assertIn(
  83. 'Exception raised on purpose caught: ZeroDivisionError',
  84. stream2.getvalue()
  85. )
  86. self.assertIn('Traceback (most recent call last)', stream2.getvalue())
  87. finally:
  88. log.removeHandler(handler1)
  89. log.removeHandler(handler2)
  90. # Both streams should contain the traceback
  91. stream1 = StringIO()
  92. stream2 = StringIO()
  93. handler1 = StreamHandler(stream1)
  94. handler2 = StreamHandler(stream2)
  95. handler1.setLevel(logging.INFO)
  96. handler2.setLevel(logging.DEBUG)
  97. log.addHandler(handler1)
  98. log.addHandler(handler2)
  99. try:
  100. raise_exception_on_purpose()
  101. except ZeroDivisionError as exc:
  102. log.error('Exception raised on purpose caught: ZeroDivisionError',
  103. exc_info_on_loglevel=logging.INFO)
  104. try:
  105. self.assertIn(
  106. 'Exception raised on purpose caught: ZeroDivisionError',
  107. stream1.getvalue()
  108. )
  109. self.assertIn('Traceback (most recent call last)', stream1.getvalue())
  110. self.assertIn(
  111. 'Exception raised on purpose caught: ZeroDivisionError',
  112. stream2.getvalue()
  113. )
  114. self.assertIn('Traceback (most recent call last)', stream2.getvalue())
  115. finally:
  116. log.removeHandler(handler1)
  117. log.removeHandler(handler2)
  118. # No streams should contain the traceback
  119. stream1 = StringIO()
  120. stream2 = StringIO()
  121. handler1 = StreamHandler(stream1)
  122. handler2 = StreamHandler(stream2)
  123. handler1.setLevel(logging.ERROR)
  124. handler2.setLevel(logging.INFO)
  125. log.addHandler(handler1)
  126. log.addHandler(handler2)
  127. try:
  128. raise_exception_on_purpose()
  129. except ZeroDivisionError as exc:
  130. log.error('Exception raised on purpose caught: ZeroDivisionError',
  131. exc_info_on_loglevel=logging.DEBUG)
  132. try:
  133. self.assertIn(
  134. 'Exception raised on purpose caught: ZeroDivisionError',
  135. stream1.getvalue()
  136. )
  137. self.assertNotIn('Traceback (most recent call last)', stream1.getvalue())
  138. self.assertIn(
  139. 'Exception raised on purpose caught: ZeroDivisionError',
  140. stream2.getvalue()
  141. )
  142. self.assertNotIn('Traceback (most recent call last)', stream2.getvalue())
  143. finally:
  144. log.removeHandler(handler1)
  145. log.removeHandler(handler2)