cron_tests.sh revision 65b599fa98d30c025c6c937e504aa690e39fbdb2
1#!/bin/sh 2 3################################################################################ 4## ## 5## Copyright (c) International Business Machines Corp., 2001 ## 6## ## 7## This program is free software; you can redistribute it and#or modify ## 8## it under the terms of the GNU General Public License as published by ## 9## the Free Software Foundation; either version 2 of the License, or ## 10## (at your option) any later version. ## 11## ## 12## This program is distributed in the hope that it will be useful, but ## 13## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ## 14## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ## 15## for more details. ## 16## ## 17## You should have received a copy of the GNU General Public License ## 18## along with this program; if not, write to the Free Software ## 19## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## 20## ## 21## ## 22################################################################################ 23# 24# File: cron_tests.sh 25# 26# Description: This testcase tests if crontab <filename> installs the cronjob 27# and cron schedules the job correctly. The job is set such that it will run 28# forever every minute of the day. 29# The cronjob runs a program that will print a string followed by the current 30# date and time. Five samples are taken inorder to verify if cron job is run 31# every minute. It is not practical to check if it works for the remaining 32# fields of the crontab file also. 33# 34# Author: Manoj Iyer manjo@mail.utexas.edu 35# 36# History: 37# Dec - 19 - 2002 - Created. 38# Dec - 20 - 2002 - Correted Test #3, grep for the filename of cronjob 39# after executing crontab -l. 40# - Fixed bug in #3, test was not installing the cronjob. 41# - Added more informational messages TINFO. 42# - Changed permissions to this file to 'x' 43 44export TST_TOTAL=3 45 46if [ -z "$LTPTMP" -a -z "$TMPBASE" ] 47then 48 LTPTMP=/tmp 49else 50 LTPTMP=$TMPBASE 51fi 52 53if [ -z "$LTPBIN" -a -z "$LTPROOT" ] 54then 55 LTPBIN=./ 56else 57 LTPBIN=$LTPROOT/testcases/bin 58fi 59 60# Set return code RC variable to 0, it will be set with a non-zero return code 61# in case of error. Set TFAILCNT to 0, increment if there occures a failure. 62 63LOCTMP=${PWD}/tmp 64TFAILCNT=0 65RC=0 66 67# Test #1 68# Test if crontab <filename> installs the crontab file and cron schedules the 69# job correctly. 70 71export TCID=cron01 72export TST_COUNT=1 73 74$LTPBIN/tst_resm TINFO "Test #1: crontab <filename> installs the crontab file" 75$LTPBIN/tst_resm TINFO "Test #1: cron schedules the job listed in crontab file." 76 77# create the cron job. The job is to run the program tst1_cronprg.sh 78# every minute, every hour, every day, every month, any weekday. 79 80cat > $LTPTMP/tst1_cronjob.cron <<EOF 81* * * * * $LTPTMP/tst1_cronprg.sh 82EOF 83 84# Create the program that will be run by the cronjob. This program will print a 85# "Hello Hell" string and date time information. 86 87cat > $LTPTMP/tst1_cronprg.sh <<EOF 88#! /bin/sh 89 90DATE=\`LANG= date\` 91echo "Hello Hell today is \$DATE " > $LTPTMP/tst1_cron.out 2>&1 92exit 0 93EOF 94 95chmod +x $LTPTMP/tst1_cronprg.sh 96 97# install the cronjob, crontab <filename> does that. Sleep for 10s and the 98# check the /var/log/messages to see if there is a record of any crontab 99# activity. 100 101$LTPBIN/tst_resm TINFO "Test #1: Installing cron job ... " 102crontab $LTPTMP/tst1_cronjob.cron >$LTPTMP/cron_tst2n1.out 2>&1 103RC=$? 104 105if [ $RC -ne 0 ] 106then 107 $LTPBIN/tst_brk TBROK $LTPTMP/cron_tst2n1.out NULL \ 108 "Test #1: crontab Broke while installing cronjob. Reason:" 109 TFAILCNT=$(( $TFAILCNT+1 )) 110else 111 $LTPBIN/tst_resm TINFO "Test #1: Cronjob installed successfully" 112fi 113 114sleep 10s 115 116tail -n 10 /var/log/messages | grep crontab | grep REPLACE \ 117 > $LTPTMP/cron_tst2n1.out 2>&1 118RC=$? 119##### 120# Some implementations log cron info to /var/log/cron instead... 121##### 122if [ "$RC" -ne 0 -a -f /var/log/cron ]; then 123 $LTPBIN/tst_resm TINFO "Test #1: /var/log/cron: Trying altenate log..." 124 tail -n 10 /var/log/cron | grep crontab | grep REPLACE \ 125 > $LTPTMP/cron_tst2n1.out 2>&1 126 RC=$? 127fi 128if [ $RC -ne 0 ] 129then 130 $LTPBIN/tst_resm TFAIL \ 131 "Test #1: crontab activity not recorded in /var/log/messages." 132 TFAILCNT=$(( $TFAILCNT+1 )) 133else 134 $LTPBIN/tst_resm TINFO \ 135 "Test #1: cron activity logged in /var/log/messages" 136fi 137 138# just wait a random time for the cron to kickoff the cronjob. 139##### 140# Sleep enough to get _just past_ the start of the next minute -- 141# like 2 or 3 seconds past... since the loop below sleeps for 62 142# seconds, we should start this 5-iteration loop closely following 143# the start of a minute... 144##### 145sleep 1m # allows cron to run once 146XS=$(expr 60 - $(date | awk '{print $4}' | cut -f3 -d:)) 147[ "$XS" -ne 0 ] && sleep ${XS}s # sleep to the _next_ minute 148sleep 3 # ... for good measure... 149 150# The program executed by the cron job tst1_cronprg.sh will record the date 151# and time in a file tst1_cron.out. Extract the minute recorded by the program 152# into TS_MIN1 sleep for 1m 10s so that the cron will update this file after 153# 1m, extract TS_MIN2 and check if the minute recorded has advanced by 1. Take 154# 5 such samples, if any one of the fail, flag a failure. 155 156LOOP_CNTR=5 157TS_MIN1=0 158FAILCNT=0 159 160while [ $LOOP_CNTR -ne 0 ] 161do 162 TS_MIN1=$(awk '{print $8}' $LTPTMP/tst1_cron.out | 163 awk -F: '{printf("%d", $2);}') 164 165 # wait for the cronjob to update the tst1_cron.out file. 166 sleep 1m 2s 167 168 # check the time recorded in the tst1_cron.out file, 169 # this should be 1 minute ahead of what was recored earlier. 170 171 TS_MIN2=$(awk '{print $8}' $LTPTMP/tst1_cron.out | 172 awk -F: '{printf("%d", $2);}') 173 174 if [ "x${TS_MIN1}" = "x" ] || [ "x${TS_MIN2}" = "x" ] 175 then 176 $LTPBIN/tst_resm TFAIL \ 177 "Test #1: Problem with $LTPTMP/tst1_cron.out file " 178 $LTPBIN/tst_resm TFAIL \ 179 "Test #1: Cause: TS_MIN1= $TS_MIN1; TS_MIN2= $TS_MIN2" 180 FAILCNT=$(( $FAILCNT+1 )) 181 break; 182 fi 183 184 if [ $TS_MIN1 -eq 59 ] 185 then 186 TS_MIN1=0 187 else 188 TS_MIN1=$(( $TS_MIN1+1 )) 189 fi 190 191 if [ $TS_MIN2 -ne $TS_MIN1 ] 192 then 193 # if the value of the minute field did not advance by 1 194 # flag as failure. 195 FAILCNT=$(( $FAILCNT+1 )) 196 echo " Expected $TS_MIN1; Received $TS_MIN2" \ 197 > $LTPTMP/tst1_cron.log 198 $LTPBIN/tst_res TFAIL $LTPTMP/tst1_cron.log \ 199 "Test #1: Failed to update every minute. Reason:" 200 crontab -r >/dev/null 2>&1 201 break 202 else 203 echo " Expected $TS_MIN1; Received $TS_MIN2" \ 204 > $LTPTMP/tst1_cron.log 205 $LTPBIN/tst_res TINFO $LTPTMP/tst1_cron.log \ 206 "Test #1: Values are good: " 207 fi 208 LOOP_CNTR=$(( $LOOP_CNTR-1 )) 209done 210 211if [ $FAILCNT -eq 0 ] 212then 213 # check if var/log/messages file was updated. 214 grep "CMD ($LTPTMP/tst1_cronprg.sh)" /var/log/messages >$LTPTMP/cron_tst2n1.out 2>&1 215 RC=$? 216##### 217# Some implementations log cron info to /var/log/cron instead... 218##### 219 if [ "$RC" -ne 0 -a -f /var/log/cron ]; then 220 $LTPBIN/tst_resm TINFO "Test #1: /var/log/cron: alternate..." 221 grep "CMD ($LTPTMP/tst1_cronprg.sh)" /var/log/cron \ 222 >$LTPTMP/cron_tst2n1.out 2>&1 223 RC=$? 224 fi 225 if [ $RC -eq 0 ] 226 then 227 $LTPBIN/tst_resm TPASS \ 228 "Test #1: installed cronjob, and cron executed the cronjob." 229 else 230 $LTPBIN/tst_res TFAIL $LTPTMP/cron_tst2n1.out \ 231 "Test #1: Test failed. Reason:" 232 TFAILCNT=$(( $TFAILCNT+1 )) 233 fi 234else 235 $LTPBIN/tst_res TFAIL $LTPTMP/cron_tst1.out \ 236 "Test #1: Cron did not execute every minute" 237 TFAILCNT=$(( $TFAILCNT+1 )) 238fi 239 240#remove the cron job that was installed. 241crontab -r >/dev/null 2>&1 242 243 244# Test #2 245# Test if crontab -r removes the installed crontab file 246 247export TCID=cron02 248export TST_COUNT=2 249 250$LTPBIN/tst_resm TINFO "Test #2: crontab -r removes the crontab file." 251 252cat > $LTPTMP/tst2_cronjob.cron <<EOF 253* * * * * $LTPTMP/tst2_cronprg.sh 254EOF 255 256cat > $LTPTMP/tst2_cronprg.sh <<EOF 257#! /bin/sh 258 259echo "Hello Hell" 260exit 0 261EOF 262 263chmod +x $LTPTMP/tst2_cronprg.sh >/dev/null 2>&1 264 265$LTPBIN/tst_resm TINFO "Test #2: installing crontab file." 266 267crontab $LTPTMP/tst2_cronjob.cron >$LTPTMP/cron_tst2n1.out 2>&1 268 269if [ $? -ne 0 ] 270then 271 $LTPBIN/tst_brk TBROK $LTPTMP/cron_tst2n1.out NULL \ 272 "Test #2: crontab Broke while installing cronjob. Reason:" 273 TFAILCNT=$(( $TFAILCNT+1 )) 274fi 275 276sleep 10s 277 278tail -n 10 /var/log/messages | grep crontab | grep REPLACE \ 279 >$LTPTMP/cron_tst2n1.out 2>&1 280RC=$? 281##### 282# Some implementations log cron info to /var/log/cron instead... 283##### 284if [ "$RC" -ne 0 -a -f /var/log/cron ]; then 285 $LTPBIN/tst_resm TINFO "Test #1: /var/log/cron: alternate..." 286 tail -n 10 /var/log/cron | grep crontab | grep REPLACE \ 287 >$LTPTMP/cron_tst2n1.out 2>&1 288 RC=$? 289fi 290if [ $RC -ne 0 ] 291then 292 $LTPBIN/tst_resm TFAIL \ 293 "Test #2: crontab activity not recorded in var/log/messages." 294 TFAILCNT=$(( $TFAILCNT+1 )) 295fi 296 297$LTPBIN/tst_resm TINFO "Test #2: uninstalling crontab file." 298 299crontab -r >$LTPTMP/cron_tst2n1.out 2>&1 300RC=$? 301 302if [ $RC -ne 0 ] 303then 304 $LTPBIN/tst_brk TBROK $LTPTMP/cron_tst2n1.out NULL \ 305 "Test #2: crontab Broke while installing cronjob. Reason:" 306 TFAILCNT=$(( $TFAILCNT+1 )) 307else 308 tail -n 10 /var/log/messages | grep DELETE >$LTPTMP/cron_tst2n1.out 2>&1 309 RC=$? 310##### 311# Some implementations log cron info to /var/log/cron instead... 312##### 313 if [ "$RC" -ne 0 -a -f /var/log/cron ]; then 314 $LTPBIN/tst_resm TINFO "Test #1: /var/log/cron: alternate..." 315 tail -n 10 /var/log/cron | grep DELETE \ 316 >$LTPTMP/cron_tst2n1.out 2>&1 317 RC=$? 318 fi 319 if [ $RC -ne 0 ] 320 then 321 $LTPBIN/tst_resm TFAIL \ 322 "Test #2: crontab activity not recorded in var/log/messages." 323 TFAILCNT=$(( $TFAILCNT+1 )) 324 else 325 $LTPBIN/tst_resm TPASS "Test #2: crontab removed the cronjob" 326 fi 327fi 328 329 330# Test #3 331# Test if crontab -l lists the cronjob installed. 332 333export TCID=cron03 334export TST_COUNT=3 335 336$LTPBIN/tst_resm TINFO "Test #3: crontab -l lists the cronjobs installed" 337 338cat > $LTPTMP/tst2_cronjob.cron <<EOF 339* * * * * $LTPTMP/tst2_cronprg.sh 340EOF 341 342cat > $LTPTMP/tst2_cronprg.sh <<EOF 343#! /bin/sh 344 345echo "Hello Hell" 346exit 0 347EOF 348 349chmod +x $LTPTMP/tst2_cronprg.sh >/dev/null 2>&1 350 351$LTPBIN/tst_resm TINFO "Test #3: installing crontab file ..." 352crontab $LTPTMP/tst2_cronjob.cron >$LTPTMP/cron_tst2n1.out 2>&1 353if [ $? -ne 0 ] 354then 355 $LTPBIN/tst_brkm TBROK NULL \ 356 "Test #3: crontab failed while installing cronjob" 357 TFAILCNT=$(( $TFAILCNT+1 )) 358else 359 $LTPBIN/tst_resm TINFO "Test #3: Cron job installed." 360fi 361 362crontab -l | grep "$LTPTMP/tst2_cronprg.sh" >$LTPTMP/cron_tst2n1.out 2>&1 363RC=$? 364if [ $RC -ne 0 ] 365then 366 $LTPBIN/tst_brkm TBROK NULL \ 367 "Test #3: crontab failed while listing cronjobs installed" 368 TFAILCNT=$(( $TFAILCNT+1 )) 369else 370 $LTPBIN/tst_resm TINFO \ 371 "Test #3: crontab -l listed cronjob tst2_cronprg.sh" 372fi 373 374$LTPBIN/tst_resm TINFO "Test #3: uninstalling crontab file." 375crontab -r >/dev/null 2>&1 376 377if [ $? -ne 0 ] 378then 379 $LTPBIN/tst_brkm TBROK NULL "Test #3: crontab failed while removing cronjob" 380 TFAILCNT=$(( $TFAILCNT+1 )) 381fi 382 383crontab -l >$LTPTMP/cron_tst2.out 2>&1 384if [ $? -ne 0 ] 385then 386 grep "no crontab for" $LTPTMP/cron_tst2.out >$LTPTMP/cron_tst2n1.out 2>&1 387 RC=$? 388 if [ $RC -ne 0 ] 389 then 390 $LTPBIN/tst_res TFAIL $LTPTMP/cron_tst2n1.out \ 391 "Test #3: crontab failed removing cronjob. Reason:" 392 TFAILCNT=$(( $TFAILCNT+1 )) 393 else 394 $LTPBIN/tst_resm TINFO "crontab uninstalled all jobs for user" 395 $LTPBIN/tst_resm TPASS "crontab did not list any cronjobs" 396 fi 397else 398 $LTPBIN/tst_res TFAIL $LTPTMP/cron_tst2n1.out \ 399 "Test #3: crontab failed removing cronjob. Reason:" 400 TFAILCNT=$(( $TFAILCNT+1 )) 401fi 402 403exit $TFAILCNT 404