-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_multilinelogger.py
115 lines (102 loc) · 3.59 KB
/
test_multilinelogger.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
114
115
import unittest
import multilinelogger
import sys
import syslog
from StringIO import StringIO
from mock import Mock, patch
class TestMain(unittest.TestCase):
def setUp(self):
sys.stdin = StringIO()
sys.stderr = StringIO()
syslog.openlog = Mock()
syslog.syslog = Mock()
def test_defaults(self):
sys.stdin.write('test')
sys.stdin.seek(0)
multilinelogger.main()
syslog.openlog.assert_called_with('multilinelogger', 0, syslog.LOG_USER)
syslog.syslog.assert_called_with(
syslog.LOG_NOTICE,
'test'
)
assert sys.stderr.read() == ''
def test_pid(self):
sys.stdin.write('test')
sys.stdin.seek(0)
sys.argv = ['multilinelogger', '-i', '1000']
multilinelogger.main()
syslog.openlog.assert_called_with('multilinelogger[1000]', 0, syslog.LOG_USER)
syslog.syslog.assert_called_with(
syslog.LOG_NOTICE,
'test'
)
assert sys.stderr.read() == ''
def test_stderr(self):
sys.stdin.write('test')
sys.stdin.seek(0)
sys.argv = ['multilinelogger', '-s']
multilinelogger.main()
syslog.openlog.assert_called_with('multilinelogger', 0, syslog.LOG_USER)
syslog.syslog.assert_called_with(
syslog.LOG_NOTICE,
'test'
)
assert 'multilinelogger: test\n' == sys.stderr.getvalue()
@patch('__builtin__.open')
def test_filename(self, open_mock):
open_mock.return_value = StringIO('test')
sys.argv = ['multilinelogger', '-f', 'a_filename']
multilinelogger.main()
open_mock.assert_called_once_with('a_filename', 'r')
syslog.openlog.assert_called_with('multilinelogger', 0, syslog.LOG_USER)
syslog.syslog.assert_called_with(
syslog.LOG_NOTICE,
'test'
)
assert '' == sys.stderr.getvalue()
def test_priority(self):
sys.stdin.write('test')
sys.stdin.seek(0)
sys.argv = ['multilinelogger', '-p', 'local3.info']
multilinelogger.main()
syslog.openlog.assert_called_with('multilinelogger', 0,
syslog.LOG_LOCAL3)
syslog.syslog.assert_called_with(
syslog.LOG_INFO,
'test'
)
def test_priority_without_a_dot(self):
sys.stdin.write('test')
sys.stdin.seek(0)
sys.argv = ['multilinelogger', '-p', 'local3']
multilinelogger.main()
syslog.openlog.assert_called_with('multilinelogger', 0,
syslog.LOG_LOCAL3)
syslog.syslog.assert_called_with(
syslog.LOG_NOTICE,
'test'
)
def test_priority_starts_with_a_dot(self):
sys.stdin.write('test')
sys.stdin.seek(0)
sys.argv = ['multilinelogger', '-p', '.info']
multilinelogger.main()
syslog.openlog.assert_called_with('multilinelogger', 0,
syslog.LOG_USER)
syslog.syslog.assert_called_with(
syslog.LOG_INFO,
'test'
)
def test_priority_ends_with_a_dot(self):
sys.stdin.write('test')
sys.stdin.seek(0)
sys.argv = ['multilinelogger', '-p', 'local3.']
multilinelogger.main()
syslog.openlog.assert_called_with('multilinelogger', 0,
syslog.LOG_LOCAL3)
syslog.syslog.assert_called_with(
syslog.LOG_NOTICE,
'test'
)
if __name__ == '__main__':
unittest.main()