1
# Copyright (c) 2001-2007 Twisted Matrix Laboratories.
2
# See LICENSE for details.
4
from twisted.trial import unittest
10
from twisted.python import logfile, runtime
13
class LogFileTestCase(unittest.TestCase):
15
Test the rotating log file.
19
self.dir = self.mktemp()
21
self.name = "test.log"
22
self.path = os.path.join(self.dir, self.name)
27
Restore back write rights on created paths: if tests modified the
28
rights, that will allow the paths to be removed easily afterwards.
30
os.chmod(self.dir, 0777)
31
if os.path.exists(self.path):
32
os.chmod(self.path, 0777)
35
def testWriting(self):
36
log = logfile.LogFile(self.name, self.dir)
43
f = open(self.path, "r")
44
self.assertEquals(f.read(), "1234567890")
47
def testRotation(self):
48
# this logfile should rotate every 10 bytes
49
log = logfile.LogFile(self.name, self.dir, rotateLength=10)
51
# test automatic rotation
55
self.assert_(os.path.exists("%s.1" % self.path))
56
self.assert_(not os.path.exists("%s.2" % self.path))
58
self.assert_(os.path.exists("%s.1" % self.path))
59
self.assert_(os.path.exists("%s.2" % self.path))
60
self.assert_(not os.path.exists("%s.3" % self.path))
62
self.assert_(not os.path.exists("%s.3" % self.path))
64
# test manual rotation
66
self.assert_(os.path.exists("%s.3" % self.path))
67
self.assert_(not os.path.exists("%s.4" % self.path))
70
self.assertEquals(log.listLogs(), [1, 2, 3])
73
log = logfile.LogFile(self.name, self.dir)
74
log.write("0123456789")
77
log = logfile.LogFile(self.name, self.dir)
78
self.assertEquals(log.size, 10)
79
self.assertEquals(log._file.tell(), log.size)
81
self.assertEquals(log.size, 13)
82
self.assertEquals(log._file.tell(), log.size)
85
self.assertEquals(f.read(), "0123456789abc")
88
def testLogReader(self):
89
log = logfile.LogFile(self.name, self.dir)
97
self.assertEquals(log.listLogs(), [1])
98
reader = log.getCurrentLog()
100
self.assertEquals(reader.readLines(), ["ghi\n"])
101
self.assertEquals(reader.readLines(), [])
103
reader = log.getLog(1)
104
self.assertEquals(reader.readLines(), ["abc\n", "def\n"])
105
self.assertEquals(reader.readLines(), [])
108
# check getting illegal log readers
109
self.assertRaises(ValueError, log.getLog, 2)
110
self.assertRaises(TypeError, log.getLog, "1")
112
# check that log numbers are higher for older logs
114
self.assertEquals(log.listLogs(), [1, 2])
115
reader = log.getLog(1)
117
self.assertEquals(reader.readLines(), ["ghi\n"])
118
self.assertEquals(reader.readLines(), [])
120
reader = log.getLog(2)
121
self.assertEquals(reader.readLines(), ["abc\n", "def\n"])
122
self.assertEquals(reader.readLines(), [])
125
def testModePreservation(self):
127
Check rotated files have same permissions as original.
129
f = open(self.path, "w").close()
130
os.chmod(self.path, 0707)
131
mode = os.stat(self.path)[stat.ST_MODE]
132
log = logfile.LogFile(self.name, self.dir)
135
self.assertEquals(mode, os.stat(self.path)[stat.ST_MODE])
138
def test_noPermission(self):
140
Check it keeps working when permission on dir changes.
142
log = logfile.LogFile(self.name, self.dir)
145
# change permissions so rotation would fail
146
os.chmod(self.dir, 0555)
148
# if this succeeds, chmod doesn't restrict us, so we can't
151
f = open(os.path.join(self.dir,"xxx"), "w")
152
except (OSError, IOError):
158
log.rotate() # this should not fail
164
self.assertEquals(f.tell(), 6)
166
self.assertEquals(f.read(), "abcdef")
170
def test_maxNumberOfLog(self):
172
Test it respect the limit on the number of files when maxRotatedFiles
175
log = logfile.LogFile(self.name, self.dir, rotateLength=10,
179
self.failUnless(os.path.exists("%s.1" % self.path))
182
self.failUnless(os.path.exists("%s.2" % self.path))
185
self.failUnless(os.path.exists("%s.3" % self.path))
186
self.assertEquals(file("%s.3" % self.path).read(), "1" * 11)
189
self.assertEquals(file("%s.3" % self.path).read(), "2" * 11)
190
self.failUnless(not os.path.exists("%s.4" % self.path))
192
def test_fromFullPath(self):
194
Test the fromFullPath method.
196
log1 = logfile.LogFile(self.name, self.dir, 10, defaultMode=0777)
197
log2 = logfile.LogFile.fromFullPath(self.path, 10, defaultMode=0777)
198
self.assertEquals(log1.name, log2.name)
199
self.assertEquals(os.path.abspath(log1.path), log2.path)
200
self.assertEquals(log1.rotateLength, log2.rotateLength)
201
self.assertEquals(log1.defaultMode, log2.defaultMode)
203
def test_defaultPermissions(self):
205
Test the default permission of the log file: if the file exist, it
206
should keep the permission.
208
f = file(self.path, "w")
209
os.chmod(self.path, 0707)
210
currentMode = stat.S_IMODE(os.stat(self.path)[stat.ST_MODE])
212
log1 = logfile.LogFile(self.name, self.dir)
213
self.assertEquals(stat.S_IMODE(os.stat(self.path)[stat.ST_MODE]),
217
def test_specifiedPermissions(self):
219
Test specifying the permissions used on the log file.
221
log1 = logfile.LogFile(self.name, self.dir, defaultMode=0066)
222
mode = stat.S_IMODE(os.stat(self.path)[stat.ST_MODE])
223
if runtime.platform.isWindows():
224
# The only thing we can get here is global read-only
225
self.assertEquals(mode, 0444)
227
self.assertEquals(mode, 0066)
230
def test_reopen(self):
232
L{logfile.LogFile.reopen} allows to rename the currently used file and
233
make L{logfile.LogFile} create a new file.
235
log1 = logfile.LogFile(self.name, self.dir)
237
savePath = os.path.join(self.dir, "save.log")
238
os.rename(self.path, savePath)
243
f = open(self.path, "r")
244
self.assertEquals(f.read(), "hello2")
246
f = open(savePath, "r")
247
self.assertEquals(f.read(), "hello1")
250
if runtime.platform.isWindows():
251
test_reopen.skip = "Can't test reopen on Windows"
255
class RiggedDailyLogFile(logfile.DailyLogFile):
259
logfile.DailyLogFile._openFile(self)
260
# rig the date to match _clock, not mtime
261
self.lastDate = self.toDate()
263
def toDate(self, *args):
265
return time.gmtime(*args)[:3]
266
return time.gmtime(self._clock)[:3]
268
class DailyLogFileTestCase(unittest.TestCase):
270
Test rotating log file.
274
self.dir = self.mktemp()
275
os.makedirs(self.dir)
276
self.name = "testdaily.log"
277
self.path = os.path.join(self.dir, self.name)
280
def testWriting(self):
281
log = RiggedDailyLogFile(self.name, self.dir)
288
f = open(self.path, "r")
289
self.assertEquals(f.read(), "1234567890")
292
def testRotation(self):
293
# this logfile should rotate every 10 bytes
294
log = RiggedDailyLogFile(self.name, self.dir)
295
days = [(self.path + '.' + log.suffix(day * 86400)) for day in range(3)]
297
# test automatic rotation
298
log._clock = 0.0 # 1970/01/01 00:00.00
300
log._clock = 43200 # 1970/01/01 12:00.00
302
log._clock = 86400 # 1970/01/02 00:00.00
304
self.assert_(os.path.exists(days[0]))
305
self.assert_(not os.path.exists(days[1]))
306
log._clock = 172800 # 1970/01/03 00:00.00
308
self.assert_(os.path.exists(days[0]))
309
self.assert_(os.path.exists(days[1]))
310
self.assert_(not os.path.exists(days[2]))
311
log._clock = 259199 # 1970/01/03 23:59.59
313
self.assert_(not os.path.exists(days[2]))