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