Home | History | Annotate | Download | only in cron
      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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 (at] 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 
     44 export TST_TOTAL=3
     45 
     46 if [ -z "$LTPTMP" -a -z "$TMPBASE" ]
     47 then
     48     LTPTMP=/tmp
     49 else
     50     LTPTMP=$TMPBASE
     51 fi
     52 
     53 if [ -z "$LTPBIN" -a -z "$LTPROOT" ]
     54 then
     55     LTPBIN=./
     56 else
     57     LTPBIN=$LTPROOT/testcases/bin
     58 fi
     59 
     60 . cmdlib.sh
     61 SYSLOG_STARTED=0
     62 
     63 if [ -n "$SYSLOG_DAEMON" ]; then
     64 	status_daemon $SYSLOG_DAEMON
     65 	if [ $? -ne 0 ]; then
     66 		restart_daemon $SYSLOG_DAEMON
     67 		SYSLOG_STARTED=1
     68 	fi
     69 fi
     70 
     71 # Set return code RC variable to 0, it will be set with a non-zero return code
     72 # in case of error. Set TFAILCNT to 0, increment if there occures a failure.
     73 
     74 LOCTMP=${PWD}/tmp
     75 TFAILCNT=0
     76 RC=0
     77 
     78 # Test #1
     79 # Test if crontab <filename> installs the crontab file and cron schedules the
     80 # job correctly.
     81 
     82 export TCID=cron01
     83 export TST_COUNT=1
     84 
     85 $LTPBIN/tst_resm TINFO "Test #1: crontab <filename> installs the crontab file"
     86 $LTPBIN/tst_resm TINFO "Test #1: cron schedules the job listed in crontab file."
     87 
     88 # create the cron job. The job is to run the program tst1_cronprg.sh
     89 # every minute, every hour, every day, every month, any weekday.
     90 
     91 cat > $LTPTMP/tst1_cronjob.cron <<EOF
     92 * * * * * $LTPTMP/tst1_cronprg.sh
     93 EOF
     94 
     95 # Create the program that will be run by the cronjob. This program will print a
     96 # "Hello Hell" string and date time information.
     97 
     98 cat > $LTPTMP/tst1_cronprg.sh <<EOF
     99 #! /bin/sh
    100 
    101 DATE=\`LANG= date\`
    102 echo "Hello Hell today is \$DATE " > $LTPTMP/tst1_cron.out 2>&1
    103 exit 0
    104 EOF
    105 
    106 chmod +x $LTPTMP/tst1_cronprg.sh
    107 
    108 # install the cronjob, crontab <filename> does that. Sleep for 10s and the
    109 # check the /var/log/messages to see if there is a record of any crontab
    110 # activity.
    111 
    112 $LTPBIN/tst_resm TINFO "Test #1: Installing cron job ... "
    113 crontab $LTPTMP/tst1_cronjob.cron >$LTPTMP/cron_tst2n1.out 2>&1
    114 RC=$?
    115 
    116 if [ $RC -ne 0 ]
    117 then
    118 	$LTPBIN/tst_brk TBROK $LTPTMP/cron_tst2n1.out NULL \
    119 		"Test #1: crontab Broke while installing cronjob. Reason:"
    120 		 TFAILCNT=$(( $TFAILCNT+1 ))
    121 else
    122 	$LTPBIN/tst_resm TINFO "Test #1: Cronjob installed successfully"
    123 fi
    124 
    125 sleep 10s
    126 
    127 tail -n 10 /var/log/messages | grep crontab | grep REPLACE \
    128 	> $LTPTMP/cron_tst2n1.out 2>&1
    129 RC=$?
    130 #####
    131 # Some implementations log cron info to /var/log/cron instead...
    132 #####
    133 if [ "$RC" -ne 0 -a -f /var/log/cron ]; then
    134 	$LTPBIN/tst_resm TINFO "Test #1: /var/log/cron: Trying altenate log..."
    135 	tail -n 10 /var/log/cron | grep crontab | grep REPLACE \
    136 	    > $LTPTMP/cron_tst2n1.out 2>&1
    137 	RC=$?
    138 fi
    139 if [ $RC -ne 0 ]
    140 then
    141 	$LTPBIN/tst_resm TFAIL \
    142 		"Test #1: crontab activity not recorded in /var/log/messages."
    143 		 TFAILCNT=$(( $TFAILCNT+1 ))
    144 else
    145 	$LTPBIN/tst_resm TINFO \
    146 		"Test #1: cron activity logged in /var/log/messages"
    147 fi
    148 
    149 # just wait a random time for the cron to kickoff the cronjob.
    150 #####
    151 # Sleep enough to get _just past_ the start of the next minute --
    152 # like 2 or 3 seconds past... since the loop below sleeps for 62
    153 # seconds, we should start this 5-iteration loop closely following
    154 # the start of a minute...
    155 #####
    156 sleep 1m	# allows cron to run once
    157 XS=$(expr 60 - $(date | awk '{print $4}' | cut -f3 -d:))
    158 [ "$XS" -ne 0 ] && sleep ${XS}s		# sleep to the _next_ minute
    159 sleep 3					# ... for good measure...
    160 
    161 # The program executed by the cron job tst1_cronprg.sh will record the date
    162 # and time in a file tst1_cron.out. Extract the minute recorded by the program
    163 # into TS_MIN1 sleep for 1m 10s so that the cron will update this file after
    164 # 1m, extract TS_MIN2 and check if the minute recorded has advanced by 1. Take
    165 # 5 such samples, if any one of the fail, flag a failure.
    166 
    167 LOOP_CNTR=5
    168 TS_MIN1=0
    169 FAILCNT=0
    170 
    171 while [ $LOOP_CNTR -ne 0 ]
    172 do
    173 	TS_MIN1=$(awk '{print $8}' $LTPTMP/tst1_cron.out |
    174 	    awk -F: '{printf("%d", $2);}')
    175 
    176 	# wait for the cronjob to update the tst1_cron.out file.
    177 	sleep 1m 2s
    178 
    179 	# check the time recorded in the tst1_cron.out file,
    180         # this should be 1 minute ahead of what was recored earlier.
    181 
    182 	TS_MIN2=$(awk '{print $8}' $LTPTMP/tst1_cron.out |
    183 	    awk -F: '{printf("%d", $2);}')
    184 
    185 	if [ "x${TS_MIN1}" = "x" ] || [ "x${TS_MIN2}" = "x" ]
    186 	then
    187 		$LTPBIN/tst_resm TFAIL \
    188 			"Test #1: Problem with $LTPTMP/tst1_cron.out file "
    189 		$LTPBIN/tst_resm TFAIL \
    190 			"Test #1: Cause: TS_MIN1= $TS_MIN1; TS_MIN2= $TS_MIN2"
    191 		FAILCNT=$(( $FAILCNT+1 ))
    192 		break;
    193 	fi
    194 
    195 	if [ $TS_MIN1 -eq 59 ]
    196 	then
    197 		TS_MIN1=0
    198 	else
    199 		TS_MIN1=$(( $TS_MIN1+1 ))
    200 	fi
    201 
    202 	if [ $TS_MIN2 -ne $TS_MIN1 ]
    203 	then
    204 		# if the value of the minute field did not advance by 1
    205 		# flag as failure.
    206 		FAILCNT=$(( $FAILCNT+1 ))
    207 		echo "    Expected $TS_MIN1;     Received $TS_MIN2" \
    208 			> $LTPTMP/tst1_cron.log
    209 		$LTPBIN/tst_res TFAIL $LTPTMP/tst1_cron.log \
    210 			"Test #1: Failed to update every minute. Reason:"
    211 		crontab -r >/dev/null 2>&1
    212 		break
    213 	else
    214 		echo "    Expected $TS_MIN1;     Received $TS_MIN2" \
    215 			> $LTPTMP/tst1_cron.log
    216 		$LTPBIN/tst_res TINFO $LTPTMP/tst1_cron.log \
    217 			"Test #1: Values are good: "
    218 	fi
    219 	LOOP_CNTR=$(( $LOOP_CNTR-1 ))
    220 done
    221 
    222 if [ $FAILCNT -eq 0 ]
    223 then
    224 	# check if var/log/messages file was updated.
    225 	grep "CMD ($LTPTMP/tst1_cronprg.sh)" /var/log/messages >$LTPTMP/cron_tst2n1.out 2>&1
    226 	RC=$?
    227 #####
    228 # Some implementations log cron info to /var/log/cron instead...
    229 #####
    230 	if [ "$RC" -ne 0 -a -f /var/log/cron ]; then
    231 		$LTPBIN/tst_resm TINFO "Test #1: /var/log/cron: alternate..."
    232 		grep "CMD ($LTPTMP/tst1_cronprg.sh)" /var/log/cron \
    233 		    >$LTPTMP/cron_tst2n1.out 2>&1
    234 		RC=$?
    235 	fi
    236 	if [ $RC -eq 0 ]
    237 	then
    238 		$LTPBIN/tst_resm TPASS  \
    239 			"Test #1: installed cronjob, and cron executed the cronjob."
    240 	else
    241 		$LTPBIN/tst_res TFAIL $LTPTMP/cron_tst2n1.out \
    242 			"Test #1: Test failed. Reason:"
    243 		 		 TFAILCNT=$(( $TFAILCNT+1 ))
    244 	fi
    245 else
    246 	$LTPBIN/tst_res TFAIL $LTPTMP/cron_tst1.out \
    247 		"Test #1: Cron did not execute every minute"
    248 		 TFAILCNT=$(( $TFAILCNT+1 ))
    249 fi
    250 
    251 #remove the cron job that was installed.
    252 crontab -r >/dev/null 2>&1
    253 
    254 
    255 # Test #2
    256 # Test if crontab -r removes the installed  crontab file
    257 
    258 export TCID=cron02
    259 export TST_COUNT=2
    260 
    261 $LTPBIN/tst_resm TINFO "Test #2: crontab -r removes the crontab file."
    262 
    263 cat > $LTPTMP/tst2_cronjob.cron <<EOF
    264 * * * * * $LTPTMP/tst2_cronprg.sh
    265 EOF
    266 
    267 cat > $LTPTMP/tst2_cronprg.sh <<EOF
    268 #! /bin/sh
    269 
    270 echo "Hello Hell"
    271 exit 0
    272 EOF
    273 
    274 chmod +x  $LTPTMP/tst2_cronprg.sh >/dev/null 2>&1
    275 
    276 $LTPBIN/tst_resm TINFO "Test #2: installing crontab file."
    277 
    278 crontab $LTPTMP/tst2_cronjob.cron >$LTPTMP/cron_tst2n1.out 2>&1
    279 
    280 if [ $? -ne 0 ]
    281 then
    282     $LTPBIN/tst_brk TBROK $LTPTMP/cron_tst2n1.out NULL \
    283         "Test #2: crontab Broke while installing cronjob. Reason:"
    284     TFAILCNT=$(( $TFAILCNT+1 ))
    285 fi
    286 
    287 sleep 10s
    288 
    289 tail -n 10 /var/log/messages | grep crontab | grep REPLACE \
    290     >$LTPTMP/cron_tst2n1.out 2>&1
    291 RC=$?
    292 #####
    293 # Some implementations log cron info to /var/log/cron instead...
    294 #####
    295 if [ "$RC" -ne 0 -a -f /var/log/cron ]; then
    296 	$LTPBIN/tst_resm TINFO "Test #1: /var/log/cron: alternate..."
    297 	tail -n 10 /var/log/cron | grep crontab | grep REPLACE \
    298 	    >$LTPTMP/cron_tst2n1.out 2>&1
    299 	RC=$?
    300 fi
    301 if [ $RC -ne 0 ]
    302 then
    303     $LTPBIN/tst_resm TFAIL \
    304         "Test #2: crontab activity not recorded in var/log/messages."
    305     TFAILCNT=$(( $TFAILCNT+1 ))
    306 fi
    307 
    308 $LTPBIN/tst_resm TINFO "Test #2: uninstalling crontab file."
    309 
    310 crontab -r  >$LTPTMP/cron_tst2n1.out 2>&1
    311 RC=$?
    312 
    313 if [ $RC -ne 0 ]
    314 then
    315     $LTPBIN/tst_brk TBROK $LTPTMP/cron_tst2n1.out NULL \
    316         "Test #2: crontab Broke while installing cronjob. Reason:"
    317     TFAILCNT=$(( $TFAILCNT+1 ))
    318 else
    319 	tail -n 10 /var/log/messages | grep DELETE >$LTPTMP/cron_tst2n1.out 2>&1
    320 	RC=$?
    321 #####
    322 # Some implementations log cron info to /var/log/cron instead...
    323 #####
    324 	if [ "$RC" -ne 0 -a -f /var/log/cron ]; then
    325 		$LTPBIN/tst_resm TINFO "Test #1: /var/log/cron: alternate..."
    326 		tail -n 10 /var/log/cron | grep DELETE \
    327 		    >$LTPTMP/cron_tst2n1.out 2>&1
    328 		RC=$?
    329 	fi
    330 	if [ $RC -ne 0 ]
    331 	then
    332 		$LTPBIN/tst_resm TFAIL \
    333 			"Test #2: crontab activity not recorded in var/log/messages."
    334 		 		 TFAILCNT=$(( $TFAILCNT+1 ))
    335 	else
    336 		$LTPBIN/tst_resm TPASS "Test #2: crontab removed the cronjob"
    337 	fi
    338 fi
    339 
    340 
    341 # Test #3
    342 # Test if crontab -l lists the cronjob installed.
    343 
    344 export TCID=cron03
    345 export TST_COUNT=3
    346 
    347 $LTPBIN/tst_resm TINFO "Test #3: crontab -l lists the cronjobs installed"
    348 
    349 cat > $LTPTMP/tst2_cronjob.cron <<EOF
    350 * * * * * $LTPTMP/tst2_cronprg.sh
    351 EOF
    352 
    353 cat > $LTPTMP/tst2_cronprg.sh <<EOF
    354 #! /bin/sh
    355 
    356 echo "Hello Hell"
    357 exit 0
    358 EOF
    359 
    360 chmod +x  $LTPTMP/tst2_cronprg.sh >/dev/null 2>&1
    361 
    362 $LTPBIN/tst_resm TINFO "Test #3: installing crontab file ..."
    363 crontab $LTPTMP/tst2_cronjob.cron >$LTPTMP/cron_tst2n1.out 2>&1
    364 if [ $? -ne 0 ]
    365 then
    366     $LTPBIN/tst_brkm TBROK NULL \
    367 		"Test #3: crontab failed while installing cronjob"
    368     TFAILCNT=$(( $TFAILCNT+1 ))
    369 else
    370     $LTPBIN/tst_resm TINFO "Test #3: Cron job installed."
    371 fi
    372 
    373 crontab -l | grep "$LTPTMP/tst2_cronprg.sh" >$LTPTMP/cron_tst2n1.out 2>&1
    374 RC=$?
    375 if [ $RC -ne 0 ]
    376 then
    377 	$LTPBIN/tst_brkm TBROK NULL \
    378 		"Test #3: crontab failed while listing cronjobs installed"
    379 		 TFAILCNT=$(( $TFAILCNT+1 ))
    380 else
    381 	$LTPBIN/tst_resm TINFO \
    382 		"Test #3: crontab -l listed cronjob tst2_cronprg.sh"
    383 fi
    384 
    385 $LTPBIN/tst_resm TINFO "Test #3: uninstalling crontab file."
    386 crontab -r >/dev/null 2>&1
    387 
    388 if [ $? -ne 0 ]
    389 then
    390 	$LTPBIN/tst_brkm TBROK NULL "Test #3: crontab failed while removing cronjob"
    391 		 TFAILCNT=$(( $TFAILCNT+1 ))
    392 fi
    393 
    394 crontab -l >$LTPTMP/cron_tst2.out 2>&1
    395 if [ $? -ne 0 ]
    396 then
    397 	grep "no crontab for" $LTPTMP/cron_tst2.out >$LTPTMP/cron_tst2n1.out 2>&1
    398 	RC=$?
    399 	if [ $RC -ne 0 ]
    400 	then
    401 		$LTPBIN/tst_res TFAIL $LTPTMP/cron_tst2n1.out \
    402 			"Test #3: crontab failed removing cronjob. Reason:"
    403 		TFAILCNT=$(( $TFAILCNT+1 ))
    404 	else
    405 		$LTPBIN/tst_resm TINFO "crontab uninstalled all jobs for user"
    406 		$LTPBIN/tst_resm TPASS "crontab did not list any cronjobs"
    407 	fi
    408 else
    409 	$LTPBIN/tst_res TFAIL $LTPTMP/cron_tst2n1.out \
    410 		"Test #3: crontab failed removing cronjob. Reason:"
    411 	TFAILCNT=$(( $TFAILCNT+1 ))
    412 fi
    413 
    414 if [ $SYSLOG_STARTED -eq 1 ]; then
    415 	stop_daemon $SYSLOG_DAEMON
    416 fi
    417 
    418 exit $TFAILCNT
    419