Richard W.M. Jones
2019-Jan-23 11:17 UTC
[Libguestfs] [PATCH nbdkit] tests: Add generic requires function.
I only did a handful of the tests to demonstrate the point. If the patch approach is accepted then I'll do the remainder of the tests in v2. Rich.
Richard W.M. Jones
2019-Jan-23 11:17 UTC
[Libguestfs] [PATCH nbdkit] tests: Add generic ‘requires’ function to testing test prerequisites.
--- 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
Eric Blake
2019-Jan-23 14:11 UTC
Re: [Libguestfs] [PATCH nbdkit] tests: Add generic ‘requires’ function to testing test prerequisites.
On 1/23/19 5:17 AM, Richard W.M. Jones wrote:> --- > 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(-) >> > +# 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 () > +{ > + "$@" || {I'd consider running: ( "$@" ) </dev/null >/dev/null 2>&1 || { just to ensure that a missing executable doesn't leak a shell error message or unrelated verbose program output, and so that the program under test is less likely to hang waiting for input if it found some other program by the same name but with different usage conventions.> +++ 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; thenFor example, if you don't make require() silence stdout, this change becomes more verbose in the logs, for no real benefit. Otherwise, the idea makes total sense, so I'm looking forward to v2. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Reasonably Related Threads
- [PATCH v2 nbdkit] tests: Add generic ‘requires’ function for test prerequisites.
- [PATCH nbdkit] tests: Add generic requires function.
- Shearing file systems on the network
- [PATCH nbdkit v3 2/5] tests: Implement a better nbdkit-partition-filter test.
- [PATCH v2 nbdkit] tests: Add generic requires.