]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/newsyslog/tests/legacy_test.sh
MFV r310115,310184:
[FreeBSD/FreeBSD.git] / usr.sbin / newsyslog / tests / legacy_test.sh
1 #!/bin/sh
2
3 # $FreeBSD$
4
5 COUNT=0
6 TMPDIR=$(pwd)/work
7 if [ $? -ne 0 ]; then
8         echo "$0: Can't create temp dir, exiting..."
9         exit 1
10 fi
11
12 # Begin an individual test
13 begin()
14 {
15         COUNT=`expr $COUNT + 1`
16         OK=1
17         NAME="$1"
18 }
19
20 # End an individual test
21 end()
22 {
23         local message
24
25         if [ $OK = 1 ]
26         then
27                 message='ok '
28         else
29                 message='not ok '
30         fi
31
32         message="$message $COUNT - $NAME"
33         if [ -n "$TODO" ]
34         then
35                 message="$message # TODO $TODO"
36         fi
37
38         echo "$message"
39 }
40
41 # Make a file that can later be verified
42 mkf()
43 {
44         CN=`basename $1`
45         echo "$CN-$CN" >$1
46 }
47
48 # Verify that the file specified is correct
49 ckf()
50 {
51         if [ -f $2 ] && echo "$1-$1" | diff - $2 >/dev/null
52         then
53                 ok
54         else
55                 notok
56         fi
57 }
58
59 # Check that a file exists
60 ckfe()
61 {
62         if [ -f $1 ]
63         then
64                 ok
65         else
66                 notok
67         fi
68 }
69
70 # Verify that the specified file does not exist
71 # (is not there)
72 cknt()
73 {
74         if [ -r $1 ]
75         then
76                 notok
77         else
78                 ok
79         fi
80 }
81
82 # Check if a file is there, depending of if it's supposed to or not -
83 # basically how many log files we are supposed to keep vs. how many we
84 # actually keep.
85 ckntfe()
86 {
87         curcnt=$1
88         keepcnt=$2
89         f=$3
90
91         if [ $curcnt -le $keepcnt ]
92         then
93                 #echo Assuming file there
94                 ckfe $f
95         else
96                 #echo Assuming file NOT there
97                 cknt $f
98         fi
99 }
100
101
102
103 # A part of a test succeeds
104 ok()
105 {
106         :
107 }
108
109 # A part of a test fails
110 notok()
111 {
112         OK=0
113 }
114
115 # Verify that the exit code passed is for unsuccessful termination
116 ckfail()
117 {
118         if [ $1 -gt 0 ]
119         then
120                 ok
121         else
122                 notok
123         fi
124 }
125
126 # Verify that the exit code passed is for successful termination
127 ckok()
128 {
129         if [ $1 -eq 0 ]
130         then
131                 ok
132         else
133                 notok
134         fi
135 }
136
137 # Check that there are X files which match expr
138 chkfcnt()
139 {
140         cnt=$1; shift
141         if [ $cnt -eq `echo "$@" | wc -w` ]
142         then
143                 ok
144         else
145                 notok
146         fi
147 }
148
149 # Check that two strings are alike
150 ckstr()
151 {
152         if [ "$1" = "$2" ]
153         then
154                 ok
155         else
156                 notok
157         fi
158 }
159
160 tmpdir_create()
161 {
162         mkdir -p ${TMPDIR}/log ${TMPDIR}/alog
163         cd ${TMPDIR}/log
164 }
165
166 tmpdir_clean()
167 {
168         cd ${TMPDIR}
169         rm -rf "${TMPDIR}/log" "${TMPDIR}/alog" newsyslog.conf
170 }
171
172 run_newsyslog()
173 {
174
175         newsyslog -f ../newsyslog.conf -F -r "$@"
176 }
177
178 tests_normal_rotate() {
179         ext="$1"
180         dir="$2"
181
182         if [ -n "$dir" ]; then
183                 newsyslog_args=" -a ${dir}"
184                 name_postfix="${ext} archive dir"
185         else
186                 newsyslog_args=""
187                 name_postfix="${ext}"
188         fi
189
190         tmpdir_create
191
192         begin "create file ${name_postfix}" -newdir
193         run_newsyslog -C
194         ckfe $LOGFNAME
195         cknt ${dir}${LOGFNAME}.0${ext}
196         end
197
198         begin "rotate normal 1 ${name_postfix}"
199         run_newsyslog $newsyslog_args
200         ckfe ${LOGFNAME}
201         ckfe ${dir}${LOGFNAME}.0${ext}
202         cknt ${dir}${LOGFNAME}.1${ext}
203         end
204
205         begin "rotate normal 2 ${name_postfix}"
206         run_newsyslog $newsyslog_args
207         ckfe ${LOGFNAME}
208         ckfe ${dir}${LOGFNAME}.0${ext}
209         ckfe ${dir}${LOGFNAME}.1${ext}
210         cknt ${dir}${LOGFNAME}.2${ext}
211         end
212
213         begin "rotate normal 3 ${name_postfix}"
214         run_newsyslog $newsyslog_args
215         ckfe ${LOGFNAME}
216         ckfe ${dir}${LOGFNAME}.0${ext}
217         ckfe ${dir}${LOGFNAME}.1${ext}
218         ckfe ${dir}${LOGFNAME}.2${ext}
219         cknt ${dir}${LOGFNAME}.3${ext}
220         end
221
222         begin "rotate normal 4 ${name_postfix}"
223         run_newsyslog $newsyslog_args
224         ckfe ${LOGFNAME}
225         ckfe ${dir}${LOGFNAME}.0${ext}
226         ckfe ${dir}${LOGFNAME}.1${ext}
227         ckfe ${dir}${LOGFNAME}.2${ext}
228         cknt ${dir}${LOGFNAME}.4${ext}
229         end
230
231         begin "rotate normal 5 ${name_postfix}"
232         run_newsyslog $newsyslog_args
233         ckfe ${LOGFNAME}
234         ckfe ${dir}${LOGFNAME}.0${ext}
235         ckfe ${dir}${LOGFNAME}.1${ext}
236         ckfe ${dir}${LOGFNAME}.2${ext}
237         cknt ${dir}${LOGFNAME}.4${ext}
238         end
239
240         # Wait a bit so we can see if the noaction test rotates files
241         sleep 1.1
242
243         begin "noaction ${name_postfix}"
244         ofiles=`ls -Tl ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`
245         run_newsyslog ${newsyslog_args} -n >/dev/null
246         ckfe ${LOGFNAME}
247         ckstr "$ofiles" "`ls -lT ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`"
248         end
249
250         tmpdir_clean
251 }
252
253 tests_normal_rotate_keepn() {
254         cnt="$1"
255         ext="$2"
256         dir="$3"
257
258         if [ -n "$dir" ]; then
259                 newsyslog_args=" -a ${dir}"
260                 name_postfix="${ext} archive dir"
261         else
262                 newsyslog_args=""
263                 name_postfix="${ext}"
264         fi
265
266         tmpdir_create
267
268         begin "create file ${name_postfix}" -newdir
269         run_newsyslog -C
270         ckfe $LOGFNAME
271         cknt ${dir}${LOGFNAME}.0${ext}
272         end
273
274         begin "rotate normal 1 cnt=$cnt ${name_postfix}"
275         run_newsyslog $newsyslog_args
276         ckfe ${LOGFNAME}
277         ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext}
278         cknt ${dir}${LOGFNAME}.1${ext}
279         end
280
281         begin "rotate normal 2 cnt=$cnt ${name_postfix}"
282         run_newsyslog $newsyslog_args
283         ckfe ${LOGFNAME}
284         ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext}
285         ckntfe 2 $cnt ${dir}${LOGFNAME}.1${ext}
286         cknt ${dir}${LOGFNAME}.2${ext}
287         end
288
289         begin "rotate normal 3 cnt=$cnt ${name_postfix}"
290         run_newsyslog $newsyslog_args
291         ckfe ${LOGFNAME}
292         ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext}
293         ckntfe 2 $cnt ${dir}${LOGFNAME}.1${ext}
294         ckntfe 3 $cnt ${dir}${LOGFNAME}.2${ext}
295         cknt ${dir}${LOGFNAME}.3${ext}
296         end
297
298         begin "rotate normal 3 cnt=$cnt ${name_postfix}"
299         run_newsyslog $newsyslog_args
300         ckfe ${LOGFNAME}
301         ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext}
302         ckntfe 2 $cnt ${dir}${LOGFNAME}.1${ext}
303         ckntfe 3 $cnt ${dir}${LOGFNAME}.2${ext}
304         ckntfe 4 $cnt ${dir}${LOGFNAME}.3${ext}
305         cknt ${dir}${LOGFNAME}.4${ext}
306         end
307
308         # Wait a bit so we can see if the noaction test rotates files
309         sleep 1.1
310
311         begin "noaction ${name_postfix}"
312         osum=`md5 ${dir}${LOGFNAME} | tr -d '\n'`
313         run_newsyslog ${newsyslog_args} -n >/dev/null
314         ckfe ${LOGFNAME}
315         ckstr "$osum" "`md5 ${dir}${LOGFNAME} | tr -d '\n'`"
316         end
317
318         tmpdir_clean
319 }
320
321 tests_time_rotate() {
322         ext="$1"
323         dir="$2"
324
325         if [ -n "$dir" ]; then
326                 newsyslog_args="-t DEFAULT -a ${dir}"
327                 name_postfix="${ext} archive dir"
328         else
329                 newsyslog_args="-t DEFAULT"
330                 name_postfix="${ext}"
331         fi
332
333         tmpdir_create
334
335         begin "create file ${name_postfix}" -newdir
336         run_newsyslog -C ${newsyslog_args}
337         ckfe ${LOGFNAME}
338         end
339
340         begin "rotate time 1 ${name_postfix}"
341         run_newsyslog ${newsyslog_args}
342         ckfe ${LOGFNAME}
343         chkfcnt 1 ${dir}${LOGFNAME}.*${ext}
344         end
345
346         sleep 1.1
347
348         (
349         TODO="rotate time 2-4 fail today; bug 212160"
350
351         begin "rotate time 2 ${name_postfix}"
352         run_newsyslog ${newsyslog_args}
353         ckfe ${LOGFNAME}
354         chkfcnt 2 ${dir}${LOGFNAME}.*${ext}
355         end
356
357         sleep 1.1
358
359         begin "rotate time 3 ${name_postfix}"
360         run_newsyslog ${newsyslog_args}
361         ckfe ${LOGFNAME}
362         chkfcnt 3 ${dir}${LOGFNAME}.*${ext}
363         end
364
365         sleep 1.1
366
367         begin "rotate time 4 ${name_postfix}"
368         run_newsyslog ${newsyslog_args}
369         ckfe ${LOGFNAME}
370         chkfcnt 3 ${dir}${LOGFNAME}.*${ext}
371         end
372         )
373
374         begin "noaction ${name_postfix}"
375         ofiles=`ls -1 ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`
376         run_newsyslog ${newsyslog_args} -n >/dev/null
377         ckfe ${LOGFNAME}
378         ckstr "$ofiles" "`ls -1 ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`"
379         end
380
381         tmpdir_clean
382 }
383
384 echo 1..126
385 mkdir -p ${TMPDIR}
386 cd ${TMPDIR}
387
388 LOGFNAME=foo.log
389 LOGFPATH=${TMPDIR}/log/${LOGFNAME}
390
391 # Normal, no archive dir, keep X files
392 echo "$LOGFPATH 640  0     *    @T00  NC" > newsyslog.conf
393 tests_normal_rotate_keepn 0
394
395 echo "$LOGFPATH 640  1     *    @T00  NC" > newsyslog.conf
396 tests_normal_rotate_keepn 1
397
398 echo "$LOGFPATH 640  2     *    @T00  NC" > newsyslog.conf
399 tests_normal_rotate_keepn 2
400
401 echo "$LOGFPATH 640  3     *    @T00  NC" > newsyslog.conf
402 tests_normal_rotate_keepn 3
403
404 # Normal, no archive dir, keep X files, gz
405 echo "$LOGFPATH 640  0     *    @T00  NCZ" > newsyslog.conf
406 tests_normal_rotate_keepn 0 ".gz"
407
408 echo "$LOGFPATH 640  1     *    @T00  NCZ" > newsyslog.conf
409 tests_normal_rotate_keepn 1 ".gz"
410
411 echo "$LOGFPATH 640  2     *    @T00  NCZ" > newsyslog.conf
412 tests_normal_rotate_keepn 2 ".gz"
413
414 echo "$LOGFPATH 640  3     *    @T00  NCZ" > newsyslog.conf
415 tests_normal_rotate_keepn 3 ".gz"
416
417 # Normal, no archive dir
418 echo "$LOGFPATH 640  3     *    @T00  NC" > newsyslog.conf
419 tests_normal_rotate
420
421 echo "$LOGFPATH 640  3     *    @T00  NCZ" > newsyslog.conf
422 tests_normal_rotate ".gz"
423
424 echo "$LOGFPATH 640  3     *    @T00  NCJ" > newsyslog.conf
425 tests_normal_rotate ".bz2"
426
427 # Normal, archive dir
428 echo "$LOGFPATH 640  3     *    @T00  NC" > newsyslog.conf
429 tests_normal_rotate "" "${TMPDIR}/alog/"
430
431 echo "$LOGFPATH 640  3     *    @T00  NCZ" > newsyslog.conf
432 tests_normal_rotate ".gz" "${TMPDIR}/alog/"
433
434 echo "$LOGFPATH 640  3     *    @T00  NCJ" > newsyslog.conf
435 tests_normal_rotate ".bz2" "${TMPDIR}/alog/"
436
437 # Time based, no archive dir
438 echo "$LOGFPATH 640  3     *    @T00  NC" > newsyslog.conf
439 tests_time_rotate
440
441 echo "$LOGFPATH 640  3     *    @T00  NCZ" > newsyslog.conf
442 tests_time_rotate "gz" ""
443
444 echo "$LOGFPATH 640  3     *    @T00  NCJ" > newsyslog.conf
445 tests_time_rotate "bz2" ""
446
447 # Time based, archive dir
448 echo "$LOGFPATH 640  3     *    @T00  NC" > newsyslog.conf
449 tests_time_rotate "" "${TMPDIR}/alog/"
450
451 echo "$LOGFPATH 640  3     *    @T00  NCZ" > newsyslog.conf
452 tests_time_rotate "gz" "${TMPDIR}/alog/"
453
454 echo "$LOGFPATH 640  3     *    @T00  NCJ" > newsyslog.conf
455 tests_time_rotate "bz2" "${TMPDIR}/alog/"
456
457 rm -rf "${TMPDIR}"