tests: Remove test-error.c and replace with a script that uses nbdsh.
[nbdkit/ericb.git] / tests / test-sh-errors.sh
blobbdbff517e7a3355b926cdd02094a032b28dd51f2
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright (C) 2018-2019 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
34 set -e
35 set -x
37 requires qemu-io --version
39 out="test-sh-errors.out"
40 expected="test-sh-errors.expected"
41 files="$out $expected"
42 rm -f $files
43 cleanup_fn rm -f $files
45 # do_test 'egrep pattern' ['write_zero']
46 # (Use of an egrep pattern makes it easy to whitelist other localized error
47 # outputs that may be system-dependent)
48 do_test ()
50 : > $out
51 if [ "$2" = write_zero ]; then
52 cmd='qemu-io -f raw -c "w -z 0 512" $nbd'
53 else
54 cmd='qemu-io -r -f raw -c "r 0 1" $nbd'
56 nbdkit -v -U - sh - --run "$cmd"' &&
57 echo qemu-io unexpectedly passed >> '$out'; :' >> $out
58 if grep "qemu-io unexpectedly passed" $out ||
59 ! egrep "$1" $out; then
60 echo "$0: output did not match expected data"
61 echo "expected: $1"
62 echo "output:"
63 cat $out
64 exit 1
68 # All caps, no trailing whitespace, ENOMEM
69 do_test 'Cannot allocate memory' <<'EOF'
70 case "$1" in
71 get_size) echo 64K ;;
72 pread) printf ENOMEM >&2; exit 1 ;;
73 *) exit 2 ;;
74 esac
75 EOF
77 # Lower case, trailing newline, EPERM
78 do_test 'Operation not permitted' <<'EOF'
79 case "$1" in
80 get_size) echo 64K ;;
81 pread) echo eperm >&2; exit 1 ;;
82 *) exit 2 ;;
83 esac
84 EOF
86 # Trailing single-line message, ENOSPC
87 do_test 'No space left on device' <<'EOF'
88 case "$1" in
89 get_size) echo 64K ;;
90 pread) echo ENOSPC custom message does not reach client >&2; exit 1 ;;
91 *) exit 2 ;;
92 esac
93 EOF
95 # Multi-line message, ESHUTDOWN
96 do_test 'Cannot send after transport endpoint shutdown' <<'EOF'
97 case "$1" in
98 get_size) echo 1M ;;
99 pread) printf 'ESHUTDOWN\nline one\nline two\n' >&2; exit 1 ;;
100 *) exit 2 ;;
101 esac
104 # Any errno that can't be sent over NBD is flattened to EIO
105 do_test 'Input/output error' <<'EOF'
106 case "$1" in
107 get_size) echo 1M ;;
108 pread) echo EPIPE >&2; exit 1 ;;
109 *) exit 2 ;;
110 esac
113 # EINVALID is not a real errno name, and should not be confused with EINVAL
114 do_test 'Input/output error' <<'EOF'
115 case "$1" in
116 get_size) echo 1M ;;
117 pread) echo EINVALID >&2; exit 1 ;;
118 *) exit 2 ;;
119 esac
122 # ENOTSUP is special to write zero; it triggers a fallback to pwrite
123 # Also test that anything on stderr is ignored when the script succeeds
124 do_test 'No space left on device' write_zero <<'EOF'
125 case "$1" in
126 get_size) echo 'EINVAL ignored' >&2; echo 1M ;;
127 can_write | can_zero) echo ENOMEM >&2; exit 0 ;;
128 pwrite) echo ENOSPC >&2; exit 1 ;;
129 zero) echo ENOTSUP >&2; exit 1 ;;
130 *) exit 2 ;;
131 esac