From: Karel Zak Date: Tue, 15 May 2007 23:38:13 +0000 (+0200) Subject: tests: code refactoring -- new ts_device_init function X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=df7e52d74bb877caa2b0ea94caf056446abe1236;p=util-linux tests: code refactoring -- new ts_device_init function The patch moves generic loop device (de)initialization code to ts_device_init and ts_device_deinit functions. Signed-off-by: Karel Zak --- diff --git a/tests/expected/ts-mkswap-doit b/tests/expected/ts-mkswap-doit index fa52f3d0..d29585c3 100644 --- a/tests/expected/ts-mkswap-doit +++ b/tests/expected/ts-mkswap-doit @@ -1,2 +1,2 @@ -Setting up swapspace version 1, size = 20967 kB +Setting up swapspace version 1, size = 5238 kB LABEL=testSwap, no uuid diff --git a/tests/functions.sh b/tests/functions.sh index 969592f7..86abfb4e 100644 --- a/tests/functions.sh +++ b/tests/functions.sh @@ -75,5 +75,28 @@ function ts_finalize { fi } +function ts_device_init { + IMAGE="$TS_OUTDIR/$TS_NAME.img" + IMAGE_RE=$( echo "$IMAGE" | sed 's:/:\\/:g' ) + dd if=/dev/zero of="$IMAGE" bs=1M count=5 &> /dev/null + $TS_CMD_LOSETUP -f "$IMAGE" 2>&1 >> $TS_OUTPUT + DEVICE=$( $TS_CMD_LOSETUP -a | gawk 'BEGIN {FS=":"} /'$IMAGE_RE'/ { print $1 }' ) + + if [ -z "$DEVICE" ]; then + ts_device_deinit + return 1 # error + fi + + return 0 # succes +} + +function ts_device_deinit { + if [ -b "$DEVICE" ]; then + $TS_CMD_UMOUNT "$DEVICE" &> /dev/null + $TS_CMD_LOSETUP -d "$DEVICE" &> /dev/null + rm -f "$IMAGE" &> /dev/null + fi +} + diff --git a/tests/ts-mkswap-doit b/tests/ts-mkswap-doit index b3632b70..cd567cf3 100755 --- a/tests/ts-mkswap-doit +++ b/tests/ts-mkswap-doit @@ -28,11 +28,8 @@ ts_skip_nonroot set -o pipefail - touch $TS_OUTPUT -IMAGE="$TS_OUTDIR/loop-swap.img" -IMAGE_RE=$( echo $IMAGE | sed 's:/:\\/:g' ) LABEL="testSwap" ldd $TS_CMD_SWAPON | grep -q 'libvolume_id' 2>&1 >> $TS_OUTPUT @@ -40,30 +37,18 @@ if [ "$?" == "0" ]; then HAS_VOLUMEID="yes" fi -dd if=/dev/zero of=$IMAGE bs=1M count=20 &> /dev/null - -DEVICE=$( $TS_CMD_LOSETUP -f ) -$TS_CMD_LOSETUP $DEVICE $IMAGE 2>&1 >> $TS_OUTPUT - -# XXX: atomic version: depends on "losetup -a" -## create the device -#$TS_CMD_LOSETUP -f $IMAGE 2>&1 >> $TS_OUTPUT -# -## get the device name -#DEVICE=$( $TS_CMD_LOSETUP -a | gawk 'BEGIN {FS=":"} /'$IMAGE_RE'/ { print $1 }' ) - -# make the swap +ts_device_init || ts_finalize $TS_CMD_MKSWAP -L $LABEL $DEVICE 2>&1 >> $TS_OUTPUT # check it if [ "$($TS_CMD_MOUNT --guess-fstype $DEVICE)" != "swap" ]; then echo "Cannot found Linux swap on $DEVICE" >> $TS_OUTPUT - $TS_CMD_LOSETUP -d $DEVICE 2>&1 >> $TS_OUTPUT + ts_device_deinit ts_finalize fi if [ -n "$HAS_VOLUMEID" ] && [ ! -L "/dev/disk/by-label/$LABEL" ]; then - $TS_CMD_LOSETUP -d $DEVICE 2>&1 >> $TS_OUTPUT + ts_device_deinit ts_skip "udev ignores /dev/loop*" fi @@ -75,7 +60,7 @@ grep -q $DEVICE /proc/swaps if [ "$?" != "0" ]; then echo "Cannot found $DEVICE in /proc/swaps" >> $TS_OUTPUT - $TS_CMD_LOSETUP -d $DEVICE 2>&1 >> $TS_OUTPUT + ts_device_deinit ts_finalize fi @@ -85,7 +70,7 @@ if [ ! -x "$TS_CMD_SWAPOFF" ]; then REMSWAPOFF="true" fi $TS_CMD_SWAPOFF $DEVICE 2>&1 >> $TS_OUTPUT -$TS_CMD_LOSETUP -d $DEVICE 2>&1 >> $TS_OUTPUT +ts_device_deinit if [ -n "$REMSWAPOFF" ]; then rm -f $TS_CMD_SWAPOFF diff --git a/tests/ts-mount-remount b/tests/ts-mount-remount index 28064e74..13a9c6d8 100755 --- a/tests/ts-mount-remount +++ b/tests/ts-mount-remount @@ -13,29 +13,13 @@ TS_DESC="remount" ts_init ts_skip_nonroot -# clean up -test_cleanup() { - $TS_CMD_UMOUNT $MOUNTPOINT &> /dev/null - rmdir $MOUNTPOINT &> /dev/null - $TS_CMD_LOSETUP -d $DEVICE &> /dev/null - rm -f $IMAGE -} - touch $TS_OUTPUT -PWD=$(pwd) - # mountpoint -MOUNTPOINT="$PWD/$TS_OUTDIR/mnt-remount" -mkdir $MOUNTPOINT - -# initializa device -IMAGE="$TS_OUTDIR/loop-remount.img" - -dd if=/dev/zero of=$IMAGE bs=1M count=20 &> /dev/null +MOUNTPOINT="$(pwd)/$TS_OUTDIR/mnt-remount" +mkdir -p $MOUNTPOINT -DEVICE=$( $TS_CMD_LOSETUP -f ) -$TS_CMD_LOSETUP $DEVICE $IMAGE 2>&1 >> $TS_OUTPUT +ts_device_init || ts_finalize mkfs.ext2 $DEVICE &> /dev/null # mount read-write @@ -46,7 +30,7 @@ egrep -q "^$DEVICE $MOUNTPOINT" /etc/mtab if [ "$?" != "0" ]; then echo "Cannot found $MOUNTPOINT in /etc/mtab" >> $TS_OUTPUT - test_cleanup + ts_device_deinit ts_finalize fi @@ -58,11 +42,11 @@ egrep -q "^$DEVICE $MOUNTPOINT ext2 ro 0 0" /etc/mtab if [ "$?" != "0" ]; then echo "Cannot found remounted $MOUNTPOINT in /etc/mtab" >> $TS_OUTPUT - test_cleanup + ts_device_deinit ts_finalize fi -test_cleanup +ts_device_deinit echo "Success" >> $TS_OUTPUT ts_finalize