2012-12-11 10:10:03 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2018-05-28 21:13:12 +00:00
|
|
|
:codeauthor: Pedro Algarvio (pedro@algarvio.me)
|
2013-09-16 16:24:00 +00:00
|
|
|
|
|
|
|
|
2012-12-11 10:10:03 +00:00
|
|
|
tests.unit.log_test
|
|
|
|
~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Test salt's "hacked" logging
|
|
|
|
'''
|
|
|
|
|
2014-08-10 02:38:54 +00:00
|
|
|
# Import python libs
|
2014-11-21 19:05:13 +00:00
|
|
|
from __future__ import absolute_import
|
2014-08-10 02:38:54 +00:00
|
|
|
import logging
|
2015-06-06 13:47:52 +00:00
|
|
|
from salt.ext.six.moves import StringIO
|
2014-08-10 02:38:54 +00:00
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
# Import Salt Testing libs
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.case import TestCase
|
2017-02-27 15:59:04 +00:00
|
|
|
from tests.support.helpers import TestsLoggingHandler
|
2012-12-11 10:10:03 +00:00
|
|
|
|
2014-08-10 02:38:54 +00:00
|
|
|
# Import Salt libs
|
|
|
|
from salt.log import setup as saltlog
|
|
|
|
from salt.log.handlers import StreamHandler
|
|
|
|
|
2012-12-11 10:10:03 +00:00
|
|
|
|
|
|
|
class TestLog(TestCase):
|
2014-08-10 02:38:54 +00:00
|
|
|
'''
|
|
|
|
Test several logging settings
|
|
|
|
'''
|
2012-12-11 10:10:03 +00:00
|
|
|
|
|
|
|
def test_issue_2853_regex_TypeError(self):
|
|
|
|
# Now, python's logging logger class is ours.
|
|
|
|
# Let's make sure we have at least one instance
|
2013-01-08 03:41:59 +00:00
|
|
|
log = saltlog.SaltLoggingClass(__name__)
|
2012-12-11 10:10:03 +00:00
|
|
|
|
|
|
|
# Test for a format which includes digits in name formatting.
|
|
|
|
log_format = '[%(name)-15s] %(message)s'
|
|
|
|
handler = TestsLoggingHandler(format=log_format)
|
|
|
|
log.addHandler(handler)
|
|
|
|
|
|
|
|
# Trigger TestsLoggingHandler.__enter__
|
|
|
|
with handler:
|
|
|
|
# Let's create another log instance to trigger salt's logging class
|
|
|
|
# calculations.
|
|
|
|
try:
|
2013-01-08 03:41:59 +00:00
|
|
|
saltlog.SaltLoggingClass('{0}.with_digits'.format(__name__))
|
2013-05-04 02:53:53 +00:00
|
|
|
except Exception as err:
|
2012-12-11 10:10:03 +00:00
|
|
|
raise AssertionError(
|
|
|
|
'No exception should have been raised: {0}'.format(err)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Remove the testing handler
|
|
|
|
log.removeHandler(handler)
|
|
|
|
|
|
|
|
# Test for a format which does not include digits in name formatting.
|
|
|
|
log_format = '[%(name)s] %(message)s'
|
|
|
|
handler = TestsLoggingHandler(format=log_format)
|
|
|
|
log.addHandler(handler)
|
|
|
|
|
|
|
|
# Trigger TestsLoggingHandler.__enter__
|
|
|
|
with handler:
|
|
|
|
# Let's create another log instance to trigger salt's logging class
|
|
|
|
# calculations.
|
|
|
|
try:
|
2013-01-08 03:41:59 +00:00
|
|
|
saltlog.SaltLoggingClass('{0}.without_digits'.format(__name__))
|
2013-05-04 02:53:53 +00:00
|
|
|
except Exception as err:
|
2012-12-11 10:10:03 +00:00
|
|
|
raise AssertionError(
|
|
|
|
'No exception should have been raised: {0}'.format(err)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Remove the testing handler
|
|
|
|
log.removeHandler(handler)
|
|
|
|
|
2014-08-10 02:38:54 +00:00
|
|
|
def test_exc_info_on_loglevel(self):
|
|
|
|
def raise_exception_on_purpose():
|
2014-08-10 05:06:41 +00:00
|
|
|
1/0 # pylint: disable=pointless-statement
|
2014-08-10 02:38:54 +00:00
|
|
|
|
|
|
|
log = saltlog.SaltLoggingClass(__name__)
|
|
|
|
|
|
|
|
# Only stream2 should contain the traceback
|
|
|
|
stream1 = StringIO()
|
|
|
|
stream2 = StringIO()
|
|
|
|
handler1 = StreamHandler(stream1)
|
|
|
|
handler2 = StreamHandler(stream2)
|
|
|
|
|
|
|
|
handler1.setLevel(logging.INFO)
|
|
|
|
handler2.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
log.addHandler(handler1)
|
|
|
|
log.addHandler(handler2)
|
|
|
|
|
|
|
|
try:
|
|
|
|
raise_exception_on_purpose()
|
|
|
|
except ZeroDivisionError as exc:
|
2015-06-06 13:47:52 +00:00
|
|
|
log.error('Exception raised on purpose caught: ZeroDivisionError',
|
2014-08-10 02:38:54 +00:00
|
|
|
exc_info_on_loglevel=logging.DEBUG)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertIn(
|
2015-06-06 13:47:52 +00:00
|
|
|
'Exception raised on purpose caught: ZeroDivisionError',
|
2014-08-10 02:38:54 +00:00
|
|
|
stream1.getvalue()
|
|
|
|
)
|
|
|
|
self.assertNotIn('Traceback (most recent call last)', stream1.getvalue())
|
|
|
|
self.assertIn(
|
2015-06-06 13:47:52 +00:00
|
|
|
'Exception raised on purpose caught: ZeroDivisionError',
|
2014-08-10 02:38:54 +00:00
|
|
|
stream2.getvalue()
|
|
|
|
)
|
|
|
|
self.assertIn('Traceback (most recent call last)', stream2.getvalue())
|
|
|
|
finally:
|
|
|
|
log.removeHandler(handler1)
|
|
|
|
log.removeHandler(handler2)
|
|
|
|
|
|
|
|
# Both streams should contain the traceback
|
|
|
|
stream1 = StringIO()
|
|
|
|
stream2 = StringIO()
|
|
|
|
handler1 = StreamHandler(stream1)
|
|
|
|
handler2 = StreamHandler(stream2)
|
|
|
|
|
|
|
|
handler1.setLevel(logging.INFO)
|
|
|
|
handler2.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
log.addHandler(handler1)
|
|
|
|
log.addHandler(handler2)
|
|
|
|
|
|
|
|
try:
|
|
|
|
raise_exception_on_purpose()
|
|
|
|
except ZeroDivisionError as exc:
|
2015-06-06 13:47:52 +00:00
|
|
|
log.error('Exception raised on purpose caught: ZeroDivisionError',
|
2014-08-10 02:38:54 +00:00
|
|
|
exc_info_on_loglevel=logging.INFO)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertIn(
|
2015-06-06 13:47:52 +00:00
|
|
|
'Exception raised on purpose caught: ZeroDivisionError',
|
2014-08-10 02:38:54 +00:00
|
|
|
stream1.getvalue()
|
|
|
|
)
|
|
|
|
self.assertIn('Traceback (most recent call last)', stream1.getvalue())
|
|
|
|
self.assertIn(
|
2015-06-06 13:47:52 +00:00
|
|
|
'Exception raised on purpose caught: ZeroDivisionError',
|
2014-08-10 02:38:54 +00:00
|
|
|
stream2.getvalue()
|
|
|
|
)
|
|
|
|
self.assertIn('Traceback (most recent call last)', stream2.getvalue())
|
|
|
|
finally:
|
|
|
|
log.removeHandler(handler1)
|
|
|
|
log.removeHandler(handler2)
|
|
|
|
|
|
|
|
# No streams should contain the traceback
|
|
|
|
stream1 = StringIO()
|
|
|
|
stream2 = StringIO()
|
|
|
|
handler1 = StreamHandler(stream1)
|
|
|
|
handler2 = StreamHandler(stream2)
|
|
|
|
|
|
|
|
handler1.setLevel(logging.ERROR)
|
|
|
|
handler2.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
log.addHandler(handler1)
|
|
|
|
log.addHandler(handler2)
|
|
|
|
|
|
|
|
try:
|
|
|
|
raise_exception_on_purpose()
|
|
|
|
except ZeroDivisionError as exc:
|
2015-06-06 13:47:52 +00:00
|
|
|
log.error('Exception raised on purpose caught: ZeroDivisionError',
|
2014-08-10 02:38:54 +00:00
|
|
|
exc_info_on_loglevel=logging.DEBUG)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertIn(
|
2015-06-06 13:47:52 +00:00
|
|
|
'Exception raised on purpose caught: ZeroDivisionError',
|
2014-08-10 02:38:54 +00:00
|
|
|
stream1.getvalue()
|
|
|
|
)
|
|
|
|
self.assertNotIn('Traceback (most recent call last)', stream1.getvalue())
|
|
|
|
self.assertIn(
|
2015-06-06 13:47:52 +00:00
|
|
|
'Exception raised on purpose caught: ZeroDivisionError',
|
2014-08-10 02:38:54 +00:00
|
|
|
stream2.getvalue()
|
|
|
|
)
|
|
|
|
self.assertNotIn('Traceback (most recent call last)', stream2.getvalue())
|
|
|
|
finally:
|
|
|
|
log.removeHandler(handler1)
|
|
|
|
log.removeHandler(handler2)
|