• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2################################################################################
3##                                                                            ##
4## Copyright (c) International Business Machines  Corp., 2001                 ##
5##                                                                            ##
6## This program is free software;  you can redistribute it and#or modify      ##
7## it under the terms of the GNU General Public License as published by       ##
8## the Free Software Foundation; either version 2 of the License, or          ##
9## (at your option) any later version.                                        ##
10##                                                                            ##
11## This program is distributed in the hope that it will be useful, but        ##
12## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
13## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
14## for more details.                                                          ##
15##                                                                            ##
16## You should have received a copy of the GNU General Public License          ##
17## along with this program;  if not, write to the Free Software               ##
18## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
19##                                                                            ##
20################################################################################
21#
22# File :        logrotate_tests.sh
23#
24# Description:    Test Basic functionality of logrotate command.
25#                Test #1: Test that logrotate -f <file.conf> rotates the logfile
26#                as per the specifications in the conf file. Create a file
27#                tst_logfile in /var/log/. Create a conf file such that this
28#                logfile is set for rotation every week. Execute the command
29#                logrotate -f <file.conf>, check to see if it forced rotation.
30#                Test #2: Check if logrotate running as a cronjob will rotate a
31#                logfile when it exceeds a specific size. Create two cronjobs
32#                1. runs a command to log a string to a logfile. 2. runs
33#                logrotate <file.conf> every minute. The conf file specifies
34#                that the rotation happen only if the log file exceeds 2k file
35#                size.
36#
37# Author:        Manoj Iyer, manjo@mail.utexas.edu
38#
39# History:       Dec 23 2002 - Created - Manoj Iyer.
40#                Dec 24 2002 - Added   - Test #2 - Test to run logrotate as a
41#                                        cron job.
42#                Feb 28 2003 - Fixed   - Modified testcase to use functions.
43#
44# Function: 	chk_ifexists
45#
46# Description:  - Check if command required for this test exits.
47#
48# Input:        - $1 - calling test case.
49#               - $2 - command that needs to be checked.
50#
51# Return:		- zero on success.
52# 				- non-zero on failure.
53chk_ifexists()
54{
55	RC=0
56
57	which $2 > $LTPTMP/tst_logrotate.err 2>&1 || RC=$?
58	if [ $RC -ne 0 ]
59	then
60		tst_brkm TBROK NULL "$1: command $2 not found."
61	fi
62	return $RC
63}
64
65
66# Function: init
67#
68# Description:  - Check if command required for this test exits.
69#               - Create temporary directories required for this test.
70#               - Initialize global variables.
71#
72# Return:		- zero on success.
73# 				- non-zero on failure.
74init()
75{
76	# Initialize global variables.
77	export RC=0
78	export TST_TOTAL=2
79	export TCID="logrotate"
80	export TST_COUNT=0
81
82	# Inititalize cleanup function.
83	trap "cleanup" 0
84
85	# create the temporary directory used by this testcase
86	if [ -z $TMP ]
87	then
88		LTPTMP=/tmp/tst_logrotate.$$
89	else
90		LTPTMP=$TMP/tst_logrotate.$$
91	fi
92
93	mkdir -p $LTPTMP > /dev/null 2>&1 || RC=$?
94	if [ $RC -ne 0 ]
95	then
96		 tst_brkm TBROK "INIT: Unable to create temporary directory"
97		 return $RC
98	fi
99
100	# check if commands tst_*, logrotate, awk exists.
101	chk_ifexists INIT tst_resm  || return $RC
102	chk_ifexists INIT logrotate || return $RC
103	chk_ifexists INIT awk       || return $RC
104
105	return $RC
106}
107
108
109# Function: 	cleanup
110#
111# Description:  - remove temporaty files and directories. Stop all jobs stated
112#                 by this testcase.
113#
114# Return:		- zero on success.
115# 				- non-zero on failure.
116cleanup()
117{
118	#remove all cronjobs that were installed.
119	tst_resm TINFO "CLEAN: removing all cron jobs."
120	crontab -r > /dev/null 2>&1
121
122	# remove all the temporary files created by this test.
123	tst_resm TINFO "CLEAN: removing $LTPTMP"
124	rm -fr $LTPTMP
125}
126
127
128# Function: 	test01
129#
130# Description:  - Test that logrotate logrotate will rotate the logfile
131#                 according to the specifications in the config file.
132#               - create a config file that will rotate the /var/log/tst_logfile
133#                 file.
134#               - use force option to force logrotate to cause the log file to
135#                 be rotated.
136#               - compress the file after rotation.
137#
138# Return:		- zero on success.
139# 				- non-zero on failure.
140test01()
141{
142	count=0
143	files=" "
144	filesize=0
145
146	TCID=logrotate01
147	TST_COUNT=1
148
149	tst_resm TINFO "Test #1: create a configfile $LTPTMP/var_mesg.config"
150	tst_resm TINFO "Test #1: use logrotate -f <config> to force rotation"
151	tst_resm TINFO "Test #1: this will rotate the log file according to"
152	tst_resm TINFO "Test #1: the specification in the configfile."
153	tst_resm TINFO "Test #1: 1. rotate /var/log/tst_logfile file."
154	tst_resm TINFO "Test #1: 2. compresses it."
155
156	# create config file.
157	cat >$LTPTMP/tst_logrotate.conf <<-EOF
158	#****** Begin Config file *******
159	# create new (empty) log files after rotating old ones
160	create
161
162	# compress the log files
163	compress
164
165	/var/log/tst_logfile {
166		rotate 5
167		weekly
168	}
169	#****** End Config file *******
170	EOF
171
172	# create a log file in /var/log/
173	cat >/var/log/tst_logfile <<-EOF
174	#****** Begin Log File ********
175	# This is a dummy log file.
176	#****** End Log File ********
177	EOF
178
179	while [ $count -lt 10 ]
180	do
181		echo "This a dummy log file used to test logrotate command." >> \
182			/var/log/tst_logfile
183		 		 count=$(( $count+1 ))
184	done
185
186	# remove all old-n-stale logfiles.
187	for files in /var/log/tst_logfile.*
188	do
189		rm -f $files > /dev/null 2>&1
190	done
191
192	chmod 644 $LTPTMP/tst_logrotate.conf
193	logrotate -fv $LTPTMP/tst_logrotate.conf > $LTPTMP/tst_logrotate.out 2>&1 \
194		|| RC=$?
195	if [ $RC -eq 0 ]
196	then
197		# check if config file $LTPTMP/tst_logrotate.conf is read
198		# check if  /etc/logrotate.d is included/
199		# check if 5 rotations are forced.
200        # check if compression is done.
201		grep "reading config file $LTPTMP/tst_logrotate.conf" \
202			$LTPTMP/tst_logrotate.out   > $LTPTMP/tst_logrotate.err 2>&1 || RC=$?
203		grep "forced from command line (5 rotations)" \
204			$LTPTMP/tst_logrotate.out   > $LTPTMP/tst_logrotate.err 2>&1 || RC=$?
205		egrep "compressing new|log with" \
206			$LTPTMP/tst_logrotate.out   > $LTPTMP/tst_logrotate.err 2>&1 || RC=$?
207		if [ $RC -ne 0 ]
208		then
209			tst_res TFAIL > $LTPTMP/tst_logrotate.err 2>&1 \
210				"Test #1: logrotate command failed. Reason:"
211		else
212			# Check if compressed log file is created.
213			if [ -f /var/log/tst_logfile.1.gz ]
214			then
215				file /var/log/tst_logfile.1.gz | grep "gzip compressed data" \
216					> $LTPTMP/tst_logrotate.out 2>&1 || RC=$?
217				if [ $RC -eq 0 ]
218				then
219					tst_resm TPASS \
220						"Test #1: logrotate created a compressed file."
221				else
222					tst_res TFAIL $LTPTMP/tst_logrotate.out \
223						"Test #1: Failed to create a compressed file. Reason:"
224				fi
225				return $RC
226			else
227				 tst_res TFAIL  $LTPTMP/tst_logrotate.out \
228				  "Test #1: Failed create /var/log/tst_logfile.1.gz. Reason:"
229				return $RC
230			fi
231		fi
232	else
233		tst_res TFAIL $LTPTMP/tst_logrotate.out \
234		"Test #1: logrotate command exited with $RC return code. Output:"
235	fi
236	return $RC
237}
238
239
240test02()
241{
242# Test #2
243# Test that logrotate logrotate will rotate the logfile if the logfile
244# exceeds a certain size.
245#     - create a config file that will rotate the /var/log/tst_largelogfile.
246#     - run logrotate in a cron job that runs every minute.
247#     - add messages to the logfile until it gets rotated when a re-dittermined
248#        size is reached.
249
250export TCID=logrotate02
251export TST_COUNT=2
252RC=0
253
254tst_resm TINFO "Test #2: create a configfile $LTPTMP/tst_largelog.conf"
255tst_resm TINFO "Test #2: logrotate $LTPTMP/tst_largelog.conf - cronjob"
256tst_resm TINFO "Test #2: set to rotate tst_largelogfile when size > 2K"
257
258
259# create config file.
260cat >$LTPTMP/tst_largelog.conf <<EOF
261# create new (empty) log files after rotating old ones
262create
263
264# compress the log files
265compress
266
267# RPM packages drop log rotation information into this directory
268include /etc/logrotate.d
269
270/var/log/tst_largelogfile {
271    rotate 5
272    size=2k
273}
274EOF
275
276# create the pseudo-log file.
277cat >/var/log/tst_largelogfile <<EOF
278# This is a psuedo-log file. This file will grow to a 2k size before
279# getting rotated.
280EOF
281
282# create logrotate cron job.
283cat >$LTPTMP/tst_logrotate.cron <<EOF
284* * * * * logrotate $LTPTMP/tst_largelog.conf
285EOF
286
287chmod 777 $LTPTMP/tst_logrotate.cron > /dev/null 2>&1
288
289tst_resm TINFO "Test #2: Installing cron job to run logrotate"
290crontab $LTPTMP/tst_logrotate.cron > $LTPTMP/tst_logrotate.out 2>&1 || RC=$?
291if [ $RC -ne 0 ]
292then
293    echo "Exit status of crontab command: $RC" >> tst_logrotate.out 2>/dev/null
294    tst_brk TBROK $LTPTMP/tst_logrotate.out NULL \
295        "Test #2: crontab Broke while installing cronjob. Reason:"
296    TFAILCNT=$(( $TFAILCN+1 ))
297else
298    tst_resm TINFO "Test #2: Cronjob installed successfully"
299fi
300
301# cron job to increase the log file size.
302cat >$LTPTMP/tst_addtolog.cron <<EOF
303
304* * * * * echo "To Err Is Human, To Really Screw Up You Need A Computer."  >>/var/log/tst_largelogfile 2>/dev/null
305EOF
306
307tst_resm TINFO "Test #2: Installing cron job to increase logsize"
308crontab $LTPTMP/tst_addtolog.cron > $LTPTMP/tst_logrotate.out 2>&1 || RC=$?
309if [ $RC -ne 0 ]
310then
311    echo "Exit status of crontab command: $RC" >> tst_logrotate.out 2>/dev/null
312    tst_brk TBROK $LTPTMP/tst_logrotate.out NULL \
313        "Test #2: crontab Broke while installing cronjob. Reason:"
314    TFAILCNT=$(( $TFAILCN+1 ))
315else
316    tst_resm TINFO "Test #2: Cronjob installed successfully"
317fi
318
319# let cron jobs get started.
320sleep 10s
321
322# increase the log file size.
323
324# wait for the /var/log/tst_largelogfile to be filled to a size greater than 2k
325tst_resm TINFO "Test #2: Checking if file size is > 2k"
326tst_resm TINFO "Test #2: Pls be patient this will take some time."
327tst_resm TINFO "Test #2: or killall -9 logrotate02 to skip.."
328if [ -f `which awk` ]
329then
330    while [ $filesize -lt 2046 ]
331    do
332        filesize=`ls -l /var/log/tst_largelogfile | awk '{print $5}'`
333    done
334	# wait for 1m  and check if logrotate has rotated the logfile. The cron job
335	# that does a logrotate runs every 1 minute so give the cron a minute...
336	sleep 1m
337else
338	tst_resm TINFO "Test #2: No AWK installed ... sleeping for 10mts"
339	sleep 10m
340fi
341
342
343if [ -f /var/log/tst_largelogfile.1.gz ]
344then
345    file /var/log/tst_largelogfile.1.gz | grep "gzip compressed data" \
346        > $LTPTMP/tst_logrotate.out 2>&1 || RC=$?
347    if [ $RC -eq 0 ]
348    then
349        tst_resm TPASS \
350            "Test #1: logrotate worked as cron, created a compressed file."
351    else
352        tst_res TFAIL $LTPTMP/tst_logrotate.out \
353            "Test #1: Failed to create a compressed file. Reason:"
354    fi
355else
356    tst_res TFAIL  $LTPTMP/tst_logrotate.out \
357        "Test #1: Failed to create /var/log/tst_largelogfile.1.gz. Reason:"
358    TFAILCNT=$(( $TFAILCNT+1 ))
359fi
360
361}
362
363# Function:	main
364#
365# Description:	- Execute all tests and report results.
366#
367# Exit:			- zero on success
368#               - non-zero on failure.
369
370RC=0
371init || exit $?
372
373test01 || RC=$?
374
375exit $RC
376