fuse: Add note in test about how to debug guestmount.
[libguestfs.git] / fuse / test-fuse.sh
1 #!/bin/bash -
2 # libguestfs
3 # Copyright (C) 2009 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 /dev/sda1 /
96   write /hello.txt hello
97   write /world.txt "hello world"
98   touch /empty
99 EOF
100
101 stage Mounting the filesystem
102 $guestmount -a "$image" -m /dev/sda1 "$mp"
103 # To debug guestmount, add this to the end of the preceding command:
104 # -v -x & sleep 60
105
106 stage Changing into mounted directory
107 cd "$mp"
108
109 stage Checking initial files exist
110 [ -n "$(echo *)" ]
111 [ "$(ls empty hello.txt world.txt)" = "empty
112 hello.txt
113 world.txt" ]
114
115 stage Checking initial files contain expected content
116 [ "$(cat hello.txt)" = "hello" ]
117 [ "$(cat world.txt)" = "hello world" ]
118 cat empty ;# should print nothing
119 [ -z "$(cat empty)" ]
120
121 stage Checking file modes of initial content
122 [ "$(stat -c %a empty)" = "644" ]
123 [ "$(stat -c %a hello.txt)" = "644" ]
124 [ "$(stat -c %a world.txt)" = "644" ]
125
126 stage Checking sizes of initial content
127 [ "$(stat -c %s empty)" -eq 0 ]
128 [ "$(stat -c %s hello.txt)" -eq 5 ]
129 [ "$(stat -c %s world.txt)" -eq 11 ]
130
131 stage Checking unlink
132 touch new
133 rm -f new ;# force because file is "owned" by root
134
135 stage Checking symbolic link
136 ln -s hello.txt symlink
137 [ -L symlink ]
138
139 stage Checking readlink
140 [ "$(readlink symlink)" = "hello.txt" ]
141
142 stage Checking hard link
143 [ "$(stat -c %h hello.txt)" -eq 1 ]
144 ln hello.txt link
145 [ "$(stat -c %h link)" -eq 2 ]
146 [ "$(stat -c %h hello.txt)" -eq 2 ]
147 rm -f link
148 [ ! -e link ]
149
150 # This fails because of caching.  The problem is that the linked file
151 # ("hello.txt") is cached with a link count of 2.  unlink("link")
152 # invalidates the cache for "link", but _not_ for "hello.txt" which
153 # still has the now-incorrect cached value.  However there's not much
154 # we can do about this since searching for all linked inodes of a file
155 # is an O(n) operation.
156 #[ "$(stat -c %h hello.txt)" -eq 1 ]
157
158 stage Checking mkdir
159 mkdir newdir
160 [ -d newdir ]
161
162 stage Checking rmdir
163 rmdir newdir
164 [ ! -e newdir ]
165
166 stage Checking rename
167 touch old
168 mv old new
169 [ -f new ]
170 [ ! -e old ]
171 rm -f new
172
173 stage Checking chmod
174 touch new
175 chmod a+x new
176 [ -x new ]
177 chmod a-x new
178 [ ! -x new ]
179 chmod a-w new
180 [ ! -w new ]
181 chmod a+w new
182 [ -w new ]
183 chmod a-r new
184 [ ! -r new ]
185 chmod a+r new
186 [ -r new ]
187 rm -f new
188
189 stage Checking truncate
190 truncate -s 10000 truncated
191 [ "$(stat -c %s truncated)" -eq 10000 ]
192 truncate -c -s 1000 truncated
193 [ "$(stat -c %s truncated)" -eq 1000 ]
194 truncate -c -s 10 truncated
195 [ "$(stat -c %s truncated)" -eq 10 ]
196 truncate -c -s 0 truncated
197 [ "$(stat -c %s truncated)" -eq 0 ]
198 rm -f truncated
199
200 stage Checking utimens and timestamps
201 for ts in 12345 1234567 987654321; do
202     # NB: It's not possible to set the ctime with touch.
203     touch -a -d @$ts timestamp
204     [ "$(stat -c %X timestamp)" -eq $ts ]
205     touch -m -d @$ts timestamp
206     [ "$(stat -c %Y timestamp)" -eq $ts ]
207     touch    -d @$ts timestamp
208     [ "$(stat -c %X timestamp)" -eq $ts ]
209     [ "$(stat -c %Y timestamp)" -eq $ts ]
210 done
211
212 stage Checking writes
213 cp hello.txt copy.txt
214 echo >> copy.txt
215 echo world >> copy.txt
216 echo bigger >> copy.txt
217 echo biggest >> copy.txt
218 [ "$(cat copy.txt)" = "hello
219 world
220 bigger
221 biggest" ]
222
223 # These ones are not yet tested by the current script:
224 #stage XXX statfs/statvfs
225 #stage XXX xattr operations
226
227 # These ones cannot easily be tested by the current script, eg because
228 # this script doesn't run as root:
229 #stage XXX fsync
230 #stage XXX chown
231 #stage XXX mknod