server: Allow file descriptors to be passed to nbdkit_read_password.
[nbdkit/ericb.git] / tests / test-blocksize.sh
blob4e6dbe52eebfb6488ce107c39d6b98e15afbdb4a
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 files="blocksize1.img blocksize1.log $sock1 blocksize1.pid
39 blocksize2.img blocksize2.log $sock2 blocksize2.pid"
40 rm -f $files
42 # Prep images, and check that qemu-io understands the actions we plan on doing.
43 # TODO: Until we implement NBD_INFO_BLOCK_SIZE, qemu-io does its own
44 # read-modify-write at 512-byte alignment, while we'd like to ultimately test
45 # 1-byte accesses
46 truncate -s 10M blocksize1.img
47 if ! qemu-io -f raw -c 'r 0 1' -c 'w -z 1000 2000' \
48 -c 'w -P 0 1M 2M' -c 'discard 3M 4M' blocksize1.img; then
49 echo "$0: missing or broken qemu-io"
50 rm blocksize1.img
51 exit 77
53 truncate -s 10M blocksize2.img
55 # For easier debugging, dump the final log files before removing them
56 # on exit.
57 cleanup ()
59 echo "Log 1 file contents:"
60 cat blocksize1.log || :
61 echo "Log 2 file contents:"
62 cat blocksize2.log || :
63 rm -f $files
65 cleanup_fn cleanup
67 # Run two parallel nbdkit; to compare the logs and see what changes.
68 start_nbdkit -P blocksize1.pid -U $sock1 \
69 --filter=log file logfile=blocksize1.log blocksize1.img
70 start_nbdkit -P blocksize2.pid -U $sock2 --filter=blocksize \
71 --filter=log file logfile=blocksize2.log blocksize2.img \
72 minblock=1024 maxdata=512k maxlen=1M
74 # Test behavior on short accesses.
75 qemu-io -f raw -c 'r 1 1' -c 'w 10001 1' -c 'w -z 20001 1' \
76 -c 'discard 30001 1' "nbd+unix://?socket=$sock1"
77 qemu-io -f raw -c 'r 1 1' -c 'w 10001 1' -c 'w -z 20001 1' \
78 -c 'discard 30001 1' "nbd+unix://?socket=$sock2"
80 # Read should round up (qemu-io may round to 512, but we must round to 1024
81 grep 'connection=1 Read .* count=0x\(1\|200\) ' blocksize1.log ||
82 { echo "qemu-io can't pass 1-byte reads"; exit 77; }
83 grep 'connection=1 Read .* offset=0x0 count=0x400 ' blocksize2.log
84 # Write should become read-modify-write
85 grep 'connection=1 Write .* count=0x\(1\|200\) ' blocksize1.log ||
86 { echo "qemu-io can't pass 1-byte writes"; exit 77; }
87 grep 'connection=1 Read .* offset=0x2400 count=0x400 ' blocksize2.log
88 grep 'connection=1 Write .* offset=0x2400 count=0x400 ' blocksize2.log
89 # Zero should become read-modify-write
90 if grep 'connection=1 Zero' blocksize2.log; then
91 echo "filter should have converted short zero to write"
92 exit 1
94 grep 'connection=1 Read .* offset=0x4c00 count=0x400 ' blocksize2.log
95 grep 'connection=1 Write .* offset=0x4c00 count=0x400 ' blocksize2.log
96 # Trim should be discarded
97 if grep 'connection=1 Trim' blocksize2.log; then
98 echo "filter should have dropped too-small trim"
99 exit 1
102 # Test behavior on overlarge accesses.
103 qemu-io -f raw -c 'w -P 11 1048575 4094305' -c 'w -z 1050000 1100000' \
104 -c 'r -P 0 1050000 1100000' -c 'r -P 11 3000000 1048577' \
105 -c 'discard 7340031 2097153' "nbd+unix://?socket=$sock1"
106 qemu-io -f raw -c 'w -P 11 1048575 4094305' -c 'w -z 1050000 1100000' \
107 -c 'r -P 0 1050000 1100000' -c 'r -P 11 3000000 1048577' \
108 -c 'discard 7340031 2097153' "nbd+unix://?socket=$sock2"
110 # Reads and writes should have been split.
111 test "$(grep -c '\(Read\|Write\) .*count=0x80000 ' blocksize2.log)" -ge 10
112 test "$(grep -c '\(Read\|Write\) .*count=0x[0-9a-f]\{6\} ' blocksize2.log)" = 0
113 # Zero and trim should be split, but at different boundary
114 grep 'Zero .*count=0x100000 ' blocksize2.log
115 test "$(grep -c 'connection=2 Zero' blocksize2.log)" = 2
116 if grep Trim blocksize1.log; then
117 test "$(grep -c 'connection=2 Trim .*count=0x100000 ' blocksize2.log)" = 2
120 # Final sanity checks.
121 if grep 'offset=0x[0-9a-f]*\([1235679abdef]00\|[0-9a-f]\(.[^0]\|[^0].\)\) ' \
122 blocksize2.log; then
123 echo "filter didn't align offset to 1024";
124 exit 1;
126 if grep 'count=0x[0-9a-f]*\([1235679abdef]00\|[0-9a-f]\(.[^0]\|[^0].\)\) ' \
127 blocksize2.log; then
128 echo"filter didn't align count to 512";
129 exit 1;
131 diff -u blocksize1.img blocksize2.img