tests: vsock: Use VMADDR_CID_LOCAL for loopback testing.
[nbdkit/ericb.git] / tests / test-fua.sh
blob1c869e9666a7c30159bf1b7979cea9b1219a7bd3
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
35 set -x
37 sockdir=`mktemp -d`
38 files="fua.img
39 fua1.log fua1.pid
40 fua2.log fua2.pid
41 fua3.log fua3.pid
42 fua4.log fua4.pid"
43 rm -f $files
45 # Prep images, and check that qemu-io understands the actions we plan on
46 # doing. We can't test trim+FUA, since qemu-io won't expose that.
47 truncate -s 1M fua.img
48 if ! qemu-io -f raw -t none -c flush -c 'w -f -z 0 64k' fua.img; then
49 echo "$0: missing or broken qemu-io"
50 rm fua.img
51 exit 77
54 # For easier debugging, dump the final log files before removing them
55 # on exit.
56 cleanup ()
58 echo "Log 1 file contents:"
59 cat fua1.log || :
60 echo "Log 2 file contents:"
61 cat fua2.log || :
62 echo "Log 3 file contents:"
63 cat fua3.log || :
64 echo "Log 4 file contents:"
65 cat fua4.log || :
66 rm -f $files
67 rm -rf $sockdir
69 cleanup_fn cleanup
71 # Run four parallel nbdkit; to compare the logs and see what changes.
72 # 1: fuamode=none (default): client should send flush instead
73 # 2: fuamode=emulate: log shows that blocksize optimizes fua to flush
74 # 3: fuamode=native: log shows that blocksize preserves fua
75 # 4: fuamode=force: log shows that fua is always enabled
76 start_nbdkit -P fua1.pid -U $sockdir/fua1.sock \
77 --filter=log --filter=fua \
78 file logfile=fua1.log fua.img
79 start_nbdkit -P fua2.pid -U $sockdir/fua2.sock \
80 --filter=blocksize --filter=log --filter=fua \
81 file logfile=fua2.log fua.img fuamode=emulate maxdata=4k maxlen=4k
82 start_nbdkit -P fua3.pid -U $sockdir/fua3.sock \
83 --filter=blocksize --filter=log --filter=fua \
84 file logfile=fua3.log fua.img fuamode=native maxdata=4k maxlen=4k
85 start_nbdkit -P fua4.pid -U $sockdir/fua4.sock \
86 --filter=fua --filter=log \
87 file logfile=fua4.log fua.img fuamode=force
89 # Perform a flush, write, and zero, first without then with FUA
90 for f in '' -f; do
91 for i in {1..4}; do
92 qemu-io -f raw -t none -c flush -c "w $f 0 64k" -c "w -z $f 64k 64k" \
93 "nbd+unix://?socket=$sockdir/fua$i.sock"
94 done
95 done
97 # Test 1: no fua sent over wire, qemu-io sent more flushes in place of fua
98 if grep 'fua=1' fua1.log; then
99 echo "filter should have prevented fua"
100 exit 1
102 test $(grep -c 'connection=1 Flush' fua1.log) -lt \
103 $(grep -c 'connection=2 Flush' fua1.log)
105 # Test 2: either last part of split has fua, or a flush is added, but
106 # all earlier parts of the transaction do not have fua
107 flush1=$(grep -c 'connection=1 Flush' fua2.log || :)
108 flush2=$(grep -c 'connection=2 Flush' fua2.log || :)
109 fua=$(grep -c 'connection=2.*fua=1 .*\.' fua2.log || :)
110 test $(( $flush2 - $flush1 + $fua )) = 2
112 # Test 3: every part of split has fua, and no flushes are added
113 flush1=$(grep -c 'connection=1 Flush' fua3.log || :)
114 flush2=$(grep -c 'connection=2 Flush' fua3.log || :)
115 test $flush1 = $flush2
116 test $(grep -c 'connection=2.*fua=1 .*\.' fua3.log) = 32
118 # Test 4: flush is no-op, and every transaction has fua
119 if grep 'fua=0' fua4.log; then
120 echo "filter should have forced fua"
121 exit 1
123 if grep 'Flush' fua4.log; then
124 echo "filter should have elided flush"
125 exit 1