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