File: test-eflags.sh

package info (click to toggle)
nbdkit 1.46.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,504 kB
  • sloc: ansic: 63,658; sh: 18,717; makefile: 6,814; python: 1,848; cpp: 1,143; perl: 504; ml: 504; tcl: 62
file content (582 lines) | stat: -rwxr-xr-x 15,071 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/usr/bin/env bash
# nbdkit
# Copyright Red Hat
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# * Neither the name of Red Hat nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# Test export flags.
#
# Run nbdkit with various can_* callbacks defined and with or without
# the -r flag, and check that nbdkit constructs the export flags
# controlling READ_ONLY, ROTATIONAL, SEND_TRIM, etc. as expected.
#
# We use the shell plugin because it gives maximum control over the
# can_* callbacks (at least, max without having to write a C plugin).

source ./functions.sh
set -e
set -x
set -u

requires_run
requires_plugin sh
requires qemu-nbd --version

# This test uses the ‘qemu-nbd --list’ option added in qemu 4.0.
if ! qemu-nbd --help | grep -sq -- --list; then
    echo "$0: skipping because qemu-nbd does not support the --list option"
    exit 77
fi

files="eflags.out eflags.err"
late_args=
rm -f $files
cleanup_fn rm -f $files

# The export flags.
# See also common/protocol/protocol.h
HAS_FLAGS=$((         1 <<  0 ))
READ_ONLY=$((         1 <<  1 ))
SEND_FLUSH=$((        1 <<  2 ))
SEND_FUA=$((          1 <<  3 ))
ROTATIONAL=$((        1 <<  4 ))
SEND_TRIM=$((         1 <<  5 ))
SEND_WRITE_ZEROES=$(( 1 <<  6 ))
SEND_DF=$((           1 <<  7 ))
CAN_MULTI_CONN=$((    1 <<  8 ))
SEND_RESIZE=$((       1 <<  9 ))
SEND_CACHE=$((        1 << 10 ))
SEND_FAST_ZERO=$((    1 << 11 ))

do_nbdkit ()
{
    # Prepend a check for internal caching to the script on stdin.
    { printf %s '
            if test -f $tmpdir/seen_$1; then
                echo "repeat call to $1" >>'"$PWD/eflags.err"'
            else
                touch $tmpdir/seen_$1
            fi
            '; cat; } | nbdkit -v "$@" sh - $late_args \
        --run 'qemu-nbd --list -k $unixsocket' |
        grep -E "flags: 0x" | grep -Eoi '0x[a-f0-9]+' >eflags.out 2>eflags.err
    printf eflags=; cat eflags.out

    # Convert hex flags to decimal and assign it to $eflags.
    eflags=$(printf "%d" $(cat eflags.out))

    # See if nbdkit failed to cache a callback.
    if test -s eflags.err; then
        echo "error: nbdkit did not cache callbacks properly"
        cat eflags.err
        exit 1
    fi
}

fail ()
{
    echo "error: $@ (actual flags were $(printf 0x%x $eflags))"
    exit 1
}

#----------------------------------------------------------------------
# can_write=false
#
# nbdkit supports DF if client requests SR.

do_nbdkit <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF"

#----------------------------------------------------------------------
# --no-sr
# can_write=false
#
# When SR is disabled, so is the DF flag.

do_nbdkit --no-sr <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY"

#----------------------------------------------------------------------
# -r
# can_write=false

do_nbdkit -r <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF"

#----------------------------------------------------------------------
# can_write=true
#
# NBD_FLAG_SEND_WRITE_ZEROES and NBD_FLAG_SEND_FAST_ZERO are set on writable
# connections even when can_zero returns false, because nbdkit reckons it
# can emulate zeroing using pwrite.

do_nbdkit <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|SEND_WRITE_ZEROES|SEND_DF|SEND_FAST_ZERO )) ] ||
    fail "$LINENO: expected HAS_FLAGS|SEND_WRITE_ZEROES|SEND_DF|SEND_FAST_ZERO"

#----------------------------------------------------------------------
# --filter=nozero
# can_write=true
#
# NBD_FLAG_SEND_WRITE_ZEROES is omitted when a filter says so.

do_nbdkit --filter=nozero <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|SEND_DF"

#----------------------------------------------------------------------
# --no=sr
# --filter=nozero
# can_write=true
#
# Absolute minimum in flags.

do_nbdkit --no-sr --filter=nozero <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS )) ] ||
    fail "$LINENO: expected HAS_FLAGS"

#----------------------------------------------------------------------
# -r
# can_write=true
#
# The -r flag overrides the plugin so this behaves as if can_write is
# false.

do_nbdkit -r <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF"

#----------------------------------------------------------------------
# can_write=false
# can_trim=true
# can_zero=true
#
# If writing is not possible then trim and zero are always disabled.

do_nbdkit <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 3 ;;
     can_trim) exit 0 ;;
     can_zero) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF"

#----------------------------------------------------------------------
# -r
# can_write=false
# can_trim=true
# can_zero=true
#
# This is a formality, but check it's the same as above.

do_nbdkit -r <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 3 ;;
     can_trim) exit 0 ;;
     can_zero) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF"

#----------------------------------------------------------------------
# can_write=true
# can_trim=true

do_nbdkit <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     can_trim) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|SEND_TRIM|SEND_WRITE_ZEROES|SEND_DF|SEND_FAST_ZERO )) ] ||
    fail "$LINENO: expected HAS_FLAGS|SEND_TRIM|SEND_WRITE_ZEROES|SEND_DF|SEND_FAST_ZERO"

#----------------------------------------------------------------------
# can_write=true
# is_rotational=true

do_nbdkit <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     is_rotational) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|ROTATIONAL|SEND_WRITE_ZEROES|SEND_DF|SEND_FAST_ZERO )) ] ||
    fail "$LINENO: expected HAS_FLAGS|ROTATIONAL|SEND_WRITE_ZEROES|SEND_DF|SEND_FAST_ZERO"

#----------------------------------------------------------------------
# -r
# can_write=true
# is_rotational=true

do_nbdkit -r <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     is_rotational) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|ROTATIONAL|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|ROTATIONAL|SEND_DF"

#----------------------------------------------------------------------
# can_write=true
# can_fua=native

do_nbdkit <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     can_fua) echo "native" ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|SEND_FUA|SEND_WRITE_ZEROES|SEND_DF|SEND_FAST_ZERO )) ] ||
    fail "$LINENO: expected HAS_FLAGS|SEND_FUA|SEND_WRITE_ZEROES|SEND_DF|SEND_FAST_ZERO"

#----------------------------------------------------------------------
# -r
# can_write=true
# can_fua=native
#
# Setting read-only should ignore can_fua.

do_nbdkit -r <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     can_fua) echo "native" ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF"

#----------------------------------------------------------------------
# -r
# can_write=true
# can_flush=true
#
# Setting read-only does not ignore can_flush.

do_nbdkit -r <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     can_flush) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_FLUSH|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_FLUSH|SEND_DF"

#----------------------------------------------------------------------
# can_write=true
# can_flush=true
#
# When can_flush is true, nbdkit reckons it can emulate fua with flush.

do_nbdkit <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     can_flush) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|SEND_FLUSH|SEND_FUA|SEND_WRITE_ZEROES|SEND_DF|SEND_FAST_ZERO )) ] ||
    fail "$LINENO: expected HAS_FLAGS|SEND_FLUSH|SEND_FUA|SEND_WRITE_ZEROES|SEND_DF|SEND_FAST_ZERO"

#----------------------------------------------------------------------
# can_write=true
# can_flush=true
# can_fua=none
#
# Explicit request for no fua emulation.

do_nbdkit <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     can_flush) exit 0 ;;
     can_fua) echo "none" ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|SEND_FLUSH|SEND_WRITE_ZEROES|SEND_DF|SEND_FAST_ZERO )) ] ||
    fail "$LINENO: expected HAS_FLAGS|SEND_FLUSH|SEND_WRITE_ZEROES|SEND_DF|SEND_FAST_ZERO"

#----------------------------------------------------------------------
# -r
# can_multi_conn=true

do_nbdkit -r <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_multi_conn) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF|CAN_MULTI_CONN )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF|CAN_MULTI_CONN"

#----------------------------------------------------------------------
# -r
# --filter=noparallel serialize=connections
# can_multi_conn=true
#
# A single-threaded server does not allow multiple connections.

late_args="serialize=connections" do_nbdkit -r --filter=noparallel <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_multi_conn) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF"

#----------------------------------------------------------------------
# -r
# thread_model=serialize_connections
# can_multi_conn=true
#
# A single-threaded server does not allow multiple connections.

do_nbdkit -r <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_multi_conn) exit 0 ;;
     thread_model) echo "serialize_connections" ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF"

#----------------------------------------------------------------------
# -r
# can_cache=emulate

do_nbdkit -r <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_cache) echo "emulate" ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF|SEND_CACHE )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF|SEND_CACHE"

#----------------------------------------------------------------------
# -r
# --filter=nocache cache-mode=none
# can_cache=emulate
#
# Filters override the plugin's choice of caching.

late_args="cache-mode=none" do_nbdkit -r --filter=nocache <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_cache) echo "emulate" ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF"

#----------------------------------------------------------------------
# -r
# can_fast_zero=true
#
# Fast zero support isn't advertised without regular zero support

do_nbdkit -r <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_fast_zero) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF"

#----------------------------------------------------------------------
# --filter=nozero
# can_write=true
# can_fast_zero=true
#
# Fast zero support isn't advertised without regular zero support

do_nbdkit --filter=nozero <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     can_fast_zero) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|SEND_DF"

#----------------------------------------------------------------------
# can_write=true
# can_zero=true
#
# Fast zero support is omitted for a plugin that has .zero but did not opt in

do_nbdkit -r <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     can_zero) exit 0 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF"

#----------------------------------------------------------------------
# can_write=true
# can_zero=true
# can_fast_zero=false
#
# Fast zero support is omitted if the plugin says so

do_nbdkit -r <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     can_zero) exit 0 ;;
     can_fast_zero) exit 3 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF"

#----------------------------------------------------------------------
# can_write=true
# can_zero=false
# can_fast_zero=false
#
# Fast zero support is omitted if the plugin says so

do_nbdkit -r <<'EOF'
case "$1" in
     get_size) echo 1M ;;
     can_write) exit 0 ;;
     can_fast_zero) exit 3 ;;
     *) exit 2 ;;
esac
EOF

[ $eflags -eq $(( HAS_FLAGS|READ_ONLY|SEND_DF )) ] ||
    fail "$LINENO: expected HAS_FLAGS|READ_ONLY|SEND_DF"