-
Notifications
You must be signed in to change notification settings - Fork 23
/
log4sh_test_rolling_file_appender.sh
executable file
·550 lines (452 loc) · 13.4 KB
/
log4sh_test_rolling_file_appender.sh
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#! /bin/sh
# $Id$
# vim:et:ft=sh:sts=2:sw=2
#
# Copyright 2008 Kate Ward. All Rights Reserved.
# Released under the LGPL (GNU Lesser General Public License)
# Author: [email protected] (Kate Ward)
#
# log4sh unit test for standard ASCII character set support.
# load test helpers
. ./log4sh_test_helpers
MY_LOG4SH="${TH_TESTDATA_DIR}/rolling_file_appender.log4sh"
APP_NAME='R'
APP_MAX_FILE_SIZE='10KiB'
APP_MAX_FILE_SIZE_REAL=10240
APP_MAX_BACKUP_INDEX='1'
#------------------------------------------------------------------------------
# custom asserts
#
assertFileExists()
{
${DEBUG} "assertFileExists($@)"
if [ $# -eq 2 ]; then
assertTrue "$1" "[ -f '$2' ]"
else
assertTrue "[ -f '$1' ]"
fi
}
assertFileNotExists()
{
${DEBUG} "assertFileNotExists($@)"
if [ $# -eq 2 ]; then
assertTrue "$1" "[ ! -f '$2' ]"
else
assertTrue "[ ! -f '$1' ]"
fi
}
#------------------------------------------------------------------------------
# suite tests
#
loadLog4sh_runtime()
{
# add a rolling file appender at the INFO level
logger_addAppender ${APP_NAME}
appender_setType ${APP_NAME} RollingFileAppender
appender_file_setFile ${APP_NAME} "${appFile}"
appender_file_setMaxFileSize ${APP_NAME} ${APP_MAX_FILE_SIZE}
appender_file_setMaxBackupIndex ${APP_NAME} ${APP_MAX_BACKUP_INDEX}
# activate the appender
appender_activateOptions ${APP_NAME}
}
loadLog4sh_config()
{
# load log4sh configuration
log4sh_doConfigure "${MY_LOG4SH}"
}
createSizedFile()
{
file=$1
size=$2
# this could be slow...
${DEBUG} "creating ${size} byte file..."
if [ ${size} -gt 0 ]; then
result=`dd if=/dev/zero of="${file}" bs=1 count=${size} 2>&1`
if [ $? -ne 0 ]; then
echo "ERROR: had problems creating the '${file}' of ${size} bytes"
return 1
fi
else
touch "${file}"
fi
}
testEmptyFile_runtime()
{ testEmptyFile runtime; }
testEmptyFile_config()
{ testEmptyFile config; }
testEmptyFile()
{
${DEBUG} 'testing empty file'
useLog4sh=$1
# configure log4sh
loadLog4sh_${useLog4sh}
# removing any previous files
rm -f "${appFile}" "${appFile}".*
# create file(s)
createSizedFile "${appFile}" 0 \
|| return 1
# log a message
logger_error 'dummy message'
# as we started with a empty file, writing a single logging message should
# not have caused an overrun, nor a rotation
assertFileNotExists \
"the file rotated, but it shouldn't have" \
"${appFile}.0"
}
testOneByteUnderSize_runtime()
{ testOneByteUnderSize runtime; }
testOneByteUnderSize_config()
{ testOneByteUnderSize config; }
testOneByteUnderSize()
{
${DEBUG} 'testing one byte under sized file'
useLog4sh=$1
# configure log4sh
loadLog4sh_${useLog4sh}
# removing any previous files
rm -f "${appFile}" "${appFile}".*
# create file(s)
createSizedFile "${appFile}" `expr ${APP_MAX_FILE_SIZE_REAL} - 1` \
|| return 1
# log a message
logger_error 'dummy message'
# this time, the file was just under the limit for doing a rotation, so
# again, no rotation should have occurred
assertFileNotExists \
"the file rotated, but it shouldn't have" \
"${appFile}.0"
}
testExactlyRightSize_runtime()
{ testExactlyRightSize runtime; }
testExactlyRightSize_config()
{ testExactlyRightSize config; }
testExactlyRightSize()
{
${DEBUG} 'testing exactly right sized file'
useLog4sh=$1
# configure log4sh
loadLog4sh_${useLog4sh}
# removing any previous files
rm -f "${appFile}" "${appFile}".*
# create file(s)
createSizedFile "${appFile}" ${APP_MAX_FILE_SIZE_REAL} \
|| return 1
# log a message
logger_error 'dummy message'
# this time, the file was exactly the right size for doing a rotation. a
# rotation should have occurred
assertFileExists \
"the file didn't rotate, but it should have" \
"${appFile}.0"
}
testOneByteOverSize_runtime()
{ testOneByteOverSize runtime; }
testOneByteOverSize_config()
{ testOneByteOverSize config; }
testOneByteOverSize()
{
${DEBUG} 'testing one byte oversize file'
useLog4sh=$1
# configure log4sh
loadLog4sh_${useLog4sh}
# removing any previous files
rm -f "${appFile}" "${appFile}".*
# create file(s)
createSizedFile "${appFile}" `expr ${APP_MAX_FILE_SIZE_REAL} + 1` \
|| return 1
# log a message
logger_error 'dummy message'
# this time, the file was just above the threshold for rotation. a rotation
# should have occurred
assertFileExists \
"the file didn't rotate, but it should have" \
"${appFile}.0"
}
testOrderOfMagnitudeLarger_runtime()
{ testOrderOfMagnitudeLarger runtime; }
testOrderOfMagnitudeLarger_config()
{ testOrderOfMagnitudeLarger config; }
testOrderOfMagnitudeLarger()
{
${DEBUG} 'testing one byte oversize file'
useLog4sh=$1
# configure log4sh
loadLog4sh_${useLog4sh}
# removing any previous files
rm -f "${appFile}" "${appFile}".*
# create file(s)
createSizedFile "${appFile}" `expr ${APP_MAX_FILE_SIZE_REAL} \* 10` \
|| return 1
# log a message
logger_error 'dummy message'
# this time, the file was way above the threshold for rotation. a rotation
# should have occurred
assertFileExists \
"the file didn't rotate, but it should have" \
"${appFile}.0"
}
testMaxBackupIndexOf2()
{
${DEBUG} 'testing a maximum backup index of 2'
# configure log4sh
logger_addAppender ${APP_NAME}
appender_setType ${APP_NAME} RollingFileAppender
appender_file_setFile ${APP_NAME} "${appFile}"
appender_file_setMaxFileSize ${APP_NAME} ${APP_MAX_FILE_SIZE}
appender_file_setMaxBackupIndex ${APP_NAME} 2
appender_activateOptions ${APP_NAME}
#
# test with no existing .1 backup
#
# removing any previous files
rm -f "${appFile}" "${appFile}".*
# create files
touch "${appFile}.0"
createSizedFile "${appFile}" ${APP_MAX_FILE_SIZE_REAL} \
|| return 1
# log a message
logger_error 'dummy message'
# with a backup index of 2, there should be only two backups (.0 and .1)
assertFileNotExists \
'a backup file with a .2 extension should not exist' \
"${appFile}.2"
assertFileExists \
'a backup file with a .1 extension should exist' \
"${appFile}.1"
assertFileExists \
'a backup file with a .0 extension should exist' \
"${appFile}.0"
assertFileExists \
'the log file should exist' \
"${appFile}"
# the .0 backup should have the same size as the file it was rotated from
if [ -f "${appFile}.0" ]; then
appFile0Size=`wc -c "${appFile}.0" |awk '{print $1}'`
else
appFile0Size=0
fi
assertTrue \
"the backup file with a .0 extension wasn't properly rotated" \
"[ ${APP_MAX_FILE_SIZE_REAL} -eq ${appFile0Size} ]"
#
# test with existing .1 backup
#
# removing any previous files
rm -f "${appFile}" "${appFile}".*
# create files
touch "${appFile}.1"
touch "${appFile}.0"
createSizedFile "${appFile}" ${APP_MAX_FILE_SIZE_REAL} \
|| return 1
# log a message
logger_error 'dummy message'
# with a backup index of 2, there should be only two backups (.0 and .1)
assertFileNotExists \
'a backup file with a .2 extension should not have been created' \
"${appFile}.2"
assertFileExists \
'a backup file with a .1 extension should exist' \
"${appFile}.1"
assertFileExists \
'a backup file with a .0 extension should exist' \
"${appFile}.0"
assertFileExists \
'the log file should exist' \
"${appFile}"
# the .0 backup should have the same size as the file it was rotated from
if [ -f "${appFile}.0" ]; then
appFile0Size=`wc -c "${appFile}.0" |awk '{print $1}'`
else
appFile0Size=0
fi
assertTrue \
"the backup file with a .0 extension wasn't properly rotated" \
"[ ${APP_MAX_FILE_SIZE_REAL} -eq ${appFile0Size} ]"
}
testMaxBackupIndexOf1()
{
${DEBUG} 'testing a maximum backup index of 1'
# configure log4sh
logger_addAppender ${APP_NAME}
appender_setType ${APP_NAME} RollingFileAppender
appender_file_setFile ${APP_NAME} "${appFile}"
appender_file_setMaxFileSize ${APP_NAME} ${APP_MAX_FILE_SIZE}
appender_file_setMaxBackupIndex ${APP_NAME} 1
appender_activateOptions ${APP_NAME}
#
# test with no existing .1 backup
#
# removing any previous files
rm -f "${appFile}" "${appFile}".*
# create files
createSizedFile "${appFile}" ${APP_MAX_FILE_SIZE_REAL} \
|| return 1
# log a message
logger_error 'dummy message'
# with a backup index of 1, there should be only one backup (.0)
assertFileNotExists \
'a backup file with a .1 extension should not exist' \
"${appFile}.1"
assertFileExists \
'a backup file with a .0 extension should exist' \
"${appFile}.0"
assertFileExists \
'the log file should exist' \
"${appFile}"
# the .0 backup should have the same size as the file it was rotated from
if [ -f "${appFile}.0" ]; then
appFile0Size=`wc -c "${appFile}.0" |awk '{print $1}'`
else
appFile0Size=0
fi
assertTrue \
"the backup file with a .0 extension wasn't properly rotated" \
"[ ${APP_MAX_FILE_SIZE_REAL} -eq ${appFile0Size} ]"
#
# test with existing .0 backup
#
# removing any previous files
rm -f "${appFile}" "${appFile}".*
# create files
touch "${appFile}.0"
createSizedFile "${appFile}" ${APP_MAX_FILE_SIZE_REAL} \
|| return 1
# log a message
logger_error 'dummy message'
# with a backup index of 1, there should be only one backup (.0)
assertFileNotExists \
'a backup file with a .1 extension should not exist' \
"${appFile}.1"
assertFileExists \
'a backup file with a .0 extension should exist' \
"${appFile}.0"
assertFileExists \
'the log file should exist' \
"${appFile}"
# the .0 backup should have the same size as the file it was rotated from
if [ -f "${appFile}.0" ]; then
appFile0Size=`wc -c "${appFile}.0" |awk '{print $1}'`
else
appFile0Size=0
fi
assertTrue \
"the backup file with a .0 extension wasn't properly rotated" \
"[ ${APP_MAX_FILE_SIZE_REAL} -eq ${appFile0Size} ]"
}
testMaxBackupIndexOf0()
{
${DEBUG} 'testing a maximum backup index of 0'
# configure log4sh
logger_addAppender ${APP_NAME}
appender_setType ${APP_NAME} RollingFileAppender
appender_file_setFile ${APP_NAME} "${appFile}"
appender_file_setMaxFileSize ${APP_NAME} ${APP_MAX_FILE_SIZE}
appender_file_setMaxBackupIndex ${APP_NAME} 0
appender_activateOptions ${APP_NAME}
#
# test with no existing .0 backup
#
# removing any previous files
rm -f "${appFile}" "${appFile}".*
# create files
createSizedFile "${appFile}" ${APP_MAX_FILE_SIZE_REAL} \
|| return 1
# log a message
logger_error 'dummy message'
# with a backup index of 0, there should no backups
assertFileNotExists \
'a backup file with a .0 extension should not exist' \
"${appFile}.0"
assertFileExists \
'the log file should exist' \
"${appFile}"
#
# test with existing .0 backup
#
# removing any previous files
rm -f "${appFile}" "${appFile}".*
# create files
createSizedFile "${appFile}" ${APP_MAX_FILE_SIZE_REAL} \
|| return 1
# log a message
logger_error 'dummy message'
# with a backup index of 0, there should be no backups
assertFileNotExists \
'a backup file with a .0 extension should not have been created' \
"${appFile}.0"
assertFileExists \
'the log file should exist' \
"${appFile}"
}
testMaxFileSizeGetterSetter()
{
# setup
logger_addAppender ${APP_NAME}
appender_setType ${APP_NAME} RollingFileAppender
appender_file_setFile ${APP_NAME} "${appFile}"
appender_activateOptions ${APP_NAME}
### getter
# check that the default size equals the default of log4sh
size=`appender_file_getMaxFileSize ${APP_NAME}`
assertEquals \
"the returned MaxFileSize (${size}) did not equal the log4sh default" \
"${size}" ${__LOG4SH_TYPE_ROLLING_FILE_MAX_FILE_SIZE}
### setter
# test passing valid units; there should be no warnings or errors
for unit in B KB KiB MB MiB GB GiB TB TiB; do
appender_file_setMaxFileSize ${APP_NAME} 1${unit}
assertTrue \
"setter failed with the '${unit}' unit" \
"[ $? -eq ${__LOG4SH_TRUE} ]"
done
# test passing an empty unit
echo '- expecting one warning'
appender_file_setMaxFileSize ${APP_NAME} 1
assertTrue \
"setter failed with an empty unit" \
"[ $? -eq ${__LOG4SH_TRUE} ]"
# test passing an invalid unit
echo '- expecting one error'
appender_file_setMaxFileSize ${APP_NAME} 1foo
assertTrue \
"setter succeeded with an invalid unit" \
"[ $? -eq ${__LOG4SH_ERROR} ]"
}
#------------------------------------------------------------------------------
# suite functions
#
oneTimeSetUp()
{
LOG4SH_CONFIGURATION='none'
th_oneTimeSetUp
appFile="${TH_TMPDIR}/rfa.log"
resultFile="${TH_TMPDIR}/result.dat"
}
setUp()
{
# reset log4sh
log4sh_resetConfiguration
}
tearDown()
{
rm -f "${appFile}" "${appFile}".*
}
suite()
{
suite_addTest testEmptyFile_runtime
suite_addTest testOneByteUnderSize_runtime
suite_addTest testExactlyRightSize_runtime
suite_addTest testOneByteOverSize_runtime
suite_addTest testOrderOfMagnitudeLarger_runtime
suite_addTest testEmptyFile_config
suite_addTest testOneByteUnderSize_config
suite_addTest testExactlyRightSize_config
suite_addTest testOneByteOverSize_config
suite_addTest testOrderOfMagnitudeLarger_config
suite_addTest testMaxBackupIndexOf2
suite_addTest testMaxBackupIndexOf1
suite_addTest testMaxBackupIndexOf0
}
# load and run shUnit2
[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
. ${TH_SHUNIT}