[Libguestfs] [PATCH v2 nbdkit 5/5] tests: Add a helper function which waits for nbdkit to start up.

Richard W.M. Jones rjones at redhat.com
Thu Sep 13 16:09:27 UTC 2018


This assumes bashisms, but bash is required to run the tests.

This is mostly refactoring.  However the changes (simplifications) are
quite substantial:

 - Since the new start_nbdkit helper function cleans up nbdkit on
   exit, most scripts no longer need to deal with the pid or kill the
   pid in the cleanup function.

 - As a result, cleanup functions are radically simpler, and often
   disappear completely.

 - Also removed the comment "# The cleanup() function is called
   implicitly on exit" passim partly because the cleanup functions
   have mostly been removed and partly because it should be clear from
   looking at functions.sh.

There is one additional change: In the test-memory*.sh tests, nbdkit
used to run in the foreground, but that seems to be a consequence of
some left over debugging.  I changed it so they work like the other
tests.
---
 tests/functions.sh.in                  | 33 ++++++++++++++++
 tests/test-blocksize.sh                | 30 ++-------------
 tests/test-cache.sh                    | 27 +------------
 tests/test-cow.sh                      | 27 +------------
 tests/test-data-7E.sh                  | 27 +------------
 tests/test-data-base64.sh              | 27 +------------
 tests/test-data-raw.sh                 | 27 +------------
 tests/test-fua.sh                      | 52 +++++++-------------------
 tests/test-ip.sh                       | 23 ++----------
 tests/test-log.sh                      | 23 ++----------
 tests/test-memory-largest-for-qemu.sh  | 30 ++-------------
 tests/test-memory-largest.sh           | 31 ++-------------
 tests/test-nozero.sh                   | 48 ++++--------------------
 tests/test-offset2.sh                  | 27 +------------
 tests/test-parallel-nbd.sh             | 19 ++--------
 tests/test-pattern-largest-for-qemu.sh | 27 +------------
 tests/test-pattern-largest.sh          | 27 +------------
 tests/test-pattern.sh                  | 27 +------------
 tests/test-start.sh                    | 17 +--------
 tests/test-tls-psk.sh                  | 26 ++-----------
 tests/test-tls.sh                      | 25 +------------
 tests/test-truncate1.sh                | 27 +------------
 tests/test-truncate2.sh                | 27 +------------
 tests/test-zero.sh                     |  3 --
 24 files changed, 104 insertions(+), 553 deletions(-)

diff --git a/tests/functions.sh.in b/tests/functions.sh.in
index 42e3925..5c1f34c 100644
--- a/tests/functions.sh.in
+++ b/tests/functions.sh.in
@@ -57,6 +57,39 @@ _run_cleanup_hooks ()
 }
 trap _run_cleanup_hooks INT QUIT TERM EXIT ERR
 
+# start_nbdkit -P pidfile args...
+#
+# Run nbdkit with args and wait for it to start up.  If it fails to
+# start up, exit with an error message.  Also a cleanup handler is
+# installed automatically which kills nbdkit on exit.
+start_nbdkit ()
+{
+    # -P <pidfile> must be the first two parameters.
+    if [ "$1" != "-P" ]; then
+       echo "$0: start_nbdkit: -P <pidfile> option must be first"
+       exit 1
+    fi
+    pidfile="$2"
+
+    # Run nbdkit.
+    nbdkit "$@"
+
+    # Wait for the pidfile to appear.
+    for i in {1..10}; do
+        if test -f "$pidfile"; then
+            break
+        fi
+        sleep 1
+    done
+    if ! test -f "$pidfile"; then
+        echo "$0: start_nbdkit: PID file $pidfile was not created"
+        exit 1
+    fi
+
+    # Kill nbdkit on exit.
+    cleanup_fn kill "$(cat $pidfile)"
+}
+
 # foreach_plugin f
 #
 # For each plugin that was built, call test function f with the plugin
diff --git a/tests/test-blocksize.sh b/tests/test-blocksize.sh
index 21b55b9..aa3cc13 100755
--- a/tests/test-blocksize.sh
+++ b/tests/test-blocksize.sh
@@ -50,14 +50,10 @@ if ! qemu-io -f raw -c 'r 0 1' -c 'w -z 1000 2000' \
 fi
 truncate -s 10M blocksize2.img
 
-pid1= pid2=
-
-# Kill any nbdkit processes on exit.
+# For easier debugging, dump the final log files before removing them
+# on exit.
 cleanup ()
 {
-    test "$pid1" && kill $pid1
-    test "$pid2" && kill $pid2
-    # For easier debugging, dump the final log files before removing them.
     echo "Log 1 file contents:"
     cat blocksize1.log || :
     echo "Log 2 file contents:"
@@ -67,28 +63,12 @@ cleanup ()
 cleanup_fn cleanup
 
 # Run two parallel nbdkit; to compare the logs and see what changes.
-nbdkit -P blocksize1.pid -U blocksize1.sock \
+start_nbdkit -P blocksize1.pid -U blocksize1.sock \
        --filter=log file logfile=blocksize1.log blocksize1.img
-nbdkit -P blocksize2.pid -U blocksize2.sock --filter=blocksize \
+start_nbdkit -P blocksize2.pid -U blocksize2.sock --filter=blocksize \
        --filter=log file logfile=blocksize2.log blocksize2.img \
        minblock=1024 maxdata=512k maxlen=1M
 
-# We may have to wait a short time for the pid files to appear.
-for i in `seq 1 10`; do
-    if test -f blocksize1.pid && test -f blocksize2.pid; then
-        break
-    fi
-    sleep 1
-done
-
-pid1="$(cat blocksize1.pid)" || :
-pid2="$(cat blocksize2.pid)" || :
-
-if ! test -f blocksize1.pid || ! test -f blocksize2.pid; then
-    echo "$0: PID files were not created"
-    exit 1
-fi
-
 # Test behavior on short accesses.
 qemu-io -f raw -c 'r 1 1' -c 'w 10001 1' -c 'w -z 20001 1' \
          -c 'discard 30001 1' 'nbd+unix://?socket=blocksize1.sock'
@@ -147,5 +127,3 @@ if grep 'count=0x[0-9a-f]*\([1235679abdef]00\|[0-9a-f]\(.[^0]\|[^0].\)\) ' \
     exit 1;
 fi
 diff -u blocksize1.img blocksize2.img
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-cache.sh b/tests/test-cache.sh
index 495469d..7b1fccb 100755
--- a/tests/test-cache.sh
+++ b/tests/test-cache.sh
@@ -37,34 +37,13 @@ set -x
 
 files="cache.img cache.sock cache.pid"
 rm -f $files
+cleanup_fn rm -f $files
 
 # Create an empty base image.
 truncate -s 1G cache.img
 
 # Run nbdkit with the caching filter.
-nbdkit -P cache.pid -U cache.sock --filter=cache file cache.img
-
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f cache.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f cache.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat cache.pid)"
-
-# Kill the nbdkit process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f $files
-}
-cleanup_fn cleanup
+start_nbdkit -P cache.pid -U cache.sock --filter=cache file cache.img
 
 # Open the overlay and perform some operations.
 guestfish --format=raw -a "nbd://?socket=$PWD/cache.sock" <<'EOF'
@@ -82,5 +61,3 @@ guestfish --ro -a cache.img -m /dev/sda1 <<'EOF'
   cat /hello
   cat /large | cat >/dev/null
 EOF
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-cow.sh b/tests/test-cow.sh
index b5c8f63..09fd16e 100755
--- a/tests/test-cow.sh
+++ b/tests/test-cow.sh
@@ -37,35 +37,14 @@ set -x
 
 files="cow-base.img cow-diff.qcow2 cow.sock cow.pid"
 rm -f $files
+cleanup_fn rm -f $files
 
 # Create a base image which is partitioned with an empty filesystem.
 guestfish -N cow-base.img=fs exit
 lastmod="$(stat -c "%y" cow-base.img)"
 
 # Run nbdkit with a COW overlay.
-nbdkit -P cow.pid -U cow.sock --filter=cow file cow-base.img
-
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f cow.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f cow.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat cow.pid)"
-
-# Kill the nbdkit process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f $files
-}
-cleanup_fn cleanup
+start_nbdkit -P cow.pid -U cow.sock --filter=cow file cow-base.img
 
 # Write some data into the overlay.
 guestfish --format=raw -a "nbd://?socket=$PWD/cow.sock" -m /dev/sda1 <<EOF
@@ -92,5 +71,3 @@ if qemu-img --version >/dev/null 2>&1; then
     # This checks the file we created exists.
     guestfish --ro -a cow-diff.qcow2 -m /dev/sda1 cat /hello
 fi
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-data-7E.sh b/tests/test-data-7E.sh
index adccd86..eae40de 100755
--- a/tests/test-data-7E.sh
+++ b/tests/test-data-7E.sh
@@ -40,6 +40,7 @@ set -x
 
 files="data-7E.out data-7E.pid data-7E.sock"
 rm -f $files
+cleanup_fn rm -f $files
 
 # Test that qemu-io works
 if ! qemu-io --help >/dev/null; then
@@ -48,7 +49,7 @@ if ! qemu-io --help >/dev/null; then
 fi
 
 # Run nbdkit.
-nbdkit -P data-7E.pid -U data-7E.sock \
+start_nbdkit -P data-7E.pid -U data-7E.sock \
        --filter=partition \
        data size=7E partition=1 \
        data="
@@ -88,28 +89,6 @@ nbdkit -P data-7E.pid -U data-7E.sock \
           0x80 0 0 0 0x80 0 0 0  0x79 0x8a 0xd0 0x7e 0 0 0 0
    "
 
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f data-7E.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f data-7E.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat data-7E.pid)"
-
-# Kill the nbdkit process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f $files
-}
-cleanup_fn cleanup
-
 # Since we're reading the empty first partition, any read returns zeroes.
 qemu-io -r -f raw 'nbd+unix://?socket=data-7E.sock' \
         -c 'r -v 498 16' | grep -E '^[[:xdigit:]]+:' > data-7E.out
@@ -119,5 +98,3 @@ then
     cat data-7E.out
     exit 1
 fi
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-data-base64.sh b/tests/test-data-base64.sh
index 72cf743..cc53edf 100755
--- a/tests/test-data-base64.sh
+++ b/tests/test-data-base64.sh
@@ -39,6 +39,7 @@ set -x
 
 files="data-base64.out data-base64.pid data-base64.sock"
 rm -f $files
+cleanup_fn rm -f $files
 
 # Test if the base64 parameter is supported in this build.
 if ! nbdkit data --dump-plugin | grep -sq "data_base64=yes"; then
@@ -53,31 +54,9 @@ if ! qemu-io --help >/dev/null; then
 fi
 
 # Run nbdkit.
-nbdkit -P data-base64.pid -U data-base64.sock \
+start_nbdkit -P data-base64.pid -U data-base64.sock \
        data base64=MTIz size=512
 
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f data-base64.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f data-base64.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat data-base64.pid)"
-
-# Kill the nbdkit process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f $files
-}
-cleanup_fn cleanup
-
 qemu-io -r -f raw 'nbd+unix://?socket=data-base64.sock' \
         -c 'r -v 0 512' | grep -E '^[[:xdigit:]]+:' > data-base64.out
 if [ "$(cat data-base64.out)" != "00000000:  31 32 33 00 00 00 00 00 00 00 00 00 00 00 00 00  123.............
@@ -117,5 +96,3 @@ then
     cat data-base64.out
     exit 1
 fi
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-data-raw.sh b/tests/test-data-raw.sh
index 00d4fb1..f4af2ec 100755
--- a/tests/test-data-raw.sh
+++ b/tests/test-data-raw.sh
@@ -39,6 +39,7 @@ set -x
 
 files="data-raw.out data-raw.pid data-raw.sock"
 rm -f $files
+cleanup_fn rm -f $files
 
 # Test that qemu-io works
 if ! qemu-io --help >/dev/null; then
@@ -47,31 +48,9 @@ if ! qemu-io --help >/dev/null; then
 fi
 
 # Run nbdkit.
-nbdkit -P data-raw.pid -U data-raw.sock \
+start_nbdkit -P data-raw.pid -U data-raw.sock \
        data raw=123 size=512
 
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f data-raw.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f data-raw.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat data-raw.pid)"
-
-# Kill the nbdkit process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f $files
-}
-cleanup_fn cleanup
-
 qemu-io -r -f raw 'nbd+unix://?socket=data-raw.sock' \
         -c 'r -v 0 512' | grep -E '^[[:xdigit:]]+:' > data-raw.out
 if [ "$(cat data-raw.out)" != "00000000:  31 32 33 00 00 00 00 00 00 00 00 00 00 00 00 00  123.............
@@ -111,5 +90,3 @@ then
     cat data-raw.out
     exit 1
 fi
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-fua.sh b/tests/test-fua.sh
index f7c4571..20ba16d 100755
--- a/tests/test-fua.sh
+++ b/tests/test-fua.sh
@@ -51,16 +51,10 @@ if ! qemu-io -f raw -t none -c flush -c 'w -f -z 0 64k' fua.img; then
     exit 77
 fi
 
-pid1= pid2= pid3= pid4=
-
-# Kill any nbdkit processes on exit.
+# For easier debugging, dump the final log files before removing them
+# on exit.
 cleanup ()
 {
-    test "$pid1" && kill $pid1
-    test "$pid2" && kill $pid2
-    test "$pid3" && kill $pid3
-    test "$pid4" && kill $pid4
-    # For easier debugging, dump the final log files before removing them.
     echo "Log 1 file contents:"
     cat fua1.log || :
     echo "Log 2 file contents:"
@@ -78,34 +72,18 @@ cleanup_fn cleanup
 # 2: fuamode=emulate: log shows that blocksize optimizes fua to flush
 # 3: fuamode=native: log shows that blocksize preserves fua
 # 4: fuamode=force: log shows that fua is always enabled
-nbdkit -P fua1.pid -U fua1.sock --filter=log --filter=fua \
-       file logfile=fua1.log fua.img
-nbdkit -P fua2.pid -U fua2.sock --filter=blocksize --filter=log --filter=fua \
-       file logfile=fua2.log fua.img fuamode=emulate maxdata=4k maxlen=4k
-nbdkit -P fua3.pid -U fua3.sock --filter=blocksize --filter=log --filter=fua \
-       file logfile=fua3.log fua.img fuamode=native maxdata=4k maxlen=4k
-nbdkit -P fua4.pid -U fua4.sock --filter=fua --filter=log \
-       file logfile=fua4.log fua.img fuamode=force
-
-# We may have to wait a short time for the pid files to appear.
-for i in `seq 1 10`; do
-    if test -f fua1.pid && test -f fua2.pid && test -f fua3.pid &&
-       test -f fua4.pid; then
-        break
-    fi
-    sleep 1
-done
-
-pid1="$(cat fua1.pid)" || :
-pid2="$(cat fua2.pid)" || :
-pid3="$(cat fua3.pid)" || :
-pid4="$(cat fua4.pid)" || :
-
-if ! test -f fua1.pid || ! test -f fua2.pid || ! test -f fua3.pid ||
-   ! test -f fua4.pid; then
-    echo "$0: PID files were not created"
-    exit 1
-fi
+start_nbdkit -P fua1.pid -U fua1.sock \
+             --filter=log --filter=fua \
+             file logfile=fua1.log fua.img
+start_nbdkit -P fua2.pid -U fua2.sock \
+             --filter=blocksize --filter=log --filter=fua \
+             file logfile=fua2.log fua.img fuamode=emulate maxdata=4k maxlen=4k
+start_nbdkit -P fua3.pid -U fua3.sock \
+             --filter=blocksize --filter=log --filter=fua \
+             file logfile=fua3.log fua.img fuamode=native maxdata=4k maxlen=4k
+start_nbdkit -P fua4.pid -U fua4.sock \
+             --filter=fua --filter=log \
+             file logfile=fua4.log fua.img fuamode=force
 
 # Perform a flush, write, and zero, first without then with FUA
 for f in '' -f; do
@@ -145,5 +123,3 @@ if grep 'Flush' fua4.log; then
     echo "filter should have elided flush"
     exit 1
 fi
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-ip.sh b/tests/test-ip.sh
index 12448c4..edf0b6a 100755
--- a/tests/test-ip.sh
+++ b/tests/test-ip.sh
@@ -34,10 +34,11 @@
 # Every other test uses a Unix domain socket.  This tests nbdkit over
 # IPv4 and IPv6 localhost connections.
 
-set -e
 source ./functions.sh
+set -e
 
 rm -f ip.pid ipv4.out ipv6.out
+cleanup_fn rm -f ip.pid ipv4.out ipv6.out
 
 # Don't fail if certain commands aren't available.
 if ! ss --version; then
@@ -65,20 +66,7 @@ echo picked unused port $port
 
 # By default nbdkit will listen on all available interfaces, ie.
 # IPv4 and IPv6.
-nbdkit -P ip.pid -p $port example1
-
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f ip.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f ip.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
+start_nbdkit -P ip.pid -p $port example1
 pid="$(cat ip.pid)"
 
 # Check the process exists.
@@ -99,8 +87,3 @@ if test -n "$ipv6_lo"; then
     cat ipv6.out
     grep -sq "^virtual size: 100M" ipv6.out
 fi
-
-# Kill the process.
-kill $pid
-rm ip.pid
-rm -f ipv4.out ipv6.out
diff --git a/tests/test-log.sh b/tests/test-log.sh
index 5ad5f22..f0eacb7 100755
--- a/tests/test-log.sh
+++ b/tests/test-log.sh
@@ -45,27 +45,12 @@ if ! qemu-io -f raw -c 'w 1M 2M' log.img; then
 fi
 
 # Run nbdkit with logging enabled to file.
-nbdkit -P log.pid -U log.sock --filter=log file log.img logfile=log.log
+start_nbdkit -P log.pid -U log.sock --filter=log file log.img logfile=log.log
 
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f log.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f log.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat log.pid)"
-
-# Kill the nbdkit process on exit.
+# For easier debugging, dump the final log files before removing them
+# on exit.
 cleanup ()
 {
-    kill $pid
-    # For easier debugging, dump the final log file before removing it.
     echo "Log file contents:"
     cat log.log
     rm -f $files
@@ -79,5 +64,3 @@ qemu-io -r -f raw -c 'r -P 11 2M 1M' 'nbd+unix://?socket=log.sock'
 # The log should show a write on connection 1, and read on connection 2.
 grep 'connection=1 Write id=1 offset=0x100000 count=0x200000 ' log.log
 grep 'connection=2 Read id=1 offset=0x200000 count=0x100000 ' log.log
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-memory-largest-for-qemu.sh b/tests/test-memory-largest-for-qemu.sh
index 65f69b6..2b56f30 100755
--- a/tests/test-memory-largest-for-qemu.sh
+++ b/tests/test-memory-largest-for-qemu.sh
@@ -39,6 +39,7 @@ set -e
 
 files="memory-largest-for-qemu.out memory-largest-for-qemu.pid memory-largest-for-qemu.sock"
 rm -f $files
+cleanup_fn rm -f $files
 
 # Test that qemu-io works
 if ! qemu-io --help >/dev/null; then
@@ -48,31 +49,8 @@ fi
 
 # Run nbdkit with memory plugin.
 # size = (2^63-1) & ~511 which is the largest supported by qemu.
-nbdkit -f -v -D memory.dir=1 \
-       -P memory-largest-for-qemu.pid -U memory-largest-for-qemu.sock \
-       memory size=9223372036854775296 &
-
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f memory-largest-for-qemu.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f memory-largest-for-qemu.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat memory-largest-for-qemu.pid)"
-
-# Kill the nbdkit process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f $files
-}
-cleanup_fn cleanup
+start_nbdkit -P memory-largest-for-qemu.pid -U memory-largest-for-qemu.sock \
+       memory size=9223372036854775296
 
 # Write some stuff to the beginning, middle and end.
 qemu-io -f raw 'nbd+unix://?socket=memory-largest-for-qemu.sock' \
@@ -240,5 +218,3 @@ then
     cat memory-largest-for-qemu.out
     exit 1
 fi
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-memory-largest.sh b/tests/test-memory-largest.sh
index d2cad1c..a6c269e 100755
--- a/tests/test-memory-largest.sh
+++ b/tests/test-memory-largest.sh
@@ -39,6 +39,7 @@ set -e
 
 files="memory-largest.out memory-largest.pid memory-largest.sock"
 rm -f $files
+cleanup_fn rm -f $files
 
 # Test that qemu-io works
 if ! qemu-io --help >/dev/null; then
@@ -48,32 +49,8 @@ fi
 
 # Run nbdkit with memory plugin.
 # size = 2^63-1
-nbdkit -f -v \
-       -D memory.dir=1 \
-       -P memory-largest.pid -U memory-largest.sock \
-       memory size=9223372036854775807 &
-
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f memory-largest.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f memory-largest.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat memory-largest.pid)"
-
-# Kill the nbdkit process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f $files
-}
-cleanup_fn cleanup
+start_nbdkit -P memory-largest.pid -U memory-largest.sock \
+       memory size=9223372036854775807
 
 # qemu cannot open this image!
 #
@@ -82,5 +59,3 @@ cleanup_fn cleanup
 # Therefore we skip the remainder of this test (in effect, testing
 # only that nbdkit can create the file).
 exit 77
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-nozero.sh b/tests/test-nozero.sh
index 239d4bd..c22363b 100755
--- a/tests/test-nozero.sh
+++ b/tests/test-nozero.sh
@@ -61,18 +61,10 @@ if test "$(stat -c %b nozero1.img)" = "$(stat -c %b nozero2.img)"; then
 fi
 cp nozero2.img nozero1.img
 
-pid1= pid2= pid3= pid4= pid5a= pid5b=
-
-# Kill any nbdkit processes on exit.
+# For easier debugging, dump the final log files before removing them
+# on exit.
 cleanup ()
 {
-    test "$pid1" && kill $pid1
-    test "$pid2" && kill $pid2
-    test "$pid3" && kill $pid3
-    test "$pid4" && kill $pid4
-    test "$pid5a" && kill $pid5a
-    test "$pid5b" && kill $pid5b
-    # For easier debugging, dump the final log files before removing them.
     echo "Log 1 file contents:"
     cat nozero1.log || :
     echo "Log 2 file contents:"
@@ -96,41 +88,19 @@ cleanup_fn cleanup
 # 4: log after filter with zeromode=emulate, to ensure no ZERO to plugin
 # 5a/b: both sides of nbd plugin: even though server side does not advertise
 # ZERO, the client side still exposes it, and just skips calling nbd's .zero
-nbdkit -P nozero1.pid -U nozero1.sock --filter=log \
+start_nbdkit -P nozero1.pid -U nozero1.sock --filter=log \
        file logfile=nozero1.log nozero1.img
-nbdkit -P nozero2.pid -U nozero2.sock --filter=log --filter=nozero \
+start_nbdkit -P nozero2.pid -U nozero2.sock --filter=log --filter=nozero \
        file logfile=nozero2.log nozero2.img
-nbdkit -P nozero3.pid -U nozero3.sock --filter=log --filter=nozero \
+start_nbdkit -P nozero3.pid -U nozero3.sock --filter=log --filter=nozero \
        file logfile=nozero3.log nozero3.img zeromode=emulate
-nbdkit -P nozero4.pid -U nozero4.sock --filter=nozero --filter=log \
+start_nbdkit -P nozero4.pid -U nozero4.sock --filter=nozero --filter=log \
        file logfile=nozero4.log nozero4.img zeromode=emulate
-nbdkit -P nozero5a.pid -U nozero5a.sock --filter=log --filter=nozero \
+start_nbdkit -P nozero5a.pid -U nozero5a.sock --filter=log --filter=nozero \
        file logfile=nozero5a.log nozero5.img
-nbdkit -P nozero5b.pid -U nozero5b.sock --filter=log \
+start_nbdkit -P nozero5b.pid -U nozero5b.sock --filter=log \
        nbd logfile=nozero5b.log socket=nozero5a.sock
 
-# We may have to wait a short time for the pid files to appear.
-for i in `seq 1 10`; do
-    if test -f nozero1.pid && test -f nozero2.pid && test -f nozero3.pid &&
-       test -f nozero4.pid && test -f nozero5a.pid && test -f nozero5b.pid; then
-        break
-    fi
-    sleep 1
-done
-
-pid1="$(cat nozero1.pid)" || :
-pid2="$(cat nozero2.pid)" || :
-pid3="$(cat nozero3.pid)" || :
-pid4="$(cat nozero4.pid)" || :
-pid5a="$(cat nozero5a.pid)" || :
-pid5b="$(cat nozero5b.pid)" || :
-
-if ! test -f nozero1.pid || ! test -f nozero2.pid || ! test -f nozero3.pid ||
-   ! test -f nozero4.pid || ! test -f nozero5a.pid || ! test -f nozero5b.pid; then
-    echo "$0: PID files were not created"
-    exit 1
-fi
-
 # Perform the zero write.
 qemu-io -f raw -c 'w -z -u 0 1M' 'nbd+unix://?socket=nozero1.sock'
 qemu-io -f raw -c 'w -z -u 0 1M' 'nbd+unix://?socket=nozero2.sock'
@@ -160,5 +130,3 @@ cmp nozero1.img nozero2.img
 cmp nozero2.img nozero3.img
 cmp nozero3.img nozero4.img
 cmp nozero4.img nozero5.img
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-offset2.sh b/tests/test-offset2.sh
index 2376301..4e1ef2b 100755
--- a/tests/test-offset2.sh
+++ b/tests/test-offset2.sh
@@ -39,6 +39,7 @@ set -x
 
 files="offset2.out offset2.pid offset2.sock"
 rm -f $files
+cleanup_fn rm -f $files
 
 # Test that qemu-io works
 if ! qemu-io --help >/dev/null; then
@@ -48,33 +49,11 @@ fi
 
 # Run nbdkit with pattern plugin and offset filter in front.
 # 8070450532247927809 = 7E - 1023
-nbdkit -P offset2.pid -U offset2.sock \
+start_nbdkit -P offset2.pid -U offset2.sock \
        --filter=offset \
        pattern size=7E \
        offset=8070450532247927809 range=512
 
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f offset2.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f offset2.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat offset2.pid)"
-
-# Kill the nbdkit process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f $files
-}
-cleanup_fn cleanup
-
 qemu-io -r -f raw 'nbd+unix://?socket=offset2.sock' \
         -c 'r -v 0 512' | grep -E '^[[:xdigit:]]+:' > offset2.out
 if [ "$(cat offset2.out)" != "00000000:  ff ff ff ff ff fc 00 6f ff ff ff ff ff fc 08 6f  .......o.......o
@@ -114,5 +93,3 @@ then
     cat offset2.out
     exit 1
 fi
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-parallel-nbd.sh b/tests/test-parallel-nbd.sh
index 7aa8a9f..e4a2352 100755
--- a/tests/test-parallel-nbd.sh
+++ b/tests/test-parallel-nbd.sh
@@ -63,21 +63,10 @@ qemu-io -f raw -c "aio_write -P 1 0 512" -c "aio_write -P 2 512 512" \
 # may have spurious failures under heavy loads on the test machine, where
 # tuning the delays may help.
 
-nbdkit --exit-with-parent -v \
-    -U test-parallel-nbd.sock -P test-parallel-nbd.pid \
-    --filter=delay \
-    file test-parallel-nbd.data wdelay=2 rdelay=1 &
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f test-parallel-nbd.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f test-parallel-nbd.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
+start_nbdkit -P test-parallel-nbd.pid \
+             -U test-parallel-nbd.sock \
+             --filter=delay \
+             file test-parallel-nbd.data wdelay=2 rdelay=1
 
 # With --threads=1, the write should complete first because it was issued first
 nbdkit -v -t 1 -U - nbd socket=test-parallel-nbd.sock --run '
diff --git a/tests/test-pattern-largest-for-qemu.sh b/tests/test-pattern-largest-for-qemu.sh
index c2fa324..1883fe6 100755
--- a/tests/test-pattern-largest-for-qemu.sh
+++ b/tests/test-pattern-largest-for-qemu.sh
@@ -39,6 +39,7 @@ set -e
 
 files="pattern-largest-for-qemu.out pattern-largest-for-qemu.pid pattern-largest-for-qemu.sock"
 rm -f $files
+cleanup_fn rm -f $files
 
 # Test that qemu-io works
 if ! qemu-io --help >/dev/null; then
@@ -48,31 +49,9 @@ fi
 
 # Run nbdkit with pattern plugin.
 # size = (2^63-1) & ~511 which is the largest supported by qemu.
-nbdkit -P pattern-largest-for-qemu.pid -U pattern-largest-for-qemu.sock \
+start_nbdkit -P pattern-largest-for-qemu.pid -U pattern-largest-for-qemu.sock \
        pattern size=9223372036854775296
 
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f pattern-largest-for-qemu.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f pattern-largest-for-qemu.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat pattern-largest-for-qemu.pid)"
-
-# Kill the nbdkit process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f $files
-}
-cleanup_fn cleanup
-
 qemu-io -r -f raw 'nbd+unix://?socket=pattern-largest-for-qemu.sock' \
         -c 'r -v 9223372036854774784 512' | grep -E '^[[:xdigit:]]+:' > pattern-largest-for-qemu.out
 if [ "$(cat pattern-largest-for-qemu.out)" != "7ffffffffffffc00:  7f ff ff ff ff ff fc 00 7f ff ff ff ff ff fc 08  ................
@@ -112,5 +91,3 @@ then
     cat pattern-largest-for-qemu.out
     exit 1
 fi
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-pattern-largest.sh b/tests/test-pattern-largest.sh
index 60a77b3..de5e1d7 100755
--- a/tests/test-pattern-largest.sh
+++ b/tests/test-pattern-largest.sh
@@ -39,6 +39,7 @@ set -e
 
 files="pattern-largest.out pattern-largest.pid pattern-largest.sock"
 rm -f $files
+cleanup_fn rm -f $files
 
 # Test that qemu-io works
 if ! qemu-io --help >/dev/null; then
@@ -48,31 +49,9 @@ fi
 
 # Run nbdkit with pattern plugin.
 # size = 2^63-1
-nbdkit -P pattern-largest.pid -U pattern-largest.sock \
+start_nbdkit -P pattern-largest.pid -U pattern-largest.sock \
        pattern size=9223372036854775807
 
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f pattern-largest.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f pattern-largest.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat pattern-largest.pid)"
-
-# Kill the nbdkit process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f $files
-}
-cleanup_fn cleanup
-
 # qemu cannot open this image!
 #
 #   can't open device nbd+unix://?socket=pattern-largest.sock: Could not get image size: File too large
@@ -92,5 +71,3 @@ then
     cat pattern-largest.out
     exit 1
 fi
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-pattern.sh b/tests/test-pattern.sh
index 98c5139..93dca94 100755
--- a/tests/test-pattern.sh
+++ b/tests/test-pattern.sh
@@ -43,6 +43,7 @@ set -e
 
 files="pattern.out pattern.pid pattern.sock"
 rm -f $files
+cleanup_fn rm -f $files
 
 # Test that qemu-io works
 if ! qemu-io --help >/dev/null; then
@@ -51,29 +52,7 @@ if ! qemu-io --help >/dev/null; then
 fi
 
 # Run nbdkit with pattern plugin.
-nbdkit -P pattern.pid -U pattern.sock pattern size=1G
-
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f pattern.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f pattern.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat pattern.pid)"
-
-# Kill the nbdkit process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f $files
-}
-cleanup_fn cleanup
+start_nbdkit -P pattern.pid -U pattern.sock pattern size=1G
 
 qemu-io -r -f raw 'nbd+unix://?socket=pattern.sock' \
         -c 'r -v 0 512' | grep -E '^[[:xdigit:]]+:' > pattern.out
@@ -114,5 +93,3 @@ then
     cat pattern.out
     exit 1
 fi
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-start.sh b/tests/test-start.sh
index dc578e7..4bea0ef 100755
--- a/tests/test-start.sh
+++ b/tests/test-start.sh
@@ -31,29 +31,16 @@
 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 
+source ./functions.sh
 set -e
 set -x
-source ./functions.sh
 
 # Test nbdkit starts up, forks in the background, writes a PID file,
 # and can be killed.
 
 rm -f start.pid start.sock
 
-nbdkit -P start.pid -U start.sock example1
-
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f start.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f start.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
+start_nbdkit -P start.pid -U start.sock example1
 pid="$(cat start.pid)"
 
 # Check the process exists.
diff --git a/tests/test-tls-psk.sh b/tests/test-tls-psk.sh
index ac59a4c..9e88782 100755
--- a/tests/test-tls-psk.sh
+++ b/tests/test-tls-psk.sh
@@ -78,29 +78,9 @@ for port in `seq 51000 65535`; do
 done
 echo picked unused port $port
 
-nbdkit -P tls-psk.pid -p $port -n --tls=require --tls-psk=keys.psk example1
-
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f tls-psk.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f tls-psk.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat tls-psk.pid)"
-
-# Kill the process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f tls-psk.pid tls-psk.out
-}
-cleanup_fn cleanup
+cleanup_fn rm -f tls-psk.pid tls-psk.out
+start_nbdkit -P tls-psk.pid -p $port -n \
+             --tls=require --tls-psk=keys.psk example1
 
 # Run qemu-img against the server.
 LANG=C \
diff --git a/tests/test-tls.sh b/tests/test-tls.sh
index 1ba262e..e41c1d5 100755
--- a/tests/test-tls.sh
+++ b/tests/test-tls.sh
@@ -71,31 +71,10 @@ for port in `seq 50000 65535`; do
 done
 echo picked unused port $port
 
-nbdkit -P tls.pid -p $port -n --tls=require \
+cleanup_fn rm -f tls.pid tls.out
+start_nbdkit -P tls.pid -p $port -n --tls=require \
        --tls-certificates="$pkidir" example1
 
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f tls.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f tls.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat tls.pid)"
-
-# Kill the process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f tls.pid tls.out
-}
-cleanup_fn cleanup
-
 # Run qemu-img against the server.
 LANG=C \
 qemu-img info \
diff --git a/tests/test-truncate1.sh b/tests/test-truncate1.sh
index c4f3b63..eb02a69 100755
--- a/tests/test-truncate1.sh
+++ b/tests/test-truncate1.sh
@@ -39,6 +39,7 @@ set -x
 
 files="truncate1.out truncate1.pid truncate1.sock"
 rm -f $files
+cleanup_fn rm -f $files
 
 # Test that qemu-io works
 if ! qemu-io --help >/dev/null; then
@@ -47,33 +48,11 @@ if ! qemu-io --help >/dev/null; then
 fi
 
 # Run nbdkit with pattern plugin and truncate filter in front.
-nbdkit -P truncate1.pid -U truncate1.sock \
+start_nbdkit -P truncate1.pid -U truncate1.sock \
        --filter=truncate \
        pattern size=503 \
        truncate=512
 
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f truncate1.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f truncate1.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat truncate1.pid)"
-
-# Kill the nbdkit process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f $files
-}
-cleanup_fn cleanup
-
 qemu-io -r -f raw 'nbd+unix://?socket=truncate1.sock' \
         -c 'r -v 0 512' | grep -E '^[[:xdigit:]]+:' > truncate1.out
 if [ "$(cat truncate1.out)" != "00000000:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08  ................
@@ -113,5 +92,3 @@ then
     cat truncate1.out
     exit 1
 fi
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-truncate2.sh b/tests/test-truncate2.sh
index 8a04661..4a3a89e 100755
--- a/tests/test-truncate2.sh
+++ b/tests/test-truncate2.sh
@@ -39,6 +39,7 @@ set -x
 
 files="truncate2.out truncate2.pid truncate2.sock"
 rm -f $files
+cleanup_fn rm -f $files
 
 # Test that qemu-io works
 if ! qemu-io --help >/dev/null; then
@@ -47,33 +48,11 @@ if ! qemu-io --help >/dev/null; then
 fi
 
 # Run nbdkit with pattern plugin and truncate filter in front.
-nbdkit -P truncate2.pid -U truncate2.sock \
+start_nbdkit -P truncate2.pid -U truncate2.sock \
        --filter=truncate \
        pattern size=503 \
        round-up=512
 
-# We may have to wait a short time for the pid file to appear.
-for i in `seq 1 10`; do
-    if test -f truncate2.pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f truncate2.pid; then
-    echo "$0: PID file was not created"
-    exit 1
-fi
-
-pid="$(cat truncate2.pid)"
-
-# Kill the nbdkit process on exit.
-cleanup ()
-{
-    kill $pid
-    rm -f $files
-}
-cleanup_fn cleanup
-
 qemu-io -r -f raw 'nbd+unix://?socket=truncate2.sock' \
         -c 'r -v 0 512' | grep -E '^[[:xdigit:]]+:' > truncate2.out
 if [ "$(cat truncate2.out)" != "00000000:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08  ................
@@ -113,5 +92,3 @@ then
     cat truncate2.out
     exit 1
 fi
-
-# The cleanup() function is called implicitly on exit.
diff --git a/tests/test-zero.sh b/tests/test-zero.sh
index 636e735..3fba24d 100755
--- a/tests/test-zero.sh
+++ b/tests/test-zero.sh
@@ -50,6 +50,3 @@ nbdkit -U - zero --run 'qemu-img convert $nbd test-zero.out'
 # Resulting file should be zero-sized.
 test -f test-zero.out
 ! test -s test-zero.out
-
-# Existing implicitly calls the trap function above to clean up.
-exit 0
-- 
2.19.0.rc0




More information about the Libguestfs mailing list