-#!/bin/sh
+#!/bin/bash
#
# run test suite for mdadm
-user=`id -un`
-if [ " $user" != " root" ]
-then echo >&2 "test: testing can only be done as 'root'."
- exit 1;
-fi
+mdadm=$PWD/mdadm
+targetdir="/var/tmp"
+logdir="$targetdir"
+config=/tmp/mdadm.conf
+testdir=$PWD/tests
+devlist=
+savelogs=0
+exitonerror=1
prefix='[0-9][0-9]'
-if [ -n "$1" ]
-then prefix=$1
-fi
-dir=`pwd`
-mdadm=$dir/mdadm
-if [ \! -x $mdadm ]
-then
- echo >&2 "test: $mdadm isn't usable."
-fi
+# use loop devices by default if doesn't specify --dev
+DEVTYPE=loop
+INTEGRITY=yes
+LVM_VOLGROUP=mdtest
+
+# make sure to test local mdmon, not system one
+export MDADM_NO_SYSTEMCTL=1
# assume md0, md1, md2 exist in /dev
-md0=/dev/md0 md1=/dev/md1 md2=/dev/md2
+md0=/dev/md0
+md1=/dev/md1
+md2=/dev/md2
mdp0=/dev/md_d0
mdp1=/dev/md_d1
-# We test mdadm on loop-back block devices.
-# dir for storing files should be settable by command line maybe
-targetdir=/var/tmp
-size=20000
-# super0, round down to multiple of 64 and substract 64
-mdsize0=19904
-# super00 is nested, subtract 128
-mdsize00=19840
-# super1.0 round down to multiple of 2, subtract 8
-mdsize1=19992
-# subtract another 4 for bitmaps
-mdsize1b=19988
-mdsize11=19992
-mdsize12=19988
-
-cleanup() {
- $mdadm -Ss
- for d in 0 1 2 3 4 5 6 7
- do
- losetup -d /dev/loop$d ; # rm -f $targetdir/mdtest$d
- done
+die() {
+ echo -e "\n\tERROR: $* \n"
+ save_log fail
+ exit 2
}
-trap cleanup 0 1 2 3 15
-
-devlist=
-for d in 0 1 2 3 4 5 6 7
-do
- [ -f $targetdir/mdtest$d ] || dd if=/dev/zero of=$targetdir/mdtest$d count=$size bs=1K > /dev/null 2>&1
- [ -b /dev/loop$d ] || mknod /dev/loop$d b 7 $d
- if [ $d -eq 7 ]
- then
- losetup /dev/loop$d $targetdir/mdtest6 # for multipath use
- else
- losetup /dev/loop$d $targetdir/mdtest$d
- fi
- eval dev$d=/dev/loop$d
- eval file$d=$targetdir/mdtest$d
- eval devlist=\"\$devlist \$dev$d\"
-done
-path0=$dev6
-path1=$dev7
-
-echo 2000 > /proc/sys/dev/raid/speed_limit_max
-echo 0 > /sys/module/md_mod/parameters/start_ro
-
-if [ " $1" = " setup" ]
-then trap 0 ; exit 0
-fi
+ctrl_c() {
+ exitonerror=1
+}
# mdadm always adds --quiet, and we want to see any unexpected messages
mdadm() {
- rm -f $targetdir/stderr
- case $* in
- *-C* ) $mdadm 2> $targetdir/stderr --quiet "$@" --auto=yes;;
- * ) $mdadm 2> $targetdir/stderr --quiet "$@"
- esac
- cat >&2 $targetdir/stderr
+ rm -f $targetdir/stderr
+ case $* in
+ *-S* )
+ udevadm settle
+ p=`cat /proc/sys/dev/raid/speed_limit_max`
+ echo 20000 > /proc/sys/dev/raid/speed_limit_max
+ ;;
+ esac
+ case $* in
+ *-C* | *--create* | *-B* | *--build* )
+ # clear superblock every time once creating or
+ # building arrays, because it's always creating
+ # and building array many times in a test case.
+ for args in $*
+ do
+ [[ $args =~ "/dev/" ]] && {
+ [[ $args =~ "md" ]] ||
+ $mdadm --zero $args > /dev/null
+ }
+ done
+ $mdadm 2> $targetdir/stderr --quiet "$@" --auto=yes
+ ;;
+ * )
+ $mdadm 2> $targetdir/stderr --quiet "$@"
+ ;;
+ esac
+ rv=$?
+ case $* in
+ *-S* )
+ udevadm settle
+ echo $p > /proc/sys/dev/raid/speed_limit_max
+ ;;
+ esac
+ cat >&2 $targetdir/stderr
+ return $rv
}
-# check various things
-check() {
- case $1 in
- spares )
- spares=`tr '] ' '\012\012' < /proc/mdstat | grep -c '(S)' || exit 0`
- if [ $spares -ne $2 ]
- then
- echo >&2 "ERROR expected $2 spares, found $spares"; exit 1;
- fi
- ;;
- raid* | linear )
- grep -s "active $1 " /proc/mdstat > /dev/null || {
- echo >&2 "ERROR active $1 not found" ; cat /proc/mdstat ; exit 1;}
- ;;
- resync | recovery | reshape)
- sleep 0.5
- grep -s $1 /proc/mdstat > /dev/null || {
- echo >&2 ERROR no $1 happening; cat /proc/mdstat; exit 1; }
- ;;
-
- nosync )
- sleep 0.5
- if grep -s -E '(resync|recovery|reshape) =' > /dev/null /proc/mdstat ; then
- echo >&2 "ERROR resync or recovery is happening!"; cat /proc/mdstat ; exit 1;
- fi
- ;;
-
- wait )
- sleep 0.1
- while grep -E '(resync|recovery|reshape|check|repair) =' > /dev/null /proc/mdstat
- do sleep 2;
- done
- ;;
-
- state )
- grep -s "blocks.*\[$2\]\$" /proc/mdstat > /dev/null || {
- echo >&2 "ERROR state $2 not found!"; cat /proc/mdstat ; exit 1; }
- sleep 0.5
- ;;
-
- bitmap )
- grep -s bitmap > /dev/null /proc/mdstat || {
- echo >&2 ERROR no bitmap ; cat /proc/mdstat ; exit 1; }
- ;;
- nobitmap )
- if grep -s "bitmap" > /dev/null /proc/mdstat
- then
- echo >&2 ERROR bitmap present ; cat /proc/mdstat ; exit 1;
- fi
- ;;
-
- * ) echo >&2 ERROR unknown check $1 ; exit 1;
- esac
+do_test() {
+ _script=$1
+ _basename=`basename $_script`
+ if [ -f "$_script" ]
+ then
+ rm -f $targetdir/stderr
+ # this might have been reset: restore the default.
+ echo 2000 > /proc/sys/dev/raid/speed_limit_max
+ do_clean
+ # source script in a subshell, so it has access to our
+ # namespace, but cannot change it.
+ echo -ne "$_script... "
+ if ( set -ex ; . $_script ) &> $targetdir/log
+ then
+ dmesg | grep -iq "error\|call trace\|segfault" &&
+ die "dmesg prints errors when testing $_basename!"
+ echo "succeeded"
+ _fail=0
+ else
+ save_log fail
+ _fail=1
+ fi
+ [ "$savelogs" == "1" ] &&
+ mv -f $targetdir/log $logdir/$_basename.log
+ [ "$_fail" == "1" -a "$exitonerror" == "1" ] && exit 1
+ fi
}
-no_errors() {
- if [ -s $targetdir/stderr ]
- then echo Bad errors from mdadm: ; cat $targetdir/stderr; exit 2;
- fi
+do_help() {
+ cat <<-EOF
+ Usage: $0 [options]
+ Example for disk mode: ./test --dev=disk --disks=/dev/sda{2..15}
+ Options:
+ --tests=test1,test2,... Comma separated list of tests to run
+ --testdir= Specify testdir as tests|clustermd_tests
+ --raidtype= raid0|linear|raid1|raid456|raid10|ddf|imsm
+ --disable-multipath Disable any tests involving multipath
+ --disable-integrity Disable slow tests of RAID[56] consistency
+ --logdir=directory Directory to save all logfiles in
+ --save-logs Usually use with --logdir together
+ --keep-going | --no-error Don't stop on error, ie. run all tests
+ --dev=loop|lvm|ram|disk Use loop devices (default), LVM, RAM or disk
+ --disks= Provide a bunch of physical devices for test
+ --volgroup=name LVM volume group for LVM test
+ setup Setup test environment and exit
+ cleanup Cleanup test environment
+ prefix Run tests with <prefix>
+ --help | -h Print this usage
+ EOF
}
-# basic device test
-testdev() {
- dev=$1
- cnt=$2
- dvsize=$3
- chunk=$4
- mkfs -j $dev > /dev/null 2>&1 && fsck -fn $dev >&2
- dsize=$[dvsize/chunk]
- dsize=$[dsize*chunk]
- rasize=$[dsize*2*cnt]
- if [ `/sbin/blockdev --getsize $dev` -eq 0 ]; then sleep 2 ; fi
- if [ $rasize -ne `/sbin/blockdev --getsize $dev` ]
- then
- echo "ERROR: size is wrong for $dev: $cnt * $dvsize (chunk=$chunk) = $rasize, not `/sbin/blockdev --getsize $dev`"
- exit 1
- fi
+parse_args() {
+ for i in $*
+ do
+ case $i in
+ --testdir=* )
+ case ${i##*=} in
+ tests )
+ testdir=tests
+ ;;
+ clustermd_tests )
+ testdir=clustermd_tests
+ CLUSTER_CONF="$PWD/$testdir/cluster_conf"
+ ;;
+ * )
+ echo "Unknown argument: $i"
+ do_help
+ exit 1
+ ;;
+ esac
+ ;;
+ esac
+ done
+ [ -z "$testdir" ] && testdir=tests
+ . $testdir/func.sh
+ for i in $*
+ do
+ case $i in
+ [0-9][0-9] )
+ prefix=$i
+ ;;
+ setup )
+ echo "mdadm test environment setup"
+ do_setup
+ trap 0
+ exit 0
+ ;;
+ cleanup )
+ cleanup
+ exit 0
+ ;;
+ --testdir=* )
+ ;;
+ --tests=* )
+ TESTLIST=($(echo ${i##*=} | sed -e 's/,/ /g'))
+ ;;
+ --raidtype=* )
+ case ${i##*=} in
+ raid0 )
+ TESTLIST=($(ls $testdir | grep "[0-9][0-9]r0\|raid0"))
+ ;;
+ linear )
+ TESTLIST=($(ls $testdir | grep "linear"))
+ ;;
+ raid1 )
+ TESTLIST=($(ls $testdir | grep "[0-9][0-9]r1\|raid1" | grep -vi "r10\|raid10"))
+ ;;
+ raid456 )
+ TESTLIST=($(ls $testdir | grep "[0-9][0-9]r[4-6]\|raid[4-6]"))
+ ;;
+ raid10 )
+ TESTLIST=($(ls $testdir | grep "[0-9][0-9]r10\|raid10"))
+ ;;
+ ddf )
+ TESTLIST=($(ls $testdir | grep "[0-9][0-9]ddf"))
+ ;;
+ imsm )
+ TESTLIST=($(ls $testdir | grep "[0-9][0-9]imsm"))
+ ;;
+ * )
+ echo "Unknown argument: $i"
+ do_help
+ exit 1
+ ;;
+ esac
+ ;;
+ --logdir=* )
+ logdir="${i##*=}"
+ ;;
+ --save-logs )
+ savelogs=1
+ ;;
+ --keep-going | --no-error )
+ exitonerror=0
+ ;;
+ --disable-multipath )
+ unset MULTIPATH
+ ;;
+ --disable-integrity )
+ unset INTEGRITY
+ ;;
+ --dev=* )
+ case ${i##*=} in
+ loop )
+ DEVTYPE=loop
+ ;;
+ lvm )
+ DEVTYPE=lvm
+ ;;
+ ram )
+ DEVTYPE=ram
+ ;;
+ disk )
+ DEVTYPE=disk
+ ;;
+ * )
+ echo "Unknown argument: $i"
+ do_help
+ exit 1
+ ;;
+ esac
+ ;;
+ --disks=* )
+ disks=(${disks[*]} ${i##*=})
+ ;;
+ --volgroup=* )
+ LVM_VOLGROUP=`expr "x$i" : 'x[^=]*=\(.*\)'`
+ ;;
+ --help | -h )
+ do_help
+ exit 0
+ ;;
+ * )
+ echo " $0: Unknown argument: $i"
+ do_help
+ exit 1
+ ;;
+ esac
+ done
}
-rotest() {
- dev=$1
- fsck -fn $dev >&2
-}
+main() {
+ do_setup
+ echo "Testing on linux-$(uname -r) kernel"
+ [ "$savelogs" == "1" ] &&
+ echo "Saving logs to $logdir"
+ if [ "x$TESTLIST" != "x" ]
+ then
+ for script in ${TESTLIST[@]}
+ do
+ do_test $testdir/$script
+ done
+ else
+ for script in $testdir/$prefix $testdir/$prefix*[^~]
+ do
+ do_test $script
+ done
+ fi
+ exit 0
+}
-for script in tests/$prefix tests/$prefix*[^~]
-do
- if [ -f "$script" ]
- then
- rm -f $targetdir/stderr
- # stop all arrays, just incase some script left an array active.
- mdadm -Ssq
- # source script in a subshell, so it has access to our
- # namespace, but cannot change it.
- if ( set -ex ; . $script ) 2> $targetdir/log
- then echo "$script succeeded"
- else cat $targetdir/log ; cat $targetdir/stderr
- echo "$script failed"
- exit 1
- fi
- fi
-done
-exit 0
+parse_args $@
+main