tests/test-captive.sh: Increase sleep times.
[nbdkit/ericb.git] / tests / functions.sh.in
blob5061ebcfe9da3d67ef3dbf63c2c7dd9fa4a49f56
1 # nbdkit
2 # Common functions used by the tests.
3 # @configure_input@
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
8 # met:
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
32 # SUCH DAMAGE.
34 # cleanup_fn f [args]
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
39 cleanup_fn ()
41 _cleanup_hook[${#_cleanup_hook[@]}]="$@"
44 _run_cleanup_hooks ()
46 status=$?
47 set +e
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
52 ${_cleanup_hook[i]}
53 done
55 exit $status
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
65 requires ()
67 ( "$@" ) </dev/null >/dev/null 2>&1 || {
68 echo "$0: ‘$*’ failed with error code $?"
69 echo "$0: test prerequisite is missing or not working"
70 exit 77
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.
79 start_nbdkit ()
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"
84 exit 1
86 pidfile="$2"
88 # Run nbdkit.
89 nbdkit -v "$@"
91 # Wait for the pidfile to appear.
92 for i in {1..60}; do
93 if test -f "$pidfile"; then
94 break
96 sleep 1
97 done
98 if ! test -f "$pidfile"; then
99 echo "$0: start_nbdkit: PID file $pidfile was not created"
100 exit 1
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
111 # remaining args.
112 foreach_plugin ()
114 f="$1"
115 shift
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.
123 "$f" "$p" "$@"
125 done
128 # pick_unused_port
130 # Picks and returns an "unused" port, setting the global variable
131 # $port.
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
135 # domain socket).
136 pick_unused_port ()
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
144 ((port++))
145 if [ $port -eq 65000 ]; then port=50000; fi
146 done
147 echo picked unused port $port