Update Red Hat Copyright Notices
[nbdkit.git] / tests / test-extentlist.sh
blob2e5aa93beaefb213353aa54ae63e680d3d29da14
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 # Test the extentlist filter.
35 source ./functions.sh
36 set -e
37 set -x
39 requires_run
40 requires jq --version
41 requires qemu-img --version
42 requires qemu-img map --help
44 out=test-extentlist.out
45 input=test-extentlist.in
46 expected=test-extentlist.expected
47 files="$out $input $expected"
48 rm -f $files
49 cleanup_fn rm $files
51 test ()
53 nbdkit -v -D extentlist.lookup=1 \
54 -r -U - \
55 --filter=extentlist \
56 null size=$1 extentlist=$input \
57 --run 'qemu-img map -f raw --output=json $nbd' |
58 jq -c '.[] | {start:.start, length:.length, data:.data, zero:.zero}' \
59 > $out
60 diff -u $out $expected
63 # Empty extent list.
64 cat > $input <<'EOF'
65 EOF
67 cat > $expected <<'EOF'
68 {"start":0,"length":0,"data":false,"zero":false}
69 EOF
70 test 0
71 cat > $expected <<'EOF'
72 {"start":0,"length":1048576,"data":false,"zero":true}
73 EOF
74 test 1M
76 # Extent list covering 0-1M with data.
77 cat > $input <<'EOF'
78 0 1M
79 EOF
81 cat > $expected <<'EOF'
82 {"start":0,"length":0,"data":false,"zero":false}
83 EOF
84 test 0
85 cat > $expected <<'EOF'
86 {"start":0,"length":1048576,"data":true,"zero":false}
87 EOF
88 test 1M
90 # Extent list covering 1-2M with data.
91 cat > $input <<'EOF'
92 1M 1M
93 EOF
95 cat > $expected <<'EOF'
96 {"start":0,"length":0,"data":false,"zero":false}
97 EOF
98 test 0
99 cat > $expected <<'EOF'
100 {"start":0,"length":1048576,"data":false,"zero":true}
102 test 1M
103 cat > $expected <<'EOF'
104 {"start":0,"length":1048576,"data":false,"zero":true}
105 {"start":1048576,"length":1048576,"data":true,"zero":false}
107 test 2M
108 cat > $expected <<'EOF'
109 {"start":0,"length":1048576,"data":false,"zero":true}
110 {"start":1048576,"length":1048576,"data":true,"zero":false}
111 {"start":2097152,"length":1048576,"data":false,"zero":true}
113 test 3M
115 # Extent list covering 1-2M with data, but in a more fragmented
116 # way than the above.
117 cat > $input <<'EOF'
118 1024K 512K
119 1536K 512K
122 cat > $expected <<'EOF'
123 {"start":0,"length":0,"data":false,"zero":false}
125 test 0
126 cat > $expected <<'EOF'
127 {"start":0,"length":1048576,"data":false,"zero":true}
129 test 1M
130 cat > $expected <<'EOF'
131 {"start":0,"length":1048576,"data":false,"zero":true}
132 {"start":1048576,"length":1048576,"data":true,"zero":false}
134 test 2M
135 cat > $expected <<'EOF'
136 {"start":0,"length":1048576,"data":false,"zero":true}
137 {"start":1048576,"length":1048576,"data":true,"zero":false}
138 {"start":2097152,"length":1048576,"data":false,"zero":true}
140 test 3M
142 # Adjacent data and holes.
143 cat > $input <<'EOF'
144 0 1M
145 2M 1M
146 4M 1M
149 cat > $expected <<'EOF'
150 {"start":0,"length":0,"data":false,"zero":false}
152 test 0
153 cat > $expected <<'EOF'
154 {"start":0,"length":1048576,"data":true,"zero":false}
156 test 1M
157 cat > $expected <<'EOF'
158 {"start":0,"length":1048576,"data":true,"zero":false}
159 {"start":1048576,"length":1048576,"data":false,"zero":true}
161 test 2M
162 cat > $expected <<'EOF'
163 {"start":0,"length":1048576,"data":true,"zero":false}
164 {"start":1048576,"length":1048576,"data":false,"zero":true}
165 {"start":2097152,"length":1048576,"data":true,"zero":false}
167 test 3M
168 cat > $expected <<'EOF'
169 {"start":0,"length":1048576,"data":true,"zero":false}
170 {"start":1048576,"length":1048576,"data":false,"zero":true}
171 {"start":2097152,"length":1048576,"data":true,"zero":false}
172 {"start":3145728,"length":1048576,"data":false,"zero":true}
173 {"start":4194304,"length":1048576,"data":true,"zero":false}
174 {"start":5242880,"length":1048576,"data":false,"zero":true}
176 test 6M