diff options
author | Karel Zak | 2017-12-06 14:21:38 +0100 |
---|---|---|
committer | Karel Zak | 2017-12-06 14:21:38 +0100 |
commit | 9deb8c7dfd9b119884543416e4c681cbe21d97b2 (patch) | |
tree | 8d442b0494fbd0faeadd4feb59367d32ab7d34b9 /tests/functions.sh | |
parent | setarch: minor code clean up (diff) | |
download | kernel-qcow2-util-linux-9deb8c7dfd9b119884543416e4c681cbe21d97b2.tar.gz kernel-qcow2-util-linux-9deb8c7dfd9b119884543416e4c681cbe21d97b2.tar.xz kernel-qcow2-util-linux-9deb8c7dfd9b119884543416e4c681cbe21d97b2.zip |
tests: add lock on shared resources (e.g. scsi_debug)
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>
Diffstat (limited to 'tests/functions.sh')
-rw-r--r-- | tests/functions.sh | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/functions.sh b/tests/functions.sh index 5b9b61c05..0409b6283 100644 --- a/tests/functions.sh +++ b/tests/functions.sh @@ -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 } |