rust: prevent dead_code warning
[nbdkit.git] / tests / test-nbd-dynamic-list.sh
blob9f85a84787b8049b43bb67052d12c2e994e04be6
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright (C) 2019-2020 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 # This test works with newer libnbd, showing that dynamic mode affects
38 # export listing.
39 requires_plugin sh
40 requires_nbdsh_uri
41 requires nbdinfo --version
43 # Does the nbd plugin support dynamic lists?
44 if ! nbdkit --dump-plugin nbd | grep -sq libnbd_dynamic_list=1; then
45 echo "$0: nbd plugin built without dynamic export list support"
46 exit 77
49 base=test-nbd-dynamic-list
50 sock1=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
51 sock2=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
52 pid1="$base.pid1"
53 pid2="$base.pid2"
54 files="$sock1 $sock2 $pid1 $pid2 $base.list $base.out1 $base.out2 $base.err"
55 rm -f $files
56 cleanup_fn rm -f $files
58 fail=0
60 # Start a long-running server with .list_exports and .default_export
61 # set to varying contents
62 start_nbdkit -P $pid1 -U $sock1 eval get_size='echo "$2"|wc -c' \
63 open='echo "$3"' list_exports="cat '$PWD/$base.list'" \
64 default_export="cat '$PWD/$base.list'"
66 # Long-running nbd bridge, which should pass export list through
67 start_nbdkit -P $pid2 -U $sock2 nbd socket=$sock1 dynamic-export=true
69 # check_success_one EXPORT
70 # - nbdinfo of EXPORT on both servers should succeed, with matching output
71 check_success_one ()
73 nbdinfo --no-content "nbd+unix:///$1?socket=$sock1" |
74 grep -v uri: > $base.out1
75 nbdinfo --no-content "nbd+unix:///$1?socket=$sock2" |
76 grep -v uri: > $base.out2
77 cat $base.out2
78 diff -u $base.out1 $base.out2
81 # check_success_list
82 # - nbdinfo --list on both servers should succeed, with matching output
83 check_success_list ()
85 nbdinfo --list --json nbd+unix://\?socket=$sock1 |
86 grep -v '"uri"' > $base.out1
87 nbdinfo --list --json nbd+unix://\?socket=$sock2 |
88 grep -v '"uri"' > $base.out2
89 cat $base.out2
90 diff -u $base.out1 $base.out2
93 # check_success EXPORT... - both sub-tests, on all EXPORTs
94 check_success()
96 for exp; do
97 check_success_one "$exp"
98 done
99 check_success_list
102 # check_fail_one EXPORT
103 # - nbdinfo of EXPORT on both servers should fail
104 check_fail_one ()
106 # Work around nbdinfo 1.6.2 bug that had error message but 0 status
107 if nbdinfo --no-content "nbd+unix:///$1?socket=$sock1" > $base.out1 \
108 2> $base.err && ! grep "server replied with error" $base.err; then
109 fail=1
111 if nbdinfo --no-content "nbd+unix:///$1?socket=$sock2" > $base.out2 \
112 2> $base.err && ! grep "server replied with error" $base.err; then
113 fail=1
117 # check_fail_list
118 # - nbdinfo --list on both servers should fail
119 check_fail_list ()
121 if nbdinfo --list --json nbd+unix://\?socket=$sock1 > $base.out1; then
122 fail=1
124 if nbdinfo --list --json nbd+unix://\?socket=$sock2 > $base.out2; then
125 fail=1
129 # With no file, list_exports and the default export fail,
130 # but other exports work
131 check_fail_one ""
132 check_success_one name
133 check_fail_list
135 # With an empty list, there are 0 exports, and any export works
136 touch $base.list
137 check_success "" name
139 # An explicit advertisement of the default export, any export works
140 echo > $base.list
141 check_success "" name
143 # A non-empty default name
144 echo name > $base.list
145 check_success "" name
147 # Multiple exports, with descriptions
148 cat > $base.list <<EOF
149 INTERLEAVED
150 name1
151 desc1
152 name2
153 desc2
155 echo name > $base.list
156 check_success "" name1
158 # Longest possible name and description
159 long=$(printf %04096d 1)
160 echo NAMES+DESCRIPTIONS > $base.list
161 echo $long >> $base.list
162 echo $long >> $base.list
163 check_success "" $long
165 # An invalid name prevents list, but we can still connect
166 echo 2$long >> $base.list
167 check_success_one ""
168 check_fail_list
170 exit $fail