file: Add an internal "mode"
[nbdkit.git] / tests / test-blocksize-extents.sh
blob4b770049c2902749998ca2c33cba83a2fbcece3d
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 # Demonstrate a fix for a bug where blocksize used to cause extents failures
35 source ./functions.sh
36 set -e
37 set -x
39 requires_plugin eval
40 requires_nbdsh_uri
41 requires nbdsh --base-allocation
43 # Script a server that requires 512-byte aligned requests, reports only one
44 # extent at a time, and with a hole placed unaligned to 4k bounds
45 exts='
46 if test $3 -gt $(( 32 * 1024 )); then
47 echo "EINVAL request too large" 2>&1
48 exit 1
50 if test $(( ($3|$4) & 511 )) != 0; then
51 echo "EINVAL request unaligned" 2>&1
52 exit 1
54 type=
55 if test $(( $4 >= 512 && $4 < 8 * 1024 )) = 1; then
56 type=hole,zero
58 echo $4 512 $type
61 # We also need an nbdsh script to parse all extents, coalescing adjacent
62 # types for simplicity, as well as testing some unaligned probes.
63 export script='
64 size = h.get_size()
65 offs = 0
66 entries = []
67 def f(metacontext, offset, e, err):
68 global entries
69 global offs
70 assert offs == offset
71 for length, flags in zip(*[iter(e)] * 2):
72 if entries and flags == entries[-1][1]:
73 entries[-1] = (entries[-1][0] + length, flags)
74 else:
75 entries.append((length, flags))
76 offs = offs + length
78 # Test a loop over the entire device
79 while offs < size:
80 h.block_status(size - offs, offs, f)
81 assert entries == [(4096, 0), (4096, 3), (57344, 0)]
83 # Unaligned status queries must also work
84 offs = 1
85 entries = []
86 h.block_status(1, offs, f, nbd.CMD_FLAG_REQ_ONE)
87 assert entries == [(1, 0)]
89 offs = 512
90 entries = []
91 h.block_status(512, offs, f)
92 assert entries == [(3584, 0)]
94 offs = 4095
95 entries=[]
96 while offs < 4097:
97 h.block_status(4097 - offs, offs, f, nbd.CMD_FLAG_REQ_ONE)
98 assert entries == [(1, 0), (1, 3)]
101 # Now run everything
102 nbdkit -U - --filter=blocksize eval minblock=4k maxlen=32k \
103 get_size='echo 64k' pread='exit 1' extents="$exts" \
104 --run 'nbdsh --base-allocation -u "$uri" -c "$script"'