docs: Move release notes to their own section.
[nbdkit/ericb.git] / tests / test-sh-extents.sh
blobb2f2816f88784cbde1516d89807b3b6128dfb6ed
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright (C) 2018-2019 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 source ./functions.sh
34 set -e
35 set -x
37 requires jq --version
38 requires qemu-img --version
39 requires qemu-img map --help
41 out="test-sh-extents.out"
42 expected="test-sh-extents.expected"
43 files="$out $expected"
44 rm -f $files
45 cleanup_fn rm -f $files
47 do_test ()
49 # We use jq to normalize the output and convert it to plain text.
50 nbdkit -v -U - sh - --run 'qemu-img map -f raw --output=json $nbd' |
51 jq -c '.[] | {start:.start, length:.length, data:.data, zero:.zero}' \
52 > $out
53 if ! cmp $out $expected; then
54 echo "$0: output did not match expected data"
55 echo "expected:"
56 cat $expected
57 echo "output:"
58 cat $out
59 exit 1
63 # Completely sparse disk.
64 cat > $expected <<'EOF'
65 {"start":0,"length":65536,"data":false,"zero":true}
66 EOF
67 do_test <<'EOF'
68 case "$1" in
69 thread_model) echo parallel ;;
70 get_size) echo 64K ;;
71 can_extents) exit 0 ;;
72 extents)
73 echo "0 64K hole,zero"
75 *) exit 2 ;;
76 esac
77 EOF
79 # Completely allocated disk.
80 # type field missing means allocated data.
81 cat > $expected <<'EOF'
82 {"start":0,"length":65536,"data":true,"zero":false}
83 EOF
84 do_test <<'EOF'
85 case "$1" in
86 thread_model) echo parallel ;;
87 get_size) echo 64K ;;
88 can_extents) exit 0 ;;
89 extents)
90 echo "0 64K"
92 *) exit 2 ;;
93 esac
94 EOF
96 # Completely allocated disk.
97 # type=0
98 cat > $expected <<'EOF'
99 {"start":0,"length":65536,"data":true,"zero":false}
101 do_test <<'EOF'
102 case "$1" in
103 thread_model) echo parallel ;;
104 get_size) echo 64K ;;
105 can_extents) exit 0 ;;
106 extents)
107 echo "0 64K 0"
109 *) exit 2 ;;
110 esac
113 # Allocated data at the start of a 1M disk.
114 cat > $expected <<'EOF'
115 {"start":0,"length":32768,"data":true,"zero":false}
116 {"start":32768,"length":1015808,"data":false,"zero":true}
118 do_test <<'EOF'
119 case "$1" in
120 thread_model) echo parallel ;;
121 get_size) echo 1M ;;
122 can_extents) exit 0 ;;
123 extents)
124 echo "0 32K"
125 echo "32K 1015808 hole,zero"
127 *) exit 2 ;;
128 esac
131 # Zeroes at the start of a 1M disk.
132 cat > $expected <<'EOF'
133 {"start":0,"length":32768,"data":true,"zero":true}
134 {"start":32768,"length":32768,"data":false,"zero":true}
135 {"start":65536,"length":983040,"data":true,"zero":false}
137 do_test <<'EOF'
138 case "$1" in
139 thread_model) echo parallel ;;
140 get_size) echo 1M ;;
141 can_extents) exit 0 ;;
142 extents)
143 echo "0 32K zero"
144 echo "32K 32K hole,zero"
145 echo "64K 983040 "
147 *) exit 2 ;;
148 esac
151 # If can_extents is not defined, extents should never be called.
152 cat > $expected <<'EOF'
153 {"start":0,"length":65536,"data":true,"zero":false}
155 do_test <<'EOF'
156 case "$1" in
157 thread_model) echo parallel ;;
158 get_size) echo 64K ;;
159 extents) exit 1 ;;
160 *) exit 2 ;;
161 esac