xref: /freebsd/usr.sbin/newsyslog/tests/legacy_test.sh (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
1#!/bin/sh
2
3# $FreeBSD$
4
5COUNT=0
6TMPDIR=$(pwd)/work
7if [ $? -ne 0 ]; then
8        echo "$0: Can't create temp dir, exiting..."
9        exit 1
10fi
11
12# Begin an individual test
13begin()
14{
15	COUNT=`expr $COUNT + 1`
16	OK=1
17	NAME="$1"
18}
19
20# End an individual test
21end()
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
42mkf()
43{
44	CN=`basename $1`
45	echo "$CN-$CN" >$1
46}
47
48# Verify that the file specified is correct
49ckf()
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
60ckfe()
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)
72cknt()
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.
85ckntfe()
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
104ok()
105{
106	:
107}
108
109# A part of a test fails
110notok()
111{
112	OK=0
113}
114
115# Verify that the exit code passed is for unsuccessful termination
116ckfail()
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
127ckok()
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
138chkfcnt()
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
150ckstr()
151{
152	if [ "$1" = "$2" ]
153	then
154		ok
155	else
156		notok
157	fi
158}
159
160tmpdir_create()
161{
162	mkdir -p ${TMPDIR}/log ${TMPDIR}/alog
163	cd ${TMPDIR}/log
164}
165
166tmpdir_clean()
167{
168	cd ${TMPDIR}
169	rm -rf "${TMPDIR}/log" "${TMPDIR}/alog" newsyslog.conf
170}
171
172run_newsyslog()
173{
174
175	newsyslog -f ../newsyslog.conf -F -r "$@"
176}
177
178tests_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
253tests_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
321tests_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
384echo 1..126
385mkdir -p ${TMPDIR}
386cd ${TMPDIR}
387
388LOGFNAME=foo.log
389LOGFPATH=${TMPDIR}/log/${LOGFNAME}
390
391# Normal, no archive dir, keep X files
392echo "$LOGFPATH	640  0	   *	@T00  NC" > newsyslog.conf
393tests_normal_rotate_keepn 0
394
395echo "$LOGFPATH	640  1	   *	@T00  NC" > newsyslog.conf
396tests_normal_rotate_keepn 1
397
398echo "$LOGFPATH	640  2	   *	@T00  NC" > newsyslog.conf
399tests_normal_rotate_keepn 2
400
401echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
402tests_normal_rotate_keepn 3
403
404# Normal, no archive dir, keep X files, gz
405echo "$LOGFPATH	640  0	   *	@T00  NCZ" > newsyslog.conf
406tests_normal_rotate_keepn 0 ".gz"
407
408echo "$LOGFPATH	640  1	   *	@T00  NCZ" > newsyslog.conf
409tests_normal_rotate_keepn 1 ".gz"
410
411echo "$LOGFPATH	640  2	   *	@T00  NCZ" > newsyslog.conf
412tests_normal_rotate_keepn 2 ".gz"
413
414echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
415tests_normal_rotate_keepn 3 ".gz"
416
417# Normal, no archive dir
418echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
419tests_normal_rotate
420
421echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
422tests_normal_rotate ".gz"
423
424echo "$LOGFPATH	640  3	   *	@T00  NCJ" > newsyslog.conf
425tests_normal_rotate ".bz2"
426
427# Normal, archive dir
428echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
429tests_normal_rotate "" "${TMPDIR}/alog/"
430
431echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
432tests_normal_rotate ".gz" "${TMPDIR}/alog/"
433
434echo "$LOGFPATH	640  3	   *	@T00  NCJ" > newsyslog.conf
435tests_normal_rotate ".bz2" "${TMPDIR}/alog/"
436
437# Time based, no archive dir
438echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
439tests_time_rotate
440
441echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
442tests_time_rotate "gz" ""
443
444echo "$LOGFPATH	640  3	   *	@T00  NCJ" > newsyslog.conf
445tests_time_rotate "bz2" ""
446
447# Time based, archive dir
448echo "$LOGFPATH	640  3	   *	@T00  NC" > newsyslog.conf
449tests_time_rotate "" "${TMPDIR}/alog/"
450
451echo "$LOGFPATH	640  3	   *	@T00  NCZ" > newsyslog.conf
452tests_time_rotate "gz" "${TMPDIR}/alog/"
453
454echo "$LOGFPATH	640  3	   *	@T00  NCJ" > newsyslog.conf
455tests_time_rotate "bz2" "${TMPDIR}/alog/"
456
457rm -rf "${TMPDIR}"
458