aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2016-10-31 14:30:02 +0100
committerKarel Zak <kzak@redhat.com>2016-10-31 14:30:02 +0100
commit10146e56b0051f723320c16d0e0f9d94ba0a8803 (patch)
treeafb734e236975888d42c2595d75a70542e9f589f
parent93fea49f2834a634babc8c7f86c01edea123ed7a (diff)
downloadutil-linux-10146e56b0051f723320c16d0e0f9d94ba0a8803.tar.gz
tests: add flock tests
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r--tests/commands.sh1
-rw-r--r--tests/expected/misc/flock3
-rw-r--r--tests/expected/misc/flock-exclusive1
-rw-r--r--tests/expected/misc/flock-no-fork1
-rw-r--r--tests/expected/misc/flock-non-block1
-rw-r--r--tests/expected/misc/flock-shared2
-rw-r--r--tests/expected/misc/flock-time-check1
-rw-r--r--tests/expected/misc/flock-timeout2
-rwxr-xr-xtests/ts/misc/flock95
9 files changed, 107 insertions, 0 deletions
diff --git a/tests/commands.sh b/tests/commands.sh
index e0a9b0d138..ac4a490cf8 100644
--- a/tests/commands.sh
+++ b/tests/commands.sh
@@ -48,6 +48,7 @@ TS_CMD_COLUMN=${TS_CMD_COLUMN:-"$top_builddir/column"}
TS_CMD_EJECT=${TS_CMD_EJECT-"$top_builddir/eject"}
TS_CMD_FALLOCATE=${TS_CMD_FALLOCATE-"$top_builddir/fallocate"}
TS_CMD_FDISK=${TS_CMD_FDISK-"$top_builddir/fdisk"}
+TS_CMD_FLOCK=${TS_CMD_FLOCK-"$top_builddir/flock"}
TS_CMD_SFDISK=${TS_CMD_SFDISK-"$top_builddir/sfdisk"}
TS_CMD_FINDMNT=${TS_CMD_FINDMNT-"$top_builddir/findmnt"}
TS_CMD_FSCKCRAMFS=${TS_CMD_FSCKCRAMFS:-"$top_builddir/fsck.cramfs"}
diff --git a/tests/expected/misc/flock b/tests/expected/misc/flock
new file mode 100644
index 0000000000..9c973962ac
--- /dev/null
+++ b/tests/expected/misc/flock
@@ -0,0 +1,3 @@
+Locking
+Unlocking
+Unlocked
diff --git a/tests/expected/misc/flock-exclusive b/tests/expected/misc/flock-exclusive
new file mode 100644
index 0000000000..35821117c8
--- /dev/null
+++ b/tests/expected/misc/flock-exclusive
@@ -0,0 +1 @@
+Success
diff --git a/tests/expected/misc/flock-no-fork b/tests/expected/misc/flock-no-fork
new file mode 100644
index 0000000000..35821117c8
--- /dev/null
+++ b/tests/expected/misc/flock-no-fork
@@ -0,0 +1 @@
+Success
diff --git a/tests/expected/misc/flock-non-block b/tests/expected/misc/flock-non-block
new file mode 100644
index 0000000000..35821117c8
--- /dev/null
+++ b/tests/expected/misc/flock-non-block
@@ -0,0 +1 @@
+Success
diff --git a/tests/expected/misc/flock-shared b/tests/expected/misc/flock-shared
new file mode 100644
index 0000000000..011f3259cb
--- /dev/null
+++ b/tests/expected/misc/flock-shared
@@ -0,0 +1,2 @@
+Have shared lock
+Success
diff --git a/tests/expected/misc/flock-time-check b/tests/expected/misc/flock-time-check
new file mode 100644
index 0000000000..2e9ba477f8
--- /dev/null
+++ b/tests/expected/misc/flock-time-check
@@ -0,0 +1 @@
+success
diff --git a/tests/expected/misc/flock-timeout b/tests/expected/misc/flock-timeout
new file mode 100644
index 0000000000..b4cd5a4cf8
--- /dev/null
+++ b/tests/expected/misc/flock-timeout
@@ -0,0 +1,2 @@
+After timeout.
+Success
diff --git a/tests/ts/misc/flock b/tests/ts/misc/flock
new file mode 100755
index 0000000000..b2fc772427
--- /dev/null
+++ b/tests/ts/misc/flock
@@ -0,0 +1,95 @@
+#!/bin/bash
+
+# This file is part of util-linux.
+#
+# 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, or
+# (at your option) any later version.
+#
+# 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.
+
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="flock"
+
+. $TS_TOPDIR/functions.sh
+ts_init "$*"
+
+ts_check_test_command "$TS_CMD_FLOCK"
+
+
+function do_lock {
+ local opts="$1"
+ local expected_rc="$2"
+ local mesg="$3"
+
+ $TS_CMD_FLOCK $1 $TS_OUTDIR/lockfile \
+ echo "$mesg" \
+ > $TS_OUTPUT 2>&1
+
+ local rc="$?"
+
+ if [ "$rc" == "$expected_rc" ]; then
+ ts_log "Success"
+ else
+ ts_log "Failed [rc=$rc]"
+ fi
+}
+
+# general lock
+GEN_OUTPUT="$TS_OUTPUT"
+START=$(date '+%s')
+$TS_CMD_FLOCK --shared $TS_OUTDIR/lockfile \
+ bash -c 'echo "Locking"; sleep 3; echo "Unlocking"' \
+ > $GEN_OUTPUT 2>&1 &
+
+
+ts_init_subtest "non-block"
+do_lock "--nonblock --conflict-exit-code 123" 123 "You will never see this!"
+ts_finalize_subtest
+
+
+ts_init_subtest "no-fork"
+do_lock "--no-fork --nonblock --conflict-exit-code 123" 123 "You will never see this!"
+ts_finalize_subtest
+
+
+ts_init_subtest "shared"
+do_lock "--shared" 0 "Have shared lock"
+ts_finalize_subtest
+
+
+# this is the same as non-block test (exclusive lock is the default), but here
+# we explicitly specify --exclusive on command line
+ts_init_subtest "exclusive"
+do_lock "--nonblock --exclusive --conflict-exit-code 123" 123 "You will never see this!"
+ts_finalize_subtest
+
+
+ts_init_subtest "timeout"
+do_lock "--timeout 5 --conflict-exit-code 5" 0 "After timeout."
+END=$(date '+%s')
+ts_finalize_subtest
+
+
+# expected is 3 seconds (see "sleep 3" for the general lock), but we should not
+# rely on exact number due to scheduler, machine load, etc. Let's check for
+# inmterval <3,5>.
+#
+ts_init_subtest "time-check"
+TIMEDIFF=$(( $END - $START ))
+if [ $TIMEDIFF -lt 3 ]; then
+ ts_log "general lock failed [$TIMEDIFF sec]"
+elif [ $TIMEDIFF -gt 5 ]; then
+ ts_log "wait too long [$TIMEDIFF sec]"
+else
+ ts_log "success"
+fi
+ts_finalize_subtest "diff ${TIMEDIFF} sec"
+
+
+echo "Unlocked" >> $GEN_OUTPUT
+ts_finalize