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.testing import FunctionalLayer, doctestsuite_for_module |
---|
33 | from waeup.sirp.utils.logger import ( |
---|
34 | ISiteLoggers, ILogger, SiteLoggers, WAeUPLogger, SiteLoggersRepository) |
---|
35 | |
---|
36 | class SiteLoggersTests(FunctionalTestCase): |
---|
37 | |
---|
38 | layer = FunctionalLayer |
---|
39 | |
---|
40 | def setUp(self): |
---|
41 | super(SiteLoggersTests, self).setUp() |
---|
42 | |
---|
43 | # Setup a sample site for each test |
---|
44 | app = University() |
---|
45 | self.dc_root = tempfile.mkdtemp() |
---|
46 | app['datacenter'].setStoragePath(self.dc_root) |
---|
47 | |
---|
48 | # Prepopulate the ZODB... |
---|
49 | self.getRootFolder()['app'] = app |
---|
50 | self.app = self.getRootFolder()['app'] |
---|
51 | |
---|
52 | self.workdir = tempfile.mkdtemp() |
---|
53 | SiteLoggersRepository.clear() |
---|
54 | return |
---|
55 | |
---|
56 | def tearDown(self): |
---|
57 | super(SiteLoggersTests, self).tearDown() |
---|
58 | shutil.rmtree(self.workdir) |
---|
59 | shutil.rmtree(self.dc_root) |
---|
60 | clearSite() |
---|
61 | return |
---|
62 | |
---|
63 | def test_iface(self): |
---|
64 | loggers = SiteLoggers() |
---|
65 | verifyClass(ISiteLoggers, SiteLoggers) |
---|
66 | verifyObject(ISiteLoggers, loggers) |
---|
67 | return |
---|
68 | |
---|
69 | def test_loggers(self): |
---|
70 | loggers = SiteLoggers() |
---|
71 | loggers.register('sample1') |
---|
72 | loggers.register('sample2') |
---|
73 | result = loggers.loggers |
---|
74 | assert len(result) == 2 |
---|
75 | |
---|
76 | def test_register(self): |
---|
77 | loggers = SiteLoggers() |
---|
78 | loggers.register('sample') |
---|
79 | assert 'sample' in loggers._loggers.keys() |
---|
80 | |
---|
81 | def test_register_duplicate(self): |
---|
82 | loggers = SiteLoggers() |
---|
83 | loggers.register('sample') |
---|
84 | self.assertRaises( |
---|
85 | KeyError, |
---|
86 | loggers.register, 'sample') |
---|
87 | |
---|
88 | def test_unregister(self): |
---|
89 | loggers = SiteLoggers() |
---|
90 | loggers.register('sample') |
---|
91 | loggers.unregister('sample') |
---|
92 | self.assertEqual(loggers._loggers, {}) |
---|
93 | |
---|
94 | def test_utility_no_site(self): |
---|
95 | # make sure we get loggers even if no site is defined/set |
---|
96 | util = queryUtility(ISiteLoggers) |
---|
97 | loggers = util.getLoggers() |
---|
98 | assert util is not None |
---|
99 | assert loggers is not None |
---|
100 | assert None in SiteLoggersRepository.keys() |
---|
101 | |
---|
102 | def test_utility(self): |
---|
103 | # make sute we get loggers for a certain site |
---|
104 | setSite(self.app) |
---|
105 | util = queryUtility(ISiteLoggers) |
---|
106 | loggers = util.getLoggers() |
---|
107 | assert util is not None |
---|
108 | assert loggers is not None |
---|
109 | assert self.app in SiteLoggersRepository.keys() |
---|
110 | |
---|
111 | def test_utility_multiple(self): |
---|
112 | # make sure we get the same loggers for same site |
---|
113 | setSite(self.app) |
---|
114 | self.assertEqual(SiteLoggersRepository.keys(), []) |
---|
115 | util = queryUtility(ISiteLoggers) |
---|
116 | loggers1 = util.getLoggers() |
---|
117 | loggers2 = util.getLoggers() |
---|
118 | assert loggers1 is loggers2 |
---|
119 | |
---|
120 | def test_utility_given_site(self): |
---|
121 | # make sure a passed site is respected |
---|
122 | util = queryUtility(ISiteLoggers) |
---|
123 | loggers = util.getLoggers(site=self.app) |
---|
124 | assert loggers is not None |
---|
125 | assert self.app in SiteLoggersRepository.keys() |
---|
126 | |
---|
127 | class LoggerTests(FunctionalTestCase): |
---|
128 | |
---|
129 | layer = FunctionalLayer |
---|
130 | |
---|
131 | def setUp(self): |
---|
132 | super(LoggerTests, self).setUp() |
---|
133 | |
---|
134 | # Setup a sample site for each test |
---|
135 | app = University() |
---|
136 | self.dc_root = tempfile.mkdtemp() |
---|
137 | app['datacenter'].setStoragePath(self.dc_root) |
---|
138 | |
---|
139 | # Prepopulate the ZODB... |
---|
140 | self.getRootFolder()['app'] = app |
---|
141 | self.app = self.getRootFolder()['app'] |
---|
142 | |
---|
143 | self.workdir = tempfile.mkdtemp() |
---|
144 | return |
---|
145 | |
---|
146 | def tearDown(self): |
---|
147 | super(LoggerTests, self).tearDown() |
---|
148 | shutil.rmtree(self.workdir) |
---|
149 | shutil.rmtree(self.dc_root) |
---|
150 | clearSite() |
---|
151 | return |
---|
152 | |
---|
153 | def test_iface(self): |
---|
154 | setSite(self.app) |
---|
155 | logger = WAeUPLogger('sample') |
---|
156 | verifyClass(ILogger, WAeUPLogger) |
---|
157 | verifyObject(ILogger, logger) |
---|
158 | return |
---|
159 | |
---|
160 | def test_logger_attr(self): |
---|
161 | setSite(self.app) |
---|
162 | logger = WAeUPLogger('sample') |
---|
163 | raw_logger = logger.logger |
---|
164 | assert isinstance(raw_logger, logging.Logger) |
---|
165 | |
---|
166 | def test_site_name(self): |
---|
167 | setSite(self.app) |
---|
168 | logger = WAeUPLogger('waeup.sirp.%s.sample', site=self.app) |
---|
169 | self.assertEqual(logger.logger.name, 'waeup.sirp.app.sample') |
---|
170 | |
---|
171 | def test_site_no_name(self): |
---|
172 | # While a site has no name, we get __app as placeholder |
---|
173 | setSite(self.app) |
---|
174 | self.app.__name__ = None |
---|
175 | logger = WAeUPLogger('waeup.sirp.%s.sample', site=self.app) |
---|
176 | self.assertEqual(logger.logger.name, 'waeup.sirp.__app.sample') |
---|
177 | |
---|
178 | def test_site_changing_name(self): |
---|
179 | # Changing the sitename (from None to a value) will be reflected |
---|
180 | setSite(self.app) |
---|
181 | self.app.__name__ = None |
---|
182 | logger = WAeUPLogger('waeup.sirp.%s.sample', site=self.app) |
---|
183 | self.assertEqual(logger.logger.name, 'waeup.sirp.__app.sample') |
---|
184 | self.app.__name__ = 'myapp' |
---|
185 | self.assertEqual(logger.logger.name, 'waeup.sirp.myapp.sample') |
---|
186 | assert len(logger.logger.handlers) > 0 |
---|
187 | |
---|
188 | def test_loggername_expansion(self): |
---|
189 | setSite(self.app) |
---|
190 | logger = WAeUPLogger('sample', site=self.app) |
---|
191 | logger.logger # Trigger setup |
---|
192 | self.assertEqual(logger._loggername, 'waeup.sirp.app.sample') |
---|
193 | |
---|
194 | def test_filename(self): |
---|
195 | setSite(self.app) |
---|
196 | logger = WAeUPLogger('sample') |
---|
197 | logger.logger # Trigger setup |
---|
198 | self.assertEqual(logger.filename, 'sample.log') |
---|
199 | |
---|
200 | def test_filename_from_dotted_name(self): |
---|
201 | setSite(self.app) |
---|
202 | logger = WAeUPLogger('waeup.sirp.%s.sample') |
---|
203 | logger.logger # Trigger setup |
---|
204 | logfile = os.path.join(self.dc_root, 'logs', 'sample.log') |
---|
205 | self.assertEqual(logger.filename, 'sample.log') |
---|
206 | assert os.path.isfile(logfile) |
---|
207 | |
---|
208 | def test_option_filename(self): |
---|
209 | setSite(self.app) |
---|
210 | logger = WAeUPLogger('sample', filename='my.log') |
---|
211 | logger.logger # Trigger setup |
---|
212 | logfile = os.path.join(self.dc_root, 'logs', 'my.log') |
---|
213 | self.assertEqual(logger.filename, 'my.log') |
---|
214 | assert os.path.isfile(logfile) |
---|
215 | |
---|
216 | def test_option_propagate(self): |
---|
217 | logger = WAeUPLogger('sample', site=self.app) |
---|
218 | assert logger.logger.propagate is False # default |
---|
219 | logger = WAeUPLogger('sample', site=self.app, propagate=True) |
---|
220 | assert logger.logger.propagate is True |
---|
221 | logger = WAeUPLogger('sample', site=self.app, propagate=False) |
---|
222 | assert logger.logger.propagate is False |
---|
223 | |
---|
224 | def test_logging(self): |
---|
225 | logger = WAeUPLogger('sample', site=self.app) |
---|
226 | logger.logger.info('Hi there!') |
---|
227 | logfile = os.path.join(self.dc_root, 'logs', 'sample.log') |
---|
228 | assert os.path.isfile(logfile) |
---|
229 | contents = open(logfile, 'rb').read() |
---|
230 | self.assertTrue(re.match( |
---|
231 | '^....-..-.. ..:..:..,... - INFO - Hi there!\n$', contents)) |
---|