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