File: test-eval-disconnect.sh

package info (click to toggle)
nbdkit 1.46.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,504 kB
  • sloc: ansic: 63,658; sh: 18,717; makefile: 6,814; python: 1,848; cpp: 1,143; perl: 504; ml: 504; tcl: 62
file content (237 lines) | stat: -rwxr-xr-x 6,532 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env bash
# nbdkit
# Copyright Red Hat
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# * Neither the name of Red Hat nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# Check shutdown/disconnect behavior triggered by special status values

source ./functions.sh
set -e
set -x
set -u

requires_plugin eval
requires_nbdsh_uri

sock=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
files="eval-disconnect.pid $sock"
rm -f $files
cleanup_fn rm -f $files

# Start nbdkit with a plugin that fails writes according to the export
# name which must be numeric: a positive value leaves stderr empty, a
# non-positive one outputs EPERM first.  Serve multiple clients.
serve()
{
    rm -f $files
    start_nbdkit -P eval-disconnect.pid -U $sock eval \
        get_size=' echo 1024 ' \
        open=' if test $3 -le 0; then \
                 echo EPERM > $tmpdir/err; echo $((-$3)); \
               else \
                 : > $tmpdir/err; echo $3; \
               fi ' \
        flush=' exit 0 ' \
        pwrite=' cat >/dev/null; cat $tmpdir/err >&2; exit $2 '
}
check_dead()
{
    # Server should die shortly, if not already dead at this point.
    for (( i=0; i < 5; i++ )); do
        kill -s 0 "$(cat eval-disconnect.pid)" || break
        sleep 1
    done
    if [ $i = 5 ]; then
        echo "nbdkit didn't exit fast enough"
        exit 1
    fi
}
serve

# Noisy status 0 (OK) succeeds, despite text to stderr
nbdsh -u "nbd+unix:///0?socket=$sock" -c - <<\EOF
h.pwrite(b"1", 0)
h.flush()
h.shutdown()
EOF

# Silent status 1 (ERROR) fails; nbdkit turns lack of error into EIO
nbdsh -u "nbd+unix:///1?socket=$sock" -c - <<\EOF
import errno
try:
  h.pwrite(b"1", 0)
  assert False
except nbd.Error as ex:
  assert ex.errnum == errno.EIO
h.flush()
h.shutdown()
EOF

# Noisy status 1 (ERROR) fails with supplied EPERM
nbdsh -u "nbd+unix:///-1?socket=$sock" -c - <<\EOF
import errno
try:
  h.pwrite(b"1", 0)
  assert False
except nbd.Error as ex:
  assert ex.errnum == errno.EPERM
h.flush()
h.shutdown()
EOF

# Silent status 4 (SHUTDOWN_OK) kills the server.  It is racy whether client
# sees success or if the connection is killed with libnbd giving ENOTCONN
nbdsh -u "nbd+unix:///4?socket=$sock" -c - <<\EOF
import errno
try:
  h.pwrite(b"1", 0)
except nbd.Error as ex:
  assert ex.errnum == errno.ENOTCONN
EOF
check_dead
serve

# Noisy status 4 (SHUTDOWN_OK) kills the server.  It is racy whether client
# sees success or if the connection is killed with libnbd giving ENOTCONN
nbdsh -u "nbd+unix:///-4?socket=$sock" -c - <<\EOF
import errno
try:
  h.pwrite(b"1", 0)
except nbd.Error as ex:
  assert ex.errnum == errno.ENOTCONN
EOF
check_dead
serve

# Silent status 5 (SHUTDOWN_ERR) kills the server.  It is racy whether client
# sees implied ESHUTDOWN or if the connection dies with libnbd giving ENOTCONN
nbdsh -u "nbd+unix:///5?socket=$sock" -c - <<\EOF
import errno
try:
  h.pwrite(b"1", 0)
  assert False
except nbd.Error as ex:
  assert ex.errnum == errno.ESHUTDOWN or ex.errnum == errno.ENOTCONN
EOF
check_dead
serve

# Noisy status 5 (SHUTDOWN_ERR) kills the server.  It is racy whether client
# sees EPERM or if the connection is killed with libnbd giving ENOTCONN
nbdsh -u "nbd+unix:///-5?socket=$sock" -c - <<\EOF
import errno
try:
  h.pwrite(b"1", 0)
  assert False
except nbd.Error as ex:
  assert ex.errnum == errno.EPERM or ex.errnum == errno.ENOTCONN
EOF
check_dead
serve

# Silent status 6 (DISC_FORCE) kills socket; libnbd detects as ENOTCONN
nbdsh -u "nbd+unix:///6?socket=$sock" -c - <<\EOF
import errno
try:
  h.pwrite(b"1", 0)
  assert False
except nbd.Error as ex:
  assert ex.errnum == errno.ENOTCONN
assert h.aio_is_ready() is False
EOF

# Noisy status 6 (DISC_FORCE) kills socket; libnbd detects as ENOTCONN
nbdsh -u "nbd+unix:///-6?socket=$sock" -c - <<\EOF
import errno
try:
  h.pwrite(b"1", 0)
  assert False
except nbd.Error as ex:
  assert ex.errnum == errno.ENOTCONN
assert h.aio_is_ready() is False
EOF

# Silent status 7 (DISC_SOFT_OK) succeeds, but next command gives ESHUTDOWN
nbdsh -u "nbd+unix:///7?socket=$sock" -c - <<\EOF
import errno
h.pwrite(b"1", 0)
try:
  h.flush()
  assert False
except nbd.Error as ex:
  assert ex.errnum == errno.ESHUTDOWN
h.shutdown()
EOF

# Noisy status 7 (DISC_SOFT_OK) succeeds, but next command gives ESHUTDOWN
nbdsh -u "nbd+unix:///-7?socket=$sock" -c - <<\EOF
import errno
h.pwrite(b"1", 0)
try:
  h.flush()
  assert False
except nbd.Error as ex:
  assert ex.errnum == errno.ESHUTDOWN
h.shutdown()
EOF

# Silent status 8 (DISC_SOFT_ERR) fails with implied ESHUTDOWN, then next
# command gives ESHUTDOWN
nbdsh -u "nbd+unix:///8?socket=$sock" -c - <<\EOF
import errno
try:
  h.pwrite(b"1", 0)
  assert False
except nbd.Error as ex:
  assert ex.errnum == errno.ESHUTDOWN
try:
  h.flush()
  assert False
except nbd.Error as ex:
  assert ex.errnum == errno.ESHUTDOWN
h.shutdown()
EOF

# Noisy status 8 (DISC_SOFT_ERR) fails with explicit EPERM, then next
# command gives ESHUTDOWN
nbdsh -u "nbd+unix:///-8?socket=$sock" -c - <<\EOF
import errno
try:
  h.pwrite(b"1", 0)
  assert False
except nbd.Error as ex:
  assert ex.errnum == errno.EPERM
try:
  h.flush()
  assert False
except nbd.Error as ex:
  assert ex.errnum == errno.ESHUTDOWN
h.shutdown()
EOF