]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: add lock on shared resources (e.g. scsi_debug)
authorKarel Zak <kzak@redhat.com>
Wed, 6 Dec 2017 13:21:38 +0000 (14:21 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 6 Dec 2017 13:21:38 +0000 (14:21 +0100)
The patch enables flock for scsi_debug to avoid collision between
tests. The patch also adds ./run.sh --nolocks to disable this feature.

Signed-off-by: Karel Zak <kzak@redhat.com>
tests/functions.sh
tests/run.sh

index 5b9b61c05c32b7c5e9122c35d1a43d74be362d72..0409b62835ae607e9b228b9e1bd42e716f4a3a0f 100644 (file)
@@ -271,6 +271,9 @@ function ts_init_env {
        TS_OUTDIR="$top_builddir/tests/output/$TS_COMPONENT"
        TS_DIFFDIR="$top_builddir/tests/diff/$TS_COMPONENT"
 
+       TS_NOLOCKS=$(ts_has_option "nolocks" "$*")
+       TS_LOCKDIR="$top_builddir/tests/output"
+
        ts_init_core_env
 
        TS_VERBOSE=$(ts_has_option "verbose" "$*")
@@ -673,11 +676,34 @@ function ts_fdisk_clean {
                $TS_OUTPUT
 }
 
+
+function ts_lock {
+       local resource="$1"
+       local lockfile="${TS_LOCKDIR}/${resource}.lock"
+
+       if [ "$TS_NOLOCKS" == "yes" ]; then
+               return 0
+       fi
+
+       # Don't lock if flock(1) is missing
+       type "flock" >/dev/null 2>&1 || return 1
+
+       eval "exec 200>$lockfile"
+       flock --exclusive --timeout 30 200 || ts_skip "failed to lock $resource"
+}
+
+# Note that flock(2) lock is released on FD close.
+function ts_unlock {
+       200<&-
+}
+
 function ts_scsi_debug_init {
        local devname
        local t
        TS_DEVICE="none"
 
+       ts_lock "scsi_debug"
+
        # dry run is not really reliable, real modprobe may still fail
        modprobe --dry-run --quiet scsi_debug &>/dev/null \
                || ts_skip "missing scsi_debug module (dry-run)"
@@ -746,6 +772,7 @@ function ts_scsi_debug_rmmod {
 
        # TODO unset TS_DEVICE, check that nobody uses it later, e.g. ts_fdisk_clean
 
+       ts_unlock "scsi_debug"
        return 0
 }
 
index 15e6c6baa80a0ef4d6725c1d2d62759f490c5ab2..b32463e615a23a29f636bc741fd60b32af1afd9e 100755 (executable)
@@ -47,6 +47,7 @@ while [ -n "$1" ]; do
        --fake |\
        --memcheck-valgrind |\
        --memcheck-asan |\
+       --nolocks |\
        --show-diff |\
        --verbose  |\
        --skip-loopdevs |\
@@ -84,6 +85,7 @@ while [ -n "$1" ]; do
                echo "  --fake               do not run, setup tests only"
                echo "  --memcheck-valgrind  run with valgrind"
                echo "  --memcheck-asan      enable ASAN (requires ./configure --enable-asan)"
+               echo "  --nolocks            don't use flock to lock resources"
                echo "  --verbose            verbose mode"
                echo "  --show-diff          show diff from failed tests"
                echo "  --nonroot            ignore test suite if user is root"