ci: Update .gitlab-ci.yml
[nbdkit.git] / tests / test-file-dir.sh
blobe393e47a08f970fda7a7f1193c653571c18f5bf5
1 #!/usr/bin/env bash
2 # nbdkit
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
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 # Test the use of the directory mode of the file plugin.
35 source ./functions.sh
36 set -e
37 set -x
39 # The dir parameter does not exist in the Windows version
40 # of the file plugin.
41 if is_windows; then
42 echo "$0: this test needs to be revised to work on Windows"
43 exit 77
46 requires nbdinfo --version
47 requires_nbdsh_uri
48 requires nbdsh -c 'print(h.set_full_info)'
49 requires jq --version
51 files="file-dir file-dir.out"
52 rm -rf $files
53 cleanup_fn rm -rf $files
54 fail=0
56 # do_nbdkit_list [--no-sort] EXPOUT
57 # Check that the advertised list of exports matches EXPOUT
58 do_nbdkit_list ()
60 sort=' | sort'
61 if [ "$1" = --no-sort ]; then
62 sort=
63 shift
65 nbdkit -U - -v file directory=file-dir \
66 --run 'nbdinfo --list --json "$uri"' >file-dir.out
67 cat 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='
73 import os
74 try:
75 h.connect_uri(os.environ["uri"])
76 exit(1)
77 except nbd.Error:
78 pass
81 # do_nbdkit_fail NAME
82 # Check that attempting to connect to export NAME fails
83 do_nbdkit_fail ()
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
91 do_nbdkit_pass ()
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
102 mkdir file-dir
103 do_nbdkit_list '[]'
104 do_nbdkit_fail ''
105 do_nbdkit_fail 'a'
106 do_nbdkit_fail '..'
107 do_nbdkit_fail '/'
109 # Serving a directory with one file
110 echo 1 > file-dir/a
111 do_nbdkit_list '["a"]'
112 do_nbdkit_fail ''
113 do_nbdkit_pass a 1
114 do_nbdkit_fail b
116 # Serving a directory with multiple files.
117 # Use 'find' to match readdir's raw order (a is not always first!)
118 echo 2 > file-dir/b
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"]'
123 do_nbdkit_fail ''
124 do_nbdkit_pass 'a' 1
125 do_nbdkit_pass 'b' 2
126 do_nbdkit_fail 'c'
128 # Serving a directory with non-regular files
129 ln -s b file-dir/c
130 mkfifo file-dir/d
131 mkdir file-dir/e
132 ln -s /dev/null file-dir/f
133 ln -s . file-dir/g
134 ln -s dangling file-dir/h
135 do_nbdkit_list '["a","b","c"]'
136 do_nbdkit_pass 'a' 1
137 do_nbdkit_pass 'b' 2
138 do_nbdkit_pass 'c' 2
139 do_nbdkit_fail 'd'
140 do_nbdkit_fail 'e'
141 do_nbdkit_fail 'f'
142 do_nbdkit_fail 'g'
143 do_nbdkit_fail 'h'
144 do_nbdkit_fail './a'
145 do_nbdkit_fail '../file-dir/a'
147 exit $fail