[Libguestfs] [libnbd PATCH 05/20] tests: Clean up qemu-nbd detection in

Martin Kletzander
Tue, 08 Jun 2021 07:53:46 -0000

It was already different in copy/ and info/, so it is nicer to merge them
together.

Signed-off-by: Martin Kletzander <mkletzan at redhat.com>
---
 tests/functions.sh.in                  | 19 +++++++++++++++++++
 copy/copy-file-to-qcow2.sh             | 16 +++-------------
 info/Makefile.am                       |  1 +
 info/info-description-qemu.sh          | 17 +++--------------
 info/info-list-json-qemu.sh            | 17 +++--------------
 info/info-list-qemu.sh                 | 17 +++--------------
 info/info-map-qemu-allocation-depth.sh | 16 +++-------------
 info/info-map-qemu-dirty-bitmap.sh     | 23 +++--------------------
 8 files changed, 38 insertions(+), 88 deletions(-)

diff --git a/tests/functions.sh.in b/tests/functions.sh.in
index f42c6906d0f8..271d8b357685 100644
--- a/tests/functions.sh.in
+++ b/tests/functions.sh.in
@@ -158,3 +158,22 @@ pick_unused_port ()
     done
     echo picked unused port $port
 }
+
+# Wait for qemu-nbd to start up.
+wait_for_pidfile ()
+{
+    local binary=$1
+    local pid=$2
+
+    for i in {1..60}; do
+        if test -f $pid; then
+            break
+        fi
+        sleep 1
+    done
+
+    if ! test -f $pid; then
+        echo "$0: $binary did not start up"
+        exit 1
+    fi
+}
diff --git a/copy/copy-file-to-qcow2.sh b/copy/copy-file-to-qcow2.sh
index 57c18b383bb7..dfd870b76413 100755
--- a/copy/copy-file-to-qcow2.sh
+++ b/copy/copy-file-to-qcow2.sh
@@ -21,7 +21,7 @@
 set -e
 set -x

-requires $QEMU_NBD --version
+requires $QEMU_NBD --pid-file=test.pid --version
 requires qemu-img --version
 requires cmp --version
 requires dd --version
@@ -54,20 +54,10 @@ qemu-img create -f qcow2 $qcow2 $size

 # Run qemu-nbd as a separate process so that we can copy to and from
 # the single process in two separate operations.
-qemu-nbd -f qcow2 -t --socket=$sock --pid-file=$pidfile $qcow2 &
+$QEMU_NBD -f qcow2 -t --socket=$sock --pid-file=$pidfile $qcow2 &
 cleanup_fn kill $!

-# Wait for qemu-nbd to start up.
-for i in {1..60}; do
-    if test -f $pidfile; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f $pidfile; then
-    echo "$0: qemu-nbd did not start up"
-    exit 1
-fi
+wait_for_pidfile qemu-nbd $pid

 $VG nbdcopy $file "nbd+unix:///?socket=$sock"
 $VG nbdcopy "nbd+unix:///?socket=$sock" $file2
diff --git a/info/Makefile.am b/info/Makefile.am
index c62d51b7b067..5c717c700b35 100644
--- a/info/Makefile.am
+++ b/info/Makefile.am
@@ -54,6 +54,7 @@ TESTS_ENVIRONMENT = \
 	MALLOC_CHECK_=1 \
 	MALLOC_PERTURB_=$(shell bash -c 'echo $$(( 1 + (RANDOM & 255) ))')
\
 	EXPECTED_VERSION=$(VERSION) \
+	QEMU_NBD=$(QEMU_NBD) \
 	$(NULL)
 LOG_COMPILER = $(top_builddir)/run
 TESTS diff --git a/info/info-description-qemu.sh
b/info/info-description-qemu.sh
index af2bd96b1cf6..e86fbe0eda5d 100755
--- a/info/info-description-qemu.sh
+++ b/info/info-description-qemu.sh
@@ -21,8 +21,7 @@
 set -e
 set -x

-requires qemu-nbd --version
-requires bash -c 'qemu-nbd --help | grep pid-file'
+requires $QEMU_NBD --pid-file=test.pid --version
 requires truncate --version

 img=info-description-qemu.img
@@ -33,20 +32,10 @@ cleanup_fn rm -f $img $out $pid $sock
 rm -f $img $out $pid $sock

 truncate -s 1M $img
-qemu-nbd -f raw -t --socket=$sock --pid-file=$pid -x "hello" -D
"world" $img &
+$QEMU_NBD -f raw -t --socket=$sock --pid-file=$pid -x "hello" -D
"world" $img &
 cleanup_fn kill $!

-# Wait for qemu-nbd to start up.
-for i in {1..60}; do
-    if test -f $pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f $pid; then
-    echo "$0: qemu-nbd did not start up"
-    exit 1
-fi
+wait_for_pidfile qemu-nbd $pid

 $VG nbdinfo "nbd+unix:///hello?socket=$sock" > $out
 cat $out
diff --git a/info/info-list-json-qemu.sh b/info/info-list-json-qemu.sh
index 9933c38a2c1d..06e8f4994ec2 100755
--- a/info/info-list-json-qemu.sh
+++ b/info/info-list-json-qemu.sh
@@ -21,8 +21,7 @@
 set -e
 set -x

-requires qemu-nbd --version
-requires bash -c 'qemu-nbd --help | grep pid-file'
+requires $QEMU_NBD --pid-file=test.pid --version
 requires truncate --version
 requires jq --version

@@ -34,20 +33,10 @@ cleanup_fn rm -f $img $out $pid $sock
 rm -f $img $out $pid $sock

 truncate -s 1M $img
-qemu-nbd -t --socket=$sock --pid-file=$pid -x "hello" -D
"world" $img &
+$QEMU_NBD -t --socket=$sock --pid-file=$pid -x "hello" -D
"world" $img &
 cleanup_fn kill $!

-# Wait for qemu-nbd to start up.
-for i in {1..60}; do
-    if test -f $pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f $pid; then
-    echo "$0: qemu-nbd did not start up"
-    exit 1
-fi
+wait_for_pidfile qemu-nbd $pid

 # Test twice, once with an export name not on the list,...
 $VG nbdinfo "nbd+unix://?socket=$sock" --list --json > $out
diff --git a/info/info-list-qemu.sh b/info/info-list-qemu.sh
index a6dcd4248b5f..fb6f57bcccf3 100755
--- a/info/info-list-qemu.sh
+++ b/info/info-list-qemu.sh
@@ -21,8 +21,7 @@
 set -e
 set -x

-requires qemu-nbd --version
-requires bash -c 'qemu-nbd --help | grep pid-file'
+requires $QEMU_NBD --pid-file=test.pid --version
 requires truncate --version

 img=info-list-qemu.img
@@ -33,20 +32,10 @@ cleanup_fn rm -f $img $out $pid $sock
 rm -f $img $out $pid $sock

 truncate -s 1M $img
-qemu-nbd -t --socket=$sock --pid-file=$pid -x "hello" -D
"world" $img &
+$QEMU_NBD -t --socket=$sock --pid-file=$pid -x "hello" -D
"world" $img &
 cleanup_fn kill $!

-# Wait for qemu-nbd to start up.
-for i in {1..60}; do
-    if test -f $pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f $pid; then
-    echo "$0: qemu-nbd did not start up"
-    exit 1
-fi
+wait_for_pidfile qemu-nbd $pid

 # Test twice, once with an export name not on the list,...
 $VG nbdinfo "nbd+unix://?socket=$sock" --list > $out
diff --git a/info/info-map-qemu-allocation-depth.sh
b/info/info-map-qemu-allocation-depth.sh
index a5dab0b83c52..0bbe07c2a73a 100755
--- a/info/info-map-qemu-allocation-depth.sh
+++ b/info/info-map-qemu-allocation-depth.sh
@@ -26,7 +26,7 @@ set -x

 requires qemu-img --version
 requires qemu-io --version
-requires qemu-nbd -A --version
+requires $QEMU_NBD -A --pid-file=test.pid --version
 requires tr --version

 base=info-map-qemu-allocation-depth
@@ -59,20 +59,10 @@ pid=$base.pid
 cleanup_fn rm -f $sock $pid
 rm -f $sock $pid

-qemu-nbd -t --socket=$sock --pid-file=$pid -f qcow2 -A $f4 &
+$QEMU_NBD -t --socket=$sock --pid-file=$pid -f qcow2 -A $f4 &
 cleanup_fn kill $!

-# Wait for qemu-nbd to start up.
-for i in {1..60}; do
-    if test -f $pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f $pid; then
-    echo "$0: qemu-nbd did not start up"
-    exit 1
-fi
+wait_for_pidfile qemu-nbd $pid

 $VG nbdinfo --map=qemu:allocation-depth "nbd+unix://?socket=$sock"
> $out
 cat $out
diff --git a/info/info-map-qemu-dirty-bitmap.sh
b/info/info-map-qemu-dirty-bitmap.sh
index 01b62c1305a2..d9a6a5f143fc 100755
--- a/info/info-map-qemu-dirty-bitmap.sh
+++ b/info/info-map-qemu-dirty-bitmap.sh
@@ -26,17 +26,10 @@ set -x

 requires qemu-img --version
 requires qemu-io --version
-requires qemu-nbd --version
-requires bash -c 'qemu-nbd --help | grep pid-file'
+requires $QEMU_NBD -B test --pid-file=test.pid --version
 requires_qemu
 requires tr --version

-# This test uses the qemu-nbd -B option.
-if ! qemu-nbd --help | grep -sq -- -B; then
-    echo "$0: skipping because qemu-nbd does not support the -B
option"
-    exit 77
-fi
-
 f=info-map-qemu-dirty-bitmap.qcow2
 out=info-map-qemu-dirty-bitmap.out
 cleanup_fn rm -f $f $out
@@ -60,20 +53,10 @@ pid=info-map-qemu-dirty-bitmap.pid
 cleanup_fn rm -f $sock $pid
 rm -f $sock $pid

-qemu-nbd -t --socket=$sock --pid-file=$pid -f qcow2 -B bitmap0 $f &
+$QEMU_NBD -t --socket=$sock --pid-file=$pid -f qcow2 -B bitmap0 $f &
 cleanup_fn kill $!

-# Wait for qemu-nbd to start up.
-for i in {1..60}; do
-    if test -f $pid; then
-        break
-    fi
-    sleep 1
-done
-if ! test -f $pid; then
-    echo "$0: qemu-nbd did not start up"
-    exit 1
-fi
+wait_for_pidfile qemu-nbd $pid

 $VG nbdinfo --map=qemu:dirty-bitmap:bitmap0
"nbd+unix://?socket=$sock" > $out
 cat $out
--
2.31.1