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