]> err.no Git - util-linux/commitdiff
tests: simplify devices usage
authorKarel Zak <kzak@redhat.com>
Fri, 18 May 2007 11:49:15 +0000 (13:49 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 18 May 2007 11:49:15 +0000 (13:49 +0200)
The patch simplifies devices usage in the tests. The patch also improves
errors checking and reporting.

Signed-off-by: Karel Zak <kzak@redhat.com>
tests/expected/ts-mkswap-doit [deleted file]
tests/expected/ts-swapon-label [new file with mode: 0644]
tests/functions.sh
tests/ts-cramfs-mkfs
tests/ts-mkswap-doit [deleted file]
tests/ts-mount-remount
tests/ts-swapon-label [new file with mode: 0755]

diff --git a/tests/expected/ts-mkswap-doit b/tests/expected/ts-mkswap-doit
deleted file mode 100644 (file)
index d29585c..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-Setting up swapspace version 1, size = 5238 kB
-LABEL=testSwap, no uuid
diff --git a/tests/expected/ts-swapon-label b/tests/expected/ts-swapon-label
new file mode 100644 (file)
index 0000000..3582111
--- /dev/null
@@ -0,0 +1 @@
+Success
index 7cb3743068eadc85c0a36fe17d654404bb7e0a48..001c39f96bb6904bf5fa967c6b998efc8c7a9675 100644 (file)
@@ -6,6 +6,9 @@ TS_INPUTDIR="input"
 
 function ts_skip {
        echo " IGNORE ($1)"
+       if [ -n "$2" ] && [ -b "$2" ]; then
+               ts_device_deinit "$2"
+       fi
        exit 0
 }
 
@@ -48,6 +51,7 @@ function ts_init {
        TS_INPUT="$TS_INPUTDIR/$TS_NAME"
 
        rm -f $TS_OUTPUT
+       touch $TS_OUTPUT
 
        printf "%15s: %-25s ..." "$TS_COMPONENT" "$TS_DESC"
 }
@@ -75,37 +79,48 @@ function ts_finalize {
        fi
 }
 
+function ts_die {
+       echo "$1" >> $TS_OUTPUT
+       if [ -n "$2" ] && [ -b "$2" ]; then
+               ts_device_deinit "$2"
+       fi
+       ts_finalize
+}
+
 function ts_device_init {
-       IMAGE="$TS_OUTDIR/$TS_NAME.img"
-       IMAGE_RE=$( echo "$IMAGE" | sed 's:/:\\/:g' )
+       local IMAGE="$TS_OUTDIR/$TS_NAME.img"
+       local IMAGE_RE=$( echo "$IMAGE" | sed 's:/:\\/:g' )
+       local DEV=""
 
        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 }' )
+       DEV=$( $TS_CMD_LOSETUP -a | gawk 'BEGIN {FS=":"} /'$IMAGE_RE'/ { print $1 }' )
 
-       if [ -z "$DEVICE" ]; then
-               ts_device_deinit
+       if [ -z "$DEV" ]; then
+               ts_device_deinit $DEV
                return 1                # error
        fi
 
+       echo $DEV
        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
+       local DEV="$1"
+
+       if [ -b "$DEV" ]; then
+               $TS_CMD_UMOUNT "$DEV" &> /dev/null
+               $TS_CMD_LOSETUP -d "$DEV" &> /dev/null
        fi
 }
 
-function ts_udev_loop_support {
+function ts_udev_dev_support {
        ldd $TS_CMD_MOUNT | grep -q 'libvolume_id' 2>&1 >> $TS_OUTPUT
        if [ "$?" == "0" ]; then
-               HAS_VOLUMEID="yes"
+               local HAS_VOLUMEID="yes"
        fi
-       if [ -n "$HAS_VOLUMEID" ] && [ ! -L "/dev/disk/by-label/$1" ]; then
+       if [ -n "$HAS_VOLUMEID" ] && [ ! -L "/dev/disk/$1/$2" ]; then
                return 1
        fi
        return 0
@@ -148,6 +163,7 @@ function ts_device_has {
        local TAG="$1"
        local VAL="$2"
        local DEV="$3"
+       local vl=""
 
        case $TAG in
                "TYPE") vl=$(ts_fstype_by_devname $DEV);;
@@ -161,3 +177,22 @@ function ts_device_has {
        fi
        return 1
 }
+
+function ts_device_has_uuid {
+       ts_uuid_by_devname "$1" | egrep -q '^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$'
+       return $?
+}
+
+function ts_swapoff {
+       local DEV="$1"
+
+       # swapoff doesn't exist in build tree
+       if [ ! -x "$TS_CMD_SWAPOFF" ]; then
+               ln -sf $TS_CMD_SWAPON $TS_CMD_SWAPOFF
+               REMSWAPOFF="true"
+       fi
+       $TS_CMD_SWAPOFF $DEV 2>&1 >> $TS_OUTPUT
+       if [ -n "$REMSWAPOFF" ]; then
+               rm -f $TS_CMD_SWAPOFF
+       fi
+}
index 715f575c099c4ec5c1e646bc12ec686b3f92ef92..b2c43e62d2184ce60b364c9736351ae30a67d403 100755 (executable)
@@ -60,11 +60,7 @@ echo >> $TS_OUTPUT
 
 echo "create cramfs image" >> $TS_OUTPUT
 $TS_CMD_MKCRAMFS -n $LABEL $IMAGE_DATA $IMAGE 2>&1 >> $TS_OUTPUT
-
-if [ ! -s "$IMAGE" ]; then
-       echo "Cannot create $IMAGE" >> $TS_OUTPUT
-       ts_finalize
-fi
+[ -s "$IMAGE" ] || ts_die "Cannot create $IMAGE"
 
 echo "count MD5 from the image" >> $TS_OUTPUT
 md5sum $IMAGE 2>&1 >> $TS_OUTPUT
@@ -75,26 +71,18 @@ DEVICE=$( $TS_CMD_LOSETUP -f )
 $TS_CMD_LOSETUP $DEVICE $IMAGE 2>&1 >> $TS_OUTPUT
 
 echo "check the image" >> $TS_OUTPUT
-if [ "$($TS_CMD_MOUNT --guess-fstype $DEVICE)" != "cramfs" ]; then
-       echo "Cannot found cramfs on $DEVICE" >> $TS_OUTPUT
-       ts_finalize
-fi
+ts_device_has "TYPE" "cramfs" $DEVICE
+[  "$?" == "0" ] || ts_die "Cannot found cramfs on $DEVICE" $DEVICE
 
-ts_udev_loop_support $LABEL
-if [ "$?" != "0" ]; then
-       ts_device_deinit;
-       ts_skip "udev ignores /dev/loop*"
-fi
+ts_udev_dev_support "by-label" $LABEL
+[ "$?" == "0" ] || ts_skip "udev ignores /dev/loop*" $DEVICE
 
 echo "mount the image" >> $TS_OUTPUT
 $TS_CMD_MOUNT -L $LABEL $MOUNTPOINT 2>&1 >> $TS_OUTPUT
 
 # check it
 grep -q $DEVICE /proc/mounts
-
-if [ "$?" != "0" ]; then
-       echo "Cannot found $DEVICE in /proc/mounts" >> $TS_OUTPUT
-fi
+[ "$?" == "0" ] || ts_die "Cannot found $DEVICE in /proc/mounts" $DEVICE
 
 echo "list the image" >> $TS_OUTPUT
 ls -laR $MOUNTPOINT >> $TS_OUTPUT
diff --git a/tests/ts-mkswap-doit b/tests/ts-mkswap-doit
deleted file mode 100755 (executable)
index 600acba..0000000
+++ /dev/null
@@ -1,76 +0,0 @@
-#!/bin/bash
-
-# 
-# Copyright (C) 2007 Karel Zak <kzak@redhat.com>
-# 
-# This file is part of util-linux-ng.
-# 
-# This file is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License.
-# 
-# This file is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-
-# Test swap device initialization and usage
-
-. commands.sh
-. functions.sh
-
-TS_COMPONENT="mkswap"
-TS_DESC="doit"
-
-ts_init
-ts_skip_nonroot
-
-set -o pipefail
-
-touch $TS_OUTPUT
-
-LABEL="testSwap"
-
-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_device_deinit
-       ts_finalize
-fi
-
-ts_udev_loop_support $LABEL
-if [ "$?" != "0" ]; then
-       ts_device_deinit;
-       ts_skip "udev ignores /dev/loop*"
-fi
-
-# try connect it to system (and found the device by label)
-$TS_CMD_SWAPON -L $LABEL 2>&1 >> $TS_OUTPUT
-
-# check it
-grep -q $DEVICE /proc/swaps
-
-if [ "$?" != "0" ]; then
-       echo "Cannot found $DEVICE in /proc/swaps" >> $TS_OUTPUT
-       ts_device_deinit
-       ts_finalize
-fi
-
-# swapoff doesn't exist in build tree
-if [ ! -x "$TS_CMD_SWAPOFF" ]; then
-       ln -sf $TS_CMD_SWAPON $TS_CMD_SWAPOFF
-       REMSWAPOFF="true"
-fi
-$TS_CMD_SWAPOFF $DEVICE 2>&1 >> $TS_OUTPUT
-ts_device_deinit
-
-if [ -n "$REMSWAPOFF" ]; then
-       rm -f $TS_CMD_SWAPOFF
-fi
-
-ts_finalize
-
index 13a9c6d89b91063b7cd545a4f71ca1ec31a4a021..dcaf28886e064e762b3ca7a480ea1e3786974cf7 100755 (executable)
@@ -3,7 +3,6 @@
 #
 # test for mount -o remount,ro
 #
-
 . commands.sh
 . functions.sh
 
@@ -13,40 +12,33 @@ TS_DESC="remount"
 ts_init
 ts_skip_nonroot
 
-touch $TS_OUTPUT
-
 # mountpoint
 MOUNTPOINT="$(pwd)/$TS_OUTDIR/mnt-remount"
 mkdir -p $MOUNTPOINT
 
-ts_device_init || ts_finalize
+DEVICE=$(ts_device_init)
+[ "$?" == 0 ] || ts_die "Cannot init device"
+
 mkfs.ext2 $DEVICE &> /dev/null
+[ "$?" == "0" ] || ts_die "Cannot make ext2 on $DEVICE" $DEVICE
 
 # mount read-write
 $TS_CMD_MOUNT $DEVICE $MOUNTPOINT
+[ "$?" == "0" ] || ts_die "Cannot mount $MOUNTPOINT" $DEVICE
 
 # check the mount
 egrep -q "^$DEVICE $MOUNTPOINT" /etc/mtab
-
-if [ "$?" != "0" ]; then
-        echo "Cannot found $MOUNTPOINT in /etc/mtab" >> $TS_OUTPUT
-       ts_device_deinit
-        ts_finalize
-fi
+[ "$?" == "0" ] || ts_die "Cannot found $MOUNTPOINT in /etc/mtab" $DEVICE
 
 # remount
 $TS_CMD_MOUNT -o remount,ro $MOUNTPOINT
+[ "$?" == "0" ] || ts_die "Cannot remount $MOUNTPOINT" $DEVICE
 
 # check the remount
 egrep -q "^$DEVICE $MOUNTPOINT ext2 ro 0 0" /etc/mtab
+[ "$?" == "0" ] || ts_die "Cannot found remounted $MOUNTPOINT in /etc/mtab" $DEVICE
 
-if [ "$?" != "0" ]; then
-        echo "Cannot found remounted $MOUNTPOINT in /etc/mtab" >> $TS_OUTPUT
-        ts_device_deinit
-       ts_finalize
-fi
-
-ts_device_deinit
+ts_device_deinit $DEVICE
 
 echo "Success" >> $TS_OUTPUT
 ts_finalize
diff --git a/tests/ts-swapon-label b/tests/ts-swapon-label
new file mode 100755 (executable)
index 0000000..2f8aef1
--- /dev/null
@@ -0,0 +1,53 @@
+#!/bin/bash
+
+# 
+# Copyright (C) 2007 Karel Zak <kzak@redhat.com>
+# 
+# This file is part of util-linux-ng.
+# 
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License.
+# 
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+
+. commands.sh
+. functions.sh
+
+TS_COMPONENT="swapon"
+TS_DESC="label"
+
+LABEL="testSwapLabel"
+
+ts_init
+ts_skip_nonroot
+
+set -o pipefail
+
+DEVICE=$(ts_device_init)
+[ "$?" == 0 ] || ts_die "Cannot init device"
+
+$TS_CMD_MKSWAP -L $LABEL $DEVICE > /dev/null 2>> $TS_OUTPUT
+[ "$?" == "0" ] || ts_die "Cannot make swap on $DEVICE" $DEVICE
+
+ts_device_has "LABEL" $LABEL $DEVICE
+[ "$?" == "0" ] || ts_die "Cannot found LABEL '$LABEL' on $DEVICE" $DEVICE
+
+ts_udev_dev_support "by-label" $LABEL
+[ "$?" == "0" ] || ts_skip "udev ignores /dev/loop*" $DEVICE
+
+$TS_CMD_SWAPON -L $LABEL 2>&1 >> $TS_OUTPUT
+
+grep -q $DEVICE /proc/swaps
+[ "$?" == "0" ] || ts_die "Cannot found $DEVICE in /proc/swaps" $DEVICE
+
+ts_swapoff $DEVICE
+ts_device_deinit $DEVICE
+
+echo "Success" >> $TS_OUTPUT
+ts_finalize
+