Update Red Hat Copyright Notices
[nbdkit.git] / tests / test-swab-extents.sh
blobf4ea9f0e97e615b039762b4de905019f3bebbdd2
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 effects of swab on extents.
35 source ./functions.sh
36 set -e
37 set -x
39 requires_plugin eval
40 requires_nbdsh_uri
41 requires nbdsh --base-allocation
43 files="swab-extents.out swab-extents.exp8 swab-extents.exp16
44 swab-extents.exp32 swab-extents.exp64"
45 rm -f $files
46 cleanup_fn rm -f $files
47 fail=0
49 # We will set up a plugin that provides the following fine-grained extents
50 # pattern, using [D]ata 0, [H]ole 1, [Z]ero 2, or [B]oth 3
51 # DDDDDDDD DDDDDDDZ ZZZZZZZB BBBBBBBH HZZBBBBH HHHHHHHD
52 # in order to see how swab-bits affects rounding to alignment:
53 # 8 DDDDDDDD DDDDDDDZ ZZZZZZZB BBBBBBBH HZZBBBBH HHHHHHHD
54 # 16 DDDDDDDD DDDDDDDD ZZZZZZZZ BBBBBBHH DDZZBBHH HHHHHHDD
55 # 32 DDDDDDDD DDDDDDDD ZZZZZZZZ BBBBHHHH DDDDHHHH HHHHDDDD
56 # 64 DDDDDDDD DDDDDDDD ZZZZZZZZ HHHHHHHH DDDDDDDD DDDDDDDD
58 cat > swab-extents.exp8 <<EOF
59 [(15, 0), (8, 2), (8, 3), (2, 1), (2, 2), (4, 3), (8, 1), (1, 0)]
60 EOF
61 cat > swab-extents.exp16 <<EOF
62 [(16, 0), (8, 2), (6, 3), (2, 1), (2, 0), (2, 2), (2, 3), (8, 1), (2, 0)]
63 EOF
64 cat > swab-extents.exp32 <<EOF
65 [(16, 0), (8, 2), (4, 3), (4, 1), (4, 0), (8, 1), (4, 0)]
66 EOF
67 cat > swab-extents.exp64 <<EOF
68 [(16, 0), (8, 2), (8, 1), (16, 0)]
69 EOF
71 # We also want to test plugins that supply all v. just one extent per call
72 all='
73 echo " 0 7"
74 echo " 7 8"
75 echo "15 8 zero"
76 echo "23 8 zero,hole"
77 echo "31 2 hole"
78 echo "33 2 zero"
79 echo "35 4 zero,hole"
80 echo "39 8 hole"
81 echo "47 1"
83 one='case $4 in
84 0 | 1 | 2 | 3 | 4 | 5 | 6 ) echo " 0 7" ;;
85 7 |\
86 8 | 9 | 10 | 11 | 12 | 13 | 14 ) echo " 7 8" ;;
87 15 |\
88 16 | 17 | 18 | 19 | 20 | 21 | 22 ) echo "15 8 zero" ;;
89 23 |\
90 24 | 25 | 26 | 27 | 28 | 29 | 30 ) echo "23 8 zero,hole" ;;
91 31 |\
92 32 ) echo "31 2 hole" ;;
93 33 | 34 ) echo "33 2 zero" ;;
94 35 | 36 | 37 | 38 ) echo "35 4 zero,hole" ;;
95 39 |\
96 40 | 41 | 42 | 43 | 44 | 45 | 46 ) echo "39 8 hole" ;;
97 47 ) echo "47 1" ;;
98 esac'
100 # We also need an nbdsh script to parse all extents, coalescing adjacent
101 # types for simplicity
102 export script='
103 size = h.get_size()
104 offs = 0
105 entries = []
106 def f(metacontext, offset, e, err):
107 global entries
108 global offs
109 assert offs == offset
110 for length, flags in zip(*[iter(e)] * 2):
111 if entries and flags == entries[-1][1]:
112 entries[-1] = (entries[-1][0] + length, flags)
113 else:
114 entries.append((length, flags))
115 offs = offs + length
116 while offs < size:
117 h.block_status(size - offs, offs, f)
118 print(entries)
121 # Now to test the combinations:
122 for bits in 8 16 32 64; do
123 for exts in "$all" "$one"; do
124 nbdkit -U - --filter=swab eval swab-bits=$bits \
125 get_size='echo 48' pread='exit 1' extents="$exts" \
126 --run 'nbdsh --base-allocation -u "$uri" -c "$script"' \
127 > swab-extents.out || fail=1
128 diff -u swab-extents.exp$bits swab-extents.out || fail=1
129 done
130 done
132 exit $fail