1210373Ssimon#!/bin/sh 2210373Ssimon 3210373Ssimon# $FreeBSD: releng/10.3/usr.sbin/newsyslog/tests/legacy_test.sh 263226 2014-03-16 04:09:22Z jmmv $ 4210373Ssimon 5210373SsimonCOUNT=0 6263226SjmmvTMPDIR=$(pwd)/work 7210373Ssimonif [ $? -ne 0 ]; then 8210373Ssimon echo "$0: Can't create temp dir, exiting..." 9210373Ssimon exit 1 10210373Ssimonfi 11210373Ssimon 12210373Ssimon# Begin an individual test 13210373Ssimonbegin() 14210373Ssimon{ 15210373Ssimon COUNT=`expr $COUNT + 1` 16210373Ssimon OK=1 17210373Ssimon NAME="$1" 18210373Ssimon} 19210373Ssimon 20210373Ssimon# End an individual test 21210373Ssimonend() 22210373Ssimon{ 23210373Ssimon if [ $OK = 1 ] 24210373Ssimon then 25210373Ssimon printf 'ok ' 26210373Ssimon else 27210373Ssimon printf 'not ok ' 28210373Ssimon fi 29210373Ssimon echo "$COUNT - $NAME" 30210373Ssimon} 31210373Ssimon 32210373Ssimon# Make a file that can later be verified 33210373Ssimonmkf() 34210373Ssimon{ 35210373Ssimon CN=`basename $1` 36210373Ssimon echo "$CN-$CN" >$1 37210373Ssimon} 38210373Ssimon 39210373Ssimon# Verify that the file specified is correct 40210373Ssimonckf() 41210373Ssimon{ 42210373Ssimon if [ -f $2 ] && echo "$1-$1" | diff - $2 >/dev/null 43210373Ssimon then 44210373Ssimon ok 45210373Ssimon else 46210373Ssimon notok 47210373Ssimon fi 48210373Ssimon} 49210373Ssimon 50210373Ssimon# Check that a file exists 51210373Ssimonckfe() 52210373Ssimon{ 53210373Ssimon if [ -f $1 ] 54210373Ssimon then 55210373Ssimon ok 56210373Ssimon else 57210373Ssimon notok 58210373Ssimon fi 59210373Ssimon} 60210373Ssimon 61210373Ssimon# Verify that the specified file does not exist 62210373Ssimon# (is not there) 63210373Ssimoncknt() 64210373Ssimon{ 65210373Ssimon if [ -r $1 ] 66210373Ssimon then 67210373Ssimon notok 68210373Ssimon else 69210373Ssimon ok 70210373Ssimon fi 71210373Ssimon} 72210373Ssimon 73228975Suqs# Check if a file is there, depending of if it's supposed to or not - 74228975Suqs# basically how many log files we are supposed to keep vs. how many we 75220927Ssimon# actually keep. 76220927Ssimonckntfe() 77220927Ssimon{ 78220927Ssimon curcnt=$1 79220927Ssimon keepcnt=$2 80220927Ssimon f=$3 81220927Ssimon 82220927Ssimon if [ $curcnt -le $keepcnt ] 83220927Ssimon then 84220927Ssimon #echo Assuming file there 85220927Ssimon ckfe $f 86220927Ssimon else 87220927Ssimon #echo Assuming file NOT there 88220927Ssimon cknt $f 89220927Ssimon fi 90220927Ssimon} 91220927Ssimon 92220927Ssimon 93220927Ssimon 94210373Ssimon# A part of a test succeeds 95210373Ssimonok() 96210373Ssimon{ 97210373Ssimon : 98210373Ssimon} 99210373Ssimon 100210373Ssimon# A part of a test fails 101210373Ssimonnotok() 102210373Ssimon{ 103210373Ssimon OK=0 104210373Ssimon} 105210373Ssimon 106210373Ssimon# Verify that the exit code passed is for unsuccessful termination 107210373Ssimonckfail() 108210373Ssimon{ 109210373Ssimon if [ $1 -gt 0 ] 110210373Ssimon then 111210373Ssimon ok 112210373Ssimon else 113210373Ssimon notok 114210373Ssimon fi 115210373Ssimon} 116210373Ssimon 117210373Ssimon# Verify that the exit code passed is for successful termination 118210373Ssimonckok() 119210373Ssimon{ 120210373Ssimon if [ $1 -eq 0 ] 121210373Ssimon then 122210373Ssimon ok 123210373Ssimon else 124210373Ssimon notok 125210373Ssimon fi 126210373Ssimon} 127210373Ssimon 128210373Ssimon# Check that there are X files which match expr 129210373Ssimonchkfcnt() 130210373Ssimon{ 131210373Ssimon cnt=$1; shift 132210373Ssimon if [ $cnt -eq `echo "$@" | wc -w` ] 133210373Ssimon then 134210373Ssimon ok 135210373Ssimon else 136210373Ssimon notok 137210373Ssimon fi 138210373Ssimon} 139210373Ssimon 140210373Ssimon# Check that two strings are alike 141210373Ssimonckstr() 142210373Ssimon{ 143210373Ssimon if [ "$1" = "$2" ] 144210373Ssimon then 145210373Ssimon ok 146210373Ssimon else 147210373Ssimon notok 148210373Ssimon fi 149210373Ssimon} 150210373Ssimon 151210373Ssimontmpdir_create() 152210373Ssimon{ 153210373Ssimon mkdir -p ${TMPDIR}/log ${TMPDIR}/alog 154210373Ssimon cd ${TMPDIR}/log 155210373Ssimon} 156210373Ssimon 157210373Ssimontmpdir_clean() 158210373Ssimon{ 159210373Ssimon cd ${TMPDIR} 160210373Ssimon rm -rf "${TMPDIR}/log" "${TMPDIR}/alog" newsyslog.conf 161210373Ssimon} 162210373Ssimon 163210373Ssimonrun_newsyslog() 164210373Ssimon{ 165210373Ssimon 166210373Ssimon newsyslog -f ../newsyslog.conf -F -r "$@" 167210373Ssimon} 168210373Ssimon 169210373Ssimontests_normal_rotate() { 170210373Ssimon ext="$1" 171210373Ssimon dir="$2" 172210373Ssimon 173210373Ssimon if [ -n "$dir" ]; then 174210373Ssimon newsyslog_args=" -a ${dir}" 175210373Ssimon name_postfix="${ext} archive dir" 176210373Ssimon else 177210373Ssimon newsyslog_args="" 178210373Ssimon name_postfix="${ext}" 179210373Ssimon fi 180210373Ssimon 181210373Ssimon tmpdir_create 182210373Ssimon 183210373Ssimon begin "create file ${name_postfix}" -newdir 184210373Ssimon run_newsyslog -C 185210373Ssimon ckfe $LOGFNAME 186210373Ssimon cknt ${dir}${LOGFNAME}.0${ext} 187210373Ssimon end 188210373Ssimon 189210373Ssimon begin "rotate normal 1 ${name_postfix}" 190210373Ssimon run_newsyslog $newsyslog_args 191210373Ssimon ckfe ${LOGFNAME} 192210373Ssimon ckfe ${dir}${LOGFNAME}.0${ext} 193210373Ssimon cknt ${dir}${LOGFNAME}.1${ext} 194210373Ssimon end 195210373Ssimon 196210373Ssimon begin "rotate normal 2 ${name_postfix}" 197210373Ssimon run_newsyslog $newsyslog_args 198210373Ssimon ckfe ${LOGFNAME} 199210373Ssimon ckfe ${dir}${LOGFNAME}.0${ext} 200210373Ssimon ckfe ${dir}${LOGFNAME}.1${ext} 201210373Ssimon cknt ${dir}${LOGFNAME}.2${ext} 202210373Ssimon end 203210373Ssimon 204210373Ssimon begin "rotate normal 3 ${name_postfix}" 205210373Ssimon run_newsyslog $newsyslog_args 206210373Ssimon ckfe ${LOGFNAME} 207210373Ssimon ckfe ${dir}${LOGFNAME}.0${ext} 208210373Ssimon ckfe ${dir}${LOGFNAME}.1${ext} 209210373Ssimon ckfe ${dir}${LOGFNAME}.2${ext} 210210373Ssimon cknt ${dir}${LOGFNAME}.3${ext} 211210373Ssimon end 212210373Ssimon 213210373Ssimon begin "rotate normal 4 ${name_postfix}" 214210373Ssimon run_newsyslog $newsyslog_args 215210373Ssimon ckfe ${LOGFNAME} 216210373Ssimon ckfe ${dir}${LOGFNAME}.0${ext} 217210373Ssimon ckfe ${dir}${LOGFNAME}.1${ext} 218210373Ssimon ckfe ${dir}${LOGFNAME}.2${ext} 219210373Ssimon cknt ${dir}${LOGFNAME}.4${ext} 220210373Ssimon end 221210373Ssimon 222210373Ssimon begin "rotate normal 5 ${name_postfix}" 223210373Ssimon run_newsyslog $newsyslog_args 224210373Ssimon ckfe ${LOGFNAME} 225210373Ssimon ckfe ${dir}${LOGFNAME}.0${ext} 226210373Ssimon ckfe ${dir}${LOGFNAME}.1${ext} 227210373Ssimon ckfe ${dir}${LOGFNAME}.2${ext} 228210373Ssimon cknt ${dir}${LOGFNAME}.4${ext} 229210373Ssimon end 230210373Ssimon 231210373Ssimon # Wait a bit so we can see if the noaction test rotates files 232210373Ssimon sleep 1.1 233210373Ssimon 234210373Ssimon begin "noaction ${name_postfix}" 235210373Ssimon ofiles=`ls -Tl ${dir}${LOGFNAME}.*${ext} | tr -d '\n'` 236210373Ssimon run_newsyslog ${newsyslog_args} -n >/dev/null 237210373Ssimon ckfe ${LOGFNAME} 238210373Ssimon ckstr "$ofiles" "`ls -lT ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`" 239210373Ssimon end 240210373Ssimon 241210373Ssimon tmpdir_clean 242210373Ssimon} 243210373Ssimon 244220927Ssimontests_normal_rotate_keepn() { 245220927Ssimon cnt="$1" 246220927Ssimon ext="$2" 247220927Ssimon dir="$3" 248220927Ssimon 249220927Ssimon if [ -n "$dir" ]; then 250220927Ssimon newsyslog_args=" -a ${dir}" 251220927Ssimon name_postfix="${ext} archive dir" 252220927Ssimon else 253220927Ssimon newsyslog_args="" 254220927Ssimon name_postfix="${ext}" 255220927Ssimon fi 256220927Ssimon 257220927Ssimon tmpdir_create 258220927Ssimon 259220927Ssimon begin "create file ${name_postfix}" -newdir 260220927Ssimon run_newsyslog -C 261220927Ssimon ckfe $LOGFNAME 262220927Ssimon cknt ${dir}${LOGFNAME}.0${ext} 263220927Ssimon end 264220927Ssimon 265220927Ssimon begin "rotate normal 1 cnt=$cnt ${name_postfix}" 266220927Ssimon run_newsyslog $newsyslog_args 267220927Ssimon ckfe ${LOGFNAME} 268220927Ssimon ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext} 269220927Ssimon cknt ${dir}${LOGFNAME}.1${ext} 270220927Ssimon end 271220927Ssimon 272220927Ssimon begin "rotate normal 2 cnt=$cnt ${name_postfix}" 273220927Ssimon run_newsyslog $newsyslog_args 274220927Ssimon ckfe ${LOGFNAME} 275220927Ssimon ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext} 276220927Ssimon ckntfe 2 $cnt ${dir}${LOGFNAME}.1${ext} 277220927Ssimon cknt ${dir}${LOGFNAME}.2${ext} 278220927Ssimon end 279220927Ssimon 280220927Ssimon begin "rotate normal 3 cnt=$cnt ${name_postfix}" 281220927Ssimon run_newsyslog $newsyslog_args 282220927Ssimon ckfe ${LOGFNAME} 283220927Ssimon ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext} 284220927Ssimon ckntfe 2 $cnt ${dir}${LOGFNAME}.1${ext} 285220927Ssimon ckntfe 3 $cnt ${dir}${LOGFNAME}.2${ext} 286220927Ssimon cknt ${dir}${LOGFNAME}.3${ext} 287220927Ssimon end 288220927Ssimon 289220927Ssimon begin "rotate normal 3 cnt=$cnt ${name_postfix}" 290220927Ssimon run_newsyslog $newsyslog_args 291220927Ssimon ckfe ${LOGFNAME} 292220927Ssimon ckntfe 1 $cnt ${dir}${LOGFNAME}.0${ext} 293220927Ssimon ckntfe 2 $cnt ${dir}${LOGFNAME}.1${ext} 294220927Ssimon ckntfe 3 $cnt ${dir}${LOGFNAME}.2${ext} 295220927Ssimon ckntfe 4 $cnt ${dir}${LOGFNAME}.3${ext} 296220927Ssimon cknt ${dir}${LOGFNAME}.4${ext} 297220927Ssimon end 298220927Ssimon 299220927Ssimon # Wait a bit so we can see if the noaction test rotates files 300220927Ssimon sleep 1.1 301220927Ssimon 302220927Ssimon begin "noaction ${name_postfix}" 303220927Ssimon osum=`md5 ${dir}${LOGFNAME} | tr -d '\n'` 304220927Ssimon run_newsyslog ${newsyslog_args} -n >/dev/null 305220927Ssimon ckfe ${LOGFNAME} 306220927Ssimon ckstr "$osum" "`md5 ${dir}${LOGFNAME} | tr -d '\n'`" 307220927Ssimon end 308220927Ssimon 309220927Ssimon tmpdir_clean 310220927Ssimon} 311220927Ssimon 312210373Ssimontests_time_rotate() { 313210373Ssimon ext="$1" 314210373Ssimon dir="$2" 315210373Ssimon 316210373Ssimon if [ -n "$dir" ]; then 317210373Ssimon newsyslog_args="-t DEFAULT -a ${dir}" 318210373Ssimon name_postfix="${ext} archive dir" 319210373Ssimon else 320210373Ssimon newsyslog_args="-t DEFAULT" 321210373Ssimon name_postfix="${ext}" 322210373Ssimon fi 323210373Ssimon 324210373Ssimon tmpdir_create 325210373Ssimon 326210373Ssimon begin "create file ${name_postfix}" -newdir 327210373Ssimon run_newsyslog -C ${newsyslog_args} 328210373Ssimon ckfe ${LOGFNAME} 329210373Ssimon end 330210373Ssimon 331210373Ssimon begin "rotate time 1 ${name_postfix}" 332210373Ssimon run_newsyslog ${newsyslog_args} 333210373Ssimon ckfe ${LOGFNAME} 334210373Ssimon chkfcnt 1 ${dir}${LOGFNAME}.*${ext} 335210373Ssimon end 336210373Ssimon 337210373Ssimon sleep 1.1 338210373Ssimon 339210373Ssimon begin "rotate time 2 ${name_postfix}" 340210373Ssimon run_newsyslog ${newsyslog_args} 341210373Ssimon ckfe ${LOGFNAME} 342210373Ssimon chkfcnt 2 ${dir}${LOGFNAME}.*${ext} 343210373Ssimon end 344210373Ssimon 345210373Ssimon sleep 1.1 346210373Ssimon 347210373Ssimon begin "rotate time 3 ${name_postfix}" 348210373Ssimon run_newsyslog ${newsyslog_args} 349210373Ssimon ckfe ${LOGFNAME} 350210373Ssimon chkfcnt 3 ${dir}${LOGFNAME}.*${ext} 351210373Ssimon end 352210373Ssimon 353210373Ssimon sleep 1.1 354210373Ssimon 355210373Ssimon begin "rotate time 4 ${name_postfix}" 356210373Ssimon run_newsyslog ${newsyslog_args} 357210373Ssimon ckfe ${LOGFNAME} 358210373Ssimon chkfcnt 3 ${dir}${LOGFNAME}.*${ext} 359210373Ssimon end 360210373Ssimon 361210373Ssimon begin "noaction ${name_postfix}" 362210373Ssimon ofiles=`ls -1 ${dir}${LOGFNAME}.*${ext} | tr -d '\n'` 363210373Ssimon run_newsyslog ${newsyslog_args} -n >/dev/null 364210373Ssimon ckfe ${LOGFNAME} 365210373Ssimon ckstr "$ofiles" "`ls -1 ${dir}${LOGFNAME}.*${ext} | tr -d '\n'`" 366210373Ssimon end 367210373Ssimon 368210373Ssimon tmpdir_clean 369210373Ssimon} 370210373Ssimon 371220927Ssimonecho 1..126 372210373Ssimonmkdir -p ${TMPDIR} 373210373Ssimoncd ${TMPDIR} 374210373Ssimon 375210373SsimonLOGFNAME=foo.log 376210373SsimonLOGFPATH=${TMPDIR}/log/${LOGFNAME} 377210373Ssimon 378220927Ssimon# Normal, no archive dir, keep X files 379220927Ssimonecho "$LOGFPATH 640 0 * @T00 NC" > newsyslog.conf 380220927Ssimontests_normal_rotate_keepn 0 381210373Ssimon 382220927Ssimonecho "$LOGFPATH 640 1 * @T00 NC" > newsyslog.conf 383220927Ssimontests_normal_rotate_keepn 1 384210373Ssimon 385220927Ssimonecho "$LOGFPATH 640 2 * @T00 NC" > newsyslog.conf 386220927Ssimontests_normal_rotate_keepn 2 387220927Ssimon 388220927Ssimonecho "$LOGFPATH 640 3 * @T00 NC" > newsyslog.conf 389220927Ssimontests_normal_rotate_keepn 3 390220927Ssimon 391220927Ssimon# Normal, no archive dir, keep X files, gz 392220927Ssimonecho "$LOGFPATH 640 0 * @T00 NCZ" > newsyslog.conf 393220927Ssimontests_normal_rotate_keepn 0 ".gz" 394220927Ssimon 395220927Ssimonecho "$LOGFPATH 640 1 * @T00 NCZ" > newsyslog.conf 396220927Ssimontests_normal_rotate_keepn 1 ".gz" 397220927Ssimon 398220927Ssimonecho "$LOGFPATH 640 2 * @T00 NCZ" > newsyslog.conf 399220927Ssimontests_normal_rotate_keepn 2 ".gz" 400220927Ssimon 401220927Ssimonecho "$LOGFPATH 640 3 * @T00 NCZ" > newsyslog.conf 402220927Ssimontests_normal_rotate_keepn 3 ".gz" 403220927Ssimon 404210373Ssimon# Normal, no archive dir 405220927Ssimonecho "$LOGFPATH 640 3 * @T00 NC" > newsyslog.conf 406210373Ssimontests_normal_rotate 407210373Ssimon 408220927Ssimonecho "$LOGFPATH 640 3 * @T00 NCZ" > newsyslog.conf 409210373Ssimontests_normal_rotate ".gz" 410210373Ssimon 411220927Ssimonecho "$LOGFPATH 640 3 * @T00 NCJ" > newsyslog.conf 412210373Ssimontests_normal_rotate ".bz2" 413210373Ssimon 414210373Ssimon# Normal, archive dir 415220927Ssimonecho "$LOGFPATH 640 3 * @T00 NC" > newsyslog.conf 416210373Ssimontests_normal_rotate "" "${TMPDIR}/alog/" 417210373Ssimon 418220927Ssimonecho "$LOGFPATH 640 3 * @T00 NCZ" > newsyslog.conf 419210373Ssimontests_normal_rotate ".gz" "${TMPDIR}/alog/" 420210373Ssimon 421220927Ssimonecho "$LOGFPATH 640 3 * @T00 NCJ" > newsyslog.conf 422210373Ssimontests_normal_rotate ".bz2" "${TMPDIR}/alog/" 423210373Ssimon 424210373Ssimon# Time based, no archive dir 425210373Ssimonecho "$LOGFPATH 640 3 * @T00 NC" > newsyslog.conf 426210373Ssimontests_time_rotate 427210373Ssimon 428210373Ssimonecho "$LOGFPATH 640 3 * @T00 NCZ" > newsyslog.conf 429210373Ssimontests_time_rotate "gz" "" 430210373Ssimon 431210373Ssimonecho "$LOGFPATH 640 3 * @T00 NCJ" > newsyslog.conf 432210373Ssimontests_time_rotate "bz2" "" 433210373Ssimon 434210373Ssimon# Time based, archive dir 435210373Ssimonecho "$LOGFPATH 640 3 * @T00 NC" > newsyslog.conf 436210373Ssimontests_time_rotate "" "${TMPDIR}/alog/" 437210373Ssimon 438210373Ssimonecho "$LOGFPATH 640 3 * @T00 NCZ" > newsyslog.conf 439210373Ssimontests_time_rotate "gz" "${TMPDIR}/alog/" 440210373Ssimon 441210373Ssimonecho "$LOGFPATH 640 3 * @T00 NCJ" > newsyslog.conf 442210373Ssimontests_time_rotate "bz2" "${TMPDIR}/alog/" 443210373Ssimon 444210373Ssimonrm -rf "${TMPDIR}" 445