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 # qemu cannot connect to ::1 if IPv6 is disabled because of
75 # the way it uses getaddrinfo. See:
76 # https://bugzilla.redhat.com/show_bug.cgi?id=808147
77 # https://lists.fedoraproject.org/archives/list/devel@lists.fedoraproject.org/thread/SXDLSZ3GKXL6NDAKP4MPJ25IMHKN67X3/
78 requires_ipv6_loopback
()
80 requires qemu-img
--version
82 # This should fail with "Connection refused". If IPv6 is broken
83 # then it fails with "Address family for hostname not supported"
84 # instead. It's very unlikely that port 1 is open.
85 if LANG
=C qemu-img info
"nbd:[::1]:1" |
& \
86 grep -sq "Address family for hostname not supported"; then
87 echo "$0: IPv6 loopback is not available, skipping this test"
92 # start_nbdkit -P pidfile args...
94 # Run nbdkit with args and wait for it to start up. If it fails to
95 # start up, exit with an error message. Also a cleanup handler is
96 # installed automatically which kills nbdkit on exit.
99 # -P <pidfile> must be the first two parameters.
100 if [ "$1" != "-P" ]; then
101 echo "$0: start_nbdkit: -P <pidfile> option must be first"
109 # Wait for the pidfile to appear.
111 if test -s "$pidfile"; then
116 if ! test -s "$pidfile"; then
117 echo "$0: start_nbdkit: PID file $pidfile was not created"
121 # Kill nbdkit on exit.
122 cleanup_fn
kill "$(cat "$pidfile")"
125 # foreach_plugin f [args]
127 # For each plugin that was built, run the function or command f with
128 # the plugin name as the first argument, optionally followed by the
135 for p
in @plugins@
; do
136 # Was the plugin built?
137 d
="@top_builddir@/plugins/$p"
138 if [ -f "$d/.libs/nbdkit-$p-plugin.so" ] ||
139 [ -f "$d/nbdkit-$p-plugin" ]; then
140 # Yes so run the test.
148 # Picks and returns an "unused" port, setting the global variable
151 # This is inherently racy so we only use it where it's absolutely
152 # necessary (eg. testing TLS because qemu cannot do TLS over a Unix
156 requires ss
--version
158 # Start at a random port to make it less likely that two parallel
159 # tests will conflict.
160 port
=$
(( 50000 + (RANDOM
%15000) ))
161 while ss
-ltn |
grep -sqE ":$port\b"; do
163 if [ $port -eq 65000 ]; then port
=50000; fi
165 echo picked unused port
$port