test-imagex-*: Set $srcdir correctly
[wimlib] / tests / test-imagex-mount
1 #!/bin/bash
2
3 # Test WIM mounting
4
5 set -e
6 cd tests
7 srcdir="${srcdir:-.}/.."
8 srcdir="$(cd $srcdir; pwd)"
9
10 if [ ! -r /dev/fuse ]; then
11         echo "WARNING: /dev/fuse is not readable."
12         echo "Skipping WIM mounting checks"
13         exit 0
14 fi
15
16 imagex() {
17         echo "imagex $@"
18         ../imagex $@  > /dev/null
19 }
20
21 imagex_info() {
22         echo "imagex info $@"
23         ../imagex info "$@"
24 }
25
26 imagex_mountrw() {
27         echo "imagex mountrw $@"
28         ../imagex mountrw --debug "$@" &> mount.log &
29         while ! mountpoint $2 >& /dev/null; do
30                 sleep 0.01;
31         done
32         return 0
33 }
34
35 cleanup() {
36         fusermount -u tmp &> /dev/null || true
37         fusermount -u tmp.mnt &> /dev/null || true
38         rm -rf dir* tmp* *.wim *.swm empty.wim tmp.orig tmp.mnt \
39                         tmp.apply mount.log test.tar wimlib-staging*
40 }
41 init() {
42         mkdir dir
43         cp $srcdir/src/*.c $srcdir/src/*.h dir
44         mkdir dir/subdir
45         echo 'hello' > dir/subdir/hello
46         echo 'hello' > dir/subdir/hello2
47         ln dir/subdir/hello dir/subdir/hellolink
48         echo -n > dir/subdir/empty_file
49         ln -s hello dir/subdir/rel_symlink
50
51         mkdir dir2
52         echo 'testing' > dir2/file
53         dd if=/dev/zero of=dir2/zeroes bs=4096 count=5
54         mkdir tmp.empty tmp.mnt tmp.apply tmp.orig
55         imagex capture tmp.empty empty.wim
56 }
57
58 error() {
59         echo "****************************************************************"
60         echo "                         Test failure                           "
61         while [ $# -gt 0 ]; do
62                 echo $1
63                 shift
64         done
65         echo "****************************************************************"
66         exit 1
67 }
68
69 cleanup
70 init
71
72 # imagex mount
73
74 for flag in "--compress=none" "--compress=maximum" "--compress=fast"; do
75         echo "Using flag $flag"
76         echo "Testing mounting WIM read-only"
77         if ! imagex capture dir dir.wim $flag; then
78                 error "Failed to capture WIM"
79         fi
80         mkdir tmp
81         if ! imagex mount dir.wim dir tmp; then
82                 error "Failed to mount test WIM read-only" \
83                       "Please read any error messages above before reporting this test failure."\
84                       "Perhaps you don't have FUSE installed, or the FUSE kernel module isn't" \
85                       "loaded, or you aren't a member of the FUSE group?"
86         fi
87         echo "Testing extracting file from mounted read-only WIM"
88         if ! cp tmp/lz.c lz.c; then
89                 error "Failed to extract file from read-only mounted WIM"
90         fi
91         if ! diff -q dir/lz.c lz.c; then
92                 error "Extracted file does not match copy in mounted WIM"
93         fi
94         if ! diff -q tmp/lz.c dir/lz.c; then
95                 error "Extractef file does not match original"
96         fi
97         rm -f lz.c
98         echo "Testing modifying mounted read-only WIM (should fail)"
99         if rm tmp/lz.c; then
100                 error "Removing file from read-only mounted WIM didn't fail"
101         fi
102         if touch tmp/newfile; then
103                 error "Creating file on read-only mounted WIM didn't fail"
104         fi
105         if echo 3 > tmp/lz.c; then
106                 error "Writing to file on read-only mounted WIM didn't fail"
107         fi
108         echo "Testing diff of mounted read-only WIM with original directory"
109         if ! diff -q -r tmp dir; then
110                 error "Recursive diff of read-only mounted WIM with original directory failed"
111         fi
112         echo "Testing unmount of read-only filesystem"
113         if ! imagex unmount tmp; then
114                 error "Unmounting read-only WIM failed"
115         fi
116         echo "Testing unmount of read-only filesystem with --commit given"
117         if ! imagex mount dir.wim dir tmp; then
118                 error "Failed to re-mount WIM read-only"
119         fi
120         if ! imagex unmount tmp --commit; then
121                 error "Failed to unmount read-only WIM with --commit flag (should be ignored)"
122         fi
123         rm -rf tmp dir.wim
124 done
125
126 # imagex mountrw
127 echo "Testing mounting WIM read-write"
128 if ! imagex capture dir dir.wim; then
129         error "Failed to capture WIM"
130 fi
131 mkdir tmp
132 if ! imagex mountrw dir.wim dir tmp; then
133         error "Failed to mount test WIM read-write"
134 fi
135 echo "Testing unmounting WIM unmodified"
136 if ! imagex unmount tmp; then
137         error "Failed to unmount test WIM unmodified"
138 fi
139 echo "Testing unmounting WIM unmodified with --commit and --check"
140 if ! imagex mountrw dir.wim dir tmp; then
141         error "Failed to re-mount test WIM read-write"
142 fi
143 if ! imagex unmount tmp --commit --check; then
144         error "Failed to unmount read-write mounted WIM with changes commited (no changes made)"
145 fi
146 echo "Testing removing file from mounted WIM"
147 if ! imagex mountrw dir.wim dir tmp; then
148         error "Failed to re-mount test WIM read-write"
149 fi
150 if ! rm tmp/lz.c; then
151         error "Failed to remove file from read-write mounted WIM"
152 fi
153 if test -f tmp/lz.c; then
154         error "Removing file from read-write mounted WIM failed"
155 fi
156 echo "Testing making directory in mounted WIM"
157 if ! mkdir tmp/newdir; then
158         error "Failed to make directory in read-write mounted WIM"
159 fi
160 if ! test -d tmp/newdir; then
161         error "Making directory in read-write mounted WIM failed"
162 fi
163 echo "Testing making new empty file in mounted WIM"
164 if ! touch tmp/newdir/empty_file; then
165         error "Could not create new empty file in read-write mounted WIM"
166 fi
167 if ! test -f tmp/newdir/empty_file; then
168         error "New empty file not created correctly in read-write mounted WIM"
169 fi
170 if ! test "`stat -c %s tmp/newdir/empty_file`" = 0; then
171         error "New empty file in read-write mounted WIM is not empty"
172 fi
173 echo "Testing making new non-empty file in mounted WIM"
174 if ! dd if=/dev/zero of=tmp/newdir/zeroes1 bs=1 count=4096; then
175         error "Failed to make new non-empty file in mounted WIM"
176 fi
177 if ! dd if=/dev/zero of=tmp/newdir/zeroes2 bs=4096 count=1; then
178         error "Failed to make new non-empty file in mounted WIM"
179 fi
180 if ! diff -q tmp/newdir/zeroes1 tmp/newdir/zeroes2; then
181         error "New files in mounted WIM not made correctly"
182 fi
183 echo "Unmounting WIM with changes committed and --check"
184 if ! imagex unmount tmp --commit --check; then
185         error "Failed to unmount read-write mounted WIM"
186 fi
187 if test "`imagex_info dir.wim | grep Integrity | awk '{print $3}'`" != "yes"; then
188         error "Integrity information was not included"
189 fi
190 rm -rf tmp
191 if ! imagex apply dir.wim tmp; then
192         error "Failed to apply WIM we had previously mounted read-write"
193 fi
194 if ! diff -q tmp/newdir/zeroes1 tmp/newdir/zeroes2; then
195         error "The new non-empty files we made in the read-write mounted WIM were not extracted correctly"
196 fi
197 if test `stat -c %s tmp/newdir/empty_file` != 0; then
198         error "The new empty file we made in the read-write mounted WIM was not extracted correctly"
199 fi
200 if test `stat -c %s tmp/newdir/zeroes1` != 4096; then
201         error "The new non-empty files we made in the read-write mounted WIM were not extracted correctly"
202 fi
203
204 # Now do some tests using tar.
205 do_tree_cmp() {
206         if ! ./tree-cmp $1 $2; then
207                 if [ -x /usr/bin/tree ]; then
208                         echo "Dumping tree of applied image"
209                         tree $2 --inodes -F -s --noreport
210                         error 'Information was lost or corrupted while capturing
211                                 and then applying a directory tree'
212                 fi
213         fi
214 }
215
216 msg() {
217         echo "--------------------------------------------------------------------"
218         echo "Testing making $1 on read-write mounted WIM"
219         echo "--------------------------------------------------------------------"
220 }
221
222 do_test() {
223
224         # Create tree, tar it up, and untar it on an empty WIM image mounted
225         # read-write
226
227         cp empty.wim test.wim
228
229         cd tmp.orig
230         eval "$1"
231         if [ -x /usr/bin/tree ]; then
232                 tree . --inodes -F -s --noreport
233         fi
234         tar cf ../test.tar .
235         cd ..
236
237         if ! imagex_mountrw test.wim tmp.mnt; then
238                 error "Failed to mount WIM read-write"
239         fi
240
241         cd tmp.mnt
242         if ! tar xf ../test.tar; then
243                 error "Failed to untar archive on read-write mounted WIM"
244         fi
245         cd ..
246
247         # Diff the original tree with the mounted WIM
248         do_tree_cmp tmp.orig tmp.mnt
249
250         # Clear the mounted WIM and do it again!  (We need to test deleting
251         # stuff as well as creating stuff.)
252         if ! rm -rf tmp.mnt/*; then
253                 error "Failed to clear mounted WIM"
254         fi
255
256         cd tmp.mnt
257         if ! tar xf ../test.tar; then
258                 error "Failed to untar archive on read-write mounted WIM"
259         fi
260         cd ..
261
262         # Diff the original tree with the mounted WIM
263         do_tree_cmp tmp.orig tmp.mnt
264
265         # Unmount the WIM, apply it, and diff the original tree with the applied
266         # tree
267         if ! imagex unmount tmp.mnt --commit; then
268                 error "Failed to unmount WIM mounted read-write"
269         fi
270         if ! imagex apply test.wim tmp.apply; then
271                 error "Failed to apply WIM we previously had mounted read-write"
272         fi
273         do_tree_cmp tmp.orig tmp.apply
274         rm -rf tmp.orig/* tmp.apply/*
275 }
276
277 . $srcdir/tests/common_tests.sh
278
279 cleanup
280
281 echo "**********************************************************"
282 echo "                 WIM mount tests passed                   "
283 echo "**********************************************************"