Update Red Hat Copyright Notices
[nbdkit.git] / tests / test-eval-disconnect.sh
blobf3a28d7b8fa516dda4553d714e7b68758cc581ee
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright Red Hat
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 # Check shutdown/disconnect behavior triggered by special status values
35 source ./functions.sh
36 set -e
37 set -x
39 requires_plugin eval
40 requires_nbdsh_uri
42 sock=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
43 files="eval-disconnect.pid $sock"
44 rm -f $files
45 cleanup_fn rm -f $files
47 # Start nbdkit with a plugin that fails writes according to the export
48 # name which must be numeric: a positive value leaves stderr empty, a
49 # non-positive one outputs EPERM first. Serve multiple clients.
50 serve()
52 rm -f $files
53 start_nbdkit -P eval-disconnect.pid -U $sock eval \
54 get_size=' echo 1024 ' \
55 open=' if test $3 -le 0; then \
56 echo EPERM > $tmpdir/err; echo $((-$3)); \
57 else \
58 : > $tmpdir/err; echo $3; \
59 fi ' \
60 flush=' exit 0 ' \
61 pwrite=' cat >/dev/null; cat $tmpdir/err >&2; exit $2 '
63 check_dead()
65 # Server should die shortly, if not already dead at this point.
66 for (( i=0; i < 5; i++ )); do
67 kill -s 0 "$(cat eval-disconnect.pid)" || break
68 sleep 1
69 done
70 if [ $i = 5 ]; then
71 echo "nbdkit didn't exit fast enough"
72 exit 1
75 serve
77 # Noisy status 0 (OK) succeeds, despite text to stderr
78 nbdsh -u "nbd+unix:///0?socket=$sock" -c - <<\EOF
79 h.pwrite(b"1", 0)
80 h.flush()
81 h.shutdown()
82 EOF
84 # Silent status 1 (ERROR) fails; nbdkit turns lack of error into EIO
85 nbdsh -u "nbd+unix:///1?socket=$sock" -c - <<\EOF
86 import errno
87 try:
88 h.pwrite(b"1", 0)
89 assert False
90 except nbd.Error as ex:
91 assert ex.errnum == errno.EIO
92 h.flush()
93 h.shutdown()
94 EOF
96 # Noisy status 1 (ERROR) fails with supplied EPERM
97 nbdsh -u "nbd+unix:///-1?socket=$sock" -c - <<\EOF
98 import errno
99 try:
100 h.pwrite(b"1", 0)
101 assert False
102 except nbd.Error as ex:
103 assert ex.errnum == errno.EPERM
104 h.flush()
105 h.shutdown()
108 # Silent status 4 (SHUTDOWN_OK) kills the server. It is racy whether client
109 # sees success or if the connection is killed with libnbd giving ENOTCONN
110 nbdsh -u "nbd+unix:///4?socket=$sock" -c - <<\EOF
111 import errno
112 try:
113 h.pwrite(b"1", 0)
114 except nbd.Error as ex:
115 assert ex.errnum == errno.ENOTCONN
117 check_dead
118 serve
120 # Noisy status 4 (SHUTDOWN_OK) kills the server. It is racy whether client
121 # sees success or if the connection is killed with libnbd giving ENOTCONN
122 nbdsh -u "nbd+unix:///-4?socket=$sock" -c - <<\EOF
123 import errno
124 try:
125 h.pwrite(b"1", 0)
126 except nbd.Error as ex:
127 assert ex.errnum == errno.ENOTCONN
129 check_dead
130 serve
132 # Silent status 5 (SHUTDOWN_ERR) kills the server. It is racy whether client
133 # sees implied ESHUTDOWN or if the connection dies with libnbd giving ENOTCONN
134 nbdsh -u "nbd+unix:///5?socket=$sock" -c - <<\EOF
135 import errno
136 try:
137 h.pwrite(b"1", 0)
138 assert False
139 except nbd.Error as ex:
140 assert ex.errnum == errno.ESHUTDOWN or ex.errnum == errno.ENOTCONN
142 check_dead
143 serve
145 # Noisy status 5 (SHUTDOWN_ERR) kills the server. It is racy whether client
146 # sees EPERM or if the connection is killed with libnbd giving ENOTCONN
147 nbdsh -u "nbd+unix:///-5?socket=$sock" -c - <<\EOF
148 import errno
149 try:
150 h.pwrite(b"1", 0)
151 assert False
152 except nbd.Error as ex:
153 assert ex.errnum == errno.EPERM or ex.errnum == errno.ENOTCONN
155 check_dead
156 serve
158 # Silent status 6 (DISC_FORCE) kills socket; libnbd detects as ENOTCONN
159 nbdsh -u "nbd+unix:///6?socket=$sock" -c - <<\EOF
160 import errno
161 try:
162 h.pwrite(b"1", 0)
163 assert False
164 except nbd.Error as ex:
165 assert ex.errnum == errno.ENOTCONN
166 assert h.aio_is_ready() is False
169 # Noisy status 6 (DISC_FORCE) kills socket; libnbd detects as ENOTCONN
170 nbdsh -u "nbd+unix:///-6?socket=$sock" -c - <<\EOF
171 import errno
172 try:
173 h.pwrite(b"1", 0)
174 assert False
175 except nbd.Error as ex:
176 assert ex.errnum == errno.ENOTCONN
177 assert h.aio_is_ready() is False
180 # Silent status 7 (DISC_SOFT_OK) succeeds, but next command gives ESHUTDOWN
181 nbdsh -u "nbd+unix:///7?socket=$sock" -c - <<\EOF
182 import errno
183 h.pwrite(b"1", 0)
184 try:
185 h.flush()
186 assert False
187 except nbd.Error as ex:
188 assert ex.errnum == errno.ESHUTDOWN
189 h.shutdown()
192 # Noisy status 7 (DISC_SOFT_OK) succeeds, but next command gives ESHUTDOWN
193 nbdsh -u "nbd+unix:///-7?socket=$sock" -c - <<\EOF
194 import errno
195 h.pwrite(b"1", 0)
196 try:
197 h.flush()
198 assert False
199 except nbd.Error as ex:
200 assert ex.errnum == errno.ESHUTDOWN
201 h.shutdown()
204 # Silent status 8 (DISC_SOFT_ERR) fails with implied ESHUTDOWN, then next
205 # command gives ESHUTDOWN
206 nbdsh -u "nbd+unix:///8?socket=$sock" -c - <<\EOF
207 import errno
208 try:
209 h.pwrite(b"1", 0)
210 assert False
211 except nbd.Error as ex:
212 assert ex.errnum == errno.ESHUTDOWN
213 try:
214 h.flush()
215 assert False
216 except nbd.Error as ex:
217 assert ex.errnum == errno.ESHUTDOWN
218 h.shutdown()
221 # Noisy status 8 (DISC_SOFT_ERR) fails with explicit EPERM, then next
222 # command gives ESHUTDOWN
223 nbdsh -u "nbd+unix:///-8?socket=$sock" -c - <<\EOF
224 import errno
225 try:
226 h.pwrite(b"1", 0)
227 assert False
228 except nbd.Error as ex:
229 assert ex.errnum == errno.EPERM
230 try:
231 h.flush()
232 assert False
233 except nbd.Error as ex:
234 assert ex.errnum == errno.ESHUTDOWN
235 h.shutdown()