3 # Copyright (C) 2018-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
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
33 # Test the use of the directory mode of the file plugin.
39 # The dir parameter does not exist in the Windows version
42 echo "$0: this test needs to be revised to work on Windows"
46 requires nbdinfo
--version
48 requires nbdsh
-c 'print(h.set_full_info)'
51 files
="file-dir file-dir.out"
53 cleanup_fn
rm -rf $files
56 # do_nbdkit_list [--no-sort] EXPOUT
57 # Check that the advertised list of exports matches EXPOUT
61 if [ "$1" = --no-sort ]; then
65 nbdkit
-U - -v file directory
=file-dir \
66 --run 'nbdinfo --list --json "$uri"' >file-dir.out
68 diff -u <(jq
-c '[.exports[]."export-name"]'"$sort" file-dir.out
) \
69 <(printf %s
\\n
"$1") || fail
=1
72 nbdsh_connect_fail_script
='
75 h.connect_uri(os.environ["uri"])
82 # Check that attempting to connect to export NAME fails
85 nbdkit
-U - -v -e "$1" file dir
=file-dir \
86 --run 'export uri; nbdsh -c "$nbdsh_connect_fail_script"' || fail
=1
89 # do_nbdkit_pass NAME DATA
90 # Check that export NAME serves DATA as its first byte
93 out
=$
(nbdkit
-U - -v -e "$1" file dir
=file-dir \
94 --run 'nbdsh -u "$uri" -c "print(h.pread(1, 0).decode(\"utf-8\"))"')
95 test "$out" = "$2" || fail
=1
98 # Not possible to serve a missing directory
99 nbdkit
-vf file dir
=nosuchdir
&& fail
=1
101 # Serving an empty directory
109 # Serving a directory with one file
111 do_nbdkit_list
'["a"]'
116 # Serving a directory with multiple files.
117 # Use 'find' to match readdir's raw order (a is not always first!)
119 raw
=$
(find file-dir
-type f |
xargs echo)
120 exp
=$
(echo $raw |
$SED 's,file-dir/\(.\),"\1",g; s/ /,/')
121 do_nbdkit_list
--no-sort "[$exp]"
122 do_nbdkit_list
'["a","b"]'
128 # Serving a directory with non-regular files
132 ln -s /dev
/null file-dir
/f
134 ln -s dangling file-dir
/h
135 do_nbdkit_list
'["a","b","c"]'
145 do_nbdkit_fail
'../file-dir/a'