]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: re-introduce bash 3 compatibility (OSX)
authorRuediger Meier <ruediger.meier@ga-group.nl>
Thu, 8 Mar 2018 10:45:30 +0000 (11:45 +0100)
committerRuediger Meier <ruediger.meier@ga-group.nl>
Thu, 8 Mar 2018 11:04:44 +0000 (12:04 +0100)
Bash 4 is now almost 10 years old and it seemed to be fine in 613a337e
to use associative arrays. Unfortunately OSX will probably never update
to 4 because of GPLv3. We don't want to lose our travis OSX build and
use plain arrays again.

BTW remove that "informative warnings" about unlocked resources. They
were only silent so far because of a bug. Any system where scsi_debug
is broken would print a lot of these warnings. This also tells us that
we could even stop calling ts_unlock() explicitly. Just exiting the
tests would be good enough.

Note that currently flock(1) is not available on our OSX build anyways.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
tests/functions.sh

index 376e78929f60bfe5f6d491273c8095c42ec9aff3..fc8f86377dc0445fbe306f3c23f0533fbac76ffa 100644 (file)
 #
 
 
-# Global array to remember all issued lock FDs. It does not seem possible to
-# declare this within a function (ts_init_env).
-declare -A TS_LOCKFILE_FD
-
-
 function ts_abspath {
        cd $1
        pwd
@@ -307,6 +302,7 @@ function ts_init_env {
        declare -a TS_SUID_USER
        declare -a TS_SUID_GROUP
        declare -a TS_LOOP_DEVS
+       declare -a TS_LOCKFILE_FD
 
        if [ -f $TS_TOPDIR/commands.sh ]; then
                . $TS_TOPDIR/commands.sh
@@ -512,12 +508,6 @@ function ts_cleanup_on_exit {
        done
        unset TS_LOOP_DEVS
 
-       # just informative warnings, currently not seen
-       for resource in "${!TS_LOCKFILE_FD[@]}"; do
-               test -n "${TS_LOCKFILE_FD["resource"]}" || continue
-               echo "[$$ $TS_TESTNAME] warning: found unlocked $resource"
-       done
-
        ts_scsi_debug_rmmod
 }
 
@@ -717,11 +707,24 @@ function ts_find_free_fd()
        return 1
 }
 
+function ts_get_lock_fd {
+       local resource=$1
+       local fd
+
+       for fd in "${!TS_LOCKFILE_FD[@]}"; do
+               if [ "${TS_LOCKFILE_FD["$fd"]}" = "$resource" ]; then
+                       echo "$fd"
+                       return 0
+               fi
+       done
+       return 1
+}
+
 function ts_have_lock {
        local resource=$1
 
        test "$TS_NOLOCKS" = "yes" && return 0
-       test -n "${TS_LOCKFILE_FD["$resource"]}" && return 0
+       ts_get_lock_fd "$resource" >/dev/null && return 0
        return 1
 }
 
@@ -735,7 +738,7 @@ function ts_lock {
        fi
 
        # Don't lock again
-       fd=${TS_LOCKFILE_FD["$resource"]}
+       fd=$(ts_get_lock_fd "$resource")
        if [ -n "$fd" ]; then
                echo "[$$ $TS_TESTNAME] ${resource} already locked!"
                return 0
@@ -745,8 +748,8 @@ function ts_lock {
 
        eval "exec $fd>$lockfile"
        flock --exclusive "$fd" || ts_skip "failed to lock $resource"
-       TS_LOCKFILE_FD["$resource"]="$fd"
 
+       TS_LOCKFILE_FD["$fd"]="$resource"
        ###echo "[$$ $TS_TESTNAME] Locked   $resource"
 }
 
@@ -759,13 +762,13 @@ function ts_unlock {
                return 0
        fi
 
-       fd=${TS_LOCKFILE_FD["$resource"]}
+       fd=$(ts_get_lock_fd "$resource")
        if [ -n "$fd" ]; then
                eval "exec $fd<&-"
-               TS_LOCKFILE_FD["$resource"]=""
+               TS_LOCKFILE_FD["$fd"]=""
                ###echo "[$$ $TS_TESTNAME] Unlocked $resource"
        else
-               echo "[$$ $TS_TESTNAME] ${resource} unlocking unlocked $resource!?"
+               echo "[$$ $TS_TESTNAME] unlocking unlocked $resource!?"
        fi
 }