rust: prevent dead_code warning
[nbdkit.git] / tests / test-nozero.sh
blob42663a170c32a7872bbb06d0d6a47013c418eb69
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright (C) 2018-2022 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 $STAT --version
39 sock2=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
40 sock3=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
41 sock4a=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
42 sock4b=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
43 sock5=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
44 files="nozero1.img nozero1.log
45 nozero2.img nozero2.log $sock2 nozero2.pid
46 nozero3.img nozero3.log $sock3 nozero3.pid
47 nozero4.img nozero4a.log nozero4b.log $sock4a $sock4b
48 nozero4a.pid nozero4b.pid
49 nozero5.img nozero5.log $sock5 nozero5.pid"
50 rm -f $files
51 fail=0
53 # For easier debugging, dump the final log files before removing them
54 # on exit.
55 cleanup ()
57 echo "Log 1 file contents:"
58 cat nozero1.log || :
59 echo "Log 2 file contents:"
60 cat nozero2.log || :
61 echo "Log 3 file contents:"
62 cat nozero3.log || :
63 echo "Log 4a file contents:"
64 cat nozero4a.log || :
65 echo "Log 4b file contents:"
66 cat nozero4b.log || :
67 echo "Log 5 file contents:"
68 cat nozero5.log || :
69 rm -f $files
71 cleanup_fn cleanup
73 # Prep images.
74 declare -a sizes
75 printf %$((2*1024*1024))s . > nozero1.img
76 cp nozero1.img nozero2.img
77 cp nozero1.img nozero3.img
78 cp nozero1.img nozero4.img
79 cp nozero1.img nozero5.img
81 # Debug number of blocks and block size in the images.
82 for f in {1..5}; do
83 $STAT -c "%n: %b allocated blocks of size %B bytes, total size %s" \
84 nozero$f.img
85 sizes[$f]=$($STAT -c %b nozero$f.img)
86 done
88 # Check that zero with trim results in a sparse image.
89 requires nbdkit -U - --filter=log file logfile=nozero1.log nozero1.img \
90 --run 'nbdsh -u "$uri" -c "h.zero(1024*1024, 0)"'
91 if test "$($STAT -c %b nozero1.img)" = "${sizes[1]}"; then
92 echo "$0: can't trim file by writing zeroes"
93 exit 77
96 # Run several parallel nbdkit; to compare the logs and see what changes.
97 # 1: unfiltered (above), check that nbdsh sends ZERO request and plugin trims
98 # 2: log before filter with zeromode=none (default), to ensure no ZERO request
99 # 3: log after filter with zeromode=emulate, to ensure no ZERO to plugin
100 # 4a/b: both sides of nbd plugin: even though server side does not advertise
101 # ZERO, the client side still exposes it, and just skips calling nbd's .zero
102 # 5: log after filter with zeromode=notrim, to ensure plugin does not trim
103 start_nbdkit -P nozero2.pid -U $sock2 --filter=log --filter=nozero \
104 file logfile=nozero2.log nozero2.img
105 start_nbdkit -P nozero3.pid -U $sock3 --filter=nozero --filter=log \
106 file logfile=nozero3.log nozero3.img zeromode=emulate
107 # Start 4b before 4a so that cleanup visits the client before the server
108 start_nbdkit -P nozero4b.pid -U $sock4b --filter=log \
109 nbd logfile=nozero4b.log socket=$sock4a
110 start_nbdkit -P nozero4a.pid -U $sock4a --filter=log --filter=nozero \
111 file logfile=nozero4a.log nozero4.img
112 start_nbdkit -P nozero5.pid -U $sock5 --filter=nozero --filter=log \
113 file logfile=nozero5.log nozero5.img zeromode=notrim
115 # Perform the zero write.
116 nbdsh -u "nbd+unix://?socket=$sock2" -c '
117 assert not h.can_zero()
118 h.pwrite (bytearray(1024*1024), 0)
120 nbdsh -u "nbd+unix://?socket=$sock3" -c 'h.zero(1024*1024, 0)'
121 nbdsh -u "nbd+unix://?socket=$sock4b" -c 'h.zero(1024*1024, 0)'
122 nbdsh -u "nbd+unix://?socket=$sock5" -c 'h.zero(1024*1024, 0)'
124 # Check for expected ZERO vs. WRITE results
125 grep 'connection=1 Zero' nozero1.log || fail=1
126 if grep 'connection=1 Zero' nozero2.log; then
127 echo "filter should have prevented zero"
128 fail=1
130 if grep 'connection=1 Zero' nozero3.log; then
131 echo "filter should have converted zero into write"
132 fail=1
134 if grep 'connection=1 Zero' nozero4a.log; then
135 echo "nbdkit should have converted zero into write before nbd plugin"
136 fail=1
138 grep 'connection=1 Zero' nozero5.log
140 # Sanity check on contents - all 5 files should read identically
141 cmp nozero1.img nozero2.img
142 cmp nozero2.img nozero3.img
143 cmp nozero3.img nozero4.img
144 cmp nozero4.img nozero5.img
146 # Sanity check on sparseness: images 2-5 should not be sparse (although the
147 # filesystem may have reserved additional space due to our writes)
148 for i in {2..5}; do
149 if test "$($STAT -c %b nozero$i.img)" -lt "${sizes[$i]}"; then
150 echo "nozero$i.img was trimmed by mistake"
151 fail=1
153 done
155 exit $fail