logrotate_tests.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #!/bin/sh
  2. ################################################################################
  3. ## ##
  4. ## Copyright (c) International Business Machines Corp., 2001 ##
  5. ## ##
  6. ## This program is free software; you can redistribute it and#or modify ##
  7. ## it under the terms of the GNU General Public License as published by ##
  8. ## the Free Software Foundation; either version 2 of the License, or ##
  9. ## (at your option) any later version. ##
  10. ## ##
  11. ## This program is distributed in the hope that it will be useful, but ##
  12. ## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
  13. ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ##
  14. ## for more details. ##
  15. ## ##
  16. ## You should have received a copy of the GNU General Public License ##
  17. ## along with this program; if not, write to the Free Software ##
  18. ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ##
  19. ## ##
  20. ################################################################################
  21. #
  22. # File : logrotate_tests.sh
  23. #
  24. # Description: Test Basic functionality of logrotate command.
  25. # Test #1: Test that logrotate -f <file.conf> rotates the logfile
  26. # as per the specifications in the conf file. Create a file
  27. # tst_logfile in /var/log/. Create a conf file such that this
  28. # logfile is set for rotation every week. Execute the command
  29. # logrotate -f <file.conf>, check to see if it forced rotation.
  30. # Test #2: Check if logrotate running as a cronjob will rotate a
  31. # logfile when it exceeds a specific size. Create two cronjobs
  32. # 1. runs a command to log a string to a logfile. 2. runs
  33. # logrotate <file.conf> every minute. The conf file specifies
  34. # that the rotation happen only if the log file exceeds 2k file
  35. # size.
  36. #
  37. # Author: Manoj Iyer, manjo@mail.utexas.edu
  38. #
  39. # History: Dec 23 2002 - Created - Manoj Iyer.
  40. # Dec 24 2002 - Added - Test #2 - Test to run logrotate as a
  41. # cron job.
  42. # Feb 28 2003 - Fixed - Modified testcase to use functions.
  43. #
  44. # Function: chk_ifexists
  45. #
  46. # Description: - Check if command required for this test exits.
  47. #
  48. # Input: - $1 - calling test case.
  49. # - $2 - command that needs to be checked.
  50. #
  51. # Return: - zero on success.
  52. # - non-zero on failure.
  53. chk_ifexists()
  54. {
  55. RC=0
  56. which $2 > $LTPTMP/tst_logrotate.err 2>&1 || RC=$?
  57. if [ $RC -ne 0 ]
  58. then
  59. tst_brkm TBROK NULL "$1: command $2 not found."
  60. fi
  61. return $RC
  62. }
  63. # Function: init
  64. #
  65. # Description: - Check if command required for this test exits.
  66. # - Create temporary directories required for this test.
  67. # - Initialize global variables.
  68. #
  69. # Return: - zero on success.
  70. # - non-zero on failure.
  71. init()
  72. {
  73. # Initialize global variables.
  74. export RC=0
  75. export TST_TOTAL=2
  76. export TCID="logrotate"
  77. export TST_COUNT=0
  78. # Inititalize cleanup function.
  79. trap "cleanup" 0
  80. # create the temporary directory used by this testcase
  81. if [ -z $TMP ]
  82. then
  83. LTPTMP=/tmp/tst_logrotate.$$
  84. else
  85. LTPTMP=$TMP/tst_logrotate.$$
  86. fi
  87. mkdir -p $LTPTMP > /dev/null 2>&1 || RC=$?
  88. if [ $RC -ne 0 ]
  89. then
  90. tst_brkm TBROK "INIT: Unable to create temporary directory"
  91. return $RC
  92. fi
  93. # check if commands tst_*, logrotate, awk and file exists.
  94. chk_ifexists INIT tst_resm || return $RC
  95. chk_ifexists INIT logrotate || return $RC
  96. chk_ifexists INIT awk || return $RC
  97. chk_ifexists INIT file || return $RC
  98. return $RC
  99. }
  100. # Function: cleanup
  101. #
  102. # Description: - remove temporaty files and directories. Stop all jobs stated
  103. # by this testcase.
  104. #
  105. # Return: - zero on success.
  106. # - non-zero on failure.
  107. cleanup()
  108. {
  109. #remove all cronjobs that were installed.
  110. tst_resm TINFO "CLEAN: removing all cron jobs."
  111. crontab -r > /dev/null 2>&1
  112. # remove all the temporary files created by this test.
  113. tst_resm TINFO "CLEAN: removing $LTPTMP"
  114. rm -fr $LTPTMP
  115. }
  116. # Function: test01
  117. #
  118. # Description: - Test that logrotate logrotate will rotate the logfile
  119. # according to the specifications in the config file.
  120. # - create a config file that will rotate the /var/log/tst_logfile
  121. # file.
  122. # - use force option to force logrotate to cause the log file to
  123. # be rotated.
  124. # - compress the file after rotation.
  125. #
  126. # Return: - zero on success.
  127. # - non-zero on failure.
  128. test01()
  129. {
  130. count=0
  131. files=" "
  132. filesize=0
  133. TCID=logrotate01
  134. TST_COUNT=1
  135. tst_resm TINFO "Test #1: create a configfile $LTPTMP/var_mesg.config"
  136. tst_resm TINFO "Test #1: use logrotate -f <config> to force rotation"
  137. tst_resm TINFO "Test #1: this will rotate the log file according to"
  138. tst_resm TINFO "Test #1: the specification in the configfile."
  139. tst_resm TINFO "Test #1: 1. rotate /var/log/tst_logfile file."
  140. tst_resm TINFO "Test #1: 2. compresses it."
  141. # Check if syslog group exists
  142. local group="syslog"
  143. grep -q $group /etc/group || group="root"
  144. # create config file.
  145. cat >$LTPTMP/tst_logrotate.conf <<-EOF
  146. #****** Begin Config file *******
  147. # create new (empty) log files after rotating old ones
  148. create
  149. # compress the log files
  150. compress
  151. /var/log/tst_logfile {
  152. su root $group
  153. rotate 5
  154. weekly
  155. }
  156. #****** End Config file *******
  157. EOF
  158. # create a log file in /var/log/
  159. cat >/var/log/tst_logfile <<-EOF
  160. #****** Begin Log File ********
  161. # This is a dummy log file.
  162. #****** End Log File ********
  163. EOF
  164. while [ $count -lt 10 ]
  165. do
  166. echo "This a dummy log file used to test logrotate command." >> \
  167. /var/log/tst_logfile
  168. count=$(( $count+1 ))
  169. done
  170. # remove all old-n-stale logfiles.
  171. for files in /var/log/tst_logfile.*
  172. do
  173. rm -f $files > /dev/null 2>&1
  174. done
  175. chmod 644 $LTPTMP/tst_logrotate.conf
  176. logrotate -fv $LTPTMP/tst_logrotate.conf > $LTPTMP/tst_logrotate.out 2>&1 \
  177. || RC=$?
  178. if [ $RC -eq 0 ]
  179. then
  180. # check if config file $LTPTMP/tst_logrotate.conf is read
  181. # check if /etc/logrotate.d is included/
  182. # check if 5 rotations are forced.
  183. # check if compression is done.
  184. grep "reading config file $LTPTMP/tst_logrotate.conf" \
  185. $LTPTMP/tst_logrotate.out > $LTPTMP/tst_logrotate.err 2>&1 || RC=$?
  186. grep "forced from command line (5 rotations)" \
  187. $LTPTMP/tst_logrotate.out > $LTPTMP/tst_logrotate.err 2>&1 || RC=$?
  188. egrep "compressing new|log with" \
  189. $LTPTMP/tst_logrotate.out > $LTPTMP/tst_logrotate.err 2>&1 || RC=$?
  190. if [ $RC -ne 0 ]
  191. then
  192. tst_res TFAIL $LTPTMP/tst_logrotate.err \
  193. "Test #1: logrotate command failed. Reason:"
  194. else
  195. # Check if compressed log file is created.
  196. if [ -f /var/log/tst_logfile.1.gz ]
  197. then
  198. file /var/log/tst_logfile.1.gz | grep "gzip compressed data" \
  199. > $LTPTMP/tst_logrotate.out 2>&1 || RC=$?
  200. if [ $RC -eq 0 ]
  201. then
  202. tst_resm TPASS \
  203. "Test #1: logrotate created a compressed file."
  204. else
  205. tst_res TFAIL $LTPTMP/tst_logrotate.out \
  206. "Test #1: Failed to create a compressed file. Reason:"
  207. fi
  208. return $RC
  209. else
  210. tst_res TFAIL $LTPTMP/tst_logrotate.out \
  211. "Test #1: Failed create /var/log/tst_logfile.1.gz. Reason:"
  212. return $RC
  213. fi
  214. fi
  215. else
  216. tst_res TFAIL $LTPTMP/tst_logrotate.out \
  217. "Test #1: logrotate command exited with $RC return code. Output:"
  218. fi
  219. return $RC
  220. }
  221. test02()
  222. {
  223. # Test #2
  224. # Test that logrotate logrotate will rotate the logfile if the logfile
  225. # exceeds a certain size.
  226. # - create a config file that will rotate the /var/log/tst_largelogfile.
  227. # - run logrotate in a cron job that runs every minute.
  228. # - add messages to the logfile until it gets rotated when a re-dittermined
  229. # size is reached.
  230. export TCID=logrotate02
  231. export TST_COUNT=2
  232. RC=0
  233. tst_resm TINFO "Test #2: create a configfile $LTPTMP/tst_largelog.conf"
  234. tst_resm TINFO "Test #2: logrotate $LTPTMP/tst_largelog.conf - cronjob"
  235. tst_resm TINFO "Test #2: set to rotate tst_largelogfile when size > 2K"
  236. # create config file.
  237. cat >$LTPTMP/tst_largelog.conf <<EOF
  238. # create new (empty) log files after rotating old ones
  239. create
  240. # compress the log files
  241. compress
  242. # RPM packages drop log rotation information into this directory
  243. include /etc/logrotate.d
  244. /var/log/tst_largelogfile {
  245. rotate 5
  246. size=2k
  247. }
  248. EOF
  249. # create the pseudo-log file.
  250. cat >/var/log/tst_largelogfile <<EOF
  251. # This is a psuedo-log file. This file will grow to a 2k size before
  252. # getting rotated.
  253. EOF
  254. # create logrotate cron job.
  255. cat >$LTPTMP/tst_logrotate.cron <<EOF
  256. * * * * * logrotate $LTPTMP/tst_largelog.conf
  257. EOF
  258. chmod 777 $LTPTMP/tst_logrotate.cron > /dev/null 2>&1
  259. tst_resm TINFO "Test #2: Installing cron job to run logrotate"
  260. crontab $LTPTMP/tst_logrotate.cron > $LTPTMP/tst_logrotate.out 2>&1 || RC=$?
  261. if [ $RC -ne 0 ]
  262. then
  263. echo "Exit status of crontab command: $RC" >> tst_logrotate.out 2>/dev/null
  264. tst_brk TBROK $LTPTMP/tst_logrotate.out NULL \
  265. "Test #2: crontab Broke while installing cronjob. Reason:"
  266. TFAILCNT=$(( $TFAILCN+1 ))
  267. else
  268. tst_resm TINFO "Test #2: Cronjob installed successfully"
  269. fi
  270. # cron job to increase the log file size.
  271. cat >$LTPTMP/tst_addtolog.cron <<EOF
  272. * * * * * echo "To Err Is Human, To Really Screw Up You Need A Computer." >>/var/log/tst_largelogfile 2>/dev/null
  273. EOF
  274. tst_resm TINFO "Test #2: Installing cron job to increase logsize"
  275. crontab $LTPTMP/tst_addtolog.cron > $LTPTMP/tst_logrotate.out 2>&1 || RC=$?
  276. if [ $RC -ne 0 ]
  277. then
  278. echo "Exit status of crontab command: $RC" >> tst_logrotate.out 2>/dev/null
  279. tst_brk TBROK $LTPTMP/tst_logrotate.out NULL \
  280. "Test #2: crontab Broke while installing cronjob. Reason:"
  281. TFAILCNT=$(( $TFAILCN+1 ))
  282. else
  283. tst_resm TINFO "Test #2: Cronjob installed successfully"
  284. fi
  285. # let cron jobs get started.
  286. sleep 10s
  287. # increase the log file size.
  288. # wait for the /var/log/tst_largelogfile to be filled to a size greater than 2k
  289. tst_resm TINFO "Test #2: Checking if file size is > 2k"
  290. tst_resm TINFO "Test #2: Pls be patient this will take some time."
  291. tst_resm TINFO "Test #2: or killall -9 logrotate02 to skip.."
  292. if [ -f `which awk` ]
  293. then
  294. while [ $filesize -lt 2046 ]
  295. do
  296. filesize=`ls -l /var/log/tst_largelogfile | awk '{print $5}'`
  297. done
  298. # wait for 1m and check if logrotate has rotated the logfile. The cron job
  299. # that does a logrotate runs every 1 minute so give the cron a minute...
  300. sleep 1m
  301. else
  302. tst_resm TINFO "Test #2: No AWK installed ... sleeping for 10mts"
  303. sleep 10m
  304. fi
  305. if [ -f /var/log/tst_largelogfile.1.gz ]
  306. then
  307. file /var/log/tst_largelogfile.1.gz | grep "gzip compressed data" \
  308. > $LTPTMP/tst_logrotate.out 2>&1 || RC=$?
  309. if [ $RC -eq 0 ]
  310. then
  311. tst_resm TPASS \
  312. "Test #1: logrotate worked as cron, created a compressed file."
  313. else
  314. tst_res TFAIL $LTPTMP/tst_logrotate.out \
  315. "Test #1: Failed to create a compressed file. Reason:"
  316. fi
  317. else
  318. tst_res TFAIL $LTPTMP/tst_logrotate.out \
  319. "Test #1: Failed to create /var/log/tst_largelogfile.1.gz. Reason:"
  320. TFAILCNT=$(( $TFAILCNT+1 ))
  321. fi
  322. }
  323. # Function: main
  324. #
  325. # Description: - Execute all tests and report results.
  326. #
  327. # Exit: - zero on success
  328. # - non-zero on failure.
  329. RC=0
  330. init || exit $?
  331. test01 || RC=$?
  332. exit $RC