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@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. cmdlib.sh 61SYSLOG_STARTED=0 62 63if [ -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 69fi 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 74LOCTMP=${PWD}/tmp 75TFAILCNT=0 76RC=0 77 78# Test #1 79# Test if crontab <filename> installs the crontab file and cron schedules the 80# job correctly. 81 82export TCID=cron01 83export 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 91cat > $LTPTMP/tst1_cronjob.cron <<EOF 92* * * * * $LTPTMP/tst1_cronprg.sh 93EOF 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 98cat > $LTPTMP/tst1_cronprg.sh <<EOF 99#! /bin/sh 100 101DATE=\`LANG= date\` 102echo "Hello Hell today is \$DATE " > $LTPTMP/tst1_cron.out 2>&1 103exit 0 104EOF 105 106chmod +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 ... " 113crontab $LTPTMP/tst1_cronjob.cron >$LTPTMP/cron_tst2n1.out 2>&1 114RC=$? 115 116if [ $RC -ne 0 ] 117then 118 $LTPBIN/tst_brk TBROK $LTPTMP/cron_tst2n1.out NULL \ 119 "Test #1: crontab Broke while installing cronjob. Reason:" 120 TFAILCNT=$(( $TFAILCNT+1 )) 121else 122 $LTPBIN/tst_resm TINFO "Test #1: Cronjob installed successfully" 123fi 124 125sleep 10s 126 127tail -n 10 /var/log/messages | grep crontab | grep REPLACE \ 128 > $LTPTMP/cron_tst2n1.out 2>&1 129RC=$? 130##### 131# Some implementations log cron info to /var/log/cron instead... 132##### 133if [ "$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=$? 138fi 139if [ $RC -ne 0 ] 140then 141 $LTPBIN/tst_resm TFAIL \ 142 "Test #1: crontab activity not recorded in /var/log/messages." 143 TFAILCNT=$(( $TFAILCNT+1 )) 144else 145 $LTPBIN/tst_resm TINFO \ 146 "Test #1: cron activity logged in /var/log/messages" 147fi 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##### 156sleep 1m # allows cron to run once 157XS=$(expr 60 - $(date | awk '{print $4}' | cut -f3 -d:)) 158[ "$XS" -ne 0 ] && sleep ${XS}s # sleep to the _next_ minute 159sleep 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 167LOOP_CNTR=5 168TS_MIN1=0 169FAILCNT=0 170 171while [ $LOOP_CNTR -ne 0 ] 172do 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 )) 220done 221 222if [ $FAILCNT -eq 0 ] 223then 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 245else 246 $LTPBIN/tst_res TFAIL $LTPTMP/cron_tst1.out \ 247 "Test #1: Cron did not execute every minute" 248 TFAILCNT=$(( $TFAILCNT+1 )) 249fi 250 251#remove the cron job that was installed. 252crontab -r >/dev/null 2>&1 253 254 255# Test #2 256# Test if crontab -r removes the installed crontab file 257 258export TCID=cron02 259export TST_COUNT=2 260 261$LTPBIN/tst_resm TINFO "Test #2: crontab -r removes the crontab file." 262 263cat > $LTPTMP/tst2_cronjob.cron <<EOF 264* * * * * $LTPTMP/tst2_cronprg.sh 265EOF 266 267cat > $LTPTMP/tst2_cronprg.sh <<EOF 268#! /bin/sh 269 270echo "Hello Hell" 271exit 0 272EOF 273 274chmod +x $LTPTMP/tst2_cronprg.sh >/dev/null 2>&1 275 276$LTPBIN/tst_resm TINFO "Test #2: installing crontab file." 277 278crontab $LTPTMP/tst2_cronjob.cron >$LTPTMP/cron_tst2n1.out 2>&1 279 280if [ $? -ne 0 ] 281then 282 $LTPBIN/tst_brk TBROK $LTPTMP/cron_tst2n1.out NULL \ 283 "Test #2: crontab Broke while installing cronjob. Reason:" 284 TFAILCNT=$(( $TFAILCNT+1 )) 285fi 286 287sleep 10s 288 289tail -n 10 /var/log/messages | grep crontab | grep REPLACE \ 290 >$LTPTMP/cron_tst2n1.out 2>&1 291RC=$? 292##### 293# Some implementations log cron info to /var/log/cron instead... 294##### 295if [ "$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=$? 300fi 301if [ $RC -ne 0 ] 302then 303 $LTPBIN/tst_resm TFAIL \ 304 "Test #2: crontab activity not recorded in var/log/messages." 305 TFAILCNT=$(( $TFAILCNT+1 )) 306fi 307 308$LTPBIN/tst_resm TINFO "Test #2: uninstalling crontab file." 309 310crontab -r >$LTPTMP/cron_tst2n1.out 2>&1 311RC=$? 312 313if [ $RC -ne 0 ] 314then 315 $LTPBIN/tst_brk TBROK $LTPTMP/cron_tst2n1.out NULL \ 316 "Test #2: crontab Broke while installing cronjob. Reason:" 317 TFAILCNT=$(( $TFAILCNT+1 )) 318else 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 338fi 339 340 341# Test #3 342# Test if crontab -l lists the cronjob installed. 343 344export TCID=cron03 345export TST_COUNT=3 346 347$LTPBIN/tst_resm TINFO "Test #3: crontab -l lists the cronjobs installed" 348 349cat > $LTPTMP/tst2_cronjob.cron <<EOF 350* * * * * $LTPTMP/tst2_cronprg.sh 351EOF 352 353cat > $LTPTMP/tst2_cronprg.sh <<EOF 354#! /bin/sh 355 356echo "Hello Hell" 357exit 0 358EOF 359 360chmod +x $LTPTMP/tst2_cronprg.sh >/dev/null 2>&1 361 362$LTPBIN/tst_resm TINFO "Test #3: installing crontab file ..." 363crontab $LTPTMP/tst2_cronjob.cron >$LTPTMP/cron_tst2n1.out 2>&1 364if [ $? -ne 0 ] 365then 366 $LTPBIN/tst_brkm TBROK NULL \ 367 "Test #3: crontab failed while installing cronjob" 368 TFAILCNT=$(( $TFAILCNT+1 )) 369else 370 $LTPBIN/tst_resm TINFO "Test #3: Cron job installed." 371fi 372 373crontab -l | grep "$LTPTMP/tst2_cronprg.sh" >$LTPTMP/cron_tst2n1.out 2>&1 374RC=$? 375if [ $RC -ne 0 ] 376then 377 $LTPBIN/tst_brkm TBROK NULL \ 378 "Test #3: crontab failed while listing cronjobs installed" 379 TFAILCNT=$(( $TFAILCNT+1 )) 380else 381 $LTPBIN/tst_resm TINFO \ 382 "Test #3: crontab -l listed cronjob tst2_cronprg.sh" 383fi 384 385$LTPBIN/tst_resm TINFO "Test #3: uninstalling crontab file." 386crontab -r >/dev/null 2>&1 387 388if [ $? -ne 0 ] 389then 390 $LTPBIN/tst_brkm TBROK NULL "Test #3: crontab failed while removing cronjob" 391 TFAILCNT=$(( $TFAILCNT+1 )) 392fi 393 394crontab -l >$LTPTMP/cron_tst2.out 2>&1 395if [ $? -ne 0 ] 396then 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 408else 409 $LTPBIN/tst_res TFAIL $LTPTMP/cron_tst2n1.out \ 410 "Test #3: crontab failed removing cronjob. Reason:" 411 TFAILCNT=$(( $TFAILCNT+1 )) 412fi 413 414if [ $SYSLOG_STARTED -eq 1 ]; then 415 stop_daemon $SYSLOG_DAEMON 416fi 417 418exit $TFAILCNT 419