docs: Use I<> instead of B<> for describing flags
[nbdkit.git] / tests / test-parallel-sh.sh
blob073ba51d4b6415a180a77e7551e1b182d7868084
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright (C) 2017-2020 Red Hat Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # * Neither the name of Red Hat nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # SUCH DAMAGE.
33 source ./functions.sh
35 # Check file-data was created by Makefile and qemu-io exists.
36 requires test -f file-data
37 requires qemu-io --version
38 requires timeout 60s true
39 requires dd iflag=count_bytes </dev/null
40 requires $STAT --version
42 nbdkit --dump-plugin sh | grep -q ^thread_model=parallel ||
43 { echo "nbdkit lacks support for parallel requests"; exit 77; }
45 # debuginfod breaks valgrinding of this test because it creates about
46 # a dozen pipe file descriptors, which breaks the leaked fd
47 # assumptions in the test below.
48 if [ "$NBDKIT_VALGRIND" = "1" ]; then
49 requires test -z "$DEBUGINFOD_URLS"
52 cleanup_fn rm -f test-parallel-sh.data test-parallel-sh.out test-parallel-sh.script
54 # Populate file, and sanity check that qemu-io can issue parallel requests
55 printf '%1024s' . > test-parallel-sh.data
56 timeout 30s </dev/null qemu-io -f raw -c "aio_write -P 1 0 512" \
57 -c "aio_write -P 2 512 512" -c aio_flush test-parallel-sh.data ||
58 { echo "'qemu-io' can't drive parallel requests"; exit 77; }
60 # Set up the sh plugin to delay both reads and writes (for a good
61 # chance that parallel requests are in flight), and with writes longer
62 # than reads (to more easily detect if out-of-order completion
63 # happens). This test may have spurious failures under heavy loads on
64 # the test machine, where tuning the delays may help.
66 # Also test for leaked fds when possible: nbdkit does not close fds it
67 # inherited, but other than 0, 1, 2, and the fd associated with the
68 # script, the child shell should not see any new fds not also present
69 # in nbdkit's parent environment. When testing for the count of open
70 # fds, use ls in a subshell (rather than a glob directly within the
71 # shell under test), to avoid yet another fd open on the /proc/self/fd
72 # directory.
74 curr_fds=
75 if test -d /proc/$$/fd; then
76 echo "parent fds:" >&2
77 ls -l /proc/$$/fd >&2
78 curr_fds=$(/usr/bin/env bash -c '(ls /proc/$$/fd)' | wc -w)
80 export curr_fds
81 echo "using curr_fds=$curr_fds"
83 export STAT
84 cat > test-parallel-sh.script <<'EOF'
85 #!/usr/bin/env bash
86 f=test-parallel-sh.data
87 if ! test -f $f; then
88 echo "can't locate test-parallel-sh.data" >&2; exit 5
90 if test -n "$curr_fds"; then
92 if test $( ls /proc/$$/fd | wc -w ) -ne $(($curr_fds + 1)); then
93 echo "nbdkit script fds:" >&2
94 ls -l /proc/$$/fd >&2
95 echo "there seem to be leaked fds, curr_fds=$curr_fds" >&2
96 exit 1
98 ) || exit 5
100 case $1 in
101 thread_model) echo parallel ;;
102 get_size) $STAT -L -c %s $f || exit 1 ;;
103 pread) dd iflag=skip_bytes,count_bytes skip=$4 count=$3 if=$f || exit 1 ;;
104 pwrite) dd oflag=seek_bytes conv=notrunc seek=$4 of=$f || exit 1 ;;
105 can_write) ;;
106 *) exit 2 ;;
107 esac
108 exit 0
110 chmod +x test-parallel-sh.script
112 # With --threads=1, the write should complete first because it was issued first
113 nbdkit -v -t 1 -U - --filter=delay sh test-parallel-sh.script \
114 wdelay=2 rdelay=1 --run 'timeout 60s </dev/null qemu-io -f raw \
115 -c "aio_write -P 2 512 512" -c "aio_read -P 1 0 512" -c aio_flush $nbd' |
116 tee test-parallel-sh.out
117 if test "$(grep '512/512' test-parallel-sh.out)" != \
118 "wrote 512/512 bytes at offset 512
119 read 512/512 bytes at offset 0"; then
120 exit 1
123 # With default --threads, the faster read should complete first
124 nbdkit -v -U - --filter=delay sh test-parallel-sh.script \
125 wdelay=2 rdelay=1 --run 'timeout 60s </dev/null qemu-io -f raw \
126 -c "aio_write -P 2 512 512" -c "aio_read -P 1 0 512" -c aio_flush $nbd' |
127 tee test-parallel-sh.out
128 if test "$(grep '512/512' test-parallel-sh.out)" != \
129 "read 512/512 bytes at offset 0
130 wrote 512/512 bytes at offset 512"; then
131 exit 1
134 # With --filter=noparallel, the write should complete first because it was
135 # issued first. Also test that the log filter doesn't leak an fd
136 nbdkit -v -U - --filter=noparallel --filter=log --filter=delay \
137 sh test-parallel-sh.script logfile=/dev/null \
138 wdelay=2 rdelay=1 --run 'timeout 60s </dev/null qemu-io -f raw \
139 -c "aio_write -P 2 512 512" -c "aio_read -P 1 0 512" -c aio_flush $nbd' |
140 tee test-parallel-sh.out
141 if test "$(grep '512/512' test-parallel-sh.out)" != \
142 "wrote 512/512 bytes at offset 512
143 read 512/512 bytes at offset 0"; then
144 exit 1
147 exit 0