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