-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.py
113 lines (97 loc) · 4.02 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
Validate log configuration
"""
import logging
import os
import unittest
import flask
# pylint: disable=no-name-in-module,import-error
from flask.ext.log import Logging
import mock
TEST_LOG_LEVEL = 'DEBUG'
TEST_FORMATTER = 'stuff'
class TestLogConfiguration(unittest.TestCase):
"""
Make sure we are setting up logging like we expect.
"""
def setUp(self):
"""Setup a fresh flask app to work on for each test"""
self.app = flask.Flask(__name__)
self.app.debug = True
def test_config_log_level(self):
"""Validate that log level can be set with application"""
root_logger = logging.getLogger()
log_level = root_logger.level
self.assertEqual(logging.NOTSET, log_level)
self.app.config['FLASK_LOG_LEVEL'] = TEST_LOG_LEVEL
Logging(self.app)
root_logger = logging.getLogger()
self.assertEqual(root_logger.level, getattr(logging, TEST_LOG_LEVEL))
def test_bad_log_level(self):
"""
Set a non-existent log level and make sure we raise properly
"""
root_logger = logging.getLogger()
log_level = root_logger.level
self.assertEqual(logging.NOTSET, log_level)
self.app.config['FLASK_LOG_LEVEL'] = 'Not a real thing'
with self.assertRaisesRegexp(ValueError, 'Invalid log level.+'):
Logging(self.app)
def test_no_log_level(self):
"""
Make sure we leave things alone if no log level is set.
"""
self.app.config['FLASK_LOG_LEVEL'] = None
flask_log = Logging(self.app)
self.assertEqual(logging.NOTSET, flask_log.log_level)
def test_syslog_devices(self):
"""Test syslog address handling and handler"""
for log_device in ['/dev/log', '/var/run/syslog', '']:
root_logger = logging.getLogger()
# Nuke syslog handlers from init_app
syslog_handlers = []
for handler in root_logger.handlers:
if isinstance(handler, logging.handlers.SysLogHandler):
syslog_handlers.append(handler)
for handler in syslog_handlers:
root_logger.removeHandler(handler)
real_exists = os.path.exists(log_device)
def mock_effect(*args):
"""Contextual choice of log device."""
# pylint: disable=cell-var-from-loop
return args[0] == log_device
# Call so that it will think /dev/log exists
with mock.patch('os.path') as os_exists:
os_exists.exists.side_effect = mock_effect
if not real_exists and log_device != '':
with self.assertRaises(Exception):
Logging(self.app)
else:
Logging(self.app)
syslog_handler = None
for handler in root_logger.handlers:
if isinstance(handler, logging.handlers.SysLogHandler):
syslog_handler = handler
self.assertIsNotNone(syslog_handler)
if log_device == '':
self.assertEqual(
syslog_handler.address, ('127.0.0.1', 514)
)
else:
self.assertEqual(syslog_handler.address, log_device)
def test_set_formatter(self):
"""Test that we can override the default formatter"""
flask_log = Logging(self.app)
flask_log.set_formatter(TEST_FORMATTER)
root_logger = logging.getLogger()
# Make fake record and assert we are formatting properly
log_record = logging.LogRecord(
'a', logging.CRITICAL, 'a.py', 1, 'things', None, None
)
# Format should ignore message and everything, and no matter
# what gets passed in we should just get 'stuff' back.
for handler in root_logger.handlers:
self.assertEqual(
handler.formatter.format(log_record),
TEST_FORMATTER
)