0a429f73b30fa3b649f1c53480064a36875ed223
[libguestfs.git] / fuse / test-fuse.sh
1 #!/bin/bash -
2 # libguestfs
3 # Copyright (C) 2009-2011 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 unset CDPATH
20 set -e
21 #set -v
22
23 if [ -z "$top_builddir" ]; then
24     echo "$0: error: environment variable \$top_builddir must be set"
25     exit 1
26 fi
27
28 nr_stages=$(grep "^stage " $0 | wc -l)
29
30 # Allow top_builddir to be a relative path, but also make it absolute,
31 # and move to that directory for the initial phase of the script.
32 top_builddir=$(cd "$top_builddir" > /dev/null; pwd)
33
34 # Set libguestfs up for running locally.
35 export LIBGUESTFS_PATH="$top_builddir/appliance"
36
37 # Paths to the other programs and files.  NB: Must be absolute paths.
38 guestfish="$top_builddir/fish/guestfish"
39 guestmount="$top_builddir/fuse/guestmount"
40 image="$top_builddir/fuse/test.img"
41 mp="$top_builddir/fuse/test-mp"
42
43 if [ ! -x "$guestfish" -o ! -x "$guestmount" ]; then
44     echo "$0: error: guestfish or guestmount are not available"
45     exit 1
46 fi
47
48 # Ensure everything is cleaned up on exit.
49 rm -f "$image"
50 mkdir -p "$mp"
51 fusermount -u "$mp" >/dev/null 2>&1 ||:
52 function cleanup ()
53 {
54     status=$?
55     set +e
56     [ $status = 0 ] || echo "*** FAILED ***"
57     echo "Unmounting filesystem and cleaning up."
58
59     # Move out of the mountpoint (otherwise our cwd will prevent the
60     # mountpoint from being unmounted).
61     cd "$top_builddir"
62
63     # Who's using this?  Should be no one, but see below.
64     if [ -x /sbin/fuser ]; then /sbin/fuser "$mp"; fi
65
66     # If you run this and you have GNOME running at the same time,
67     # then randomly /usr/libexec/gvfs-gdu-volume-monitor will decide
68     # to do whatever it does in the mountpoint directory, preventing
69     # you from unmounting it!  Hence the need for this loop.
70     count=10
71     while ! fusermount -u "$mp" && [ $count -gt 0 ]; do
72         sleep 1
73         ((count--))
74     done
75
76     rm -f "$image"
77     rm -rf "$mp"
78     exit $status
79 }
80 trap cleanup INT TERM QUIT EXIT
81
82 s=1
83 function stage ()
84 {
85     echo "test-fuse: $s/$nr_stages:" "$@" "..."
86     ((s++))
87 }
88
89 stage Create filesystem with some initial content
90 $guestfish <<EOF
91   sparse "$image" 10M
92   run
93   part-disk /dev/sda mbr
94   mkfs ext2 /dev/sda1
95   mount_options acl,user_xattr /dev/sda1 /
96   write /hello.txt hello
97   write /world.txt "hello world"
98   touch /empty
99   touch /user_xattr
100   setxattr user.test hello123 8 /user_xattr
101   touch /acl
102   # XXX hack until libguestfs gets ACL support
103   debug sh "setfacl -m u:500:r /sysroot/acl" | cat > /dev/null
104 EOF
105
106 stage Mounting the filesystem
107 $guestmount \
108     -a "$image" -m /dev/sda1:/:acl,user_xattr \
109     -o uid="$(id -u)" -o gid="$(id -g)" "$mp"
110 # To debug guestmount, add this to the end of the preceding command:
111 # -v -x & sleep 60
112
113 stage Changing into mounted directory
114 cd "$mp"
115
116 stage Checking initial files exist
117 [ -n "$(echo *)" ]
118 [ "$(ls empty hello.txt world.txt)" = "empty
119 hello.txt
120 world.txt" ]
121
122 stage Checking initial files contain expected content
123 [ "$(cat hello.txt)" = "hello" ]
124 [ "$(cat world.txt)" = "hello world" ]
125 cat empty ;# should print nothing
126 [ -z "$(cat empty)" ]
127
128 stage Checking file modes of initial content
129 [ "$(stat -c %a empty)" = "644" ]
130 [ "$(stat -c %a hello.txt)" = "644" ]
131 [ "$(stat -c %a world.txt)" = "644" ]
132
133 stage Checking sizes of initial content
134 [ "$(stat -c %s empty)" -eq 0 ]
135 [ "$(stat -c %s hello.txt)" -eq 5 ]
136 [ "$(stat -c %s world.txt)" -eq 11 ]
137
138 stage Checking unlink
139 touch new
140 rm -f new ;# force because file is "owned" by root
141
142 stage Checking symbolic link
143 ln -s hello.txt symlink
144 [ -L symlink ]
145
146 stage Checking readlink
147 [ "$(readlink symlink)" = "hello.txt" ]
148
149 stage Checking hard link
150 [ "$(stat -c %h hello.txt)" -eq 1 ]
151 ln hello.txt link
152 [ "$(stat -c %h link)" -eq 2 ]
153 [ "$(stat -c %h hello.txt)" -eq 2 ]
154 rm -f link
155 [ ! -e link ]
156
157 # This fails because of caching.  The problem is that the linked file
158 # ("hello.txt") is cached with a link count of 2.  unlink("link")
159 # invalidates the cache for "link", but _not_ for "hello.txt" which
160 # still has the now-incorrect cached value.  However there's not much
161 # we can do about this since searching for all linked inodes of a file
162 # is an O(n) operation.
163 #[ "$(stat -c %h hello.txt)" -eq 1 ]
164
165 stage Checking mkdir
166 mkdir newdir
167 [ -d newdir ]
168
169 stage Checking rmdir
170 rmdir newdir
171 [ ! -e newdir ]
172
173 stage Checking rename
174 touch old
175 mv old new
176 [ -f new ]
177 [ ! -e old ]
178 rm -f new
179
180 stage Checking chmod
181 touch new
182 chmod a+x new
183 [ -x new ]
184 chmod a-x new
185 [ ! -x new ]
186 chmod a-w new
187 [ ! -w new ]
188 chmod a+w new
189 [ -w new ]
190 chmod a-r new
191 [ ! -r new ]
192 chmod a+r new
193 [ -r new ]
194 rm -f new
195
196 stage Checking truncate
197 truncate -s 10000 truncated
198 [ "$(stat -c %s truncated)" -eq 10000 ]
199 truncate -c -s 1000 truncated
200 [ "$(stat -c %s truncated)" -eq 1000 ]
201 truncate -c -s 10 truncated
202 [ "$(stat -c %s truncated)" -eq 10 ]
203 truncate -c -s 0 truncated
204 [ "$(stat -c %s truncated)" -eq 0 ]
205 rm -f truncated
206
207 # Disabled because of RHBZ#660687 on Debian.
208 # stage Checking utimens and timestamps
209 # for ts in 12345 1234567 987654321; do
210 #     # NB: It's not possible to set the ctime with touch.
211 #     touch -a -d @$ts timestamp
212 #     [ "$(stat -c %X timestamp)" -eq $ts ]
213 #     touch -m -d @$ts timestamp
214 #     [ "$(stat -c %Y timestamp)" -eq $ts ]
215 #     touch    -d @$ts timestamp
216 #     [ "$(stat -c %X timestamp)" -eq $ts ]
217 #     [ "$(stat -c %Y timestamp)" -eq $ts ]
218 # done
219
220 stage Checking writes
221 cp hello.txt copy.txt
222 echo >> copy.txt
223 echo world >> copy.txt
224 echo bigger >> copy.txt
225 echo biggest >> copy.txt
226 [ "$(cat copy.txt)" = "hello
227 world
228 bigger
229 biggest" ]
230
231 stage 'Checking extended attribute (xattr) read operation'
232 if getfattr --help > /dev/null 2>&1 ; then
233   [ "$(getfattr -d user_xattr | grep -v ^#)" = 'user.test="hello123"' ]
234 fi
235
236 stage Checking POSIX ACL read operation
237 if getfacl --help > /dev/null 2>&1 ; then
238   [ "$(getfacl -n acl | grep -v ^#)" = "user::rw-
239 user:500:r--
240 group::r--
241 mask::r--
242 other::r--" ]
243 fi
244
245 # These ones are not yet tested by the current script:
246 #stage XXX statfs/statvfs
247
248 # These ones cannot easily be tested by the current script, eg because
249 # this script doesn't run as root:
250 #stage XXX fsync
251 #stage XXX chown
252 #stage XXX mknod