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