]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: stop caching loopdev
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 20 Mar 2020 19:17:11 +0000 (20:17 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 28 Mar 2020 10:51:29 +0000 (11:51 +0100)
It is more trouble than it is worth. The setup is of a loopback device
is very quick, so it's better to always create it when needed and
immediately drop afterwards.

test/TEST-02-CRYPTSETUP/test.sh
test/TEST-24-UNIT-TESTS/test.sh
test/test-functions

index cdaed13047602b3e4fb4dbb58dc4e359f69ad7d3..157a70021b486966f80efe0c06c16ea6da28dbac 100755 (executable)
@@ -8,16 +8,15 @@ TEST_NO_NSPAWN=1
 
 check_result_qemu() {
     ret=1
-    mkdir -p $initdir
-    mount ${LOOPDEV}p1 $initdir
+    mount_initdir
     [[ -e $initdir/testok ]] && ret=0
     [[ -f $initdir/failed ]] && cp -a $initdir/failed $TESTDIR
     cryptsetup luksOpen ${LOOPDEV}p2 varcrypt <$TESTDIR/keyfile
     mount /dev/mapper/varcrypt $initdir/var
     cp -a $initdir/var/log/journal $TESTDIR
     rm -r $initdir/var/log/journal/*
-    umount $initdir/var
-    umount $initdir
+    _umount_dir $initdir/var
+    _umount_dir $initdir
     cryptsetup luksClose /dev/mapper/varcrypt
     [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
     ls -l $TESTDIR/journal/*/*.journal
index 2c70d42914fba5d30f9928c7aed02a60d8192aee..251ac9ef9be26f77d5185cccad6ca59a62d80017 100755 (executable)
@@ -24,7 +24,7 @@ check_result_nspawn() {
     fi
     cp -a $1/var/log/journal $TESTDIR
     rm -r $1/var/log/journal/*
-    umount_initdir
+    _umount_dir $initdir
     [[ -n "$TIMED_OUT" ]] && _ret=$(($_ret+1))
     return $_ret
 }
@@ -49,7 +49,7 @@ check_result_qemu() {
     fi
     cp -a $initdir/var/log/journal $TESTDIR
     rm -r $initdir/var/log/journal/*
-    umount_initdir
+    _umount_dir $initdir
     [[ -n "$TIMED_OUT" ]] && _ret=$(($_ret+1))
     return $_ret
 }
index 732e25a8b27044e5ddb073fc1c88ace4b1bddfcb..73213c3aa7a8099d6b128061e1c4c9e9dc927ff5 100644 (file)
@@ -18,6 +18,7 @@ EFI_MOUNT="${EFI_MOUNT:-$(bootctl -x 2>/dev/null || echo /boot)}"
 QEMU_MEM="${QEMU_MEM:-512M}"
 IMAGE_NAME=${IMAGE_NAME:-default}
 TEST_REQUIRE_INSTALL_TESTS="${TEST_REQUIRE_INSTALL_TESTS:-1}"
+LOOPDEV=
 
 # Decide if we can (and want to) run QEMU with KVM acceleration.
 # Check if nested KVM is explicitly enabled (TEST_NESTED_KVM). If not,
@@ -284,7 +285,7 @@ run_qemu() {
     find_qemu_bin || return 1
 
     # Umount initdir to avoid concurrent access to the filesystem
-    umount_initdir
+    _umount_dir $initdir
 
     local _cgroup_args
     if [[ "$UNIFIED_CGROUP_HIERARCHY" = "yes" ]]; then
@@ -667,6 +668,15 @@ install_missing_libraries() {
     done
 }
 
+cleanup_loopdev() {
+    if [ -n "${LOOPDEV}" ]; then
+        ddebug "losetup -d $LOOPDEV"
+        losetup -d "${LOOPDEV}"
+    fi
+}
+
+trap cleanup_loopdev EXIT
+
 create_empty_image() {
     if [ -z "$IMAGE_NAME" ]; then
         echo "create_empty_image: \$IMAGE_NAME not set"
@@ -689,7 +699,6 @@ create_empty_image() {
 
     LOOPDEV=$(losetup --show -P -f "$public")
     [ -b "$LOOPDEV" ] || return 1
-    echo "LOOPDEV=$LOOPDEV" >>$STATEFILE
     sfdisk "$LOOPDEV" <<EOF
 ,$((_size-50))M
 ,
@@ -700,7 +709,8 @@ EOF
     local _label="-L systemd.${name}"
     # mkfs.reiserfs doesn't know -L. so, use --label instead
     [[ "$FSTYPE" == "reiserfs" ]] && _label="--label systemd.${name}"
-    if ! mkfs -t "${FSTYPE}" ${_label} "${LOOPDEV}p1" -q; then
+    mkfs -t "${FSTYPE}" ${_label} "${LOOPDEV}p1" -q; ret=$?
+    if [ $ret -ne 0 ] ; then
         dfatal "Failed to mkfs -t ${FSTYPE}"
         exit 1
     fi
@@ -711,22 +721,15 @@ mount_initdir() {
         image="${IMAGESTATEDIR}/${IMAGE_NAME}.img"
         LOOPDEV=$(losetup --show -P -f "$image")
         [ -b "$LOOPDEV" ] || return 1
-        echo "LOOPDEV=$LOOPDEV" >>$STATEFILE
-    fi
 
-    mkdir -p $initdir
-    mount ${LOOPDEV}p1 $initdir
-    TEST_SETUP_CLEANUP_ROOTDIR=1
-}
+        udevadm settle
+    fi
 
-umount_initdir() {
-    _umount_dir $initdir
-    if [[ $LOOPDEV && -b $LOOPDEV ]]; then
-        ddebug "losetup -d $LOOPDEV"
-        losetup -d $LOOPDEV
+    if ! mountpoint -q $initdir; then
+        mkdir -p $initdir
+        mount ${LOOPDEV}p1 $initdir
+        TEST_SETUP_CLEANUP_ROOTDIR=1
     fi
-    LOOPDEV=
-    sed -i /LOOPDEV=/d $STATEFILE
 }
 
 cleanup_initdir() {
@@ -800,7 +803,7 @@ check_result_nspawn() {
     test -s $TESTDIR/failed && ret=$(($ret+1))
     [ -n "$TIMED_OUT" ] && ret=$(($ret+1))
     check_asan_reports "$1" || ret=$(($ret+1))
-    umount_initdir
+    _umount_dir $initdir
     return $ret
 }
 
@@ -813,7 +816,7 @@ check_result_qemu() {
     cp -a $initdir/var/log/journal $TESTDIR
     rm -r $initdir/var/log/journal/*
     check_asan_reports "$initdir" || ret=$(($ret+1))
-    umount $initdir
+    _umount_dir $initdir
     [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
     ls -l $TESTDIR/journal/*/*.journal
     test -s $TESTDIR/failed && ret=$(($ret+1))
@@ -1164,7 +1167,10 @@ inst_libs() {
 }
 
 import_testdir() {
+    # make sure we don't get a stale LOOPDEV value from old times
+    __LOOPDEV=$LOOPDEV
     [[ -e $STATEFILE ]] && . $STATEFILE
+    LOOPDEV=$__LOOPDEV
     if [[ ! -d "$TESTDIR" ]]; then
         if [[ -z "$TESTDIR" ]]; then
             TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
@@ -1174,7 +1180,6 @@ import_testdir() {
 
         cat >$STATEFILE<<EOF
 TESTDIR="$TESTDIR"
-LOOPDEV="$LOOPDEV"
 EOF
         export TESTDIR
     fi
@@ -1926,7 +1931,7 @@ _test_cleanup() {
     # (post-test) cleanup should always ignore failure and cleanup as much as possible
     (
         set +e
-        umount_initdir
+        _umount_dir $initdir
         rm -vf "${IMAGESTATEDIR}/${IMAGE_NAME}.img"
         rm -vfr "$TESTDIR"
         rm -vf "$STATEFILE"
@@ -1941,7 +1946,7 @@ test_cleanup() {
 test_cleanup_again() {
     [ -n "$TESTDIR" ] || return
     rm -rf "$TESTDIR/unprivileged-nspawn-root"
-    umount_initdir
+    _umount_dir $initdir
 }
 
 test_create_image() {