tests: Test --vsock option.
[nbdkit/ericb.git] / tests / functions.sh.in
blob0452d547d4ab06f69f8a3bce86a02992b486c843
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 # 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"
88 exit 77
92 # Test host kernel is Linux and minimum version. It's usually better
93 # to test features rather than using this, but there are cases where
94 # testing features of the current kernel is too hard.
95 requires_linux_kernel_version ()
97 # Test the kernel is Linux.
98 requires test "$(uname -s)" = "Linux"
100 # Test that it's the minimum version.
101 requires cut --version
102 requires bc --version
103 kver=$(uname -r | cut -d. -f1-2)
104 requires test "$(echo "$kver >= $1" | bc -l)" = "1"
107 # start_nbdkit -P pidfile args...
109 # Run nbdkit with args and wait for it to start up. If it fails to
110 # start up, exit with an error message. Also a cleanup handler is
111 # installed automatically which kills nbdkit on exit.
112 start_nbdkit ()
114 # -P <pidfile> must be the first two parameters.
115 if [ "$1" != "-P" ]; then
116 echo "$0: start_nbdkit: -P <pidfile> option must be first"
117 exit 1
119 pidfile="$2"
121 # Run nbdkit.
122 nbdkit -v "$@"
124 # Wait for the pidfile to appear.
125 for i in {1..60}; do
126 if test -s "$pidfile"; then
127 break
129 sleep 1
130 done
131 if ! test -s "$pidfile"; then
132 echo "$0: start_nbdkit: PID file $pidfile was not created"
133 exit 1
136 # Kill nbdkit on exit.
137 cleanup_fn kill "$(cat "$pidfile")"
140 # foreach_plugin f [args]
142 # For each plugin that was built, run the function or command f with
143 # the plugin name as the first argument, optionally followed by the
144 # remaining args.
145 foreach_plugin ()
147 f="$1"
148 shift
150 for p in @plugins@; do
151 # Was the plugin built?
152 d="@top_builddir@/plugins/$p"
153 if [ -f "$d/.libs/nbdkit-$p-plugin.so" ] ||
154 [ -f "$d/nbdkit-$p-plugin" ]; then
155 # Yes so run the test.
156 "$f" "$p" "$@"
158 done
161 # pick_unused_port
163 # Picks and returns an "unused" port, setting the global variable
164 # $port.
166 # This is inherently racy so we only use it where it's absolutely
167 # necessary (eg. testing TLS because qemu cannot do TLS over a Unix
168 # domain socket).
169 pick_unused_port ()
171 requires ss --version
173 # Start at a random port to make it less likely that two parallel
174 # tests will conflict.
175 port=$(( 50000 + (RANDOM%15000) ))
176 while ss -ltn | grep -sqE ":$port\b"; do
177 ((port++))
178 if [ $port -eq 65000 ]; then port=50000; fi
179 done
180 echo picked unused port $port