Update Red Hat Copyright Notices
[nbdkit.git] / tests / test-eval-exports.sh
blob6a21d4fd5f928aea404250427cecc4458eafd9aa
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright Red Hat
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 # This is an example from the nbdkit-eval-plugin(1) manual page.
34 # Check here that it doesn't regress.
36 source ./functions.sh
37 set -e
38 set -x
40 requires_plugin eval
41 requires_nbdinfo
42 requires_nbdsh_uri
43 requires nbdsh -c 'print(h.set_full_info)'
44 requires jq --version
46 sock=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
47 files="eval-exports.list eval-exports.out eval-exports.pid $sock"
48 rm -f $files
49 cleanup_fn rm -f $files
50 fail=0
52 # Control case: no .list_exports, which defaults to advertising ""
53 rm -f eval-exports.list
54 nbdkit -U - -v eval \
55 open='[ "$3" = "" ] || { echo EINVAL wrong export >&2; exit 1; }' \
56 get_size='echo 0' --run 'nbdinfo --list --json "$uri"' > eval-exports.out
57 cat eval-exports.out
58 diff -u <(jq -c \
59 '[.exports[] | [."export-name", .description, ."export-size"]]' \
60 eval-exports.out) <(printf %s\\n '[["",null,0]]')
62 # Start a long-running server with .list_exports and .default_export
63 # set to varying contents
64 start_nbdkit -P eval-exports.pid -U $sock eval get_size='echo "$2"|wc -c' \
65 open='echo "$3"' list_exports="cat '$PWD/eval-exports.list'" \
66 default_export="cat '$PWD/eval-exports.list'"
68 # do_nbdkit EXPNAME EXPOUT
69 do_nbdkit ()
71 # Check how the default export name is handled
72 nbdinfo --no-content nbd+unix://\?socket=$sock >eval-exports.out
73 diff -u <($SED -n 's/export="\(.*\)":/\1/p' eval-exports.out) \
74 <(printf %s\\n "$1")
75 # Check what exports are listed
76 nbdinfo --list --json nbd+unix://\?socket=$sock >eval-exports.out
77 cat eval-exports.out
78 diff -u <(jq -c \
79 '[.exports[] | [."export-name", .description, ."export-size"]]' \
80 eval-exports.out) <(printf %s\\n "$2")
83 # With no file, .list_exports and .default_export both fail, preventing
84 # connection to the default export, but not other exports
85 nbdinfo --list --json nbd+unix://\?socket=$sock && fail=1
86 nbdsh -c '
87 import os
88 try:
89 h.connect_uri("nbd+unix://?socket='"$sock"'")
90 exit(1)
91 except nbd.Error:
92 pass
93 ' || fail=1
94 nbdsh -u nbd+unix:///name\?socket=$sock -c 'quit()'
96 # Setting .default_export but not .list_exports advertises the canonical name
97 nbdkit -U - eval default_export='echo hello' get_size='echo 0' \
98 --run 'nbdinfo --list "$uri"' >eval-exports.out
99 diff -u <(grep '^export=' eval-exports.out) <(echo 'export="hello":')
101 # Failing .default_export without .list_exports results in an empty list
102 nbdkit -U - eval default_export='echo ENOENT >&2; exit 1' get_size='echo 0' \
103 --run 'nbdinfo --list "$uri"' >eval-exports.out
104 diff -u <(grep '^export=' eval-exports.out) /dev/null
106 # Various spellings of empty lists, producing 0 exports
107 for fmt in '' 'NAMES\n' 'INTERLEAVED\n' 'NAMES+DESCRIPTIONS\n'; do
108 printf "$fmt" >eval-exports.list
109 do_nbdkit '' '[]'
110 done
112 # Various spellings of explicit list for the default export, no description
113 for fmt in '\n' 'NAMES\n\n' 'INTERLEAVED\n\n' 'INTERLEAVED\n\n\n' \
114 'NAMES+DESCRIPTIONS\n\n' 'NAMES+DESCRIPTIONS\n\n\n'; do
115 printf "$fmt" >eval-exports.list
116 do_nbdkit '' '[["",null,1]]'
117 done
119 # A non-default name
120 for fmt in 'name\n' 'NAMES\nname\n'; do
121 printf "$fmt" >eval-exports.list
122 do_nbdkit name '[["name",null,5]]'
123 done
125 # One export with a description
126 for fmt in 'INTERLEAVED\nname\ndesc\n' 'NAMES+DESCRIPTIONS\nname\ndesc\n'; do
127 printf "$fmt" >eval-exports.list
128 do_nbdkit name '[["name","desc",5]]'
129 done
131 # Multiple exports, with correct number of lines
132 for fmt in 'INTERLEAVED\nname 1\ndesc 1\nname two\ndesc 2\n' \
133 'NAMES+DESCRIPTIONS\nname 1\nname two\ndesc 1\ndesc 2\n'; do
134 printf "$fmt" >eval-exports.list
135 do_nbdkit 'name 1' '[["name 1","desc 1",7],["name two","desc 2",9]]'
136 done
138 # Multiple exports, with final description line missing
139 for fmt in 'INTERLEAVED\nname 1\ndesc 1\nname two\n' \
140 'NAMES+DESCRIPTIONS\nname 1\nname two\ndesc 1\n'; do
141 printf "$fmt" >eval-exports.list
142 do_nbdkit 'name 1' '[["name 1","desc 1",7],["name two",null,9]]'
143 done
145 # Largest possible name and description
146 long=$(printf %04096d 1)
147 echo NAMES+DESCRIPTIONS >eval-exports.list
148 echo $long >>eval-exports.list
149 echo $long >>eval-exports.list
150 do_nbdkit $long "[[\"$long\",\"$long\",4097]]"
152 # Invalid name (too long) causes an error response to NBD_OPT_LIST
153 nbdkit -U - -v eval list_exports="echo 2$long" \
154 get_size='echo 0' --run 'nbdinfo --list --json "$uri"' && fail=1
156 exit $fail