tests: Add tests that just including the header works.
[nbdkit/ericb.git] / tests / test-eflags.sh
blob84dcfe895bcb5ee25c11f89bdc77bbb9de350045
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 # Test export flags.
35 # Run nbdkit with various can_* callbacks defined and with or without
36 # the -r flag, and check that nbdkit constructs the export flags
37 # controlling READ_ONLY, ROTATIONAL, SEND_TRIM, etc. as expected.
39 # We use the shell plugin because it gives maximum control over the
40 # can_* callbacks (at least, max without having to write a C plugin).
42 source ./functions.sh
43 set -e
45 requires qemu-nbd --version
47 # This test uses the ‘qemu-nbd --list’ option added in qemu 4.0.
48 if ! qemu-nbd --help | grep -sq -- --list; then
49 echo "$0: skipping because qemu-nbd does not support the --list option"
50 exit 77
53 files="eflags.out"
54 rm -f $files
55 cleanup_fn rm -f $files
57 # The export flags.
58 # See also common/protocol/protocol.h
59 HAS_FLAGS=$(( 1 << 0 ))
60 READ_ONLY=$(( 1 << 1 ))
61 SEND_FLUSH=$(( 1 << 2 ))
62 SEND_FUA=$(( 1 << 3 ))
63 ROTATIONAL=$(( 1 << 4 ))
64 SEND_TRIM=$(( 1 << 5 ))
65 SEND_WRITE_ZEROES=$(( 1 << 6 ))
66 SEND_DF=$(( 1 << 7 ))
67 CAN_MULTI_CONN=$(( 1 << 8 ))
68 SEND_RESIZE=$(( 1 << 9 ))
69 SEND_CACHE=$(( 1 << 10 ))
71 do_nbdkit ()
73 nbdkit -v -U - "$@" sh - --run 'qemu-nbd --list -k $unixsocket' |
74 grep -E "flags: 0x" | grep -Eoi '0x[a-f0-9]+' > eflags.out
75 echo -n eflags=; cat eflags.out
77 # Convert hex flags to decimal and assign it to $eflags.
78 eflags=$(printf "%d" $(cat eflags.out))
81 fail ()
83 echo "error: $@ (actual flags were $eflags)"
84 exit 1
87 #----------------------------------------------------------------------
88 # can_write=false
90 do_nbdkit <<'EOF'
91 case "$1" in
92 get_size) echo 1M ;;
93 *) exit 2 ;;
94 esac
95 EOF
97 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
98 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF"
100 #----------------------------------------------------------------------
101 # -r
102 # can_write=false
104 do_nbdkit -r <<'EOF'
105 case "$1" in
106 get_size) echo 1M ;;
107 *) exit 2 ;;
108 esac
111 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
112 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF"
114 #----------------------------------------------------------------------
115 # can_write=true
117 # NBD_FLAG_SEND_WRITE_ZEROES is always set on writable connections
118 # even if can_zero returns false, because nbdkit reckons it can
119 # emulate zeroing using pwrite.
121 do_nbdkit <<'EOF'
122 case "$1" in
123 get_size) echo 1M ;;
124 can_write) exit 0 ;;
125 *) exit 2 ;;
126 esac
129 [ $eflags -eq $(( HAS_FLAGS|SEND_WRITE_ZEROES|SEND_DF )) ] ||
130 fail "expected HAS_FLAGS|SEND_WRITE_ZEROES|SEND_DF"
132 #----------------------------------------------------------------------
133 # -r
134 # can_write=true
136 # The -r flag overrides the plugin so this behaves as if can_write is
137 # false.
139 do_nbdkit -r <<'EOF'
140 case "$1" in
141 get_size) echo 1M ;;
142 can_write) exit 0 ;;
143 *) exit 2 ;;
144 esac
147 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
148 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF"
150 #----------------------------------------------------------------------
151 # can_write=false
152 # can_trim=true
153 # can_zero=true
155 # If writing is not possible then trim and zero are always disabled.
157 do_nbdkit <<'EOF'
158 case "$1" in
159 get_size) echo 1M ;;
160 can_write) exit 3 ;;
161 can_trim) exit 0 ;;
162 can_zero) exit 0 ;;
163 *) exit 2 ;;
164 esac
167 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
168 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF"
170 #----------------------------------------------------------------------
171 # -r
172 # can_write=false
173 # can_trim=true
174 # can_zero=true
176 # This is a formality, but check it's the same as above.
178 do_nbdkit -r <<'EOF'
179 case "$1" in
180 get_size) echo 1M ;;
181 can_write) exit 3 ;;
182 can_trim) exit 0 ;;
183 can_zero) exit 0 ;;
184 *) exit 2 ;;
185 esac
188 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
189 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF"
191 #----------------------------------------------------------------------
192 # can_write=true
193 # can_trim=true
195 do_nbdkit <<'EOF'
196 case "$1" in
197 get_size) echo 1M ;;
198 can_write) exit 0 ;;
199 can_trim) exit 0 ;;
200 *) exit 2 ;;
201 esac
204 [ $eflags -eq $(( HAS_FLAGS|SEND_TRIM|SEND_WRITE_ZEROES|SEND_DF )) ] ||
205 fail "expected HAS_FLAGS|SEND_TRIM|SEND_WRITE_ZEROES|SEND_DF"
207 #----------------------------------------------------------------------
208 # can_write=true
209 # is_rotational=true
211 do_nbdkit <<'EOF'
212 case "$1" in
213 get_size) echo 1M ;;
214 can_write) exit 0 ;;
215 is_rotational) exit 0 ;;
216 *) exit 2 ;;
217 esac
220 [ $eflags -eq $(( HAS_FLAGS|ROTATIONAL|SEND_WRITE_ZEROES|SEND_DF )) ] ||
221 fail "expected HAS_FLAGS|ROTATIONAL|SEND_WRITE_ZEROES|SEND_DF"
223 #----------------------------------------------------------------------
224 # -r
225 # can_write=true
226 # is_rotational=true
228 do_nbdkit -r <<'EOF'
229 case "$1" in
230 get_size) echo 1M ;;
231 can_write) exit 0 ;;
232 is_rotational) exit 0 ;;
233 *) exit 2 ;;
234 esac
237 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|ROTATIONAL|SEND_DF )) ] ||
238 fail "expected HAS_FLAGS|READ_ONLY|ROTATIONAL|SEND_DF"
240 #----------------------------------------------------------------------
241 # can_write=true
242 # can_fua=native
244 do_nbdkit <<'EOF'
245 case "$1" in
246 get_size) echo 1M ;;
247 can_write) exit 0 ;;
248 can_fua) echo "native" ;;
249 *) exit 2 ;;
250 esac
253 [ $eflags -eq $(( HAS_FLAGS|SEND_FUA|SEND_WRITE_ZEROES|SEND_DF )) ] ||
254 fail "expected HAS_FLAGS|SEND_FUA|SEND_WRITE_ZEROES|SEND_DF"
256 #----------------------------------------------------------------------
257 # -r
258 # can_write=true
259 # can_fua=native
261 # Setting read-only should ignore can_fua.
263 do_nbdkit -r <<'EOF'
264 case "$1" in
265 get_size) echo 1M ;;
266 can_write) exit 0 ;;
267 can_fua) echo "native" ;;
268 *) exit 2 ;;
269 esac
272 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
273 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF"
275 #----------------------------------------------------------------------
276 # -r
277 # can_multi_conn=true
279 do_nbdkit -r <<'EOF'
280 case "$1" in
281 get_size) echo 1M ;;
282 can_multi_conn) echo 0 ;;
283 *) exit 2 ;;
284 esac
287 [ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF|CAN_MULTI_CONN )) ] ||
288 fail "expected HAS_FLAGS|READ_ONLY|SEND_DF|CAN_MULTI_CONN"