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