1 | ## |
---|
2 | ## test_logger.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Mon Jun 13 14:40:04 2011 Uli Fouquet |
---|
5 | ## $Id$ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2011 Uli Fouquet |
---|
8 | ## This program is free software; you can redistribute it and/or modify |
---|
9 | ## it under the terms of the GNU General Public License as published by |
---|
10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
11 | ## (at your option) any later version. |
---|
12 | ## |
---|
13 | ## This program is distributed in the hope that it will be useful, |
---|
14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | ## GNU General Public License for more details. |
---|
17 | ## |
---|
18 | ## You should have received a copy of the GNU General Public License |
---|
19 | ## along with this program; if not, write to the Free Software |
---|
20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | ## |
---|
22 | import logging |
---|
23 | import os |
---|
24 | import re |
---|
25 | import shutil |
---|
26 | import tempfile |
---|
27 | from zope.app.testing.functional import FunctionalTestCase |
---|
28 | from zope.component import queryUtility |
---|
29 | from zope.component.hooks import setSite, clearSite |
---|
30 | from zope.interface.verify import verifyClass, verifyObject |
---|
31 | from waeup.sirp.app import University |
---|
32 | from waeup.sirp.interfaces import ISiteLoggers, ILogger |
---|
33 | from waeup.sirp.testing import FunctionalLayer, doctestsuite_for_module |
---|
34 | from waeup.sirp.utils.logger import ( |
---|
35 | SiteLoggers, WAeUPLogger, SiteLoggersRepository) |
---|
36 | |
---|
37 | class SiteLoggersTests(FunctionalTestCase): |
---|
38 | |
---|
39 | layer = FunctionalLayer |
---|
40 | |
---|
41 | def setUp(self): |
---|
42 | super(SiteLoggersTests, self).setUp() |
---|
43 | |
---|
44 | # Setup a sample site for each test |
---|
45 | app = University() |
---|
46 | self.dc_root = tempfile.mkdtemp() |
---|
47 | app['datacenter'].setStoragePath(self.dc_root) |
---|
48 | |
---|
49 | # Prepopulate the ZODB... |
---|
50 | self.getRootFolder()['app'] = app |
---|
51 | self.app = self.getRootFolder()['app'] |
---|
52 | |
---|
53 | self.workdir = tempfile.mkdtemp() |
---|
54 | SiteLoggersRepository.clear() |
---|
55 | return |
---|
56 | |
---|
57 | def tearDown(self): |
---|
58 | super(SiteLoggersTests, self).tearDown() |
---|
59 | shutil.rmtree(self.workdir) |
---|
60 | shutil.rmtree(self.dc_root) |
---|
61 | clearSite() |
---|
62 | return |
---|
63 | |
---|
64 | def test_iface(self): |
---|
65 | loggers = SiteLoggers() |
---|
66 | verifyClass(ISiteLoggers, SiteLoggers) |
---|
67 | verifyObject(ISiteLoggers, loggers) |
---|
68 | return |
---|
69 | |
---|
70 | def test_loggers(self): |
---|
71 | loggers = SiteLoggers() |
---|
72 | loggers.register('sample1') |
---|
73 | loggers.register('sample2') |
---|
74 | result = loggers.loggers |
---|
75 | assert len(result) == 2 |
---|
76 | |
---|
77 | def test_register(self): |
---|
78 | loggers = SiteLoggers() |
---|
79 | loggers.register('sample') |
---|
80 | assert 'sample' in loggers._loggers.keys() |
---|
81 | |
---|
82 | def test_register_duplicate(self): |
---|
83 | loggers = SiteLoggers() |
---|
84 | loggers.register('sample') |
---|
85 | self.assertRaises( |
---|
86 | KeyError, |
---|
87 | loggers.register, 'sample') |
---|
88 | |
---|
89 | def test_unregister(self): |
---|
90 | loggers = SiteLoggers() |
---|
91 | loggers.register('sample') |
---|
92 | loggers.unregister('sample') |
---|
93 | self.assertEqual(loggers._loggers, {}) |
---|
94 | |
---|
95 | def test_utility_no_site(self): |
---|
96 | # make sure we get loggers even if no site is defined/set |
---|
97 | util = queryUtility(ISiteLoggers) |
---|
98 | loggers = util.getLoggers() |
---|
99 | assert util is not None |
---|
100 | assert loggers is not None |
---|
101 | assert None in SiteLoggersRepository.keys() |
---|
102 | |
---|
103 | def test_utility(self): |
---|
104 | # make sute we get loggers for a certain site |
---|
105 | setSite(self.app) |
---|
106 | util = queryUtility(ISiteLoggers) |
---|
107 | loggers = util.getLoggers() |
---|
108 | assert util is not None |
---|
109 | assert loggers is not None |
---|
110 | assert self.app in SiteLoggersRepository.keys() |
---|
111 | |
---|
112 | def test_utility_multiple(self): |
---|
113 | # make sure we get the same loggers for same site |
---|
114 | setSite(self.app) |
---|
115 | self.assertEqual(SiteLoggersRepository.keys(), []) |
---|
116 | util = queryUtility(ISiteLoggers) |
---|
117 | loggers1 = util.getLoggers() |
---|
118 | loggers2 = util.getLoggers() |
---|
119 | assert loggers1 is loggers2 |
---|
120 | |
---|
121 | def test_utility_given_site(self): |
---|
122 | # make sure a passed site is respected |
---|
123 | util = queryUtility(ISiteLoggers) |
---|
124 | loggers = util.getLoggers(site=self.app) |
---|
125 | assert loggers is not None |
---|
126 | assert self.app in SiteLoggersRepository.keys() |
---|
127 | |
---|
128 | class LoggerTests(FunctionalTestCase): |
---|
129 | |
---|
130 | layer = FunctionalLayer |
---|
131 | |
---|
132 | def setUp(self): |
---|
133 | super(LoggerTests, self).setUp() |
---|
134 | |
---|
135 | # Setup a sample site for each test |
---|
136 | app = University() |
---|
137 | self.dc_root = tempfile.mkdtemp() |
---|
138 | app['datacenter'].setStoragePath(self.dc_root) |
---|
139 | |
---|
140 | # Prepopulate the ZODB... |
---|
141 | self.getRootFolder()['app'] = app |
---|
142 | self.app = self.getRootFolder()['app'] |
---|
143 | |
---|
144 | self.workdir = tempfile.mkdtemp() |
---|
145 | return |
---|
146 | |
---|
147 | def tearDown(self): |
---|
148 | super(LoggerTests, self).tearDown() |
---|
149 | shutil.rmtree(self.workdir) |
---|
150 | shutil.rmtree(self.dc_root) |
---|
151 | clearSite() |
---|
152 | return |
---|
153 | |
---|
154 | def test_iface(self): |
---|
155 | setSite(self.app) |
---|
156 | logger = WAeUPLogger('sample') |
---|
157 | verifyClass(ILogger, WAeUPLogger) |
---|
158 | verifyObject(ILogger, logger) |
---|
159 | return |
---|
160 | |
---|
161 | def test_logger_attr(self): |
---|
162 | setSite(self.app) |
---|
163 | logger = WAeUPLogger('sample') |
---|
164 | raw_logger = logger.logger |
---|
165 | assert isinstance(raw_logger, logging.Logger) |
---|
166 | |
---|
167 | def test_site_name(self): |
---|
168 | setSite(self.app) |
---|
169 | logger = WAeUPLogger('waeup.sirp.%s.sample', site=self.app) |
---|
170 | self.assertEqual(logger.logger.name, 'waeup.sirp.app.sample') |
---|
171 | |
---|
172 | def test_site_no_name(self): |
---|
173 | # While a site has no name, we get __app as placeholder |
---|
174 | setSite(self.app) |
---|
175 | self.app.__name__ = None |
---|
176 | logger = WAeUPLogger('waeup.sirp.%s.sample', site=self.app) |
---|
177 | self.assertEqual(logger.logger.name, 'waeup.sirp.__app.sample') |
---|
178 | |
---|
179 | def test_site_changing_name(self): |
---|
180 | # Changing the sitename (from None to a value) will be reflected |
---|
181 | setSite(self.app) |
---|
182 | self.app.__name__ = None |
---|
183 | logger = WAeUPLogger('waeup.sirp.%s.sample', site=self.app) |
---|
184 | self.assertEqual(logger.logger.name, 'waeup.sirp.__app.sample') |
---|
185 | self.app.__name__ = 'myapp' |
---|
186 | self.assertEqual(logger.logger.name, 'waeup.sirp.myapp.sample') |
---|
187 | assert len(logger.logger.handlers) > 0 |
---|
188 | |
---|
189 | def test_loggername_expansion(self): |
---|
190 | setSite(self.app) |
---|
191 | logger = WAeUPLogger('sample', site=self.app) |
---|
192 | logger.logger # Trigger setup |
---|
193 | self.assertEqual(logger._loggername, 'waeup.sirp.app.sample') |
---|
194 | |
---|
195 | def test_filename(self): |
---|
196 | setSite(self.app) |
---|
197 | logger = WAeUPLogger('sample') |
---|
198 | logger.logger # Trigger setup |
---|
199 | self.assertEqual(logger.filename, 'sample.log') |
---|
200 | |
---|
201 | def test_filename_from_dotted_name(self): |
---|
202 | setSite(self.app) |
---|
203 | logger = WAeUPLogger('waeup.sirp.%s.sample') |
---|
204 | logger.logger # Trigger setup |
---|
205 | logfile = os.path.join(self.dc_root, 'logs', 'sample.log') |
---|
206 | self.assertEqual(logger.filename, 'sample.log') |
---|
207 | assert os.path.isfile(logfile) |
---|
208 | |
---|
209 | def test_option_filename(self): |
---|
210 | setSite(self.app) |
---|
211 | logger = WAeUPLogger('sample', filename='my.log') |
---|
212 | logger.logger # Trigger setup |
---|
213 | logfile = os.path.join(self.dc_root, 'logs', 'my.log') |
---|
214 | self.assertEqual(logger.filename, 'my.log') |
---|
215 | assert os.path.isfile(logfile) |
---|
216 | |
---|
217 | def test_option_propagate(self): |
---|
218 | logger = WAeUPLogger('sample', site=self.app) |
---|
219 | assert logger.logger.propagate is False # default |
---|
220 | logger = WAeUPLogger('sample', site=self.app, propagate=True) |
---|
221 | assert logger.logger.propagate is True |
---|
222 | logger = WAeUPLogger('sample', site=self.app, propagate=False) |
---|
223 | assert logger.logger.propagate is False |
---|
224 | |
---|
225 | def test_logging(self): |
---|
226 | logger = WAeUPLogger('sample', site=self.app) |
---|
227 | logger.logger.info('Hi there!') |
---|
228 | logfile = os.path.join(self.dc_root, 'logs', 'sample.log') |
---|
229 | assert os.path.isfile(logfile) |
---|
230 | contents = open(logfile, 'rb').read() |
---|
231 | self.assertTrue(re.match( |
---|
232 | '^....-..-.. ..:..:..,... - INFO - Hi there!\n$', contents)) |
---|