2 # Common functions used by the tests.
4 # Copyright (C) 2017-2019 Red Hat Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
17 # * Neither the name of Red Hat nor the names of its contributors may be
18 # used to endorse or promote products derived from this software without
19 # specific prior written permission.
21 # THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
22 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
25 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 # A generic trap handling function. This runs the function or command
37 # f plus optional args when the script exits for any reason.
38 declare -a _cleanup_hook
41 _cleanup_hook
[${#_cleanup_hook[@]}]="$@"
48 trap '' INT QUIT TERM EXIT ERR
49 echo $0: run cleanup hooks
: exit code
$status
51 for (( i
= 0; i
< ${#_cleanup_hook[@]}; ++i
)); do
57 trap _run_cleanup_hooks INT QUIT TERM EXIT ERR
59 # requires program [args]
61 # Check that ‘program [args]’ works. If not, skip the test.
62 # For example to check that qemu-img is available, do:
64 # requires qemu-img --version
67 ( "$@" ) </dev
/null
>/dev
/null
2>&1 ||
{
68 echo "$0: ‘$*’ failed with error code $?"
69 echo "$0: test prerequisite is missing or not working"
74 # start_nbdkit -P pidfile args...
76 # Run nbdkit with args and wait for it to start up. If it fails to
77 # start up, exit with an error message. Also a cleanup handler is
78 # installed automatically which kills nbdkit on exit.
81 # -P <pidfile> must be the first two parameters.
82 if [ "$1" != "-P" ]; then
83 echo "$0: start_nbdkit: -P <pidfile> option must be first"
91 # Wait for the pidfile to appear.
93 if test -f "$pidfile"; then
98 if ! test -f "$pidfile"; then
99 echo "$0: start_nbdkit: PID file $pidfile was not created"
103 # Kill nbdkit on exit.
104 cleanup_fn
kill "$(cat "$pidfile")"
107 # foreach_plugin f [args]
109 # For each plugin that was built, run the function or command f with
110 # the plugin name as the first argument, optionally followed by the
117 for p
in @plugins@
; do
118 # Was the plugin built?
119 d
="@top_builddir@/plugins/$p"
120 if [ -f "$d/.libs/nbdkit-$p-plugin.so" ] ||
121 [ -f "$d/nbdkit-$p-plugin" ]; then
122 # Yes so run the test.
130 # Picks and returns an "unused" port, setting the global variable
133 # This is inherently racy so we only use it where it's absolutely
134 # necessary (eg. testing TLS because qemu cannot do TLS over a Unix
138 requires ss
--version
140 # Start at a random port to make it less likely that two parallel
141 # tests will conflict.
142 port
=$
(( 50000 + (RANDOM
%15000) ))
143 while ss
-ltn |
grep -sqE ":$port\b"; do
145 if [ $port -eq 65000 ]; then port
=50000; fi
147 echo picked unused port
$port