forked from spotify/luigi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_logging_test.py
207 lines (166 loc) · 6.08 KB
/
setup_logging_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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from luigi.setup_logging import DaemonLogging, InterfaceLogging
from luigi.configuration import LuigiTomlParser, LuigiConfigParser, get_config
from helpers import unittest
class TestDaemonLogging(unittest.TestCase):
cls = DaemonLogging
def setUp(self):
self.cls._configured = False
def tearDown(self):
self.cls._configured = False
self.cls.config = get_config()
def test_cli(self):
opts = type('opts', (), {})
opts.background = True
result = self.cls._cli(opts)
self.assertTrue(result)
opts.background = False
opts.logdir = './tests/'
result = self.cls._cli(opts)
self.assertTrue(result)
opts.background = False
opts.logdir = False
result = self.cls._cli(opts)
self.assertFalse(result)
def test_section(self):
self.cls.config = {
'logging': {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'mockformatter': {
'format': '{levelname}: {message}',
'style': '{',
'datefmt': '%Y-%m-%d %H:%M:%S',
},
},
'handlers': {
'mockhandler': {
'class': 'logging.StreamHandler',
'level': 'INFO',
'formatter': 'mockformatter',
},
},
'loggers': {
'mocklogger': {
'handlers': ('mockhandler',),
'level': 'INFO',
'disabled': False,
'propagate': False,
},
},
},
}
result = self.cls._section(None)
self.assertTrue(result)
self.cls.config = LuigiTomlParser()
self.cls.config.read(['./test/testconfig/luigi_logging.toml'])
result = self.cls._section(None)
self.assertTrue(result)
self.cls.config = {}
result = self.cls._section(None)
self.assertFalse(result)
def test_section_cfg(self):
self.cls.config = LuigiConfigParser.instance()
result = self.cls._section(None)
self.assertFalse(result)
def test_cfg(self):
self.cls.config = LuigiTomlParser()
self.cls.config.data = {}
result = self.cls._conf(None)
self.assertFalse(result)
self.cls.config.data = {'core': {'logging_conf_file': './blah'}}
with self.assertRaises(OSError):
self.cls._conf(None)
self.cls.config.data = {'core': {
'logging_conf_file': './test/testconfig/logging.cfg',
}}
result = self.cls._conf(None)
self.assertTrue(result)
def test_default(self):
result = self.cls._default(None)
self.assertTrue(result)
class TestInterfaceLogging(TestDaemonLogging):
cls = InterfaceLogging
def test_cli(self):
opts = type('opts', (), {})
result = self.cls._cli(opts)
self.assertFalse(result)
# test_section inherited from TestDaemonLogging
def test_cfg(self):
self.cls.config = LuigiTomlParser()
self.cls.config.data = {}
opts = type('opts', (), {})
opts.logging_conf_file = ''
result = self.cls._conf(opts)
self.assertFalse(result)
opts.logging_conf_file = './blah'
with self.assertRaises(OSError):
self.cls._conf(opts)
opts.logging_conf_file = './test/testconfig/logging.cfg'
result = self.cls._conf(opts)
self.assertTrue(result)
def test_default(self):
opts = type('opts', (), {})
opts.log_level = 'INFO'
result = self.cls._default(opts)
self.assertTrue(result)
class PatchedLogging(InterfaceLogging):
@classmethod
def _cli(cls, *args):
cls.calls.append('_cli')
return '_cli' not in cls.patched
@classmethod
def _conf(cls, *args):
cls.calls.append('_conf')
return '_conf' not in cls.patched
@classmethod
def _section(cls, *args):
cls.calls.append('_section')
return '_section' not in cls.patched
@classmethod
def _default(cls, *args):
cls.calls.append('_default')
return '_default' not in cls.patched
class TestSetup(unittest.TestCase):
def setUp(self):
self.opts = type('opts', (), {})
self.cls = PatchedLogging
self.cls.calls = []
self.cls.config = LuigiTomlParser()
self.cls._configured = False
self.cls.patched = '_cli', '_conf', '_section', '_default'
def tearDown(self):
self.cls.config = get_config()
def test_configured(self):
self.cls._configured = True
result = self.cls.setup(self.opts)
self.assertEqual(self.cls.calls, [])
self.assertFalse(result)
def test_disabled(self):
self.cls.config.data = {'core': {'no_configure_logging': True}}
result = self.cls.setup(self.opts)
self.assertEqual(self.cls.calls, [])
self.assertFalse(result)
def test_order(self):
self.cls.setup(self.opts)
self.assertEqual(self.cls.calls, ['_cli', '_conf', '_section', '_default'])
def test_cli(self):
self.cls.patched = ()
result = self.cls.setup(self.opts)
self.assertTrue(result)
self.assertEqual(self.cls.calls, ['_cli'])
def test_conf(self):
self.cls.patched = ('_cli', )
result = self.cls.setup(self.opts)
self.assertTrue(result)
self.assertEqual(self.cls.calls, ['_cli', '_conf'])
def test_section(self):
self.cls.patched = ('_cli', '_conf')
result = self.cls.setup(self.opts)
self.assertTrue(result)
self.assertEqual(self.cls.calls, ['_cli', '_conf', '_section'])
def test_default(self):
self.cls.patched = ('_cli', '_conf', '_section')
result = self.cls.setup(self.opts)
self.assertTrue(result)
self.assertEqual(self.cls.calls, ['_cli', '_conf', '_section', '_default'])