Update Red Hat Copyright Notices
[nbdkit.git] / tests / test-cow-extents1.sh
blobf1e57885f7dcf9921eb1a4e49485418092a932e1
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 # Test does not run on Windows because we must mark the test file
38 # specially so that Windows recognizes it as sparse. We do not have
39 # the tools available usually to do this (plus I also suspect that
40 # Wine does not emulate this properly).
41 if is_windows; then
42 echo "$0: this test needs to be revised to work on Windows"
43 exit 77
46 requires_filter cow
47 requires_plugin file
49 requires test -r /dev/urandom
51 requires $CUT --version
52 requires dd --version
53 requires_nbdinfo
54 requires nbdsh --version
55 requires $STAT --version
56 requires tr --version
57 requires $TRUNCATE --version
59 if ! nbdinfo --help | grep -- --map ; then
60 echo "$0: nbdinfo --map option required to run this test"
61 exit 77
64 base=cow-extents1-base.img
65 pid=cow-extents1.pid
66 out=cow-extents1.out
67 sock=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
68 files="$base $pid $out $sock"
69 rm -f $files
70 cleanup_fn rm -f $files
72 # Create a base file which is half allocated, half sparse.
73 dd if=/dev/urandom of=$base count=128 bs=1K
74 $TRUNCATE -s 4M $base
75 lastmod="$($STAT -c "%y" $base)"
77 # Run nbdkit with a COW overlay.
78 start_nbdkit -P $pid -U $sock --filter=cow file $base
79 uri="nbd+unix:///?socket=$sock"
81 # The map should reflect the base image.
82 nbdinfo --map "$uri" > $out
83 cat $out
84 if [ "$(tr -s ' ' < $out | $CUT -d' ' -f 1-4)" != " 0 131072 0
85 131072 4063232 3" ]; then
86 echo "$0: unexpected initial file map"
87 exit 1
90 # Punch some holes.
91 nbdsh -u "$uri" \
92 -c 'bs = 65536' \
93 -c 'h.trim(bs, bs)' \
94 -c 'h.trim(bs+2, 4*bs-1)' \
95 -c 'h.pwrite(b"1"*bs, 16*bs)' \
96 -c 'h.trim(2*bs, 32*bs)' \
97 -c 'h.pwrite(b"2"*(2*bs), 48*bs)'
99 # The extents map should be fully allocated.
100 nbdinfo --map "$uri" > $out
101 cat $out
102 if [ "$(tr -s ' ' < $out | $CUT -d' ' -f 1-4)" != " 0 65536 0
103 65536 131072 3
104 196608 65536 0
105 262144 65536 3
106 327680 65536 0
107 393216 655360 3
108 1048576 65536 0
109 1114112 2031616 3
110 3145728 131072 0
111 3276800 917504 3" ]; then
112 echo "$0: unexpected trimmed file map"
113 exit 1
116 # The original file must not be modified.
117 currmod="$($STAT -c "%y" $base)"
118 if [ "$lastmod" != "$currmod" ]; then
119 echo "$0: FAILED last modified time of base file changed"
120 exit 1