test-common.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # common variables and functions for legacy tests
  2. LOGROTATE="$(readlink -f $LOGROTATE)"
  3. RLR="$LOGROTATE -m ./mailer -s state"
  4. TESTDIR="$(basename "$0" .sh)"
  5. mkdir -p "$TESTDIR"
  6. cd "$TESTDIR" || exit $?
  7. TESTNUM="$(printf "%s\n" "$TESTDIR" | sed -e 's/^test-0*//')"
  8. import() {
  9. [ -e "$1" ] && return
  10. [ -e "../$1" ] || return
  11. ln -s "../$1"
  12. }
  13. import "compress"
  14. import "compress-error"
  15. import "mailer"
  16. import "test-common-acl.sh"
  17. import "test-common-selinux.sh"
  18. import "test-config.$TESTNUM.in"
  19. cleanup() {
  20. rm -f test*.log* anothertest*.log* state test-config. scriptout mail-out compress-args compress-env different*.log* test-config.*[0-9]
  21. [ -n "$1" ] && echo "Running test $1"
  22. return 0
  23. }
  24. genconfig() {
  25. input=test-config.$1.in
  26. output=test-config.$1
  27. user=$(id -u -n)
  28. group=$(id -g -n)
  29. sed "s,&DIR&,$PWD,g" < $input | sed "s,&USER&,$user,g" | sed "s,&GROUP&,$group,g" > $output
  30. config_crc=$(md5sum $output)
  31. }
  32. createlog() {
  33. num=$1
  34. file=$2
  35. cl_compressed=$3
  36. case $num in
  37. 0)
  38. what=zero
  39. ;;
  40. 1)
  41. what=first
  42. ;;
  43. 2)
  44. what=second
  45. ;;
  46. 3)
  47. what=third
  48. ;;
  49. 4)
  50. what=fourth
  51. ;;
  52. 5)
  53. what=fifth
  54. ;;
  55. 6)
  56. what=sixth
  57. ;;
  58. 7)
  59. what=seventh
  60. ;;
  61. 8)
  62. what=eight
  63. ;;
  64. 9)
  65. what=ninth
  66. ;;
  67. *)
  68. exit 1
  69. ;;
  70. esac
  71. echo $what > $file
  72. [ -n "$cl_compressed" ] && gzip -9 $file
  73. }
  74. createlogs() {
  75. base=$1
  76. numlogs=$2
  77. cls_compressed=$3
  78. rm -f ${base}*
  79. createlog 0 $base
  80. num=1
  81. while [ $num -le $numlogs ]; do
  82. createlog $num ${base}.$num $cls_compressed
  83. num=`expr $num + 1`
  84. done
  85. }
  86. checkmail() {
  87. (echo -s $PWD/$1 user@invalid.; echo $2) | diff -u - mail-out
  88. if [ $? != 0 ]; then
  89. exit 5
  90. fi
  91. }
  92. checkoutput() {
  93. while read line; do
  94. set $line
  95. file=$1
  96. co_compressed=$2
  97. if [ "$#" -le 2 ]; then
  98. shift $#
  99. else
  100. shift 2
  101. fi
  102. fileother=`echo $line | awk '{print $1}'`
  103. expected=`echo $line | cut -s -d\ -f3-`
  104. if [ $file != $fileother ]; then
  105. echo "unexpected file $file'" >&2
  106. exit 2
  107. fi
  108. if [ ! -f $file ]; then
  109. echo "file $file does not exist"
  110. fi
  111. if [ -n "$co_compressed" ] && [ "$co_compressed" != 0 ]; then
  112. contents=`gunzip -c $file`
  113. else
  114. contents=`cat $file | tr -d '\000'`
  115. fi
  116. if [ "$contents" != "$expected" ]; then
  117. echo "file $file does not contain expected results (compressed $co_compressed, args $*)" >&2
  118. echo contains: \'$contents\'
  119. echo expected: \'$expected\'
  120. exit 2
  121. fi
  122. echo "$config_crc" | md5sum -c - >/dev/null
  123. if [ $? != 0 ]; then
  124. echo "config file $output has been altered: MD5 sum mismatch"
  125. exit 3
  126. fi
  127. done
  128. }
  129. preptest() {
  130. base=$1
  131. confignum=$2
  132. numlogs=$3
  133. pt_compressed=$4
  134. rm -f $base*
  135. rm -f state
  136. genconfig $confignum
  137. createlogs $base $numlogs $pt_compressed
  138. }