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