1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#!/usr/bin/python
#
# test-log.py: this file is part of the GRS suite
# Copyright (C) 2015 Anthony G. Basile
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
sys.path.append(os.path.abspath('..'))
import hashlib
import shutil
from grs import Log
logdir = '/tmp/test-log'
def doit(lo, stamped = False):
# Create a log with knowing contents and rotate 3 times.
for i in range(10):
lo.log('first %d' % i, stamped)
lo.rotate_logs()
lo.rotate_logs()
lo.rotate_logs()
for i in list(range(9,-1,-1)):
lo.log('second %d' % i, stamped)
def hashtest(expect_pass = True):
# Hash up our log and three rotations. Do we get what we expected?
m = hashlib.md5()
for i in [ '', '.0', '.1', '.2']:
log = os.path.join(logdir, 'test.log%s' % i)
with open(log, 'r') as f:
m.update(f.read().encode('ascii'))
if expect_pass:
assert(m.hexdigest() == '485b8bf3a9e08bd5ccfdff7e1a8fe4e1')
else:
assert(m.hexdigest() != '485b8bf3a9e08bd5ccfdff7e1a8fe4e1')
if __name__ == "__main__":
if os.path.isdir(logdir):
shutil.rmtree(logdir)
os.makedirs(logdir)
logfile = os.path.join(logdir, 'test.log')
lo = Log(logfile)
doit(lo, stamped=False)
hashtest(expect_pass=True)
doit(lo, stamped=True)
hashtest(expect_pass=False)
# Make sure we're dropping past the upper limit.
lo.rotate_logs(upper_limit=2)
assert(os.path.isfile(logfile))
assert(os.path.isfile(logfile+'.0'))
assert(os.path.isfile(logfile+'.1'))
assert(os.path.isfile(logfile+'.2'))
assert(not os.path.isfile(logfile+'.3'))
assert(not os.path.isfile(logfile+'.4'))
assert(not os.path.isfile(logfile+'.5'))
|