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 (at] 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. 53 chk_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. 74 init() 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 and file exists. 101 chk_ifexists INIT tst_resm || return $RC 102 chk_ifexists INIT logrotate || return $RC 103 chk_ifexists INIT awk || return $RC 104 chk_ifexists INIT file || return $RC 105 106 return $RC 107 } 108 109 110 # Function: cleanup 111 # 112 # Description: - remove temporaty files and directories. Stop all jobs stated 113 # by this testcase. 114 # 115 # Return: - zero on success. 116 # - non-zero on failure. 117 cleanup() 118 { 119 #remove all cronjobs that were installed. 120 tst_resm TINFO "CLEAN: removing all cron jobs." 121 crontab -r > /dev/null 2>&1 122 123 # remove all the temporary files created by this test. 124 tst_resm TINFO "CLEAN: removing $LTPTMP" 125 rm -fr $LTPTMP 126 } 127 128 129 # Function: test01 130 # 131 # Description: - Test that logrotate logrotate will rotate the logfile 132 # according to the specifications in the config file. 133 # - create a config file that will rotate the /var/log/tst_logfile 134 # file. 135 # - use force option to force logrotate to cause the log file to 136 # be rotated. 137 # - compress the file after rotation. 138 # 139 # Return: - zero on success. 140 # - non-zero on failure. 141 test01() 142 { 143 count=0 144 files=" " 145 filesize=0 146 147 TCID=logrotate01 148 TST_COUNT=1 149 150 tst_resm TINFO "Test #1: create a configfile $LTPTMP/var_mesg.config" 151 tst_resm TINFO "Test #1: use logrotate -f <config> to force rotation" 152 tst_resm TINFO "Test #1: this will rotate the log file according to" 153 tst_resm TINFO "Test #1: the specification in the configfile." 154 tst_resm TINFO "Test #1: 1. rotate /var/log/tst_logfile file." 155 tst_resm TINFO "Test #1: 2. compresses it." 156 157 # Check if syslog group exists 158 local group="syslog" 159 grep -q $group /etc/group || group="root" 160 161 # create config file. 162 cat >$LTPTMP/tst_logrotate.conf <<-EOF 163 #****** Begin Config file ******* 164 # create new (empty) log files after rotating old ones 165 create 166 167 # compress the log files 168 compress 169 170 /var/log/tst_logfile { 171 su root $group 172 rotate 5 173 weekly 174 } 175 #****** End Config file ******* 176 EOF 177 178 # create a log file in /var/log/ 179 cat >/var/log/tst_logfile <<-EOF 180 #****** Begin Log File ******** 181 # This is a dummy log file. 182 #****** End Log File ******** 183 EOF 184 185 while [ $count -lt 10 ] 186 do 187 echo "This a dummy log file used to test logrotate command." >> \ 188 /var/log/tst_logfile 189 count=$(( $count+1 )) 190 done 191 192 # remove all old-n-stale logfiles. 193 for files in /var/log/tst_logfile.* 194 do 195 rm -f $files > /dev/null 2>&1 196 done 197 198 chmod 644 $LTPTMP/tst_logrotate.conf 199 logrotate -fv $LTPTMP/tst_logrotate.conf > $LTPTMP/tst_logrotate.out 2>&1 \ 200 || RC=$? 201 if [ $RC -eq 0 ] 202 then 203 # check if config file $LTPTMP/tst_logrotate.conf is read 204 # check if /etc/logrotate.d is included/ 205 # check if 5 rotations are forced. 206 # check if compression is done. 207 grep "reading config file $LTPTMP/tst_logrotate.conf" \ 208 $LTPTMP/tst_logrotate.out > $LTPTMP/tst_logrotate.err 2>&1 || RC=$? 209 grep "forced from command line (5 rotations)" \ 210 $LTPTMP/tst_logrotate.out > $LTPTMP/tst_logrotate.err 2>&1 || RC=$? 211 egrep "compressing new|log with" \ 212 $LTPTMP/tst_logrotate.out > $LTPTMP/tst_logrotate.err 2>&1 || RC=$? 213 if [ $RC -ne 0 ] 214 then 215 tst_res TFAIL $LTPTMP/tst_logrotate.err \ 216 "Test #1: logrotate command failed. Reason:" 217 else 218 # Check if compressed log file is created. 219 if [ -f /var/log/tst_logfile.1.gz ] 220 then 221 file /var/log/tst_logfile.1.gz | grep "gzip compressed data" \ 222 > $LTPTMP/tst_logrotate.out 2>&1 || RC=$? 223 if [ $RC -eq 0 ] 224 then 225 tst_resm TPASS \ 226 "Test #1: logrotate created a compressed file." 227 else 228 tst_res TFAIL $LTPTMP/tst_logrotate.out \ 229 "Test #1: Failed to create a compressed file. Reason:" 230 fi 231 return $RC 232 else 233 tst_res TFAIL $LTPTMP/tst_logrotate.out \ 234 "Test #1: Failed create /var/log/tst_logfile.1.gz. Reason:" 235 return $RC 236 fi 237 fi 238 else 239 tst_res TFAIL $LTPTMP/tst_logrotate.out \ 240 "Test #1: logrotate command exited with $RC return code. Output:" 241 fi 242 return $RC 243 } 244 245 246 test02() 247 { 248 # Test #2 249 # Test that logrotate logrotate will rotate the logfile if the logfile 250 # exceeds a certain size. 251 # - create a config file that will rotate the /var/log/tst_largelogfile. 252 # - run logrotate in a cron job that runs every minute. 253 # - add messages to the logfile until it gets rotated when a re-dittermined 254 # size is reached. 255 256 export TCID=logrotate02 257 export TST_COUNT=2 258 RC=0 259 260 tst_resm TINFO "Test #2: create a configfile $LTPTMP/tst_largelog.conf" 261 tst_resm TINFO "Test #2: logrotate $LTPTMP/tst_largelog.conf - cronjob" 262 tst_resm TINFO "Test #2: set to rotate tst_largelogfile when size > 2K" 263 264 265 # create config file. 266 cat >$LTPTMP/tst_largelog.conf <<EOF 267 # create new (empty) log files after rotating old ones 268 create 269 270 # compress the log files 271 compress 272 273 # RPM packages drop log rotation information into this directory 274 include /etc/logrotate.d 275 276 /var/log/tst_largelogfile { 277 rotate 5 278 size=2k 279 } 280 EOF 281 282 # create the pseudo-log file. 283 cat >/var/log/tst_largelogfile <<EOF 284 # This is a psuedo-log file. This file will grow to a 2k size before 285 # getting rotated. 286 EOF 287 288 # create logrotate cron job. 289 cat >$LTPTMP/tst_logrotate.cron <<EOF 290 * * * * * logrotate $LTPTMP/tst_largelog.conf 291 EOF 292 293 chmod 777 $LTPTMP/tst_logrotate.cron > /dev/null 2>&1 294 295 tst_resm TINFO "Test #2: Installing cron job to run logrotate" 296 crontab $LTPTMP/tst_logrotate.cron > $LTPTMP/tst_logrotate.out 2>&1 || RC=$? 297 if [ $RC -ne 0 ] 298 then 299 echo "Exit status of crontab command: $RC" >> tst_logrotate.out 2>/dev/null 300 tst_brk TBROK $LTPTMP/tst_logrotate.out NULL \ 301 "Test #2: crontab Broke while installing cronjob. Reason:" 302 TFAILCNT=$(( $TFAILCN+1 )) 303 else 304 tst_resm TINFO "Test #2: Cronjob installed successfully" 305 fi 306 307 # cron job to increase the log file size. 308 cat >$LTPTMP/tst_addtolog.cron <<EOF 309 310 * * * * * echo "To Err Is Human, To Really Screw Up You Need A Computer." >>/var/log/tst_largelogfile 2>/dev/null 311 EOF 312 313 tst_resm TINFO "Test #2: Installing cron job to increase logsize" 314 crontab $LTPTMP/tst_addtolog.cron > $LTPTMP/tst_logrotate.out 2>&1 || RC=$? 315 if [ $RC -ne 0 ] 316 then 317 echo "Exit status of crontab command: $RC" >> tst_logrotate.out 2>/dev/null 318 tst_brk TBROK $LTPTMP/tst_logrotate.out NULL \ 319 "Test #2: crontab Broke while installing cronjob. Reason:" 320 TFAILCNT=$(( $TFAILCN+1 )) 321 else 322 tst_resm TINFO "Test #2: Cronjob installed successfully" 323 fi 324 325 # let cron jobs get started. 326 sleep 10s 327 328 # increase the log file size. 329 330 # wait for the /var/log/tst_largelogfile to be filled to a size greater than 2k 331 tst_resm TINFO "Test #2: Checking if file size is > 2k" 332 tst_resm TINFO "Test #2: Pls be patient this will take some time." 333 tst_resm TINFO "Test #2: or killall -9 logrotate02 to skip.." 334 if [ -f `which awk` ] 335 then 336 while [ $filesize -lt 2046 ] 337 do 338 filesize=`ls -l /var/log/tst_largelogfile | awk '{print $5}'` 339 done 340 # wait for 1m and check if logrotate has rotated the logfile. The cron job 341 # that does a logrotate runs every 1 minute so give the cron a minute... 342 sleep 1m 343 else 344 tst_resm TINFO "Test #2: No AWK installed ... sleeping for 10mts" 345 sleep 10m 346 fi 347 348 349 if [ -f /var/log/tst_largelogfile.1.gz ] 350 then 351 file /var/log/tst_largelogfile.1.gz | grep "gzip compressed data" \ 352 > $LTPTMP/tst_logrotate.out 2>&1 || RC=$? 353 if [ $RC -eq 0 ] 354 then 355 tst_resm TPASS \ 356 "Test #1: logrotate worked as cron, created a compressed file." 357 else 358 tst_res TFAIL $LTPTMP/tst_logrotate.out \ 359 "Test #1: Failed to create a compressed file. Reason:" 360 fi 361 else 362 tst_res TFAIL $LTPTMP/tst_logrotate.out \ 363 "Test #1: Failed to create /var/log/tst_largelogfile.1.gz. Reason:" 364 TFAILCNT=$(( $TFAILCNT+1 )) 365 fi 366 367 } 368 369 # Function: main 370 # 371 # Description: - Execute all tests and report results. 372 # 373 # Exit: - zero on success 374 # - non-zero on failure. 375 376 RC=0 377 init || exit $? 378 379 test01 || RC=$? 380 381 exit $RC 382