]> err.no Git - util-linux/commitdiff
tests: code refactoring -- new ts_device_init function
authorKarel Zak <kzak@redhat.com>
Tue, 15 May 2007 23:38:13 +0000 (01:38 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 18 May 2007 11:09:12 +0000 (13:09 +0200)
The patch moves generic loop device (de)initialization code to
ts_device_init and ts_device_deinit functions.

Signed-off-by: Karel Zak <kzak@redhat.com>
tests/expected/ts-mkswap-doit
tests/functions.sh
tests/ts-mkswap-doit
tests/ts-mount-remount

index fa52f3d0d0054e006284efcbc523cd20b3bd6a51..d29585c3f43377b1344a33d3f001468563fd6ebc 100644 (file)
@@ -1,2 +1,2 @@
-Setting up swapspace version 1, size = 20967 kB
+Setting up swapspace version 1, size = 5238 kB
 LABEL=testSwap, no uuid
index 969592f7e471f7bcff91c4bdb355128ac774bee8..86abfb4e98aca3b6583194d43818c3a48d87c964 100644 (file)
@@ -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
+}
+               
index b3632b70772ff9f9981edbc83ddaae3537c5a677..cd567cf32dead88841e36508fae338da5177b14d 100755 (executable)
@@ -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
index 28064e745e9fa2efdda237a44afd7cd35b184748..13a9c6d89b91063b7cd545a4f71ca1ec31a4a021 100755 (executable)
@@ -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