Update Red Hat Copyright Notices
[nbdkit.git] / tests / test-sh-extents.sh
blobf8a06b098e6232b0394cc00faa107ac49c21e46b
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 sh
38 requires jq --version
39 requires qemu-img --version
40 requires qemu-img map --help
42 out="test-sh-extents.out"
43 expected="test-sh-extents.expected"
44 files="$out $expected"
45 rm -f $files
46 cleanup_fn rm -f $files
48 do_test ()
50 # We use jq to normalize the output and convert it to plain text.
51 nbdkit -v -U - sh - --run 'qemu-img map -f raw --output=json $nbd' |
52 jq -c '.[] | {start:.start, length:.length, data:.data, zero:.zero}' \
53 > $out
54 if ! cmp $out $expected; then
55 echo "$0: output did not match expected data"
56 echo "expected:"
57 cat $expected
58 echo "output:"
59 cat $out
60 exit 1
64 # Completely sparse disk.
65 cat > $expected <<'EOF'
66 {"start":0,"length":65536,"data":false,"zero":true}
67 EOF
68 do_test <<'EOF'
69 case "$1" in
70 thread_model) echo parallel ;;
71 get_size) echo 64K ;;
72 can_extents) exit 0 ;;
73 extents)
74 echo "0 64K hole,zero"
76 *) exit 2 ;;
77 esac
78 EOF
80 # Completely allocated disk.
81 # type field missing means allocated data.
82 cat > $expected <<'EOF'
83 {"start":0,"length":65536,"data":true,"zero":false}
84 EOF
85 do_test <<'EOF'
86 case "$1" in
87 thread_model) echo parallel ;;
88 get_size) echo 64K ;;
89 can_extents) exit 0 ;;
90 extents)
91 echo "0 64K"
93 *) exit 2 ;;
94 esac
95 EOF
97 # Completely allocated disk.
98 # type=0
99 cat > $expected <<'EOF'
100 {"start":0,"length":65536,"data":true,"zero":false}
102 do_test <<'EOF'
103 case "$1" in
104 thread_model) echo parallel ;;
105 get_size) echo 64K ;;
106 can_extents) exit 0 ;;
107 extents)
108 echo "0 64K 0"
110 *) exit 2 ;;
111 esac
114 # Allocated data at the start of a 1M disk.
115 cat > $expected <<'EOF'
116 {"start":0,"length":32768,"data":true,"zero":false}
117 {"start":32768,"length":1015808,"data":false,"zero":true}
119 do_test <<'EOF'
120 case "$1" in
121 thread_model) echo parallel ;;
122 get_size) echo 1M ;;
123 can_extents) exit 0 ;;
124 extents)
125 echo "0 32K"
126 echo "32K 1015808 hole,zero"
128 *) exit 2 ;;
129 esac
132 # Zeroes at the start of a 1M disk.
133 cat > $expected <<'EOF'
134 {"start":0,"length":32768,"data":true,"zero":true}
135 {"start":32768,"length":32768,"data":false,"zero":true}
136 {"start":65536,"length":983040,"data":true,"zero":false}
138 do_test <<'EOF'
139 case "$1" in
140 thread_model) echo parallel ;;
141 get_size) echo 1M ;;
142 can_extents) exit 0 ;;
143 extents)
144 echo "0 32K zero"
145 echo "32K 32K hole,zero"
146 echo "64K 983040 "
148 *) exit 2 ;;
149 esac
152 # If can_extents is not defined, extents should never be called.
153 cat > $expected <<'EOF'
154 {"start":0,"length":65536,"data":true,"zero":false}
156 do_test <<'EOF'
157 case "$1" in
158 thread_model) echo parallel ;;
159 get_size) echo 64K ;;
160 extents) exit 1 ;;
161 *) exit 2 ;;
162 esac