[Libguestfs] [PATCH nbdkit] tests: Add generic ‘requires’ function to testing test prerequisites.

Richard W.M. Jones rjones at redhat.com
Wed Jan 23 11:17:31 UTC 2019


---
 tests/functions.sh.in         | 17 ++++++++++++++++-
 tests/test-memory-largest.sh  |  8 ++------
 tests/test-partition1.sh      | 22 ++++------------------
 tests/test-partition2.sh      |  6 +-----
 tests/test-pattern-largest.sh |  8 ++------
 5 files changed, 25 insertions(+), 36 deletions(-)

diff --git a/tests/functions.sh.in b/tests/functions.sh.in
index 35647f7..97afbbf 100644
--- a/tests/functions.sh.in
+++ b/tests/functions.sh.in
@@ -1,7 +1,7 @@
 # nbdkit
 # Common functions used by the tests.
 # @configure_input@
-# Copyright (C) 2017-2018 Red Hat Inc.
+# Copyright (C) 2017-2019 Red Hat Inc.
 # All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
@@ -57,6 +57,21 @@ _run_cleanup_hooks ()
 }
 trap _run_cleanup_hooks INT QUIT TERM EXIT ERR
 
+# requires program [args]
+#
+# Check that ‘program [args]’ works.  If not, skip the test.
+# For example to check that qemu-img is available, do:
+#
+# requires qemu-img --help
+requires ()
+{
+    "$@" || {
+        echo "$0: ‘$@’ failed with error code $?"
+        echo "$0: test prerequisite is missing or not working"
+        exit 77
+    }
+}
+
 # start_nbdkit -P pidfile args...
 #
 # Run nbdkit with args and wait for it to start up.  If it fails to
diff --git a/tests/test-memory-largest.sh b/tests/test-memory-largest.sh
index a6c269e..50708f9 100755
--- a/tests/test-memory-largest.sh
+++ b/tests/test-memory-largest.sh
@@ -37,16 +37,12 @@
 source ./functions.sh
 set -e
 
+requires qemu-io --help
+
 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
-    echo "$0: missing or broken qemu-io"
-    exit 77
-fi
-
 # Run nbdkit with memory plugin.
 # size = 2^63-1
 start_nbdkit -P memory-largest.pid -U memory-largest.sock \
diff --git a/tests/test-partition1.sh b/tests/test-partition1.sh
index 1bb0240..7fa817f 100755
--- a/tests/test-partition1.sh
+++ b/tests/test-partition1.sh
@@ -35,29 +35,15 @@ source ./functions.sh
 set -e
 set -x
 
+requires sfdisk --help
+requires test -r /dev/urandom
+requires qemu-img --help
+
 d="partition1.d"
 rm -rf $d
 cleanup_fn rm -rf $d
 mkdir $d
 
-# Test that sfdisk is available and working.
-if ! sfdisk --help >/dev/null; then
-    echo "$0: missing or broken sfdisk"
-    exit 77
-fi
-
-# Test that /dev/urandom exists and can be read.
-if ! test -r /dev/urandom; then
-    echo "$0: mising or unreadable /dev/urandom"
-    exit 77
-fi
-
-# Test that qemu-img is available and working.
-if ! qemu-img --help >/dev/null; then
-    echo "$0: missing or broken qemu-img"
-    exit 77
-fi
-
 test ()
 {
     label=$1
diff --git a/tests/test-partition2.sh b/tests/test-partition2.sh
index 71bac55..1cb6900 100755
--- a/tests/test-partition2.sh
+++ b/tests/test-partition2.sh
@@ -35,11 +35,7 @@ source ./functions.sh
 set -e
 set -x
 
-# Test that qemu-img is available and working.
-if ! qemu-img --help >/dev/null; then
-    echo "$0: missing or broken qemu-img"
-    exit 77
-fi
+requires qemu-img --help
 
 files="partition2.log"
 rm -f $files
diff --git a/tests/test-pattern-largest.sh b/tests/test-pattern-largest.sh
index de5e1d7..6132430 100755
--- a/tests/test-pattern-largest.sh
+++ b/tests/test-pattern-largest.sh
@@ -37,16 +37,12 @@
 source ./functions.sh
 set -e
 
+requires qemu-io --help
+
 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
-    echo "$0: missing or broken qemu-io"
-    exit 77
-fi
-
 # Run nbdkit with pattern plugin.
 # size = 2^63-1
 start_nbdkit -P pattern-largest.pid -U pattern-largest.sock \
-- 
2.20.1




More information about the Libguestfs mailing list