tests: vsock: Use VMADDR_CID_LOCAL for loopback testing.
[nbdkit/ericb.git] / tests / test-nozero.sh
blobfc22420bf5a65da68ac58e0f1effd2a29a97ea6d
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright (C) 2018 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
36 sock1=`mktemp -u`
37 sock2=`mktemp -u`
38 sock3=`mktemp -u`
39 sock4=`mktemp -u`
40 sock5a=`mktemp -u`
41 sock5b=`mktemp -u`
42 sock6=`mktemp -u`
43 files="nozero1.img nozero1.log $sock1 nozero1.pid
44 nozero2.img nozero2.log $sock2 nozero2.pid
45 nozero3.img nozero3.log $sock3 nozero3.pid
46 nozero4.img nozero4.log $sock4 nozero4.pid
47 nozero5.img nozero5a.log nozero5b.log $sock5a $sock5b
48 nozero5a.pid nozero5b.pid
49 nozero6.img nozero6.log $sock6 nozero6.pid"
50 rm -f $files
52 # Prep images, and check that qemu-io understands the actions we plan on
53 # doing, and that zero with trim results in a sparse image.
54 for f in {0..1023}; do printf '%1024s' . >> nozero1.img; done
55 cp nozero1.img nozero2.img
56 cp nozero1.img nozero3.img
57 cp nozero1.img nozero4.img
58 cp nozero1.img nozero5.img
59 cp nozero1.img nozero6.img
60 if ! qemu-io -f raw -d unmap -c 'w -z -u 0 1M' nozero1.img; then
61 echo "$0: missing or broken qemu-io"
62 rm nozero?.img
63 exit 77
65 if test "$(stat -c %b nozero1.img)" = "$(stat -c %b nozero2.img)"; then
66 echo "$0: can't trim file by writing zeroes"
67 rm nozero?.img
68 exit 77
70 cp nozero2.img nozero1.img
72 # For easier debugging, dump the final log files before removing them
73 # on exit.
74 cleanup ()
76 echo "Log 1 file contents:"
77 cat nozero1.log || :
78 echo "Log 2 file contents:"
79 cat nozero2.log || :
80 echo "Log 3 file contents:"
81 cat nozero3.log || :
82 echo "Log 4 file contents:"
83 cat nozero4.log || :
84 echo "Log 5a file contents:"
85 cat nozero5a.log || :
86 echo "Log 5b file contents:"
87 cat nozero5b.log || :
88 echo "Log 6 file contents:"
89 cat nozero6.log || :
90 rm -f $files
92 cleanup_fn cleanup
94 # Run several parallel nbdkit; to compare the logs and see what changes.
95 # 1: unfiltered, to check that qemu-io sends ZERO request and plugin trims
96 # 2: log before filter with zeromode=none (default), to ensure no ZERO request
97 # 3: log before filter with zeromode=emulate, to ensure ZERO from client
98 # 4: log after filter with zeromode=emulate, to ensure no ZERO to plugin
99 # 5a/b: both sides of nbd plugin: even though server side does not advertise
100 # ZERO, the client side still exposes it, and just skips calling nbd's .zero
101 # 6: log after filter with zeromode=notrim, to ensure plugin does not trim
102 start_nbdkit -P nozero1.pid -U $sock1 --filter=log \
103 file logfile=nozero1.log nozero1.img
104 start_nbdkit -P nozero2.pid -U $sock2 --filter=log --filter=nozero \
105 file logfile=nozero2.log nozero2.img
106 start_nbdkit -P nozero3.pid -U $sock3 --filter=log --filter=nozero \
107 file logfile=nozero3.log nozero3.img zeromode=emulate
108 start_nbdkit -P nozero4.pid -U $sock4 --filter=nozero --filter=log \
109 file logfile=nozero4.log nozero4.img zeromode=emulate
110 start_nbdkit -P nozero5a.pid -U $sock5a --filter=log --filter=nozero \
111 file logfile=nozero5a.log nozero5.img
112 start_nbdkit -P nozero5b.pid -U $sock5b --filter=log \
113 nbd logfile=nozero5b.log socket=$sock5a
114 start_nbdkit -P nozero6.pid -U $sock6 --filter=nozero --filter=log \
115 file logfile=nozero6.log nozero6.img zeromode=notrim
117 # Perform the zero write.
118 qemu-io -f raw -c 'w -z -u 0 1M' "nbd+unix://?socket=$sock1"
119 qemu-io -f raw -c 'w -z -u 0 1M' "nbd+unix://?socket=$sock2"
120 qemu-io -f raw -c 'w -z -u 0 1M' "nbd+unix://?socket=$sock3"
121 qemu-io -f raw -c 'w -z -u 0 1M' "nbd+unix://?socket=$sock4"
122 qemu-io -f raw -c 'w -z -u 0 1M' "nbd+unix://?socket=$sock5b"
123 qemu-io -f raw -c 'w -z -u 0 1M' "nbd+unix://?socket=$sock6"
125 # Check for expected ZERO vs. WRITE results
126 grep 'connection=1 Zero' nozero1.log
127 if grep 'connection=1 Zero' nozero2.log; then
128 echo "filter should have prevented zero"
129 exit 1
131 grep 'connection=1 Zero' nozero3.log
132 if grep 'connection=1 Zero' nozero4.log; then
133 echo "filter should have converted zero into write"
134 exit 1
136 grep 'connection=1 Zero' nozero5b.log
137 if grep 'connection=1 Zero' nozero5a.log; then
138 echo "nbdkit should have converted zero into write before nbd plugin"
139 exit 1
141 grep 'connection=1 Zero' nozero6.log
143 # Sanity check on contents - all 5 files should read identically
144 cmp nozero1.img nozero2.img
145 cmp nozero2.img nozero3.img
146 cmp nozero3.img nozero4.img
147 cmp nozero4.img nozero5.img
148 cmp nozero5.img nozero6.img
150 # Sanity check on sparseness; only image 1 should be sparse
151 if test "$(stat -c %b nozero1.img)" = "$(stat -c %b nozero2.img)"; then
152 echo "nozero1.img was not trimmed"
153 exit 1
155 for i in 3 4 5 6; do
156 if test "$(stat -c %b nozero2.img)" != "$(stat -c %b nozero$i.img)"; then
157 echo "nozero$i.img was trimmed by mistake"
158 exit 1
160 done