file: Add an internal "mode"
[nbdkit.git] / tests / test-exportname.sh
blobebe17cfa40c924fb2f9e76813f7c52677e92dc12
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 requires_plugin sh
38 requires nbdinfo --version
39 requires_nbdsh_uri
40 requires nbdsh -c 'print(h.set_full_info)'
41 requires jq --version
43 files="exportname.out exportname.sh"
44 rm -f $files
45 cleanup_fn rm -f $files
47 query='[ [.exports[]] | sort_by(."export-name")[] |
48 [."export-name", .description, ."export-size"] ]'
49 fail=0
51 cat >exportname.sh <<\EOF
52 case $1 in
53 list_exports)
54 echo INTERLEAVED
55 echo a; echo x
56 echo b; echo y
57 echo c; echo z
59 default_export) echo a ;;
60 open) echo "$3" ;;
61 export_description | get_size)
62 case $2 in
63 a) echo 1 ;;
64 b) echo 2 ;;
65 c) echo 3 ;;
66 *) exit 1 ;
67 esac ;;
68 *) exit 2 ;;
69 esac
70 EOF
71 chmod +x exportname.sh
73 # Establish a baseline
74 nbdkit -U - sh exportname.sh \
75 --run 'nbdinfo --json --list "$uri"' > exportname.out
76 cat exportname.out
77 test "$(jq -c "$query" exportname.out)" = \
78 '[["a","x",1],["b","y",2],["c","z",3]]'
80 # Set the default export
81 nbdkit -U - --filter=exportname sh exportname.sh default-export= \
82 --run 'nbdinfo --no-content --json "$uri"' > exportname.out
83 cat exportname.out
84 test "$(jq -c "$query" exportname.out)" = '[["","1",1]]'
86 nbdkit -U - --filter=exportname sh exportname.sh default-export=b \
87 --run 'nbdinfo --no-content --json "$uri"' > exportname.out
88 cat exportname.out
89 test "$(jq -c "$query" exportname.out)" = '[["b","2",2]]'
91 # Test export list policies
92 nbdkit -U - --filter=exportname sh exportname.sh exportname-list=keep \
93 --run 'nbdinfo --json --list "$uri"' > exportname.out
94 cat exportname.out
95 test "$(jq -c "$query" exportname.out)" = \
96 '[["a","x",1],["b","y",2],["c","z",3]]'
98 nbdkit -U - --filter=exportname sh exportname.sh exportname-list=error \
99 --run 'nbdinfo --json --list "$uri"' > exportname.out && fail=1 || :
100 test ! -s exportname.out
102 nbdkit -U - --filter=exportname sh exportname.sh exportname-list=empty \
103 --run 'nbdinfo --json --list "$uri"' > exportname.out
104 cat exportname.out
105 test "$(jq -c "$query" exportname.out)" = '[]'
107 nbdkit -U - --filter=exportname sh exportname.sh exportname-list=defaultonly \
108 --run 'nbdinfo --json --list "$uri"' > exportname.out
109 cat exportname.out
110 got="$(jq -c "$query" exportname.out)"
111 # libnbd 1.4.0 and 1.4.1 differ on whether --list grabs description
112 test "$got" = '[["a",null,1]]' || test "$got" = '[["a","1",1]]' || fail=1
114 nbdkit -U - --filter=exportname sh exportname.sh default-export=b \
115 exportname-list=defaultonly exportname=a exportname=b \
116 --run 'nbdinfo --json --list "$uri"' > exportname.out
117 cat exportname.out
118 got="$(jq -c "$query" exportname.out)"
119 test "$got" = '[["b",null,2]]' || test "$got" = '[["b","2",2]]' || fail=1
121 nbdkit -U - --filter=exportname sh exportname.sh \
122 exportname-list=explicit exportname=b exportname=a \
123 --run 'nbdinfo --json --list "$uri"' > exportname.out
124 cat exportname.out
125 got="$(jq -c "$query" exportname.out)"
126 test "$got" = '[["a",null,1],["b",null,2]]' ||
127 test "$got" = '[["a","1",1],["b","2",2]]' || fail=1
129 nbdkit -U - --filter=exportname sh exportname.sh exportname-list=explicit \
130 --run 'nbdinfo --json --list "$uri"' > exportname.out
131 cat exportname.out
132 test "$(jq -c "$query" exportname.out)" = '[]'
134 # Test description modes with lists
135 nbdkit -U - --filter=exportname sh exportname.sh exportdesc=keep \
136 --run 'nbdinfo --json --list "$uri"' > exportname.out
137 cat exportname.out
138 test "$(jq -c "$query" exportname.out)" = \
139 '[["a","x",1],["b","y",2],["c","z",3]]'
141 nbdkit -U - --filter=exportname sh exportname.sh exportdesc=none \
142 --run 'nbdinfo --json --list "$uri"' > exportname.out
143 cat exportname.out
144 test "$(jq -c "$query" exportname.out)" = \
145 '[["a",null,1],["b",null,2],["c",null,3]]'
147 nbdkit -U - --filter=exportname sh exportname.sh exportdesc=fixed:hi \
148 --run 'nbdinfo --json --list "$uri"' > exportname.out
149 cat exportname.out
150 test "$(jq -c "$query" exportname.out)" = \
151 '[["a","hi",1],["b","hi",2],["c","hi",3]]'
153 nbdkit -U - --filter=exportname sh exportname.sh \
154 exportdesc=script:'echo $name$name' \
155 --run 'nbdinfo --json --list "$uri"' > exportname.out
156 cat exportname.out
157 test "$(jq -c "$query" exportname.out)" = \
158 '[["a","aa",1],["b","bb",2],["c","cc",3]]'
160 # Test description modes with connections
161 nbdkit -U - -e c --filter=exportname sh exportname.sh exportdesc=fixed:hi \
162 --run 'nbdinfo --no-content --json "$uri"' > exportname.out
163 cat exportname.out
164 test "$(jq -c "$query" exportname.out)" = '[["c","hi",3]]'
166 nbdkit -U - -e c --filter=exportname sh exportname.sh \
167 exportdesc=script:'echo $name$name' \
168 --run 'nbdinfo --no-content --json "$uri"' > exportname.out
169 cat exportname.out
170 test "$(jq -c "$query" exportname.out)" = '[["c","cc",3]]'
172 # Test strict mode. Tolerate nbdinfo 1.6.2 which gave invalid JSON but 0 status
173 st=0
174 nbdkit -U - --filter=exportname sh exportname.sh exportname-strict=true \
175 --run 'nbdinfo --no-content --json "$uri"' > exportname.out || st=$?
176 cat exportname.out
177 if test $? = 0; then
178 test -s exportname.out && jq -c "$query" exportname.out && fail=1
181 st=0
182 nbdkit -U - --filter=exportname sh exportname.sh exportname-strict=true \
183 exportname=a exportname=b exportname=c \
184 --run 'nbdinfo --no-content --json "$uri"' > exportname.out || st=$?
185 cat exportname.out
186 if test $? = 0; then
187 test -s exportname.out && jq -c "$query" exportname.out && fail=1
190 nbdkit -U - --filter=exportname sh exportname.sh exportname-strict=true \
191 exportname=a exportname=b exportname= default-export=a\
192 --run 'nbdinfo --no-content --json "$uri"' > exportname.out
193 cat exportname.out
194 test "$(jq -c "$query" exportname.out)" = '[["a","1",1]]'
196 nbdkit -U - -e a --filter=exportname sh exportname.sh exportname-strict=true \
197 exportname=a exportname=b exportname=c \
198 --run 'nbdinfo --no-content --json "$uri"' > exportname.out
199 cat exportname.out
200 test "$(jq -c "$query" exportname.out)" = '[["a","1",1]]'
202 exit $fail