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