From 3dfa278ea5462fceec0c6dcae7211f70eaf1fbb3 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 18 May 2007 13:49:15 +0200 Subject: [PATCH] tests: simplify devices usage The patch simplifies devices usage in the tests. The patch also improves errors checking and reporting. Signed-off-by: Karel Zak --- tests/expected/ts-mkswap-doit | 2 - tests/expected/ts-swapon-label | 1 + tests/functions.sh | 59 ++++++++++++++++++++------ tests/ts-cramfs-mkfs | 24 +++-------- tests/ts-mkswap-doit | 76 ---------------------------------- tests/ts-mount-remount | 26 ++++-------- tests/ts-swapon-label | 53 ++++++++++++++++++++++++ 7 files changed, 116 insertions(+), 125 deletions(-) delete mode 100644 tests/expected/ts-mkswap-doit create mode 100644 tests/expected/ts-swapon-label delete mode 100755 tests/ts-mkswap-doit create mode 100755 tests/ts-swapon-label diff --git a/tests/expected/ts-mkswap-doit b/tests/expected/ts-mkswap-doit deleted file mode 100644 index d29585c3..00000000 --- a/tests/expected/ts-mkswap-doit +++ /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 index 00000000..35821117 --- /dev/null +++ b/tests/expected/ts-swapon-label @@ -0,0 +1 @@ +Success diff --git a/tests/functions.sh b/tests/functions.sh index 7cb37430..001c39f9 100644 --- a/tests/functions.sh +++ b/tests/functions.sh @@ -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 +} diff --git a/tests/ts-cramfs-mkfs b/tests/ts-cramfs-mkfs index 715f575c..b2c43e62 100755 --- a/tests/ts-cramfs-mkfs +++ b/tests/ts-cramfs-mkfs @@ -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 index 600acbac..00000000 --- a/tests/ts-mkswap-doit +++ /dev/null @@ -1,76 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2007 Karel Zak -# -# 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 - diff --git a/tests/ts-mount-remount b/tests/ts-mount-remount index 13a9c6d8..dcaf2888 100755 --- a/tests/ts-mount-remount +++ b/tests/ts-mount-remount @@ -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 index 00000000..2f8aef10 --- /dev/null +++ b/tests/ts-swapon-label @@ -0,0 +1,53 @@ +#!/bin/bash + +# +# Copyright (C) 2007 Karel Zak +# +# 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 + -- 2.39.5