[LTP] [PATCH] Rewrite logrotate case with using `tst_test.sh`

lufei lufei@uniontech.com
Tue Mar 5 03:02:47 CET 2024


1. Rewrite case with using `tst_test.sh`
2. Enable steps in test#2 in old case (`test02` not called in old
   code).

Signed-off-by: 路斐 <lufei@uniontech.com>
---
 .../commands/logrotate/logrotate_tests.sh     | 438 +++++-------------
 1 file changed, 106 insertions(+), 332 deletions(-)

diff --git a/testcases/commands/logrotate/logrotate_tests.sh b/testcases/commands/logrotate/logrotate_tests.sh
index 105237716..fa61b7a69 100755
--- a/testcases/commands/logrotate/logrotate_tests.sh
+++ b/testcases/commands/logrotate/logrotate_tests.sh
@@ -1,27 +1,8 @@
 #!/bin/sh
-################################################################################
-##                                                                            ##
-## Copyright (c) International Business Machines  Corp., 2001                 ##
-##                                                                            ##
-## This program is free software;  you can redistribute it and#or modify      ##
-## it under the terms of the GNU General Public License as published by       ##
-## the Free Software Foundation; either version 2 of the License, or          ##
-## (at your option) any later version.                                        ##
-##                                                                            ##
-## This program is distributed in the hope that it will be useful, but        ##
-## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
-## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
-## for more details.                                                          ##
-##                                                                            ##
-## You should have received a copy of the GNU General Public License          ##
-## along with this program;  if not, write to the Free Software               ##
-## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
-##                                                                            ##
-################################################################################
-#
-# File :        logrotate_tests.sh
-#
-# Description:    Test Basic functionality of logrotate command.
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) International Business Machines  Corp., 2001
+# Author: Manoj Iyer, manjo@mail.utexas.edu
+# Description:   Test Basic functionality of logrotate command.
 #                Test #1: Test that logrotate -f <file.conf> rotates the logfile
 #                as per the specifications in the conf file. Create a file
 #                tst_logfile in /var/log/. Create a conf file such that this
@@ -33,349 +14,142 @@
 #                logrotate <file.conf> every minute. The conf file specifies
 #                that the rotation happen only if the log file exceeds 2k file
 #                size.
-#
-# Author:        Manoj Iyer, manjo@mail.utexas.edu
-#
-# History:       Dec 23 2002 - Created - Manoj Iyer.
-#                Dec 24 2002 - Added   - Test #2 - Test to run logrotate as a
-#                                        cron job.
-#                Feb 28 2003 - Fixed   - Modified testcase to use functions.
-#
-# Function: 	chk_ifexists
-#
-# Description:  - Check if command required for this test exits.
-#
-# Input:        - $1 - calling test case.
-#               - $2 - command that needs to be checked.
-#
-# Return:		- zero on success.
-# 				- non-zero on failure.
-chk_ifexists()
-{
-	RC=0
 
-	which $2 > $LTPTMP/tst_logrotate.err 2>&1 || RC=$?
-	if [ $RC -ne 0 ]
-	then
-		tst_brkm TBROK NULL "$1: command $2 not found."
-	fi
-	return $RC
-}
-
-
-# Function: init
-#
-# Description:  - Check if command required for this test exits.
-#               - Create temporary directories required for this test.
-#               - Initialize global variables.
-#
-# Return:		- zero on success.
-# 				- non-zero on failure.
-init()
-{
-	# Initialize global variables.
-	export RC=0
-	export TST_TOTAL=2
-	export TCID="logrotate"
-	export TST_COUNT=0
-
-	# Inititalize cleanup function.
-	trap "cleanup" 0
 
-	# create the temporary directory used by this testcase
-	if [ -z $TMP ]
-	then
-		LTPTMP=/tmp/tst_logrotate.$$
-	else
-		LTPTMP=$TMP/tst_logrotate.$$
-	fi
-
-	mkdir -p $LTPTMP > /dev/null 2>&1 || RC=$?
-	if [ $RC -ne 0 ]
-	then
-		 tst_brkm TBROK "INIT: Unable to create temporary directory"
-		 return $RC
-	fi
+TST_NEEDS_CMDS="crontab file grep logrotate"
+TST_TESTFUNC=test
+TST_NEEDS_TMPDIR=1
+TST_CNT=2
+TST_CLEANUP=cleanup
 
-	# check if commands tst_*, logrotate, awk and file exists.
-	chk_ifexists INIT tst_resm  || return $RC
-	chk_ifexists INIT logrotate || return $RC
-	chk_ifexists INIT awk       || return $RC
-	chk_ifexists INIT file      || return $RC
-
-	return $RC
+cleanup(){
+	(crontab -l | grep -v tst_largelog) | crontab -
+	rm -rf /var/log/tst_logfile*
+	rm -rf /var/log/tst_largelogfile*
 }
 
+test1(){
+	# Function:     test01
+        #
+        # Description:  - Test that logrotate logrotate will rotate the logfile
+        #                 according to the specifications in the config file.
+        #               - create a config file that will rotate the /var/log/tst_logfile
+        #                 file.
+        #               - use force option to force logrotate to cause the log file to
+        #                 be rotated.
+        #               - compress the file after rotation.
 
-# Function: 	cleanup
-#
-# Description:  - remove temporaty files and directories. Stop all jobs stated
-#                 by this testcase.
-#
-# Return:		- zero on success.
-# 				- non-zero on failure.
-cleanup()
-{
-	#remove all cronjobs that were installed.
-	tst_resm TINFO "CLEAN: removing all cron jobs."
-	crontab -r > /dev/null 2>&1
-
-	# remove all the temporary files created by this test.
-	tst_resm TINFO "CLEAN: removing $LTPTMP"
-	rm -fr $LTPTMP
-}
-
-
-# Function: 	test01
-#
-# Description:  - Test that logrotate logrotate will rotate the logfile
-#                 according to the specifications in the config file.
-#               - create a config file that will rotate the /var/log/tst_logfile
-#                 file.
-#               - use force option to force logrotate to cause the log file to
-#                 be rotated.
-#               - compress the file after rotation.
-#
-# Return:		- zero on success.
-# 				- non-zero on failure.
-test01()
-{
-	count=0
-	files=" "
-	filesize=0
-
-	TCID=logrotate01
-	TST_COUNT=1
-
-	tst_resm TINFO "Test #1: create a configfile $LTPTMP/var_mesg.config"
-	tst_resm TINFO "Test #1: use logrotate -f <config> to force rotation"
-	tst_resm TINFO "Test #1: this will rotate the log file according to"
-	tst_resm TINFO "Test #1: the specification in the configfile."
-	tst_resm TINFO "Test #1: 1. rotate /var/log/tst_logfile file."
-	tst_resm TINFO "Test #1: 2. compresses it."
-
-	# Check if syslog group exists
 	local group="syslog"
 	grep -q $group /etc/group || group="root"
 
-	# create config file.
-	cat >$LTPTMP/tst_logrotate.conf <<-EOF
-	#****** Begin Config file *******
-	# create new (empty) log files after rotating old ones
-	create
+	cat >tst_logrotate.conf <<-EOF
+        #****** Begin Config file *******
+        # create new (empty) log files after rotating old ones
+        create
 
-	# compress the log files
-	compress
+        # compress the log files
+        compress
 
-	/var/log/tst_logfile {
-		su root $group
-		rotate 5
-		weekly
-	}
-	#****** End Config file *******
+        /var/log/tst_logfile {
+                su root $group
+                rotate 5
+                weekly
+        }
+        #****** End Config file *******
 	EOF
 
-	# create a log file in /var/log/
 	cat >/var/log/tst_logfile <<-EOF
-	#****** Begin Log File ********
-	# This is a dummy log file.
-	#****** End Log File ********
+        #****** Begin Log File ********
+        # This is a dummy log file.
+        #****** End Log File ********
 	EOF
 
-	while [ $count -lt 10 ]
-	do
-		echo "This a dummy log file used to test logrotate command." >> \
-			/var/log/tst_logfile
-		 		 count=$(( $count+1 ))
-	done
-
-	# remove all old-n-stale logfiles.
-	for files in /var/log/tst_logfile.*
+        for i in `seq 10`
 	do
-		rm -f $files > /dev/null 2>&1
-	done
-
-	chmod 644 $LTPTMP/tst_logrotate.conf
-	logrotate -fv $LTPTMP/tst_logrotate.conf > $LTPTMP/tst_logrotate.out 2>&1 \
-		|| RC=$?
-	if [ $RC -eq 0 ]
-	then
-		# check if config file $LTPTMP/tst_logrotate.conf is read
-		# check if  /etc/logrotate.d is included/
-		# check if 5 rotations are forced.
-        # check if compression is done.
-		grep "reading config file $LTPTMP/tst_logrotate.conf" \
-			$LTPTMP/tst_logrotate.out   > $LTPTMP/tst_logrotate.err 2>&1 || RC=$?
-		grep "forced from command line (5 rotations)" \
-			$LTPTMP/tst_logrotate.out   > $LTPTMP/tst_logrotate.err 2>&1 || RC=$?
-		egrep "compressing new|log with" \
-			$LTPTMP/tst_logrotate.out   > $LTPTMP/tst_logrotate.err 2>&1 || RC=$?
-		if [ $RC -ne 0 ]
-		then
-			tst_res TFAIL $LTPTMP/tst_logrotate.err \
-				"Test #1: logrotate command failed. Reason:"
-		else
-			# Check if compressed log file is created.
-			if [ -f /var/log/tst_logfile.1.gz ]
-			then
-				file /var/log/tst_logfile.1.gz | grep "gzip compressed data" \
-					> $LTPTMP/tst_logrotate.out 2>&1 || RC=$?
-				if [ $RC -eq 0 ]
-				then
-					tst_resm TPASS \
-						"Test #1: logrotate created a compressed file."
-				else
-					tst_res TFAIL $LTPTMP/tst_logrotate.out \
-						"Test #1: Failed to create a compressed file. Reason:"
-				fi
-				return $RC
-			else
-				 tst_res TFAIL  $LTPTMP/tst_logrotate.out \
-				  "Test #1: Failed create /var/log/tst_logfile.1.gz. Reason:"
-				return $RC
-			fi
-		fi
-	else
-		tst_res TFAIL $LTPTMP/tst_logrotate.out \
-		"Test #1: logrotate command exited with $RC return code. Output:"
-	fi
-	return $RC
-}
-
+                echo "This a dummy log file used to test logrotate command." >>\
+                        /var/log/tst_logfile
+        done
 
-test02()
-{
-# Test #2
-# Test that logrotate logrotate will rotate the logfile if the logfile
-# exceeds a certain size.
-#     - create a config file that will rotate the /var/log/tst_largelogfile.
-#     - run logrotate in a cron job that runs every minute.
-#     - add messages to the logfile until it gets rotated when a re-dittermined
-#        size is reached.
+	ROD rm -f /var/log/tst_logfile.*
+	ROD chmod 644 tst_logrotate.conf
+	ROD logrotate -fv tst_logrotate.conf > tst_logrotate.out 2>&1
 
-export TCID=logrotate02
-export TST_COUNT=2
-RC=0
+	EXPECT_PASS grep -q "reading config file tst_logrotate.conf" \
+		tst_logrotate.out
 
-tst_resm TINFO "Test #2: create a configfile $LTPTMP/tst_largelog.conf"
-tst_resm TINFO "Test #2: logrotate $LTPTMP/tst_largelog.conf - cronjob"
-tst_resm TINFO "Test #2: set to rotate tst_largelogfile when size > 2K"
+	EXPECT_PASS grep -q "forced from command line (5 rotations)" \
+		tst_logrotate.out
 
+	EXPECT_PASS grep -E -q "compressing new|log with" tst_logrotate.out
 
-# create config file.
-cat >$LTPTMP/tst_largelog.conf <<EOF
-# create new (empty) log files after rotating old ones
-create
+	ROD test -f /var/log/tst_logfile.1.gz
 
-# compress the log files
-compress
-
-# RPM packages drop log rotation information into this directory
-include /etc/logrotate.d
-
-/var/log/tst_largelogfile {
-    rotate 5
-    size=2k
+	file /var/log/tst_logfile.1.gz | grep -q "gzip compressed data"
+	if [ $? -ne 0 ]; then
+		tst_res TFAIL "Test #1: Failed to create a compressed file."
+	fi
 }
-EOF
-
-# create the pseudo-log file.
-cat >/var/log/tst_largelogfile <<EOF
-# This is a psuedo-log file. This file will grow to a 2k size before
-# getting rotated.
-EOF
-
-# create logrotate cron job.
-cat >$LTPTMP/tst_logrotate.cron <<EOF
-* * * * * logrotate $LTPTMP/tst_largelog.conf
-EOF
 
-chmod 777 $LTPTMP/tst_logrotate.cron > /dev/null 2>&1
+test2(){
+        # Test #2
+        # Test that logrotate logrotate will rotate the logfile if the logfile
+        # exceeds a certain size.
+        #     - create a config file that will rotate the /var/log/tst_largelogfile.
+        #     - run logrotate in a cron job that runs every minute.
+        #     - add messages to the logfile until it gets rotated when a re-dittermined
+        #        size is reached.
+
+	cat >tst_largelog.conf <<-EOF
+        # create new (empty) log files after rotating old ones
+        create
+        # compress the log files
+        compress
+        # RPM packages drop log rotation information into this directory
+        include /etc/logrotate.d 
+        /var/log/tst_largelogfile {
+            rotate 5
+            size=2k
+        }
+	EOF
 
-tst_resm TINFO "Test #2: Installing cron job to run logrotate"
-crontab $LTPTMP/tst_logrotate.cron > $LTPTMP/tst_logrotate.out 2>&1 || RC=$?
-if [ $RC -ne 0 ]
-then
-    echo "Exit status of crontab command: $RC" >> tst_logrotate.out 2>/dev/null
-    tst_brk TBROK $LTPTMP/tst_logrotate.out NULL \
-        "Test #2: crontab Broke while installing cronjob. Reason:"
-    TFAILCNT=$(( $TFAILCN+1 ))
-else
-    tst_resm TINFO "Test #2: Cronjob installed successfully"
-fi
+	ROD chmod 644 tst_largelog.conf
 
-# cron job to increase the log file size.
-cat >$LTPTMP/tst_addtolog.cron <<EOF
+	cat >/var/log/tst_largelogfile <<-EOF
+        # This is a psuedo-log file. This file will grow to a 2k size before
+        # getting rotated.
+	EOF
 
-* * * * * echo "To Err Is Human, To Really Screw Up You Need A Computer."  >>/var/log/tst_largelogfile 2>/dev/null
-EOF
+	local logcontent="To Err Is Human, To Really Screw Up You Need A Computer."
+	for i in `seq 40`
+	do
+		echo "$logcontent" >> /var/log/tst_largelogfile
+	done
 
-tst_resm TINFO "Test #2: Installing cron job to increase logsize"
-crontab $LTPTMP/tst_addtolog.cron > $LTPTMP/tst_logrotate.out 2>&1 || RC=$?
-if [ $RC -ne 0 ]
-then
-    echo "Exit status of crontab command: $RC" >> tst_logrotate.out 2>/dev/null
-    tst_brk TBROK $LTPTMP/tst_logrotate.out NULL \
-        "Test #2: crontab Broke while installing cronjob. Reason:"
-    TFAILCNT=$(( $TFAILCN+1 ))
-else
-    tst_resm TINFO "Test #2: Cronjob installed successfully"
-fi
+	local fullcmd=`command -v logrotate`
 
-# let cron jobs get started.
-sleep 10s
+	# cron job for logrotating
+	(crontab -l 2>/dev/null; echo \
+		"* * * * * ${fullcmd} $(pwd)/tst_largelog.conf") | crontab -
+	if [ $? -ne 0 ]; then
+		tst_brk TBROK "create cron job failed."
+	fi
 
-# increase the log file size.
+	# let cron jobs get started.
+	tst_sleep 5s
 
-# wait for the /var/log/tst_largelogfile to be filled to a size greater than 2k
-tst_resm TINFO "Test #2: Checking if file size is > 2k"
-tst_resm TINFO "Test #2: Pls be patient this will take some time."
-tst_resm TINFO "Test #2: or killall -9 logrotate02 to skip.."
-if [ -f `which awk` ]
-then
-    while [ $filesize -lt 2046 ]
-    do
-        filesize=`ls -l /var/log/tst_largelogfile | awk '{print $5}'`
-    done
-	# wait for 1m  and check if logrotate has rotated the logfile. The cron job
-	# that does a logrotate runs every 1 minute so give the cron a minute...
-	sleep 1m
-else
-	tst_resm TINFO "Test #2: No AWK installed ... sleeping for 10mts"
-	sleep 10m
-fi
+	# wait for 1m and check if logrotate has rotated the logfile. The cron
+	# job that does a logrotate runs every 1 minute so give the cron a 
+	# minute...
+	tst_sleep 60s
 
+	EXPECT_PASS test -f /var/log/tst_largelogfile.1.gz
 
-if [ -f /var/log/tst_largelogfile.1.gz ]
-then
-    file /var/log/tst_largelogfile.1.gz | grep "gzip compressed data" \
-        > $LTPTMP/tst_logrotate.out 2>&1 || RC=$?
-    if [ $RC -eq 0 ]
-    then
-        tst_resm TPASS \
-            "Test #1: logrotate worked as cron, created a compressed file."
-    else
-        tst_res TFAIL $LTPTMP/tst_logrotate.out \
-            "Test #1: Failed to create a compressed file. Reason:"
-    fi
-else
-    tst_res TFAIL  $LTPTMP/tst_logrotate.out \
-        "Test #1: Failed to create /var/log/tst_largelogfile.1.gz. Reason:"
-    TFAILCNT=$(( $TFAILCNT+1 ))
-fi
+	file /var/log/tst_largelogfile.1.gz | grep -q "gzip compressed data"
+	if [ $? -ne 0 ]; then
+		tst_res TFAIL "Failed to create a compressed file."
+	fi
 
 }
 
-# Function:	main
-#
-# Description:	- Execute all tests and report results.
-#
-# Exit:			- zero on success
-#               - non-zero on failure.
-
-RC=0
-init || exit $?
-
-test01 || RC=$?
-
-exit $RC
+. tst_test.sh
+tst_run
-- 
2.39.3



More information about the ltp mailing list