1#!/bin/sh 2# SPDX-License-Identifier: GPL-2.0-or-later 3# Copyright (c) International Business Machines Corp., 2001 4# Copyright (c) Linux Test Project, 2002-2024 5# Author: Manoj Iyer <[email protected]> 6# 7# Test Basic functionality of logrotate command. 8# 9# Test #1 10# Test that logrotate logrotate will rotate the logfile according to the 11# specifications in the config file. 12# - Create a config file that will rotate the /var/log/tst_logfile file. 13# - Use force option to force logrotate to cause the log file to be rotated. 14# - Compress the file after rotation. 15# 16# Test #2 17# Test that logrotate logrotate will rotate the logfile if the logfile 18# exceeds a certain size. 19# - Create a config file that will rotate the /var/log/tst_largelogfile. 20# - Run logrotate in a cron job that runs every minute. 21# - Add messages to the logfile until it gets rotated when a re-dittermined size 22# is reached. 23 24TST_NEEDS_CMDS="crontab file grep logrotate" 25TST_TESTFUNC=test 26TST_NEEDS_TMPDIR=1 27TST_CNT=2 28TST_CLEANUP=cleanup 29 30cleanup() 31{ 32 (crontab -l | grep -v tst_largelog) | crontab - 33 rm -rf /var/log/tst_logfile* 34 rm -rf /var/log/tst_largelogfile* 35} 36 37check_log() 38{ 39 local file="$1" 40 41 EXPECT_PASS [ -f "$file" ] 42 43 if ! file "$file" | grep -q "gzip compressed data"; then 44 tst_res TFAIL "Failed to create a compressed file" 45 fi 46} 47 48test1() 49{ 50 local group="syslog" 51 52 grep -q $group /etc/group || group="root" 53 54 cat >tst_logrotate.conf <<-EOF 55 #****** Begin Config file ******* 56 # create new (empty) log files after rotating old ones 57 create 58 59 # compress the log files 60 compress 61 62 /var/log/tst_logfile { 63 su root $group 64 rotate 5 65 weekly 66 } 67 #****** End Config file ******* 68 EOF 69 70 cat >/var/log/tst_logfile <<-EOF 71 #****** Begin Log File ******** 72 # This is a dummy log file. 73 #****** End Log File ******** 74 EOF 75 76 for i in $(seq 10); do 77 echo "This a dummy log file used to test logrotate command." >> /var/log/tst_logfile 78 done 79 80 ROD rm -f /var/log/tst_logfile.* 81 ROD chmod 644 tst_logrotate.conf 82 ROD logrotate -fv tst_logrotate.conf > tst_logrotate.out 2>&1 83 84 EXPECT_PASS grep -q "reading config file tst_logrotate.conf" tst_logrotate.out 85 EXPECT_PASS grep -q "forced from command line (5 rotations)" tst_logrotate.out 86 EXPECT_PASS grep -E -q "compressing new|log with" tst_logrotate.out 87 88 check_log /var/log/tst_logfile.1.gz 89} 90 91test2() 92{ 93 cat >tst_largelog.conf <<-EOF 94 # create new (empty) log files after rotating old ones 95 create 96 # compress the log files 97 compress 98 # RPM packages drop log rotation information into this directory 99 include /etc/logrotate.d 100 /var/log/tst_largelogfile { 101 rotate 5 102 size=2k 103 } 104 EOF 105 106 ROD chmod 644 tst_largelog.conf 107 108 cat >/var/log/tst_largelogfile <<-EOF 109 # This is a psuedo-log file. This file will grow to a 2k size before 110 # getting rotated. 111 EOF 112 113 for i in $(seq 75); do 114 echo "Some text for testing rotation" >> /var/log/tst_largelogfile 115 done 116 117 # cron job for logrotating 118 (crontab -l 2>/dev/null; echo \ 119 "* * * * * $(command -v logrotate) $(pwd)/tst_largelog.conf") | crontab - 120 if [ $? -ne 0 ]; then 121 tst_brk TBROK "Failed to create a cron job" 122 fi 123 124 # 5 sec for cron job to start, 1 min for logrotate to rotate the logs 125 tst_res TINFO "sleep 1 min to wait for rotating logs" 126 tst_sleep 65s 127 128 check_log /var/log/tst_largelogfile.1.gz 129} 130 131. tst_test.sh 132tst_run 133