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