Update Red Hat Copyright Notices
[nbdkit.git] / tests / test-nbd-extents.sh
blob3c5a96720c818371b947cc259dffc3ebd8117b8a
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 # Unfortunately the output of this test depends on the PAGE_SIZE
34 # defined in common/allocators/sparse.c and would change (breaking the
35 # test) if we ever changed that definition.
37 source ./functions.sh
38 set -e
39 set -x
41 requires jq --version
42 requires qemu-img --version
43 requires qemu-img map --help
45 # Because of macOS SIP misfeature the DYLD_* environment variable
46 # added by libnbd/run is filtered out and the test won't work. Skip
47 # it entirely on Macs.
48 requires_not test "$(uname)" = "Darwin"
50 out="test-nbd-extents.out"
51 expected="test-nbd-extents.expected"
52 sock=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
53 pid1="test-nbd-extents.pid1"
54 pid2="test-nbd-extents.pid2"
55 pid3="test-nbd-extents.pid3"
56 pid4="test-nbd-extents.pid4"
57 pid5="test-nbd-extents.pid5"
58 files="$out $expected $sock $pid1 $pid2 $pid3 $pid4 $pid5"
59 rm -f $files
60 cleanup_fn rm -f $files
62 do_test ()
64 start_nbdkit -P "$4" -U "$sock" \
65 --filter=truncate \
66 data "$1" size="$2" \
67 truncate="$3"
68 # We use jq to normalize the output and convert it to plain text.
69 nbdkit -U - nbd socket="$sock" \
70 --run 'qemu-img map -f raw --output=json $nbd' |
71 jq -c '.[] | {start:.start, length:.length, data:.data, zero:.zero}' \
72 > $out
73 rm -f "$sock"
74 if ! cmp $out $expected; then
75 echo "$0: output did not match expected data"
76 echo "expected:"
77 cat $expected
78 echo "output:"
79 cat $out
80 exit 1
84 # Completely sparse disk.
85 cat > $expected <<'EOF'
86 {"start":0,"length":65536,"data":false,"zero":true}
87 EOF
88 do_test "" 1M 65536 "$pid1"
90 # Completely allocated disk.
91 cat > $expected <<'EOF'
92 {"start":0,"length":32768,"data":true,"zero":false}
93 EOF
94 do_test "1 @32768 1 @65536 1 @98304 1" 128K 32768 "$pid2"
96 #----------------------------------------------------------------------
97 # The above are the easy cases. Now let's truncate to a larger
98 # size which should create a hole at the end.
100 # Completely sparse disk.
101 cat > $expected <<'EOF'
102 {"start":0,"length":1048576,"data":false,"zero":true}
104 do_test "" 65536 1M "$pid3"
106 # Completely allocated disk.
107 cat > $expected <<'EOF'
108 {"start":0,"length":512,"data":true,"zero":false}
109 {"start":512,"length":1048064,"data":false,"zero":true}
111 do_test "1" 512 1M "$pid4"
113 # Zero-length plugin. Unlike nbdkit-zero-plugin, the data plugin
114 # advertises extents and so will behave differently.
115 cat > $expected <<'EOF'
116 {"start":0,"length":0,"data":false,"zero":false}
118 do_test "" 0 0 "$pid5"