Update Red Hat Copyright Notices
[nbdkit.git] / tests / test-blocksize-write-disconnect.sh
blob1d3c1326c46883ae136d711cb200880be9d63502
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 source ./functions.sh
34 set -e
35 set -x
37 requires_plugin eval
38 requires nbdsh -c 'print(h.get_block_size)'
39 requires nbdsh -c 'print(h.get_strict_mode)'
40 requires_nbdsh_uri
41 requires dd iflag=count_bytes </dev/null
43 # Libnbd does not let us test pwrite larger than 64M, so we can't
44 # test nbdkit's graceful behavior of writes up to 128M.
45 # In this test, odd size writes fail with EINVAL from the filter (size 1 too
46 # small, all others unaligned); evens 2 to 8M pass, 8M+2 to 16M fail with
47 # ENOMEM from the plugin, 16M+2 to 32M fail with EINVAL from the filter,
48 # 32M+1 to 64M kill the connection (ENOTCONN visible to client), and
49 # 64M+1 and above fails with ERANGE in libnbd.
51 nbdkit -v -U - eval \
52 block_size="echo 2 4096 16M" \
53 get_size="echo 64M" \
54 pread=' dd if=/dev/zero count=$3 iflag=count_bytes ' \
55 pwrite='
56 dd of=/dev/null 2>/dev/null
57 if test $3 -gt $((8*1024*1024)); then
58 echo ENOMEM >&2; exit 1
60 ' \
61 --filter=blocksize-policy \
62 blocksize-error-policy=error blocksize-write-disconnect=32M \
63 --run '
64 nbdsh -u "$uri" -c "
65 import errno
66 import sys
68 def header(msg):
69 print(\"=== %s ===\" % msg)
70 sys.stdout.flush()
72 def check(h, size, expect_value, expect_traffic=True):
73 print(\"Testing size=%d\" % size)
74 sys.stdout.flush()
76 assert h.aio_is_ready() is True
78 buf = b\"0\" * size
79 if hasattr(h, \"stats_bytes_sent\"):
80 start = h.stats_bytes_sent()
81 try:
82 h.pwrite(buf, 0)
83 assert expect_value == 0
84 except nbd.Error as ex:
85 assert expect_value == ex.errnum
86 if hasattr(h, \"stats_bytes_sent\"):
87 if expect_traffic:
88 assert h.stats_bytes_sent() > start
89 else:
90 assert h.stats_bytes_sent() == start
92 h.set_strict_mode(0) # Bypass client-side safety checks
93 header(\"Beyond 64M\")
94 check(h, 64*1024*1024 + 1, errno.ERANGE, False)
95 check(h, 64*1024*1024 + 2, errno.ERANGE, False)
96 header(\"Small reads\")
97 check(h, 1, errno.EINVAL)
98 check(h, 2, 0)
99 header(\"Near 8M boundary\")
100 check(h, 8*1024*1024 - 2, 0)
101 check(h, 8*1024*1024 - 1, errno.EINVAL)
102 check(h, 8*1024*1024, 0)
103 check(h, 8*1024*1024 + 1, errno.EINVAL)
104 check(h, 8*1024*1024 + 2, errno.ENOMEM)
105 header(\"Near 16M boundary\")
106 check(h, 16*1024*1024 - 2, errno.ENOMEM)
107 check(h, 16*1024*1024 - 1, errno.EINVAL)
108 check(h, 16*1024*1024, errno.ENOMEM)
109 check(h, 16*1024*1024 + 1, errno.EINVAL)
110 check(h, 16*1024*1024 + 2, errno.EINVAL)
111 header(\"Near 32M boundary\")
112 check(h, 32*1024*1024 - 2, errno.EINVAL)
113 check(h, 32*1024*1024 - 1, errno.EINVAL)
114 check(h, 32*1024*1024, errno.EINVAL)
115 check(h, 32*1024*1024 + 1, errno.ENOTCONN)
116 assert h.aio_is_ready() is False