forked from reddit/monitors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerts_test.py
executable file
·48 lines (39 loc) · 1.7 KB
/
alerts_test.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
#!/usr/bin/env python
import ConfigParser
import logging
import tempfile
import unittest
import alerts
class ConfigureLoggingTest(unittest.TestCase):
@staticmethod
def _config(**data):
config = ConfigParser.RawConfigParser()
config.add_section('logging')
for k, v in data.iteritems():
config.set('logging', k, v)
return config
def test_get_logging_handler(self):
def assertHandler(mode, expected_class):
with tempfile.NamedTemporaryFile() as f:
config = self._config(mode=mode, file=f.name,
syslog_addr='/dev/log')
self.assertTrue(
isinstance(
alerts._get_logging_handler(config), expected_class))
assertHandler('file', logging.FileHandler)
assertHandler('stderr', logging.StreamHandler)
# we can count on the alerts module importing logging.handlers
assertHandler('syslog', logging.handlers.SysLogHandler)
self.assertRaises(ValueError, assertHandler, 'asdf', None)
def test_get_logging_formatter(self):
f = alerts._get_logging_formatter(self._config(mode='syslog'))
self.assertFalse(isinstance(f, alerts.StreamLoggingFormatter))
f = alerts._get_logging_formatter(self._config(mode='not-syslog'))
self.assertTrue(isinstance(f, alerts.StreamLoggingFormatter))
def test_get_logging_level(self):
config = self._config()
self.assertEquals(logging.INFO, alerts._get_logging_level(config))
config = self._config(level='DEBUG')
self.assertEquals('DEBUG', alerts._get_logging_level(config))
if __name__ == '__main__':
unittest.main()