Package flumotion :: Package extern :: Package log :: Module test_log
[hide private]

Source Code for Module flumotion.extern.log.test_log

  1  # -*- Mode: Python; test-case-name: test_log -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  from twisted.trial import unittest 
 23   
 24  import log 
 25   
 26  __version__ = "$Rev: 6842 $" 
 27   
 28   
29 -class LogTester(log.Loggable):
30 logCategory = 'testlog'
31
32 -class LogFunctionTester(log.Loggable):
33 - def logFunction(self, format, *args):
34 return (("override " + format),) + args[1:]
35
36 -class TestLog(unittest.TestCase):
37 - def setUp(self):
38 self.category = self.level = self.message = None 39 self.tester = LogTester() 40 # we want to remove the default handler so it doesn't show up stuff 41 log.reset()
42 43 # just test for parsing semi- or non-valid FLU_DEBUG variables
44 - def testSetDebug(self):
45 log.setDebug(":5") 46 log.setDebug("*") 47 log.setDebug("5")
48 49 # test for adding a log handler
50 - def handler(self, level, object, category, file, line, message):
51 self.level = level 52 self.object = object 53 self.category = category 54 self.file = file 55 self.line = line 56 self.message = message
57
58 - def testLimitInvisible(self):
59 log.setDebug("testlog:3") 60 log.addLimitedLogHandler(self.handler) 61 62 # log 2 we shouldn't get 63 self.tester.log("not visible") 64 assert not self.category 65 assert not self.level 66 assert not self.message 67 68 self.tester.debug("not visible") 69 assert not self.category 70 assert not self.level 71 assert not self.message
72
73 - def testLimitedVisible(self):
74 log.setDebug("testlog:3") 75 log.addLimitedLogHandler(self.handler) 76 77 # log 3 we should get 78 self.tester.info("visible") 79 assert self.category == 'testlog' 80 assert self.level == log.INFO 81 assert self.message == 'visible' 82 83 self.tester.warning("also visible") 84 assert self.category == 'testlog' 85 assert self.level == log.WARN 86 assert self.message == 'also visible'
87
88 - def testFormatStrings(self):
89 log.setDebug("testlog:3") 90 log.addLimitedLogHandler(self.handler) 91 92 self.tester.info("%d %s", 42, 'the answer') 93 assert self.category == 'testlog' 94 assert self.level == log.INFO 95 assert self.message == '42 the answer'
96
97 - def testLimitedError(self):
98 log.setDebug("testlog:3") 99 log.addLimitedLogHandler(self.handler) 100 101 self.assertRaises(SystemExit, self.tester.error, "error") 102 assert self.category == 'testlog' 103 assert self.level == log.ERROR 104 assert self.message == 'error'
105
107 log.setDebug("testlog:3") 108 log.addLimitedLogHandler(self.handler) 109 110 # now try debug and log again too 111 log.setDebug("testlog:5") 112 113 self.tester.debug("debug") 114 assert self.category == 'testlog' 115 assert self.level == log.DEBUG 116 assert self.message == 'debug' 117 118 self.tester.log("log") 119 assert self.category == 'testlog' 120 assert self.level == log.LOG 121 assert self.message == 'log'
122 123 # test that we get all log messages
124 - def testLogHandler(self):
125 log.setDebug("testlog:3") 126 log.addLogHandler(self.handler) 127 128 self.tester.log("visible") 129 assert self.message == 'visible' 130 131 self.tester.warning("also visible") 132 assert self.message == 'also visible'
133
134 -class TestOwnLogHandler(unittest.TestCase):
135 - def setUp(self):
136 self.category = self.level = self.message = None 137 self.tester = LogFunctionTester()
138
139 - def handler(self, level, object, category, file, line, message):
140 self.level = level 141 self.object = object 142 self.category = category 143 self.file = file 144 self.line = line 145 self.message = message
146 147 # test if our own log handler correctly mangles the message
148 - def testOwnLogHandlerLimited(self):
149 log.setDebug("testlog:3") 150 log.addLogHandler(self.handler) 151 152 self.tester.log("visible") 153 assert self.message == 'override visible'
154
155 - def testLogHandlerAssertion(self):
156 self.assertRaises(TypeError, log.addLimitedLogHandler, None)
157
158 -class TestGetExceptionMessage(unittest.TestCase):
159 - def func3(self):
160 self.func2()
161
162 - def func2(self):
163 self.func1()
164
165 - def func1(self):
166 raise TypeError, "I am in func1"
167
168 - def testLevel2(self):
169 try: 170 self.func2() 171 self.fail() 172 except TypeError, e: 173 self.verifyException(e)
174
175 - def testLevel3(self):
176 try: 177 self.func3() 178 self.fail() 179 except TypeError, e: 180 self.verifyException(e)
181
182 - def verifyException(self, e):
183 message = log.getExceptionMessage(e) 184 self.failUnless("func1()" in message) 185 self.failUnless("test_log.py" in message) 186 self.failUnless("TypeError" in message)
187
188 -class TestLogSettings(unittest.TestCase):
189 - def testSet(self):
190 old = log.getLogSettings() 191 log.setDebug('*:5') 192 self.assertNotEquals(old, log.getLogSettings()) 193 194 log.setLogSettings(old) 195 self.assertEquals(old, log.getLogSettings())
196
197 -class TestWriteMark(unittest.TestCase):
198
199 - def handler(self, level, object, category, file, line, message):
200 self.level = level 201 self.object = object 202 self.category = category 203 self.file = file 204 self.line = line 205 self.message = message
206
207 - def testWriteMarkInDebug(self):
208 loggable = log.Loggable() 209 log.setDebug("4") 210 log.addLogHandler(self.handler) 211 marker = 'test' 212 loggable.writeMarker(marker, log.DEBUG) 213 self.assertEquals(self.message, marker)
214
215 - def testWriteMarkInWarn(self):
216 loggable = log.Loggable() 217 log.setDebug("2") 218 log.addLogHandler(self.handler) 219 marker = 'test' 220 loggable.writeMarker(marker, log.WARN) 221 self.assertEquals(self.message, marker)
222
223 - def testWriteMarkInInfo(self):
224 loggable = log.Loggable() 225 log.setDebug("3") 226 log.addLogHandler(self.handler) 227 marker = 'test' 228 loggable.writeMarker(marker, log.INFO) 229 self.assertEquals(self.message, marker)
230
231 - def testWriteMarkInLog(self):
232 loggable = log.Loggable() 233 log.setDebug("5") 234 log.addLogHandler(self.handler) 235 marker = 'test' 236 loggable.writeMarker(marker, log.LOG) 237 self.assertEquals(self.message, marker)
238
239 - def testWriteMarkInError(self):
240 loggable = log.Loggable() 241 log.setDebug("4") 242 log.addLogHandler(self.handler) 243 marker = 'test' 244 self.assertRaises(SystemExit, loggable.writeMarker, marker, log.ERROR) 245 self.assertEquals(self.message, marker)
246
247 -class TestLogNames(unittest.TestCase):
248
249 - def testGetLevelNames(self):
250 self.assertEquals(['ERROR', 'WARN', 'INFO', 'DEBUG', 'LOG'], log.getLevelNames())
251
252 - def testGetLevelCode(self):
253 self.assertEquals(1, log.getLevelInt('ERROR')) 254 self.assertEquals(2, log.getLevelInt('WARN')) 255 self.assertEquals(3, log.getLevelInt('INFO')) 256 self.assertEquals(4, log.getLevelInt('DEBUG')) 257 self.assertEquals(5, log.getLevelInt('LOG'))
258
259 - def testGetLevelName(self):
260 self.assertEquals('ERROR', log.getLevelName(1)) 261 self.assertEquals('WARN', log.getLevelName(2)) 262 self.assertEquals('INFO', log.getLevelName(3)) 263 self.assertEquals('DEBUG', log.getLevelName(4)) 264 self.assertEquals('LOG', log.getLevelName(5))
265 266 if __name__ == '__main__': 267 unittest.main() 268